blob: 91088858ae4fcc793cecb5901c1f9703f4da4afc [file] [log] [blame]
Daniel Parkdeefa702018-07-17 17:55:51 +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.OpenstackSshAuth;
22
23/**
24 * Hamcrest matcher for openstack SSH auth.
25 */
26public final class OpenstackSshAuthJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final OpenstackSshAuth sshAuth;
29
30 private static final String ID = "id";
31 private static final String PASSWORD = "password";
32
33 private OpenstackSshAuthJsonMatcher(OpenstackSshAuth sshAuth) {
34 this.sshAuth = sshAuth;
35 }
36
37 @Override
38 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
39
40 // check id
41 String jsonId = jsonNode.get(ID).asText();
42 String id = sshAuth.id();
43 if (!jsonId.equals(id)) {
44 description.appendText("id was " + jsonId);
45 return false;
46 }
47
48 // check password
49 String jsonPassword = jsonNode.get(PASSWORD).asText();
50 String password = sshAuth.id();
51 if (!jsonPassword.equals(password)) {
52 description.appendText("password was " + jsonId);
53 return false;
54 }
55
56 return true;
57 }
58
59 @Override
60 public void describeTo(Description description) {
61 description.appendText(sshAuth.toString());
62 }
63
64 /**
65 * Factory to allocate an openstack SSH auth matcher.
66 *
67 * @param sshAuth openstack SSH auth object we are looking for
68 * @return matcher
69 */
70 public static OpenstackSshAuthJsonMatcher matchOpenstackSshAuth(OpenstackSshAuth sshAuth) {
71 return new OpenstackSshAuthJsonMatcher(sshAuth);
72 }
73}