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> {
if (mastersJson != null) {
IntStream.range(0, mastersJson.size()).forEach(i -> {
ObjectNode setsJson = get(mastersJson, i);
JsonNode setsJson = mastersJson.get(i);
final Set<NodeId> nodeIds = Sets.newHashSet();
if (setsJson != null && setsJson.isArray()) {
Set<NodeId> localNodeIds = Sets.newHashSet();
IntStream.range(0, mastersJson.size()).forEach(j -> {
ObjectNode nodeIdJson = get(setsJson, j);
IntStream.range(0, setsJson.size()).forEach(j -> {
JsonNode nodeIdJson = setsJson.get(j);
localNodeIds.add(decodeNodeId(nodeIdJson));
});
nodeIds.addAll(localNodeIds);
......@@ -136,7 +136,7 @@ public class RegionCodec extends JsonCodec<Region> {
* @param json json object
* @return decoded node id object
*/
private NodeId decodeNodeId(ObjectNode json) {
private NodeId decodeNodeId(JsonNode json) {
NodeId nodeId = NodeId.nodeId(nullIsIllegal(json, NODE_ID +
MISSING_MEMBER_MESSAGE).asText());
......
......@@ -91,6 +91,13 @@ public class RegionCodecTest {
checkCommonData(region);
assertThat(region.masters().size(), is(2));
NodeId nodeId1 = NodeId.nodeId("1");
NodeId nodeId2 = NodeId.nodeId("2");
Set<NodeId> nodeIds1 = region.masters().get(0);
Set<NodeId> nodeIds2 = region.masters().get(1);
assertThat(nodeIds1.containsAll(ImmutableSet.of(nodeId1)), is(true));
assertThat(nodeIds2.containsAll(ImmutableSet.of(nodeId1, nodeId2)), is(true));
}
/**
......
......@@ -16,10 +16,14 @@
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.cluster.NodeId;
import org.onosproject.net.region.Region;
import java.util.Set;
/**
* Hamcrest matcher for region.
*/
......@@ -64,11 +68,36 @@ public final class RegionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode>
return false;
}
// TODO: check the content inside masters
// check master
for (Set<NodeId> set : region.masters()) {
boolean masterFound = false;
for (int masterIndex = 0; masterIndex < jsonMasters.size(); masterIndex++) {
masterFound = checkEquality(jsonMasters.get(masterIndex), set);
}
if (!masterFound) {
description.appendText("master not found " + set.toString());
return false;
}
}
return true;
}
private Set<NodeId> jsonToSet(JsonNode nodes) {
final Set<NodeId> nodeIds = Sets.newHashSet();
nodes.forEach(node -> nodeIds.add(NodeId.nodeId(node.asText())));
return nodeIds;
}
private boolean checkEquality(JsonNode nodes, Set<NodeId> nodeIds) {
Set<NodeId> jsonSet = jsonToSet(nodes);
if (jsonSet.size() == nodes.size()) {
return jsonSet.containsAll(nodeIds);
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText(region.toString());
......