summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/common/src/test/java/org/onosproject/codec/impl/DriverJsonMatcher.java
blob: 6f0070e5b90722b118f6fe79ca5002ab92b69eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.codec.impl;

import java.util.Map;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.net.driver.Driver;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * Hamcrest matcher for drivers.
 */
public final class DriverJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
    private final Driver driver;

    private DriverJsonMatcher(Driver driver) {
        this.driver = driver;
    }

    @Override
    public boolean matchesSafely(JsonNode jsonDriver, Description description) {
        // check id
        String jsonDriverName = jsonDriver.get("name").asText();
        String driverName = driver.name();
        if (!jsonDriverName.equals(driverName)) {
            description.appendText("name was " + jsonDriverName);
            return false;
        }


        // check parent
        String jsonParent = jsonDriver.get("parent").asText();
        String parent = driver.parent().name();
        if (!jsonParent.equals(parent)) {
            description.appendText("parent was " + jsonParent);
            return false;
        }

        // check manufacturer
        String jsonManufacturer = jsonDriver.get("manufacturer").asText();
        String manufacturer = driver.manufacturer();
        if (!jsonManufacturer.equals(manufacturer)) {
            description.appendText("manufacturer was " + jsonManufacturer);
            return false;
        }

        // check HW version
        String jsonHWVersion = jsonDriver.get("hwVersion").asText();
        String hwVersion = driver.hwVersion();
        if (!jsonHWVersion.equals(hwVersion)) {
            description.appendText("HW version was " + jsonHWVersion);
            return false;
        }

        // check SW version
        String jsonSWVersion = jsonDriver.get("swVersion").asText();
        String swVersion = driver.swVersion();
        if (!jsonSWVersion.equals(swVersion)) {
            description.appendText("SW version was " + jsonSWVersion);
            return false;
        }

        // Check properties
        JsonNode jsonProperties = jsonDriver.get("properties");
        if (driver.properties().size() != jsonProperties.size()) {
            description.appendText("properties map size was was " + jsonProperties.size());
            return false;
        }
        for (Map.Entry<String, String> entry : driver.properties().entrySet()) {
            boolean propertyFound = false;
            for (int propertyIndex = 0; propertyIndex < jsonProperties.size(); propertyIndex++) {
                String jsonName = jsonProperties.get(propertyIndex).get("name").asText();
                String jsonValue = jsonProperties.get(propertyIndex).get("value").asText();
                if (!jsonName.equals(entry.getKey()) ||
                        !jsonValue.equals(entry.getValue())) {
                    propertyFound = true;
                    break;
                }
            }
            if (!propertyFound) {
                description.appendText("property not found " + entry.getKey());
                return false;
            }
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(driver.toString());
    }

    /**
     * Factory to allocate a driver matcher.
     *
     * @param driver driver object we are looking for
     * @return matcher
     */
    public static DriverJsonMatcher matchesDriver(Driver driver) {
        return new DriverJsonMatcher(driver);
    }
}