github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configvalues/channel/application/organization_test.go (about) 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 application 18 19 import ( 20 "testing" 21 22 api "github.com/hyperledger/fabric/common/configvalues" 23 cb "github.com/hyperledger/fabric/protos/common" 24 pb "github.com/hyperledger/fabric/protos/peer" 25 26 logging "github.com/op/go-logging" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func init() { 31 logging.SetLevel(logging.DEBUG, "") 32 } 33 34 func makeInvalidConfigValue() *cb.ConfigValue { 35 return &cb.ConfigValue{ 36 Value: []byte("Garbage Data"), 37 } 38 } 39 40 func groupToKeyValue(configGroup *cb.ConfigGroup) (string, *cb.ConfigValue) { 41 for _, group := range configGroup.Groups[GroupKey].Groups { 42 for key, value := range group.Values { 43 return key, value 44 } 45 } 46 panic("No value encoded") 47 } 48 49 func TestApplicationOrgInterface(t *testing.T) { 50 _ = api.ValueProposer(NewApplicationOrgConfig("id", nil)) 51 } 52 53 func TestApplicationOrgDoubleBegin(t *testing.T) { 54 m := NewApplicationOrgConfig("id", nil) 55 m.BeginValueProposals(nil) 56 assert.Panics(t, func() { m.BeginValueProposals(nil) }, "Two begins back to back should have caused a panic") 57 } 58 59 func TestApplicationOrgCommitWithoutBegin(t *testing.T) { 60 m := NewApplicationOrgConfig("id", nil) 61 assert.Panics(t, m.CommitProposals, "Committing without beginning should have caused a panic") 62 } 63 64 func TestApplicationOrgRollback(t *testing.T) { 65 m := NewApplicationOrgConfig("id", nil) 66 m.pendingConfig = &applicationOrgConfig{} 67 m.RollbackProposals() 68 assert.Nil(t, m.pendingConfig, "Should have cleared pending config on rollback") 69 } 70 71 func TestApplicationOrgAnchorPeers(t *testing.T) { 72 endVal := []*pb.AnchorPeer{ 73 &pb.AnchorPeer{Host: "foo", Port: 234}, 74 &pb.AnchorPeer{Host: "bar", Port: 237}, 75 } 76 invalidMessage := makeInvalidConfigValue() 77 validMessage := TemplateAnchorPeers("id", endVal) 78 m := NewApplicationOrgConfig("id", nil) 79 m.BeginValueProposals(nil) 80 81 assert.Error(t, m.ProposeValue(AnchorPeersKey, invalidMessage), "Should have failed on invalid message") 82 assert.NoError(t, m.ProposeValue(groupToKeyValue(validMessage)), "Should not have failed on invalid message") 83 m.CommitProposals() 84 85 assert.Equal(t, m.AnchorPeers(), endVal, "Did not set updated anchor peers") 86 }