blob: 81bbf10840c73010d833b2eafa72fdb139e01212 [file] [log] [blame]
Jian Li9ee9c8b2019-01-24 11:48:12 +09001/*
2 * Copyright 2019-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.k8snetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.k8snetworking.api.K8sIpam;
22
23/**
24 * Hamcrest matcher for kubernetes IPAM.
25 */
26public final class K8sIpamJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final K8sIpam ipam;
29
30 private static final String IP_ADDRESS = "ipAddress";
31 private static final String NETWORK_ID = "networkId";
32
33 private K8sIpamJsonMatcher(K8sIpam ipam) {
34 this.ipam = ipam;
35 }
36
37 @Override
38 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
39
40 // check IP address
41 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
42 String ipAddress = ipam.ipAddress().toString();
43 if (!jsonIpAddress.equals(ipAddress)) {
44 description.appendText("IP address was " + jsonIpAddress);
45 return false;
46 }
47
48 // check network ID
49 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
50 String networkId = ipam.networkId();
51 if (!jsonNetworkId.equals(networkId)) {
52 description.appendText("Network ID was " + jsonNetworkId);
53 return false;
54 }
55
56 return true;
57 }
58
59 @Override
60 public void describeTo(Description description) {
61 description.appendText(ipam.toString());
62 }
63
64 /**
65 * Factory to allocate kubernetes IPAM matcher.
66 *
67 * @param ipam kubernetes IPAM object we are looking for
68 * @return matcher
69 */
70 public static K8sIpamJsonMatcher matchesK8sIpam(K8sIpam ipam) {
71 return new K8sIpamJsonMatcher(ipam);
72 }
73}