blob: df1ac2f3ee0383eba8499114b9730936515c6e79 [file] [log] [blame]
Jian Li3defa842019-02-12 00:31:35 +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.k8snode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.k8snode.api.K8sApiConfig;
22
23/**
24 * Hamcrest matcher for kubernetes API config.
25 */
26public final class K8sApiConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final K8sApiConfig k8sApiConfig;
29
30 private static final String SCHEME = "scheme";
31 private static final String IP_ADDRESS = "ipAddress";
32 private static final String PORT = "port";
33 private static final String TOKEN = "token";
34 private static final String CA_CERT_DATA = "caCertData";
35 private static final String CLIENT_CERT_DATA = "clientCertData";
36 private static final String CLIENT_KEY_DATA = "clientKeyData";
37
38 private K8sApiConfigJsonMatcher(K8sApiConfig k8sApiConfig) {
39 this.k8sApiConfig = k8sApiConfig;
40 }
41
42 @Override
43 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
44
45 // check scheme
46 String jsonScheme = jsonNode.get(SCHEME).asText();
47 String scheme = k8sApiConfig.scheme().name();
48 if (!jsonScheme.equals(scheme)) {
49 description.appendText("scheme was " + jsonScheme);
50 return false;
51 }
52
53 // check IP address
54 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
55 String ipAddress = k8sApiConfig.ipAddress().toString();
56 if (!jsonIpAddress.equals(ipAddress)) {
57 description.appendText("ipAddress was " + jsonIpAddress);
58 return false;
59 }
60
61 // check port
62 int jsonPort = jsonNode.get(PORT).asInt();
63 int port = k8sApiConfig.port();
64 if (jsonPort != port) {
65 description.appendText("port was " + jsonPort);
66 return false;
67 }
68
69 // check token
70 JsonNode jsonToken = jsonNode.get(TOKEN);
71 String token = k8sApiConfig.token();
72 if (jsonToken != null) {
73 if (!jsonToken.asText().equals(token)) {
74 description.appendText("token was " + jsonToken);
75 return false;
76 }
77 }
78
79 // check caCertData
80 JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA);
81 String caCertData = k8sApiConfig.caCertData();
82 if (jsonCaCertData != null) {
83 if (!jsonCaCertData.asText().equals(caCertData)) {
84 description.appendText("caCertData was " + jsonCaCertData);
85 return false;
86 }
87 }
88
89 // check clientCertData
90 JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA);
91 String clientCertData = k8sApiConfig.clientCertData();
92
93 if (jsonClientCertData != null) {
94 if (!jsonClientCertData.asText().equals(clientCertData)) {
95 description.appendText("clientCertData was " + jsonClientCertData);
96 return false;
97 }
98 }
99
100 // check clientKeyData
101 JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA);
102 String clientKeyData = k8sApiConfig.clientKeyData();
103
104 if (jsonClientKeyData != null) {
105 if (!jsonClientKeyData.asText().equals(clientKeyData)) {
106 description.appendText("clientKeyData was " + jsonClientKeyData);
107 return false;
108 }
109 }
110
111 return true;
112 }
113
114 @Override
115 public void describeTo(Description description) {
116 description.appendText(k8sApiConfig.toString());
117 }
118
119 /**
120 * Factory to allocate an k8sApiConfig matcher.
121 *
122 * @param config k8sApiConfig object we are looking for
123 * @return matcher
124 */
125 public static K8sApiConfigJsonMatcher matchesK8sApiConfig(K8sApiConfig config) {
126 return new K8sApiConfigJsonMatcher(config);
127 }
128}