Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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.k8snode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.onosproject.k8snode.api.HostNodesInfo; |
| 23 | |
| 24 | /** |
| 25 | * Hamcrest matcher for HostNodesInfo. |
| 26 | */ |
| 27 | public final class HostNodesInfoJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 28 | |
| 29 | private final HostNodesInfo hostNodesInfo; |
| 30 | |
| 31 | private static final String HOST_IP = "hostIp"; |
| 32 | private static final String NODES = "nodes"; |
| 33 | |
| 34 | private HostNodesInfoJsonMatcher(HostNodesInfo hostNodesInfo) { |
| 35 | this.hostNodesInfo = hostNodesInfo; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 40 | |
| 41 | // check host IP |
| 42 | String jsonHostIp = jsonNode.get(HOST_IP).asText(); |
| 43 | String hostIp = hostNodesInfo.hostIp().toString(); |
| 44 | if (!jsonHostIp.equals(hostIp)) { |
| 45 | description.appendText("host IP was " + jsonHostIp); |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | // check nodes |
| 50 | JsonNode jsonNodes = jsonNode.get(NODES); |
| 51 | if (jsonNodes.size() != hostNodesInfo.nodes().size()) { |
| 52 | description.appendText("Nodes size was " + jsonNodes.size()); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | boolean nodeFound = true; |
| 57 | ArrayNode jsonNodeArray = (ArrayNode) jsonNodes; |
| 58 | for (JsonNode jsonNodeTmp : jsonNodeArray) { |
| 59 | if (!hostNodesInfo.nodes().contains(jsonNodeTmp.asText())) { |
| 60 | nodeFound = false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!nodeFound) { |
| 65 | description.appendText("Node not found"); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void describeTo(Description description) { |
| 74 | description.appendText(hostNodesInfo.toString()); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Factory to allocate a hostNodesInfo matcher. |
| 79 | * |
| 80 | * @param info host IP address to nodes mapping info |
| 81 | * @return matcher |
| 82 | */ |
| 83 | public static HostNodesInfoJsonMatcher matchesHostNodesInfo(HostNodesInfo info) { |
| 84 | return new HostNodesInfoJsonMatcher(info); |
| 85 | } |
| 86 | } |