Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Jian Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 22 | import org.onosproject.cluster.NodeId; |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 23 | import org.onosproject.net.region.Region; |
| 24 | |
Jian Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 25 | import java.util.Set; |
| 26 | |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 27 | /** |
| 28 | * Hamcrest matcher for region. |
| 29 | */ |
| 30 | public 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 Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 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 | } |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
Jian Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 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 | |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 101 | @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 | } |