blob: 8739bb71935ab23edb52085274f4506ad79365c0 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey39616f32015-05-14 15:43:00 -07003 *
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 org.hamcrest.Description;
19import org.hamcrest.TypeSafeDiagnosingMatcher;
20import org.onosproject.net.group.GroupBucket;
21
22import com.fasterxml.jackson.databind.JsonNode;
23
24/**
25 * Hamcrest matcher for instructions.
26 */
27public final class GroupBucketJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private final GroupBucket bucket;
30
31 private GroupBucketJsonMatcher(GroupBucket bucket) {
32 this.bucket = bucket;
33 }
34
35 /**
36 * Matches the contents of a group bucket.
37 *
38 * @param bucketJson JSON representation of bucket to match
39 * @param description Description object used for recording errors
40 * @return true if contents match, false otherwise
41 */
42 @Override
43 public boolean matchesSafely(JsonNode bucketJson, Description description) {
44
45 // check type
46 final String jsonType = bucketJson.get("type").textValue();
47 if (!bucket.type().name().equals(jsonType)) {
48 description.appendText("type was " + jsonType);
49 return false;
50 }
51
52 final long jsonWeight = bucketJson.get("weight").longValue();
53 if (bucket.weight() != jsonWeight) {
54 description.appendText("weight was " + jsonWeight);
55 return false;
56 }
57
58 final long packetsJson = bucketJson.get("packets").asLong();
59 if (bucket.packets() != packetsJson) {
60 description.appendText("packets was " + packetsJson);
61 return false;
62 }
63
64 final long bytesJson = bucketJson.get("bytes").asLong();
65 if (bucket.bytes() != bytesJson) {
66 description.appendText("bytes was " + packetsJson);
67 return false;
68 }
69
70 return true;
71 }
72
73 @Override
74 public void describeTo(Description description) {
75 description.appendText(bucket.toString());
76 }
77
78 /**
79 * Factory to allocate an bucket matcher.
80 *
81 * @param bucket bucket object we are looking for
82 * @return matcher
83 */
84 public static GroupBucketJsonMatcher matchesGroupBucket(GroupBucket bucket) {
85 return new GroupBucketJsonMatcher(bucket);
86 }
87}