blob: 0e9c8f0bc0e6422ad7e7594ba63b3c6da53e4447 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
kmcpeake4fe18c82015-11-17 20:07:39 +00003 *
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
Andrea Campanella65f9eb92017-05-02 11:36:14 -070018import com.fasterxml.jackson.databind.JsonNode;
kmcpeake4fe18c82015-11-17 20:07:39 +000019import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
22
kmcpeake4fe18c82015-11-17 20:07:39 +000023/**
24 * Hamcrest matcher for alarms.
25 */
26public final class AlarmJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final Alarm alarm;
29
kmcpeakea5404812015-12-08 11:52:50 +000030 private AlarmJsonMatcher(Alarm alarm) {
kmcpeake4fe18c82015-11-17 20:07:39 +000031 this.alarm = alarm;
32 }
33
34 @Override
kmcpeakea5404812015-12-08 11:52:50 +000035 public boolean matchesSafely(JsonNode jsonAlarm, Description description) {
kmcpeakeb172d5f2015-12-10 11:30:43 +000036 String jsonAlarmId = jsonAlarm.get("id").asText();
Andrea Campanella65f9eb92017-05-02 11:36:14 -070037 String alarmId = alarm.id().toString();
kmcpeake4fe18c82015-11-17 20:07:39 +000038 if (!jsonAlarmId.equals(alarmId)) {
39 description.appendText("alarm id was " + jsonAlarmId);
40 return false;
41 }
42
kmcpeakeb172d5f2015-12-10 11:30:43 +000043 String jsonDeviceId = jsonAlarm.get("deviceId").asText();
44 String alarmDeviceId = alarm.deviceId().toString();
kmcpeake4fe18c82015-11-17 20:07:39 +000045 if (!jsonDeviceId.equals(alarmDeviceId)) {
46 description.appendText("DeviceId was " + jsonDeviceId);
47 return false;
48 }
49
50
kmcpeakeb172d5f2015-12-10 11:30:43 +000051 String jsonDescription = jsonAlarm.get("description").asText();
52 String alarmDesc = alarm.description();
kmcpeake4fe18c82015-11-17 20:07:39 +000053 if (!jsonDescription.equals(alarmDesc)) {
54 description.appendText("description was " + jsonDescription);
55 return false;
56 }
57
kmcpeakeb172d5f2015-12-10 11:30:43 +000058 long jsonTimeRaised = jsonAlarm.get("timeRaised").asLong();
59 long timeRaised = alarm.timeRaised();
kmcpeake4fe18c82015-11-17 20:07:39 +000060 if (timeRaised != jsonTimeRaised) {
61 description.appendText("timeRaised was " + jsonTimeRaised);
62 return false;
63 }
64
65
kmcpeakeb172d5f2015-12-10 11:30:43 +000066 long jsonTimeUpdated = jsonAlarm.get("timeUpdated").asLong();
67 long timeUpdated = alarm.timeUpdated();
kmcpeake4fe18c82015-11-17 20:07:39 +000068 if (timeUpdated != jsonTimeUpdated) {
69 description.appendText("timeUpdated was " + jsonTimeUpdated);
70 return false;
71 }
72
kmcpeakeb172d5f2015-12-10 11:30:43 +000073 JsonNode jsonTimeClearedNode = jsonAlarm.get("timeCleared");
kmcpeake4fe18c82015-11-17 20:07:39 +000074
75 if (alarm.timeCleared() != null) {
kmcpeakeb172d5f2015-12-10 11:30:43 +000076 Long jsonTimeCleared = jsonTimeClearedNode.longValue();
77 Long timeCleared = alarm.timeCleared();
kmcpeake4fe18c82015-11-17 20:07:39 +000078
79 if (!timeCleared.equals(jsonTimeCleared)) {
80 description.appendText("Time Cleared was " + jsonTimeCleared);
81 return false;
82 }
83 } else {
84 // No clear time not specified, JSON representation must be empty
85 if (!jsonTimeClearedNode.isNull()) {
86 description.appendText("Time Cleared should be null");
87 return false;
88 }
89 }
90
kmcpeakeb172d5f2015-12-10 11:30:43 +000091 String jsonSeverity = jsonAlarm.get("severity").asText();
92 String severity = alarm.severity().toString();
kmcpeake4fe18c82015-11-17 20:07:39 +000093 if (!severity.equals(jsonSeverity)) {
94 description.appendText("severity was " + jsonSeverity);
95 return false;
96 }
97
kmcpeakeb172d5f2015-12-10 11:30:43 +000098 JsonNode jsonAlarmNode = jsonAlarm.get("source");
kmcpeake4fe18c82015-11-17 20:07:39 +000099
100 if (alarm.source() != null) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000101 String jsonSource = jsonAlarmNode.textValue();
102 String source = alarm.source().toString();
kmcpeake4fe18c82015-11-17 20:07:39 +0000103
104 if (!source.equals(jsonSource)) {
105 description.appendText("source was " + jsonSource);
106 return false;
107 }
108 } else {
109 // source not specified, JSON representation must be empty
110 if (!jsonAlarmNode.isNull()) {
111 description.appendText("source should be null");
112 return false;
113 }
114 }
115
116 // In progress
117 return true;
118 }
119
120 @Override
kmcpeakea5404812015-12-08 11:52:50 +0000121 public void describeTo(Description description) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000122 description.appendText(alarm.toString());
123 }
124
125 /**
126 * Factory to allocate a alarm matcher.
127 *
128 * @param alarm alarm object we are looking for
129 * @return matcher
130 */
kmcpeakea5404812015-12-08 11:52:50 +0000131 public static AlarmJsonMatcher matchesAlarm(Alarm alarm) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000132 return new AlarmJsonMatcher(alarm);
133 }
134}