blob: 2eb42b9d39ef452593768e1dd3f568c401938a70 [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";
Jian Li1cee9882019-02-13 11:25:25 +090033 private static final String STATE = "state";
Jian Li3defa842019-02-12 00:31:35 +090034 private static final String TOKEN = "token";
35 private static final String CA_CERT_DATA = "caCertData";
36 private static final String CLIENT_CERT_DATA = "clientCertData";
37 private static final String CLIENT_KEY_DATA = "clientKeyData";
38
39 private K8sApiConfigJsonMatcher(K8sApiConfig k8sApiConfig) {
40 this.k8sApiConfig = k8sApiConfig;
41 }
42
43 @Override
44 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
45
46 // check scheme
47 String jsonScheme = jsonNode.get(SCHEME).asText();
48 String scheme = k8sApiConfig.scheme().name();
49 if (!jsonScheme.equals(scheme)) {
50 description.appendText("scheme was " + jsonScheme);
51 return false;
52 }
53
54 // check IP address
55 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
56 String ipAddress = k8sApiConfig.ipAddress().toString();
57 if (!jsonIpAddress.equals(ipAddress)) {
58 description.appendText("ipAddress was " + jsonIpAddress);
59 return false;
60 }
61
62 // check port
63 int jsonPort = jsonNode.get(PORT).asInt();
64 int port = k8sApiConfig.port();
65 if (jsonPort != port) {
66 description.appendText("port was " + jsonPort);
67 return false;
68 }
69
Jian Li1cee9882019-02-13 11:25:25 +090070 // check state
71 JsonNode jsonState = jsonNode.get(STATE);
72 String state = k8sApiConfig.state().name();
73 if (jsonState != null) {
74 if (!jsonState.asText().equals(state)) {
75 description.appendText("state was " + jsonState);
76 return false;
77 }
78 }
79
Jian Li3defa842019-02-12 00:31:35 +090080 // check token
81 JsonNode jsonToken = jsonNode.get(TOKEN);
82 String token = k8sApiConfig.token();
83 if (jsonToken != null) {
84 if (!jsonToken.asText().equals(token)) {
85 description.appendText("token was " + jsonToken);
86 return false;
87 }
88 }
89
90 // check caCertData
91 JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA);
92 String caCertData = k8sApiConfig.caCertData();
93 if (jsonCaCertData != null) {
94 if (!jsonCaCertData.asText().equals(caCertData)) {
95 description.appendText("caCertData was " + jsonCaCertData);
96 return false;
97 }
98 }
99
100 // check clientCertData
101 JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA);
102 String clientCertData = k8sApiConfig.clientCertData();
103
104 if (jsonClientCertData != null) {
105 if (!jsonClientCertData.asText().equals(clientCertData)) {
106 description.appendText("clientCertData was " + jsonClientCertData);
107 return false;
108 }
109 }
110
111 // check clientKeyData
112 JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA);
113 String clientKeyData = k8sApiConfig.clientKeyData();
114
115 if (jsonClientKeyData != null) {
116 if (!jsonClientKeyData.asText().equals(clientKeyData)) {
117 description.appendText("clientKeyData was " + jsonClientKeyData);
118 return false;
119 }
120 }
121
122 return true;
123 }
124
125 @Override
126 public void describeTo(Description description) {
127 description.appendText(k8sApiConfig.toString());
128 }
129
130 /**
131 * Factory to allocate an k8sApiConfig matcher.
132 *
133 * @param config k8sApiConfig object we are looking for
134 * @return matcher
135 */
136 public static K8sApiConfigJsonMatcher matchesK8sApiConfig(K8sApiConfig config) {
137 return new K8sApiConfigJsonMatcher(config);
138 }
139}