blob: 62173350630fe58de739f0547af4e65f41c27373 [file] [log] [blame]
Jian Lifeb84802021-01-12 16:34:49 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
22
23/**
24 * Hamcrest matcher for kubevirt IP pool interface.
25 */
26public final class KubevirtIpPoolJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final KubevirtIpPool ipPool;
29 private static final String START = "start";
30 private static final String END = "end";
31
32 private KubevirtIpPoolJsonMatcher(KubevirtIpPool ipPool) {
33 this.ipPool = ipPool;
34 }
35
36 @Override
37 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
38 // check start
39 String jsonStart = jsonNode.get(START).asText();
40 String start = ipPool.start().toString();
41 if (!jsonStart.equals(start)) {
42 description.appendText("start was " + jsonStart);
43 return false;
44 }
45
46 // check end
47 String jsonEnd = jsonNode.get(END).asText();
48 String end = ipPool.end().toString();
49 if (!jsonEnd.equals(end)) {
50 description.appendText("end was " + jsonEnd);
51 return false;
52 }
53
54 return true;
55 }
56
57 @Override
58 public void describeTo(Description description) {
59 description.appendText(ipPool.toString());
60 }
61
62 /**
63 * Factory to allocate an kubevirt IP pool matcher.
64 *
65 * @param ipPool kubevirt IP pool object we are looking for
66 * @return matcher
67 */
68 public static KubevirtIpPoolJsonMatcher matchesKubevirtIpPool(KubevirtIpPool ipPool) {
69 return new KubevirtIpPoolJsonMatcher(ipPool);
70 }
71}