blob: bed83ad09b594d03fa19636cacff401c63ef8f68 [file] [log] [blame]
Jian Li27841662018-04-14 01:59:47 +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.OpenstackAuth;
22
23/**
Daniel Parkdeefa702018-07-17 17:55:51 +090024 * Hamcrest matcher for openstack auth.
Jian Li27841662018-04-14 01:59:47 +090025 */
26public final class OpenstackAuthJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final OpenstackAuth auth;
29
30 private static final String VERSION = "version";
31 private static final String PORT = "port";
32 private static final String PROTOCOL = "protocol";
33 private static final String USERNAME = "username";
34 private static final String PASSWORD = "password";
35 private static final String PROJECT = "project";
36 private static final String PERSPECTIVE = "perspective";
37
38 private OpenstackAuthJsonMatcher(OpenstackAuth auth) {
39 this.auth = auth;
40 }
41
42 @Override
43 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
44
45 // check version
46 String jsonVersion = jsonNode.get(VERSION).asText();
47 String version = auth.version();
48 if (!jsonVersion.equals(version)) {
49 description.appendText("version was " + jsonVersion);
50 return false;
51 }
52
Jian Li27841662018-04-14 01:59:47 +090053 // check protocol
54 String jsonProtocol = jsonNode.get(PROTOCOL).asText();
55 String protocol = auth.protocol().name();
56 if (!jsonProtocol.equals(protocol)) {
57 description.appendText("protocol was " + jsonProtocol);
58 return false;
59 }
60
61 // check username
62 String jsonUsername = jsonNode.get(USERNAME).asText();
63 String username = auth.username();
64 if (!jsonUsername.equals(username)) {
65 description.appendText("username was " + jsonUsername);
66 return false;
67 }
68
69 // check password
70 String jsonPassword = jsonNode.get(PASSWORD).asText();
71 String password = auth.password();
72 if (!jsonPassword.equals(password)) {
73 description.appendText("password was " + jsonPassword);
74 return false;
75 }
76
77 // check project
78 String jsonProject = jsonNode.get(PROJECT).asText();
79 String project = auth.project();
80 if (!jsonProject.equals(project)) {
81 description.appendText("project was " + jsonProject);
82 return false;
83 }
84
85 // check perspective
86 String jsonPerspective = jsonNode.get(PERSPECTIVE).asText();
87 String perspective = auth.perspective().name();
88 if (!jsonPerspective.equals(perspective)) {
89 description.appendText("perspective was " + jsonPerspective);
90 return false;
91 }
92
93 return true;
94 }
95
96 @Override
97 public void describeTo(Description description) {
98 description.appendText(auth.toString());
99 }
100
101 /**
102 * Factory to allocate an openstack auth matcher.
103 *
104 * @param auth openstack auth object we are looking for
105 * @return matcher
106 */
107 public static OpenstackAuthJsonMatcher matchOpenstackAuth(OpenstackAuth auth) {
108 return new OpenstackAuthJsonMatcher(auth);
109 }
110}