Jian Li | c704b67 | 2018-09-04 18:52:53 +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.NeutronConfig; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for neutron config. |
| 25 | */ |
| 26 | public final class NeutronConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final NeutronConfig neutronConfig; |
| 29 | |
| 30 | private static final String USE_METADATA_PROXY = "useMetadataProxy"; |
| 31 | private static final String METADATA_PROXY_SECRET = "metadataProxySecret"; |
| 32 | |
| 33 | private NeutronConfigJsonMatcher(NeutronConfig neutronConfig) { |
| 34 | this.neutronConfig = neutronConfig; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 39 | |
| 40 | // check useMetaDataProxy |
| 41 | JsonNode jsonUseMetadataProxy = jsonNode.get(USE_METADATA_PROXY); |
| 42 | if (jsonUseMetadataProxy != null) { |
| 43 | boolean useMetadataProxy = neutronConfig.useMetadataProxy(); |
| 44 | if (jsonUseMetadataProxy.asBoolean() != useMetadataProxy) { |
| 45 | description.appendText("useMetadataProxy was " + jsonUseMetadataProxy); |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // check metadataProxySecret |
| 51 | JsonNode jsonMetadataProxySecret = jsonNode.get(METADATA_PROXY_SECRET); |
| 52 | if (jsonMetadataProxySecret != null) { |
| 53 | String metadataProxySecret = neutronConfig.metadataProxySecret(); |
| 54 | if (!jsonMetadataProxySecret.asText().equals(metadataProxySecret)) { |
| 55 | description.appendText("metadataProxySecret was " + jsonUseMetadataProxy); |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void describeTo(Description description) { |
| 65 | description.appendText(neutronConfig.toString()); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Factory to allocate neutron config matcher. |
| 70 | * |
| 71 | * @param config neutron config object we are looking for |
| 72 | * @return matcher |
| 73 | */ |
| 74 | public static NeutronConfigJsonMatcher matchNeutronConfig(NeutronConfig config) { |
| 75 | return new NeutronConfigJsonMatcher(config); |
| 76 | } |
| 77 | } |