blob: f1c9068e5efff414c5a27be33b30bdc6a097774a [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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.Group;
21import org.onosproject.net.group.GroupBucket;
22
23import com.fasterxml.jackson.databind.JsonNode;
24
25/**
26 * Hamcrest matcher for groups.
27 */
28
29public final class GroupJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
30
31 private final Group group;
32
33 private GroupJsonMatcher(Group group) {
34 this.group = group;
35 }
36
37 @Override
38 public boolean matchesSafely(JsonNode jsonGroup, Description description) {
39 // check id
40 String jsonGroupId = jsonGroup.get("id").asText();
Prince Pereira3ff504c2016-08-30 14:23:43 +053041 String groupId = group.id().id().toString();
Ray Milkey39616f32015-05-14 15:43:00 -070042 if (!jsonGroupId.equals(groupId)) {
43 description.appendText("group id was " + jsonGroupId);
44 return false;
45 }
46
47 // check state
48 String jsonState = jsonGroup.get("state").asText();
49 String state = group.state().toString();
50 if (!jsonState.equals(state)) {
51 description.appendText("state was " + jsonState);
52 return false;
53 }
54
55 // check life
56 long jsonLife = jsonGroup.get("life").asLong();
57 long life = group.life();
58 if (life != jsonLife) {
59 description.appendText("life was " + jsonLife);
60 return false;
61 }
62
63 // check bytes
64 long jsonBytes = jsonGroup.get("bytes").asLong();
65 long bytes = group.bytes();
66 if (bytes != jsonBytes) {
67 description.appendText("bytes was " + jsonBytes);
68 return false;
69 }
70
71 // check packets
72 long jsonPackets = jsonGroup.get("packets").asLong();
73 long packets = group.packets();
74 if (packets != jsonPackets) {
75 description.appendText("packets was " + jsonPackets);
76 return false;
77 }
78
79 // check size of bucket array
80 JsonNode jsonBuckets = jsonGroup.get("buckets");
81 if (jsonBuckets.size() != group.buckets().buckets().size()) {
82 description.appendText("buckets size was " + jsonBuckets.size());
83 return false;
84 }
85
86 // Check buckets
87 for (GroupBucket bucket : group.buckets().buckets()) {
88 boolean bucketFound = false;
89 for (int bucketIndex = 0; bucketIndex < jsonBuckets.size(); bucketIndex++) {
90 GroupBucketJsonMatcher bucketMatcher =
91 GroupBucketJsonMatcher.matchesGroupBucket(bucket);
92 if (bucketMatcher.matches(jsonBuckets.get(bucketIndex))) {
93 bucketFound = true;
94 break;
95 }
96 }
97 if (!bucketFound) {
98 description.appendText("bucket not found " + bucket.toString());
99 return false;
100 }
101 }
102
103 return true;
104 }
105
106 @Override
107 public void describeTo(Description description) {
108 description.appendText(group.toString());
109 }
110
111 /**
112 * Factory to allocate a group matcher.
113 *
114 * @param group group object we are looking for
115 * @return matcher
116 */
117 public static GroupJsonMatcher matchesGroup(Group group) {
118 return new GroupJsonMatcher(group);
119 }
120}