Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.openstacknode.api.OpenstackAuth; |
| 22 | |
| 23 | /** |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame^] | 24 | * Hamcrest matcher for openstack auth. |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 25 | */ |
| 26 | public 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 | |
| 53 | // check port |
| 54 | Integer jsonPort = jsonNode.get(PORT).asInt(); |
| 55 | Integer port = auth.port(); |
| 56 | if (!jsonPort.equals(port)) { |
| 57 | description.appendText("port was " + jsonPort); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // check protocol |
| 62 | String jsonProtocol = jsonNode.get(PROTOCOL).asText(); |
| 63 | String protocol = auth.protocol().name(); |
| 64 | if (!jsonProtocol.equals(protocol)) { |
| 65 | description.appendText("protocol was " + jsonProtocol); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // check username |
| 70 | String jsonUsername = jsonNode.get(USERNAME).asText(); |
| 71 | String username = auth.username(); |
| 72 | if (!jsonUsername.equals(username)) { |
| 73 | description.appendText("username was " + jsonUsername); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // check password |
| 78 | String jsonPassword = jsonNode.get(PASSWORD).asText(); |
| 79 | String password = auth.password(); |
| 80 | if (!jsonPassword.equals(password)) { |
| 81 | description.appendText("password was " + jsonPassword); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // check project |
| 86 | String jsonProject = jsonNode.get(PROJECT).asText(); |
| 87 | String project = auth.project(); |
| 88 | if (!jsonProject.equals(project)) { |
| 89 | description.appendText("project was " + jsonProject); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // check perspective |
| 94 | String jsonPerspective = jsonNode.get(PERSPECTIVE).asText(); |
| 95 | String perspective = auth.perspective().name(); |
| 96 | if (!jsonPerspective.equals(perspective)) { |
| 97 | description.appendText("perspective was " + jsonPerspective); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void describeTo(Description description) { |
| 106 | description.appendText(auth.toString()); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Factory to allocate an openstack auth matcher. |
| 111 | * |
| 112 | * @param auth openstack auth object we are looking for |
| 113 | * @return matcher |
| 114 | */ |
| 115 | public static OpenstackAuthJsonMatcher matchOpenstackAuth(OpenstackAuth auth) { |
| 116 | return new OpenstackAuthJsonMatcher(auth); |
| 117 | } |
| 118 | } |