Jian Li
Committed by Gerrit Code Review

[ONOS-4016] Enhance unit test to check the content inside masters

Change-Id: I461b76457d4abdadad3b458cad8bf9d1a8a38f6b
...@@ -99,12 +99,12 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -99,12 +99,12 @@ public class RegionCodec extends JsonCodec<Region> {
99 99
100 if (mastersJson != null) { 100 if (mastersJson != null) {
101 IntStream.range(0, mastersJson.size()).forEach(i -> { 101 IntStream.range(0, mastersJson.size()).forEach(i -> {
102 - ObjectNode setsJson = get(mastersJson, i); 102 + JsonNode setsJson = mastersJson.get(i);
103 final Set<NodeId> nodeIds = Sets.newHashSet(); 103 final Set<NodeId> nodeIds = Sets.newHashSet();
104 if (setsJson != null && setsJson.isArray()) { 104 if (setsJson != null && setsJson.isArray()) {
105 Set<NodeId> localNodeIds = Sets.newHashSet(); 105 Set<NodeId> localNodeIds = Sets.newHashSet();
106 - IntStream.range(0, mastersJson.size()).forEach(j -> { 106 + IntStream.range(0, setsJson.size()).forEach(j -> {
107 - ObjectNode nodeIdJson = get(setsJson, j); 107 + JsonNode nodeIdJson = setsJson.get(j);
108 localNodeIds.add(decodeNodeId(nodeIdJson)); 108 localNodeIds.add(decodeNodeId(nodeIdJson));
109 }); 109 });
110 nodeIds.addAll(localNodeIds); 110 nodeIds.addAll(localNodeIds);
...@@ -136,7 +136,7 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -136,7 +136,7 @@ public class RegionCodec extends JsonCodec<Region> {
136 * @param json json object 136 * @param json json object
137 * @return decoded node id object 137 * @return decoded node id object
138 */ 138 */
139 - private NodeId decodeNodeId(ObjectNode json) { 139 + private NodeId decodeNodeId(JsonNode json) {
140 NodeId nodeId = NodeId.nodeId(nullIsIllegal(json, NODE_ID + 140 NodeId nodeId = NodeId.nodeId(nullIsIllegal(json, NODE_ID +
141 MISSING_MEMBER_MESSAGE).asText()); 141 MISSING_MEMBER_MESSAGE).asText());
142 142
......
...@@ -91,6 +91,13 @@ public class RegionCodecTest { ...@@ -91,6 +91,13 @@ public class RegionCodecTest {
91 checkCommonData(region); 91 checkCommonData(region);
92 92
93 assertThat(region.masters().size(), is(2)); 93 assertThat(region.masters().size(), is(2));
94 +
95 + NodeId nodeId1 = NodeId.nodeId("1");
96 + NodeId nodeId2 = NodeId.nodeId("2");
97 + Set<NodeId> nodeIds1 = region.masters().get(0);
98 + Set<NodeId> nodeIds2 = region.masters().get(1);
99 + assertThat(nodeIds1.containsAll(ImmutableSet.of(nodeId1)), is(true));
100 + assertThat(nodeIds2.containsAll(ImmutableSet.of(nodeId1, nodeId2)), is(true));
94 } 101 }
95 102
96 /** 103 /**
......
...@@ -16,10 +16,14 @@ ...@@ -16,10 +16,14 @@
16 package org.onosproject.codec.impl; 16 package org.onosproject.codec.impl;
17 17
18 import com.fasterxml.jackson.databind.JsonNode; 18 import com.fasterxml.jackson.databind.JsonNode;
19 +import com.google.common.collect.Sets;
19 import org.hamcrest.Description; 20 import org.hamcrest.Description;
20 import org.hamcrest.TypeSafeDiagnosingMatcher; 21 import org.hamcrest.TypeSafeDiagnosingMatcher;
22 +import org.onosproject.cluster.NodeId;
21 import org.onosproject.net.region.Region; 23 import org.onosproject.net.region.Region;
22 24
25 +import java.util.Set;
26 +
23 /** 27 /**
24 * Hamcrest matcher for region. 28 * Hamcrest matcher for region.
25 */ 29 */
...@@ -64,11 +68,36 @@ public final class RegionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> ...@@ -64,11 +68,36 @@ public final class RegionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode>
64 return false; 68 return false;
65 } 69 }
66 70
67 - // TODO: check the content inside masters 71 + // check master
72 + for (Set<NodeId> set : region.masters()) {
73 + boolean masterFound = false;
74 + for (int masterIndex = 0; masterIndex < jsonMasters.size(); masterIndex++) {
75 + masterFound = checkEquality(jsonMasters.get(masterIndex), set);
76 + }
77 +
78 + if (!masterFound) {
79 + description.appendText("master not found " + set.toString());
80 + return false;
81 + }
82 + }
68 83
69 return true; 84 return true;
70 } 85 }
71 86
87 + private Set<NodeId> jsonToSet(JsonNode nodes) {
88 + final Set<NodeId> nodeIds = Sets.newHashSet();
89 + nodes.forEach(node -> nodeIds.add(NodeId.nodeId(node.asText())));
90 + return nodeIds;
91 + }
92 +
93 + private boolean checkEquality(JsonNode nodes, Set<NodeId> nodeIds) {
94 + Set<NodeId> jsonSet = jsonToSet(nodes);
95 + if (jsonSet.size() == nodes.size()) {
96 + return jsonSet.containsAll(nodeIds);
97 + }
98 + return false;
99 + }
100 +
72 @Override 101 @Override
73 public void describeTo(Description description) { 102 public void describeTo(Description description) {
74 description.appendText(region.toString()); 103 description.appendText(region.toString());
......