blob: 1e51616beeb15d7dd111a743276e798c65276b93 [file] [log] [blame]
Jian Licc01e452020-12-21 01:44:05 +09001/*
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 */
16package org.onosproject.kubevirtnode.codec;
17
18import com.eclipsesource.json.JsonArray;
19import com.eclipsesource.json.JsonObject;
20import org.hamcrest.Description;
21import org.hamcrest.TypeSafeMatcher;
22import org.onosproject.kubevirtnode.api.KubevirtNode;
23
24/**
25 * Hamcrest matcher for kubevirt node array.
26 */
27public class KubevirtNodeJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
28
29 private final KubevirtNode node;
30 private String reason = "";
31
32 public KubevirtNodeJsonArrayMatcher(KubevirtNode node) {
33 this.node = node;
34 }
35
36 @Override
37 protected boolean matchesSafely(JsonArray json) {
38 boolean nodeFound = false;
39 for (int jsonNodeIndex = 0; jsonNodeIndex < json.size(); jsonNodeIndex++) {
40 final JsonObject jsonNode = json.get(jsonNodeIndex).asObject();
41
42 final String hostname = node.hostname();
43 final String jsonHostname = jsonNode.get("hostname").asString();
44 if (jsonHostname.equals(hostname)) {
45 nodeFound = true;
46 }
47 }
48
49 if (!nodeFound) {
50 reason = "Node with hostname " + node.hostname() + " not found";
51 return false;
52 } else {
53 return true;
54 }
55 }
56
57 @Override
58 public void describeTo(Description description) {
59 description.appendText(reason);
60 }
61
62 /**
63 * Factory to allocate a node array matcher.
64 *
65 * @param node node object we are looking for
66 * @return matcher
67 */
68 public static KubevirtNodeJsonArrayMatcher hasNode(KubevirtNode node) {
69 return new KubevirtNodeJsonArrayMatcher(node);
70 }
71}