|
| 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 | + groupName: "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 | + groupName: "innerGroup1", |
| 43 | + group: &cb.ConfigGroup{ |
| 44 | + Values: map[string]*cb.ConfigValue{ |
| 45 | + "inner1": &cb.ConfigValue{ModPolicy: "mod3"}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + deserializedValues: map[string]proto.Message{ |
| 49 | + "inner1": &ab.ConsensusType{Type: "inner1"}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + &configResult{ |
| 53 | + groupName: "innerGroup2", |
| 54 | + group: &cb.ConfigGroup{ |
| 55 | + Values: map[string]*cb.ConfigValue{ |
| 56 | + "inner2": &cb.ConfigValue{ModPolicy: "mod3"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + deserializedValues: map[string]proto.Message{ |
| 60 | + "inner2": &ab.ConsensusType{Type: "inner2"}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + deserializedValues: map[string]proto.Message{ |
| 65 | + "outer": &ab.ConsensusType{Type: "outer"}, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + buffer := &bytes.Buffer{} |
| 70 | + assert.NoError(t, json.Indent(buffer, []byte(cr.JSON()), "", ""), "JSON should parse nicely") |
| 71 | + |
| 72 | + expected := "{\"rootGroup\":{\"Values\":{\"outer\":{\"Version\":\"1\",\"ModPolicy\":\"mod1\",\"Value\":{\"type\":\"outer\"}}},\"Groups\":{\"innerGroup1\":{\"Values\":{\"inner1\":{\"Version\":\"0\",\"ModPolicy\":\"mod3\",\"Value\":{\"type\":\"inner1\"}}},\"Groups\":{}},\"innerGroup2\":{\"Values\":{\"inner2\":{\"Version\":\"0\",\"ModPolicy\":\"mod3\",\"Value\":{\"type\":\"inner2\"}}},\"Groups\":{}}}}}" |
| 73 | + |
| 74 | + // Remove all newlines and spaces from the JSON |
| 75 | + compactedJSON := strings.Replace(strings.Replace(buffer.String(), "\n", "", -1), " ", "", -1) |
| 76 | + |
| 77 | + assert.Equal(t, expected, compactedJSON) |
| 78 | + |
| 79 | +} |
0 commit comments