blob: 1b99ae31e821591ce9d8b5be20b77892fbe7ae6a [file] [log] [blame]
Jian Lic704b672018-09-04 18:52:53 +09001/*
2 * Copyright 2018-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.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacknode.api.KeystoneConfig;
22import org.onosproject.openstacknode.api.OpenstackAuth;
23
24/**
25 * Hamcrest matcher for keystone config.
26 */
27public final class KeystoneConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private final KeystoneConfig keystoneConfig;
30
31 private static final String ENDPOINT = "endpoint";
32 private static final String AUTHENTICATION = "authentication";
33
34 private KeystoneConfigJsonMatcher(KeystoneConfig keystoneConfig) {
35 this.keystoneConfig = keystoneConfig;
36 }
37
38 @Override
39 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
40
41 // check endpoint
42 JsonNode jsonEndpoint = jsonNode.get(ENDPOINT);
43 if (jsonEndpoint != null) {
44 String endpoint = keystoneConfig.endpoint();
45 if (!jsonEndpoint.asText().equals(endpoint)) {
46 description.appendText("endpoint was " + jsonEndpoint);
47 return false;
48 }
49 }
50
51 // check openstack auth
52 JsonNode jsonAuth = jsonNode.get(AUTHENTICATION);
53 if (jsonAuth != null) {
54 OpenstackAuth auth = keystoneConfig.authentication();
55 OpenstackAuthJsonMatcher authMatcher =
56 OpenstackAuthJsonMatcher.matchOpenstackAuth(auth);
57 return authMatcher.matches(jsonAuth);
58 }
59
60 return true;
61 }
62
63 @Override
64 public void describeTo(Description description) {
65 description.appendText(keystoneConfig.toString());
66 }
67
68 /**
69 * Factory to allocate keystone config matcher.
70 *
71 * @param config keystone config object we are looking for
72 * @return matcher
73 */
74 public static KeystoneConfigJsonMatcher matchKeystoneConfig(KeystoneConfig config) {
75 return new KeystoneConfigJsonMatcher(config);
76 }
77}