github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/configtx/config_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 configtx 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "strings" 23 "testing" 24 25 cb "github.com/hyperledger/fabric/protos/common" 26 ab "github.com/hyperledger/fabric/protos/orderer" 27 28 "github.com/golang/protobuf/proto" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestJSON(t *testing.T) { 33 cr := &configResult{ 34 groupKey: "rootGroup", 35 group: &cb.ConfigGroup{ 36 Values: map[string]*cb.ConfigValue{ 37 "outer": &cb.ConfigValue{Version: 1, ModPolicy: "mod1"}, 38 }, 39 }, 40 subResults: []*configResult{ 41 &configResult{ 42 groupKey: "innerGroup1", 43 group: &cb.ConfigGroup{ 44 Values: map[string]*cb.ConfigValue{ 45 "inner1": &cb.ConfigValue{ModPolicy: "mod3"}, 46 }, 47 Policies: map[string]*cb.ConfigPolicy{ 48 "policy1": &cb.ConfigPolicy{Policy: &cb.Policy{}, ModPolicy: "mod1"}, 49 }, 50 }, 51 deserializedValues: map[string]proto.Message{ 52 "inner1": &ab.ConsensusType{Type: "inner1"}, 53 }, 54 deserializedPolicies: map[string]proto.Message{ 55 "policy1": &ab.ConsensusType{Type: "policy1"}, 56 }, 57 }, 58 &configResult{ 59 groupKey: "innerGroup2", 60 group: &cb.ConfigGroup{ 61 Values: map[string]*cb.ConfigValue{ 62 "inner2": &cb.ConfigValue{ModPolicy: "mod3"}, 63 }, 64 Policies: map[string]*cb.ConfigPolicy{ 65 "policy2": &cb.ConfigPolicy{Policy: &cb.Policy{Type: 1}, ModPolicy: "mod2"}, 66 }, 67 }, 68 deserializedValues: map[string]proto.Message{ 69 "inner2": &ab.ConsensusType{Type: "inner2"}, 70 }, 71 deserializedPolicies: map[string]proto.Message{ 72 "policy2": &ab.ConsensusType{Type: "policy2"}, 73 }, 74 }, 75 }, 76 deserializedValues: map[string]proto.Message{ 77 "outer": &ab.ConsensusType{Type: "outer"}, 78 }, 79 } 80 81 crWrapper := &configResult{subResults: []*configResult{cr}} 82 83 buffer := &bytes.Buffer{} 84 assert.NoError(t, json.Indent(buffer, []byte(crWrapper.JSON()), "", ""), "JSON should parse nicely") 85 86 expected := "{\"rootGroup\":{\"Values\":{\"outer\":{\"Version\":\"1\",\"ModPolicy\":\"mod1\",\"Value\":{\"type\":\"outer\"}}},\"Policies\":{},\"Groups\":{\"innerGroup1\":{\"Values\":{\"inner1\":{\"Version\":\"0\",\"ModPolicy\":\"mod3\",\"Value\":{\"type\":\"inner1\"}}},\"Policies\":{\"policy1\":{\"Version\":\"0\",\"ModPolicy\":\"mod1\",\"Policy\":{\"PolicyType\":\"0\",\"Policy\":{\"type\":\"policy1\"}}}},\"Groups\":{}},\"innerGroup2\":{\"Values\":{\"inner2\":{\"Version\":\"0\",\"ModPolicy\":\"mod3\",\"Value\":{\"type\":\"inner2\"}}},\"Policies\":{\"policy2\":{\"Version\":\"0\",\"ModPolicy\":\"mod2\",\"Policy\":{\"PolicyType\":\"1\",\"Policy\":{\"type\":\"policy2\"}}}},\"Groups\":{}}}}}" 87 88 // Remove all newlines and spaces from the JSON 89 compactedJSON := strings.Replace(strings.Replace(buffer.String(), "\n", "", -1), " ", "", -1) 90 91 assert.Equal(t, expected, compactedJSON) 92 }