blob: 4ad86fa7af008e7933217492a3a4f8ab4145b0a5 [file] [log] [blame]
Jian Li4a3fffa2018-06-10 23:12:40 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
Jian Li7fe7eaf2018-12-31 17:00:33 +090016package org.onosproject.openstacktelemetry.codec.rest;
Jian Li4a3fffa2018-06-10 23:12:40 +090017
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacktelemetry.api.StatsInfo;
22
23/**
24 * Hamcrest matcher for StatsInfoJsonCodec.
25 */
26public final class StatsInfoJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final StatsInfo statsInfo;
29
30 private static final String STARTUP_TIME = "startupTime";
31 private static final String FST_PKT_ARR_TIME = "fstPktArrTime";
32 private static final String LST_PKT_OFFSET = "lstPktOffset";
33 private static final String PREV_ACC_BYTES = "prevAccBytes";
34 private static final String PREV_ACC_PKTS = "prevAccPkts";
35 private static final String CURR_ACC_BYTES = "currAccBytes";
36 private static final String CURR_ACC_PKTS = "currAccPkts";
37 private static final String ERROR_PKTS = "errorPkts";
38 private static final String DROP_PKTS = "dropPkts";
39
40 private StatsInfoJsonMatcher(StatsInfo statsInfo) {
41 this.statsInfo = statsInfo;
42 }
43
44 @Override
45 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
46
47 // check startup time
48 long jsonStartupTime = jsonNode.get(STARTUP_TIME).asLong();
49 long startupTime = statsInfo.startupTime();
50 if (jsonStartupTime != startupTime) {
51 description.appendText("startup time was " + jsonStartupTime);
52 return false;
53 }
54
55 // check first packet arrival time
56 long jsonFstPktArrTime = jsonNode.get(FST_PKT_ARR_TIME).asLong();
57 long fstPktArrTime = statsInfo.fstPktArrTime();
58 if (jsonFstPktArrTime != fstPktArrTime) {
59 description.appendText("first packet arrival time was " + jsonFstPktArrTime);
60 return false;
61 }
62
63 // check last packet offset
64 int jsonLstPktOffset = jsonNode.get(LST_PKT_OFFSET).asInt();
65 int lstPktOffset = statsInfo.lstPktOffset();
66 if (jsonLstPktOffset != lstPktOffset) {
67 description.appendText("last packet offset was " + jsonLstPktOffset);
68 return false;
69 }
70
71 // check previous accumulated bytes
72 long jsonPrevAccBytes = jsonNode.get(PREV_ACC_BYTES).asLong();
73 long preAccBytes = statsInfo.prevAccBytes();
74 if (jsonPrevAccBytes != preAccBytes) {
75 description.appendText("previous accumulated bytes was " + jsonPrevAccBytes);
76 return false;
77 }
78
79 // check previous accumulated packets
80 int jsonPreAccPkts = jsonNode.get(PREV_ACC_PKTS).asInt();
81 int preAccPkts = statsInfo.prevAccPkts();
82 if (jsonPreAccPkts != preAccPkts) {
83 description.appendText("previous accumulated packets was " + jsonPreAccPkts);
84 return false;
85 }
86
87 // check current accumulated bytes
88 long jsonCurrAccBytes = jsonNode.get(CURR_ACC_BYTES).asLong();
89 long currAccBytes = statsInfo.currAccBytes();
90 if (jsonCurrAccBytes != currAccBytes) {
91 description.appendText("current accumulated bytes was " + jsonCurrAccBytes);
92 return false;
93 }
94
95 // check current accumulated packets
96 int jsonCurrAccPkts = jsonNode.get(CURR_ACC_PKTS).asInt();
97 int currAccPkts = statsInfo.currAccPkts();
98 if (jsonCurrAccPkts != currAccPkts) {
99 description.appendText("current accumulated packets was " + jsonCurrAccPkts);
100 return false;
101 }
102
103 // check error packets
104 short jsonErrorPkts = (short) jsonNode.get(ERROR_PKTS).asInt();
105 short errorPkts = statsInfo.errorPkts();
106 if (jsonErrorPkts != errorPkts) {
107 description.appendText("error packets was " + jsonErrorPkts);
108 return false;
109 }
110
111 // check drop packets
112 short jsonDropPkts = (short) jsonNode.get(DROP_PKTS).asInt();
113 short dropPkts = statsInfo.dropPkts();
114 if (jsonDropPkts != dropPkts) {
115 description.appendText("drop packets was " + jsonDropPkts);
116 return false;
117 }
118
119 return true;
120 }
121
122 @Override
123 public void describeTo(Description description) {
124 description.appendText(statsInfo.toString());
125 }
126
127 /**
128 * Factory to allocate a stats info matcher.
129 *
130 * @param statsInfo stats info object we are looking for
131 * @return matcher
132 */
133 public static StatsInfoJsonMatcher matchStatsInfo(StatsInfo statsInfo) {
134 return new StatsInfoJsonMatcher(statsInfo);
135 }
136}