aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java
blob: 14bb45f37deba8db6e3ea8774c1538996a497bd9 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * 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.faultmanagement.web;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * Hamcrest matcher for alarms.
 */
public final class AlarmJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {

    private final Alarm alarm;

    private AlarmJsonMatcher(final Alarm alarm) {
        this.alarm = alarm;
    }

    @Override
    public boolean matchesSafely(final JsonNode jsonAlarm, final Description description) {
        final String jsonAlarmId = jsonAlarm.get("id").asText();
        final String alarmId = Long.toString(alarm.id().fingerprint());
        if (!jsonAlarmId.equals(alarmId)) {
            description.appendText("alarm id was " + jsonAlarmId);
            return false;
        }

        final String jsonDeviceId = jsonAlarm.get("deviceId").asText();
        final String alarmDeviceId = alarm.deviceId().toString();
        if (!jsonDeviceId.equals(alarmDeviceId)) {
            description.appendText("DeviceId was " + jsonDeviceId);
            return false;
        }


        final String jsonDescription = jsonAlarm.get("description").asText();
        final String alarmDesc = alarm.description();
        if (!jsonDescription.equals(alarmDesc)) {
            description.appendText("description was " + jsonDescription);
            return false;
        }

        final long jsonTimeRaised = jsonAlarm.get("timeRaised").asLong();
        final long timeRaised = alarm.timeRaised();
        if (timeRaised != jsonTimeRaised) {
            description.appendText("timeRaised was " + jsonTimeRaised);
            return false;
        }


        final long jsonTimeUpdated = jsonAlarm.get("timeUpdated").asLong();
        final long timeUpdated = alarm.timeUpdated();
        if (timeUpdated != jsonTimeUpdated) {
            description.appendText("timeUpdated was " + jsonTimeUpdated);
            return false;
        }

        final JsonNode jsonTimeClearedNode = jsonAlarm.get("timeCleared");

        if (alarm.timeCleared() != null) {
            final Long jsonTimeCleared = jsonTimeClearedNode.longValue();
            final Long timeCleared = alarm.timeCleared();

            if (!timeCleared.equals(jsonTimeCleared)) {
                description.appendText("Time Cleared was " + jsonTimeCleared);
                return false;
            }
        } else {
            //  No clear time not specified, JSON representation must be empty
            if (!jsonTimeClearedNode.isNull()) {
                description.appendText("Time Cleared should be null");
                return false;
            }
        }

        final String jsonSeverity = jsonAlarm.get("severity").asText();
        final String severity = alarm.severity().toString();
        if (!severity.equals(jsonSeverity)) {
            description.appendText("severity was " + jsonSeverity);
            return false;
        }

        final JsonNode jsonAlarmNode = jsonAlarm.get("source");

        if (alarm.source() != null) {
            final String jsonSource = jsonAlarmNode.textValue();
            final String source = alarm.source().toString();

            if (!source.equals(jsonSource)) {
                description.appendText("source was " + jsonSource);
                return false;
            }
        } else {
            //  source not specified, JSON representation must be empty
            if (!jsonAlarmNode.isNull()) {
                description.appendText("source should be null");
                return false;
            }
        }

        // In progress
        return true;
    }

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

    /**
     * Factory to allocate a alarm matcher.
     *
     * @param alarm alarm object we are looking for
     * @return matcher
     */
    public static AlarmJsonMatcher matchesAlarm(final Alarm alarm) {
        return new AlarmJsonMatcher(alarm);
    }
}