blob: 532ab92122436bf6f1a86747a9122c289fd8a618 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jian Li5c411232015-12-16 15:29:16 -08003 *
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.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.net.meter.Band;
22import org.onosproject.net.meter.Meter;
23
24/**
25 * Hamcrest matcher for meters.
26 */
27public final class MeterJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private final Meter meter;
30
31 private MeterJsonMatcher(Meter meter) {
32 this.meter = meter;
33 }
34
35 @Override
36 protected boolean matchesSafely(JsonNode jsonMeter, Description description) {
37 // check id
38 String jsonMeterId = jsonMeter.get("id").asText();
39 String meterId = meter.id().toString();
40 if (!jsonMeterId.equals(meterId)) {
41 description.appendText("meter id was " + jsonMeterId);
42 return false;
43 }
44
45 // check unit
46 String jsonUnit = jsonMeter.get("unit").asText();
47 String unit = meter.unit().toString();
48 if (!jsonUnit.equals(unit)) {
49 description.appendText("unit was " + jsonUnit);
50 return false;
51 }
52
53 // check burst
54 boolean jsonBurst = jsonMeter.get("burst").asBoolean();
55 boolean burst = meter.isBurst();
56 if (jsonBurst != burst) {
57 description.appendText("isBurst was " + jsonBurst);
58 return false;
59 }
60
61 // check state
62 JsonNode jsonNodeState = jsonMeter.get("state");
63 if (jsonNodeState != null) {
64 String state = meter.state().toString();
65 if (!jsonNodeState.asText().equals(state)) {
66 description.appendText("state was " + jsonNodeState.asText());
67 return false;
68 }
69 }
70
71 // check life
72 JsonNode jsonNodeLife = jsonMeter.get("life");
73 if (jsonNodeLife != null) {
74 long life = meter.life();
75 if (jsonNodeLife.asLong() != life) {
76 description.appendText("life was " + jsonNodeLife.asLong());
77 return false;
78 }
79 }
80
81 // check bytes
82 JsonNode jsonNodeBytes = jsonMeter.get("bytes");
83 if (jsonNodeBytes != null) {
84 long bytes = meter.bytesSeen();
85 if (jsonNodeBytes.asLong() != bytes) {
86 description.appendText("bytes was " + jsonNodeBytes.asLong());
87 return false;
88 }
89 }
90
91 // check packets
92 JsonNode jsonNodePackets = jsonMeter.get("packets");
93 if (jsonNodePackets != null) {
94 long packets = meter.packetsSeen();
95 if (jsonNodePackets.asLong() != packets) {
96 description.appendText("packets was " + jsonNodePackets.asLong());
97 return false;
98 }
99 }
100
101 // check size of band array
102 JsonNode jsonBands = jsonMeter.get("bands");
103 if (jsonBands.size() != meter.bands().size()) {
104 description.appendText("bands size was " + jsonBands.size());
105 return false;
106 }
107
108 // check bands
109 for (Band band : meter.bands()) {
110 boolean bandFound = false;
111 for (int bandIndex = 0; bandIndex < jsonBands.size(); bandIndex++) {
112 MeterBandJsonMatcher bandMatcher = MeterBandJsonMatcher.matchesMeterBand(band);
113 if (bandMatcher.matches(jsonBands.get(bandIndex))) {
114 bandFound = true;
115 break;
116 }
117 }
118 if (!bandFound) {
119 description.appendText("band not found " + band.toString());
120 return false;
121 }
122 }
123
124 return true;
125 }
126
127 @Override
128 public void describeTo(Description description) {
129 description.appendText(meter.toString());
130 }
131
132 /**
133 * Factory to allocate a meter matcher.
134 *
135 * @param meter meter object we are looking for
136 * @return matcher
137 */
138 public static MeterJsonMatcher matchesMeter(Meter meter) {
139 return new MeterJsonMatcher(meter);
140 }
141}