Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.onosproject.net.driver.Driver; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.JsonNode; |
| 25 | |
| 26 | /** |
| 27 | * Hamcrest matcher for drivers. |
| 28 | */ |
| 29 | public final class DriverJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 30 | private final Driver driver; |
| 31 | |
| 32 | private DriverJsonMatcher(Driver driver) { |
| 33 | this.driver = driver; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public boolean matchesSafely(JsonNode jsonDriver, Description description) { |
| 38 | // check id |
| 39 | String jsonDriverName = jsonDriver.get("name").asText(); |
| 40 | String driverName = driver.name(); |
| 41 | if (!jsonDriverName.equals(driverName)) { |
| 42 | description.appendText("name was " + jsonDriverName); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | // check parent |
| 48 | String jsonParent = jsonDriver.get("parent").asText(); |
| 49 | String parent = driver.parent().name(); |
| 50 | if (!jsonParent.equals(parent)) { |
| 51 | description.appendText("parent was " + jsonParent); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // check manufacturer |
| 56 | String jsonManufacturer = jsonDriver.get("manufacturer").asText(); |
| 57 | String manufacturer = driver.manufacturer(); |
| 58 | if (!jsonManufacturer.equals(manufacturer)) { |
| 59 | description.appendText("manufacturer was " + jsonManufacturer); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // check HW version |
| 64 | String jsonHWVersion = jsonDriver.get("hwVersion").asText(); |
| 65 | String hwVersion = driver.hwVersion(); |
| 66 | if (!jsonHWVersion.equals(hwVersion)) { |
| 67 | description.appendText("HW version was " + jsonHWVersion); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // check SW version |
| 72 | String jsonSWVersion = jsonDriver.get("swVersion").asText(); |
| 73 | String swVersion = driver.swVersion(); |
| 74 | if (!jsonSWVersion.equals(swVersion)) { |
| 75 | description.appendText("SW version was " + jsonSWVersion); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Check properties |
| 80 | JsonNode jsonProperties = jsonDriver.get("properties"); |
| 81 | if (driver.properties().size() != jsonProperties.size()) { |
| 82 | description.appendText("properties map size was was " + jsonProperties.size()); |
| 83 | return false; |
| 84 | } |
| 85 | for (Map.Entry<String, String> entry : driver.properties().entrySet()) { |
| 86 | boolean propertyFound = false; |
| 87 | for (int propertyIndex = 0; propertyIndex < jsonProperties.size(); propertyIndex++) { |
| 88 | String jsonName = jsonProperties.get(propertyIndex).get("name").asText(); |
| 89 | String jsonValue = jsonProperties.get(propertyIndex).get("value").asText(); |
| 90 | if (!jsonName.equals(entry.getKey()) || |
| 91 | !jsonValue.equals(entry.getValue())) { |
| 92 | propertyFound = true; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | if (!propertyFound) { |
| 97 | description.appendText("property not found " + entry.getKey()); |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void describeTo(Description description) { |
| 106 | description.appendText(driver.toString()); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Factory to allocate a driver matcher. |
| 111 | * |
| 112 | * @param driver driver object we are looking for |
| 113 | * @return matcher |
| 114 | */ |
| 115 | public static DriverJsonMatcher matchesDriver(Driver driver) { |
| 116 | return new DriverJsonMatcher(driver); |
| 117 | } |
| 118 | } |