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.eclipsesource.json.JsonArray; |
| 19 | import com.eclipsesource.json.JsonObject; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeMatcher; |
| 22 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 23 | |
| 24 | public final class KubevirtNetworkJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 25 | |
| 26 | private final KubevirtNetwork network; |
| 27 | private String reason = ""; |
| 28 | |
| 29 | public KubevirtNetworkJsonArrayMatcher(KubevirtNetwork network) { |
| 30 | this.network = network; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | protected boolean matchesSafely(JsonArray json) { |
| 35 | boolean networkFound = false; |
| 36 | for (int jsonNetworkIndex = 0; jsonNetworkIndex < json.size(); jsonNetworkIndex++) { |
| 37 | final JsonObject jsonNode = json.get(jsonNetworkIndex).asObject(); |
| 38 | |
| 39 | final String networkId = network.networkId(); |
| 40 | final String jsonNetworkId = jsonNode.get("networkId").asString(); |
| 41 | if (jsonNetworkId.equals(networkId)) { |
| 42 | networkFound = true; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (!networkFound) { |
| 47 | reason = "Network with networkId " + network.networkId() + " not found"; |
| 48 | return false; |
| 49 | } else { |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void describeTo(Description description) { |
| 56 | description.appendText(reason); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Factory to allocate a network array matcher. |
| 61 | * |
| 62 | * @param network network object we are looking for |
| 63 | * @return matcher |
| 64 | */ |
| 65 | public static KubevirtNetworkJsonArrayMatcher hasNetwork(KubevirtNetwork network) { |
| 66 | return new KubevirtNetworkJsonArrayMatcher(network); |
| 67 | } |
| 68 | } |