blob: 2f08a5c8faed45bee0f5beb5b627d9804ec2ded7 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
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.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.net.meter.Band;
22
23/**
24 * Hamcrest matcher for bands.
25 */
26public final class MeterBandJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final Band band;
29
30 private MeterBandJsonMatcher(Band band) {
31 this.band = band;
32 }
33
34 /**
35 * Matches the contents of a meter band.
36 *
37 * @param bandJson JSON representation of band to match
38 * @param description Description object used for recording errors
39 * @return true if contents match, false otherwise
40 */
41 @Override
42 protected boolean matchesSafely(JsonNode bandJson, Description description) {
43 // check type
44 final String jsonType = bandJson.get("type").textValue();
45 if (!band.type().name().equals(jsonType)) {
46 description.appendText("type was " + jsonType);
47 return false;
48 }
49
50 // check rate
51 final long jsonRate = bandJson.get("rate").longValue();
52 if (band.rate() != jsonRate) {
53 description.appendText("rate was " + jsonRate);
54 return false;
55 }
56
57 // check burst size
58 final long jsonBurstSize = bandJson.get("burstSize").longValue();
59 if (band.burst() != jsonBurstSize) {
60 description.appendText("burst size was " + jsonBurstSize);
61 return false;
62 }
63
64 // check precedence
65 final JsonNode jsonNodePrec = bandJson.get("prec");
66 if (jsonNodePrec != null) {
67 if (band.dropPrecedence() != jsonNodePrec.shortValue()) {
68 description.appendText("drop precedence was " + jsonNodePrec.shortValue());
69 return false;
70 }
71 }
72
73 // check packets
74 final JsonNode jsonNodePackets = bandJson.get("packets");
75 if (jsonNodePackets != null) {
76 if (band.packets() != jsonNodePackets.asLong()) {
77 description.appendText("packets was " + jsonNodePackets.asLong());
78 return false;
79 }
80 }
81
82 final JsonNode jsonNodeBytes = bandJson.get("bytes");
83 if (jsonNodeBytes != null) {
84 if (band.bytes() != jsonNodeBytes.asLong()) {
85 description.appendText("bytes was " + jsonNodeBytes.asLong());
86 return false;
87 }
88 }
89
90 return true;
91 }
92
93 @Override
94 public void describeTo(Description description) {
95 description.appendText(band.toString());
96 }
97
98 /**
99 * Factory to allocate a band matcher.
100 *
101 * @param band band object we are looking for
102 * @return matcher
103 */
104 public static MeterBandJsonMatcher matchesMeterBand(Band band) {
105 return new MeterBandJsonMatcher(band);
106 }
107}