Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import org.hamcrest.Description; |
| 19 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 20 | import org.onosproject.net.group.Group; |
| 21 | import org.onosproject.net.group.GroupBucket; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | |
| 25 | /** |
| 26 | * Hamcrest matcher for groups. |
| 27 | */ |
| 28 | |
| 29 | public 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(); |
| 41 | String groupId = group.id().toString(); |
| 42 | 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 | } |