blob: 14bb45f37deba8db6e3ea8774c1538996a497bd9 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.faultmanagement.web;
17
18import org.hamcrest.Description;
19import org.hamcrest.TypeSafeDiagnosingMatcher;
20import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
21
22import com.fasterxml.jackson.databind.JsonNode;
23
24/**
25 * Hamcrest matcher for alarms.
26 */
27public final class AlarmJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private final Alarm alarm;
30
31 private AlarmJsonMatcher(final Alarm alarm) {
32 this.alarm = alarm;
33 }
34
35 @Override
36 public boolean matchesSafely(final JsonNode jsonAlarm, final Description description) {
37 final String jsonAlarmId = jsonAlarm.get("id").asText();
38 final String alarmId = Long.toString(alarm.id().fingerprint());
39 if (!jsonAlarmId.equals(alarmId)) {
40 description.appendText("alarm id was " + jsonAlarmId);
41 return false;
42 }
43
44 final String jsonDeviceId = jsonAlarm.get("deviceId").asText();
45 final String alarmDeviceId = alarm.deviceId().toString();
46 if (!jsonDeviceId.equals(alarmDeviceId)) {
47 description.appendText("DeviceId was " + jsonDeviceId);
48 return false;
49 }
50
51
52 final String jsonDescription = jsonAlarm.get("description").asText();
53 final String alarmDesc = alarm.description();
54 if (!jsonDescription.equals(alarmDesc)) {
55 description.appendText("description was " + jsonDescription);
56 return false;
57 }
58
59 final long jsonTimeRaised = jsonAlarm.get("timeRaised").asLong();
60 final long timeRaised = alarm.timeRaised();
61 if (timeRaised != jsonTimeRaised) {
62 description.appendText("timeRaised was " + jsonTimeRaised);
63 return false;
64 }
65
66
67 final long jsonTimeUpdated = jsonAlarm.get("timeUpdated").asLong();
68 final long timeUpdated = alarm.timeUpdated();
69 if (timeUpdated != jsonTimeUpdated) {
70 description.appendText("timeUpdated was " + jsonTimeUpdated);
71 return false;
72 }
73
74 final JsonNode jsonTimeClearedNode = jsonAlarm.get("timeCleared");
75
76 if (alarm.timeCleared() != null) {
77 final Long jsonTimeCleared = jsonTimeClearedNode.longValue();
78 final Long timeCleared = alarm.timeCleared();
79
80 if (!timeCleared.equals(jsonTimeCleared)) {
81 description.appendText("Time Cleared was " + jsonTimeCleared);
82 return false;
83 }
84 } else {
85 // No clear time not specified, JSON representation must be empty
86 if (!jsonTimeClearedNode.isNull()) {
87 description.appendText("Time Cleared should be null");
88 return false;
89 }
90 }
91
92 final String jsonSeverity = jsonAlarm.get("severity").asText();
93 final String severity = alarm.severity().toString();
94 if (!severity.equals(jsonSeverity)) {
95 description.appendText("severity was " + jsonSeverity);
96 return false;
97 }
98
99 final JsonNode jsonAlarmNode = jsonAlarm.get("source");
100
101 if (alarm.source() != null) {
102 final String jsonSource = jsonAlarmNode.textValue();
103 final String source = alarm.source().toString();
104
105 if (!source.equals(jsonSource)) {
106 description.appendText("source was " + jsonSource);
107 return false;
108 }
109 } else {
110 // source not specified, JSON representation must be empty
111 if (!jsonAlarmNode.isNull()) {
112 description.appendText("source should be null");
113 return false;
114 }
115 }
116
117 // In progress
118 return true;
119 }
120
121 @Override
122 public void describeTo(final Description description) {
123 description.appendText(alarm.toString());
124 }
125
126 /**
127 * Factory to allocate a alarm matcher.
128 *
129 * @param alarm alarm object we are looking for
130 * @return matcher
131 */
132 public static AlarmJsonMatcher matchesAlarm(final Alarm alarm) {
133 return new AlarmJsonMatcher(alarm);
134 }
135}