blob: 937dbe94590dd8338e2a2b96f8cfc53eb84eb13c [file] [log] [blame]
Jian Lib68b9a82016-02-23 10:25:54 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lib68b9a82016-02-23 10:25:54 +09003 *
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 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
Jian Li79359df2016-02-24 17:29:54 +090019import com.google.common.collect.Sets;
Jian Lib68b9a82016-02-23 10:25:54 +090020import org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
Jian Li79359df2016-02-24 17:29:54 +090022import org.onosproject.cluster.NodeId;
Jian Lib68b9a82016-02-23 10:25:54 +090023import org.onosproject.net.region.Region;
24
Jian Li79359df2016-02-24 17:29:54 +090025import java.util.Set;
26
Jian Lib68b9a82016-02-23 10:25:54 +090027/**
28 * Hamcrest matcher for region.
29 */
30public final class RegionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
31
32 private final Region region;
33
34 private RegionJsonMatcher(Region region) {
35 this.region = region;
36 }
37
38 @Override
39 protected boolean matchesSafely(JsonNode jsonRegion, Description description) {
40 // check id
41 String jsonRegionId = jsonRegion.get("id").asText();
42 String regionId = region.id().toString();
43 if (!jsonRegionId.equals(regionId)) {
44 description.appendText("region id was " + jsonRegionId);
45 return false;
46 }
47
48 // check type
49 String jsonType = jsonRegion.get("type").asText();
50 String type = region.type().toString();
51 if (!jsonType.equals(type)) {
52 description.appendText("type was " + jsonType);
53 return false;
54 }
55
56 // check name
57 String jsonName = jsonRegion.get("name").asText();
58 String name = region.name();
59 if (!jsonName.equals(name)) {
60 description.appendText("name was " + jsonName);
61 return false;
62 }
63
64 // check size of master array
65 JsonNode jsonMasters = jsonRegion.get("masters");
66 if (jsonMasters.size() != region.masters().size()) {
67 description.appendText("masters size was " + jsonMasters.size());
68 return false;
69 }
70
Jian Li79359df2016-02-24 17:29:54 +090071 // 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 }
Jian Lib68b9a82016-02-23 10:25:54 +090083
84 return true;
85 }
86
Jian Li79359df2016-02-24 17:29:54 +090087 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
Jian Lib68b9a82016-02-23 10:25:54 +0900101 @Override
102 public void describeTo(Description description) {
103 description.appendText(region.toString());
104 }
105
106 /**
107 * Factory to allocate a region matcher.
108 *
109 * @param region region object we are looking for
110 * @return matcher
111 */
112 public static RegionJsonMatcher matchesRegion(Region region) {
113 return new RegionJsonMatcher(region);
114 }
115}