Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for kubevirt host route interface. |
| 25 | */ |
| 26 | public final class KubevirtHostRouteJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final KubevirtHostRoute hostRoute; |
| 29 | |
| 30 | private static final String DESTINATION = "destination"; |
| 31 | private static final String NEXTHOP = "nexthop"; |
| 32 | |
| 33 | private KubevirtHostRouteJsonMatcher(KubevirtHostRoute hostRoute) { |
| 34 | this.hostRoute = hostRoute; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 39 | // check destination |
| 40 | String jsonDestination = jsonNode.get(DESTINATION).asText(); |
| 41 | String destination = hostRoute.destination().toString(); |
| 42 | if (!jsonDestination.equals(destination)) { |
| 43 | description.appendText("destination was " + jsonDestination); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | // check nexthop |
| 48 | JsonNode jsonNexthop = jsonNode.get(NEXTHOP); |
| 49 | if (jsonNexthop != null) { |
| 50 | String nexthop = hostRoute.nexthop().toString(); |
| 51 | if (!jsonNexthop.asText().equals(nexthop)) { |
| 52 | description.appendText("nexthop was " + jsonNexthop); |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void describeTo(Description description) { |
| 62 | description.appendText(description.toString()); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Factory to allocate an kubevirt host route matcher. |
| 67 | * |
| 68 | * @param route kubevirt host route object we are looking for |
| 69 | * @return matcher |
| 70 | */ |
| 71 | public static KubevirtHostRouteJsonMatcher matchesKubevirtHostRoute(KubevirtHostRoute route) { |
| 72 | return new KubevirtHostRouteJsonMatcher(route); |
| 73 | } |
| 74 | } |