Skip to content

Commit

Permalink
FAB-3: Fix Json output for empty network list
Browse files Browse the repository at this point in the history
The generated pb.PeersMessage struct will be added "omitempty" tag automatically. So when
 marshal an empty PeersMessage, we get a "{}". Add a new tempoprary struct without
"omitempty" to replace PeerMesssage. Now we can get "{"Peer": []}"

Change-Id: I1fc310c951cf4e85a61660f2c45f700136dc5010
Signed-off-by: jiangyaoguo <jiangyaoguo@gmail.com>
  • Loading branch information
jiangyaoguo committed Aug 19, 2016
1 parent 0363e1d commit 845e795
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bddtests/peer_cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ Feature: Peer Command Line Interface
When I execute "peer network list" in container vp0
Then the command should complete successfully
And stdout should contain JSON
And I should get result with "{"Peers":[]}"

Scenario: List Peers when two are up
Given we compose "docker-compose-2.yml"
When I execute "peer network list" in container vp0
Then the command should complete successfully
And stdout should contain JSON with "peers" array of length 1
And stdout should contain JSON with "Peers" array of length 1
6 changes: 5 additions & 1 deletion bddtests/steps/peer_cli_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def step_impl(context, stream, attribute, length):
array = getAttribute(attribute, json)
assertLength(array, int(length))

@then(u'I should get result with "{expectResult}"')
def step_impl(context, expectResult):
assert context.command["stdout"].strip('\n') == expectResult

def assertIsJson(data):
assert isJson(data), "Data is not in JSON format"

Expand All @@ -86,4 +90,4 @@ def getAttribute(attribute, json):

def assertLength(array, length):
arrayLength = len(array)
assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)
assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)
4 changes: 3 additions & 1 deletion peer/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func networkList() (err error) {
return
}

jsonOutput, _ := json.Marshal(peers)
// The generated pb.PeersMessage struct will be added "omitempty" tag automatically.
// But we still want to print it when pb.PeersMessage is empty.
jsonOutput, _ := json.Marshal(struct{ Peers []*pb.PeerEndpoint }{append([]*pb.PeerEndpoint{}, peers.GetPeers()...)})
fmt.Println(string(jsonOutput))
return nil
}

0 comments on commit 845e795

Please sign in to comment.