Jian Li | 789fadb | 2018-07-10 13: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.net.behaviour.ControllerInfo; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for openstack controller. |
| 25 | */ |
| 26 | public final class OpenstackControllerJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final ControllerInfo controller; |
| 29 | private static final String IP = "ip"; |
| 30 | private static final String PORT = "port"; |
| 31 | |
| 32 | private OpenstackControllerJsonMatcher(ControllerInfo controller) { |
| 33 | this.controller = controller; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 38 | |
| 39 | // check IP address |
| 40 | String jsonIp = jsonNode.get(IP).asText(); |
| 41 | String ip = controller.ip().toString(); |
| 42 | if (!jsonIp.equals(ip)) { |
| 43 | description.appendText("controller IP was " + jsonIp); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | // check port |
| 48 | int jsonPort = jsonNode.get(PORT).asInt(); |
| 49 | int port = controller.port(); |
| 50 | |
| 51 | if (jsonPort != port) { |
| 52 | description.appendText("controller port was " + jsonPort); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void describeTo(Description description) { |
| 61 | description.appendText(controller.toString()); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Factory to allocate an openstack controller matcher. |
| 66 | * |
| 67 | * @param controller openstack controller object we are looking for |
| 68 | * @return matcher |
| 69 | */ |
| 70 | public static OpenstackControllerJsonMatcher |
| 71 | matchesOpenstackController(ControllerInfo controller) { |
| 72 | return new OpenstackControllerJsonMatcher(controller); |
| 73 | } |
| 74 | } |