-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2577] Add JSON rendering of configResult
https://jira.hyperledger.org/browse/FAB-2577 FAB-2574 split the config result from the config manager to allow other pieces fo the system to utilize it. This CR adds the ability for the config result to render the config result as JSON to be used in a later changeset for inspecting the configuration. Change-Id: Ieeccaf8486706f0a47ea09196eca12caae007a2b Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Mar 7, 2017
1 parent
093394b
commit 86f65d3
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package configtx | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
"testing" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
ab "github.com/hyperledger/fabric/protos/orderer" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJSON(t *testing.T) { | ||
cr := &configResult{ | ||
groupName: "rootGroup", | ||
group: &cb.ConfigGroup{ | ||
Values: map[string]*cb.ConfigValue{ | ||
"outer": &cb.ConfigValue{Version: 1, ModPolicy: "mod1"}, | ||
}, | ||
}, | ||
subResults: []*configResult{ | ||
&configResult{ | ||
groupName: "innerGroup1", | ||
group: &cb.ConfigGroup{ | ||
Values: map[string]*cb.ConfigValue{ | ||
"inner1": &cb.ConfigValue{ModPolicy: "mod3"}, | ||
}, | ||
}, | ||
deserializedValues: map[string]proto.Message{ | ||
"inner1": &ab.ConsensusType{Type: "inner1"}, | ||
}, | ||
}, | ||
&configResult{ | ||
groupName: "innerGroup2", | ||
group: &cb.ConfigGroup{ | ||
Values: map[string]*cb.ConfigValue{ | ||
"inner2": &cb.ConfigValue{ModPolicy: "mod3"}, | ||
}, | ||
}, | ||
deserializedValues: map[string]proto.Message{ | ||
"inner2": &ab.ConsensusType{Type: "inner2"}, | ||
}, | ||
}, | ||
}, | ||
deserializedValues: map[string]proto.Message{ | ||
"outer": &ab.ConsensusType{Type: "outer"}, | ||
}, | ||
} | ||
|
||
buffer := &bytes.Buffer{} | ||
assert.NoError(t, json.Indent(buffer, []byte(cr.JSON()), "", ""), "JSON should parse nicely") | ||
|
||
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\":{}}}}}" | ||
|
||
// Remove all newlines and spaces from the JSON | ||
compactedJSON := strings.Replace(strings.Replace(buffer.String(), "\n", "", -1), " ", "", -1) | ||
|
||
assert.Equal(t, expected, compactedJSON) | ||
|
||
} |