blob: 3934ea9b3ad15060668231d9df97ada02906c6a8 [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
Jian Li9b199162019-02-10 18:00:35 +090030 private static final String IPAM_ID = "ipamId";
Jian Li9ee9c8b2019-01-24 11:48:12 +090031 private static final String IP_ADDRESS = "ipAddress";
32 private static final String NETWORK_ID = "networkId";
33
34 private K8sIpamJsonMatcher(K8sIpam ipam) {
35 this.ipam = ipam;
36 }
37
38 @Override
39 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
40
Jian Li9b199162019-02-10 18:00:35 +090041 // check IPAM ID
42 String jsonIpamId = jsonNode.get(IPAM_ID).asText();
43 String ipamId = ipam.ipamId();
44 if (!jsonIpamId.equals(ipamId)) {
45 description.appendText("IPAM ID was " + jsonIpamId);
46 return false;
47 }
48
Jian Li9ee9c8b2019-01-24 11:48:12 +090049 // check IP address
50 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
51 String ipAddress = ipam.ipAddress().toString();
52 if (!jsonIpAddress.equals(ipAddress)) {
53 description.appendText("IP address was " + jsonIpAddress);
54 return false;
55 }
56
57 // check network ID
58 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
59 String networkId = ipam.networkId();
60 if (!jsonNetworkId.equals(networkId)) {
61 description.appendText("Network ID was " + jsonNetworkId);
62 return false;
63 }
64
65 return true;
66 }
67
68 @Override
69 public void describeTo(Description description) {
70 description.appendText(ipam.toString());
71 }
72
73 /**
74 * Factory to allocate kubernetes IPAM matcher.
75 *
76 * @param ipam kubernetes IPAM object we are looking for
77 * @return matcher
78 */
79 public static K8sIpamJsonMatcher matchesK8sIpam(K8sIpam ipam) {
80 return new K8sIpamJsonMatcher(ipam);
81 }
82}