|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + cb "github.com/hyperledger/fabric/protos/common" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +type foo struct { |
| 28 | + Msg1 *cb.Envelope |
| 29 | + Msg2 *cb.Payload |
| 30 | +} |
| 31 | + |
| 32 | +type bar struct { |
| 33 | + Msg3 *cb.Header |
| 34 | +} |
| 35 | + |
| 36 | +type conflict struct { |
| 37 | + Msg1 *cb.Header |
| 38 | +} |
| 39 | + |
| 40 | +type nonProtos struct { |
| 41 | + Msg *cb.Envelope |
| 42 | + Wrong string |
| 43 | +} |
| 44 | + |
| 45 | +type unexported struct { |
| 46 | + msg *cb.Envelope |
| 47 | +} |
| 48 | + |
| 49 | +func TestSingle(t *testing.T) { |
| 50 | + fooVal := &foo{} |
| 51 | + sv, err := NewStandardValues(fooVal) |
| 52 | + assert.NoError(t, err, "Valid non-nested structure provided") |
| 53 | + assert.NotNil(t, fooVal.Msg1, "Should have initialized Msg1") |
| 54 | + assert.NotNil(t, fooVal.Msg2, "Should have initialized Msg2") |
| 55 | + |
| 56 | + msg1, ok := sv.ProtoMsg("Msg1") |
| 57 | + assert.True(t, ok, "Should have found map entry") |
| 58 | + assert.Equal(t, msg1, fooVal.Msg1, "Should be same entry") |
| 59 | + |
| 60 | + msg2, ok := sv.ProtoMsg("Msg2") |
| 61 | + assert.True(t, ok, "Should have found map entry") |
| 62 | + assert.Equal(t, msg2, fooVal.Msg2, "Should be same entry") |
| 63 | +} |
| 64 | + |
| 65 | +func TestPair(t *testing.T) { |
| 66 | + fooVal := &foo{} |
| 67 | + barVal := &bar{} |
| 68 | + sv, err := NewStandardValues(fooVal, barVal) |
| 69 | + assert.NoError(t, err, "Valid non-nested structure provided") |
| 70 | + assert.NotNil(t, fooVal.Msg1, "Should have initialized Msg1") |
| 71 | + assert.NotNil(t, fooVal.Msg2, "Should have initialized Msg2") |
| 72 | + assert.NotNil(t, barVal.Msg3, "Should have initialized Msg3") |
| 73 | + |
| 74 | + msg1, ok := sv.ProtoMsg("Msg1") |
| 75 | + assert.True(t, ok, "Should have found map entry") |
| 76 | + assert.Equal(t, msg1, fooVal.Msg1, "Should be same entry") |
| 77 | + |
| 78 | + msg2, ok := sv.ProtoMsg("Msg2") |
| 79 | + assert.True(t, ok, "Should have found map entry") |
| 80 | + assert.Equal(t, msg2, fooVal.Msg2, "Should be same entry") |
| 81 | + |
| 82 | + msg3, ok := sv.ProtoMsg("Msg3") |
| 83 | + assert.True(t, ok, "Should have found map entry") |
| 84 | + assert.Equal(t, msg3, barVal.Msg3, "Should be same entry") |
| 85 | +} |
| 86 | + |
| 87 | +func TestNestingConflict(t *testing.T) { |
| 88 | + _, err := NewStandardValues(&foo{}, &conflict{}) |
| 89 | + assert.Error(t, err, "Conflicting keys provided") |
| 90 | +} |
| 91 | + |
| 92 | +func TestNonProtosStruct(t *testing.T) { |
| 93 | + _, err := NewStandardValues(&nonProtos{}) |
| 94 | + assert.Error(t, err, "Structure with non-struct non-proto fields provided") |
| 95 | +} |
| 96 | + |
| 97 | +func TestUnexportedField(t *testing.T) { |
| 98 | + _, err := NewStandardValues(&unexported{}) |
| 99 | + assert.Error(t, err, "Structure with unexported fields") |
| 100 | +} |
| 101 | + |
| 102 | +func TestNonPointerParam(t *testing.T) { |
| 103 | + _, err := NewStandardValues(foo{}) |
| 104 | + assert.Error(t, err, "Parameter must be pointer") |
| 105 | +} |
| 106 | + |
| 107 | +func TestPointerToNonStruct(t *testing.T) { |
| 108 | + nonStruct := "foo" |
| 109 | + _, err := NewStandardValues(&nonStruct) |
| 110 | + assert.Error(t, err, "Pointer must be to a struct") |
| 111 | +} |
0 commit comments