blob: b87162b3301a211651237cd59a10c34f93746ef6 [file] [log] [blame]
Jian Lib68b9a82016-02-23 10:25:54 +09001/*
2 * Copyright 2016 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.region.Region;
22
23/**
24 * Hamcrest matcher for region.
25 */
26public final class RegionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final Region region;
29
30 private RegionJsonMatcher(Region region) {
31 this.region = region;
32 }
33
34 @Override
35 protected boolean matchesSafely(JsonNode jsonRegion, Description description) {
36 // check id
37 String jsonRegionId = jsonRegion.get("id").asText();
38 String regionId = region.id().toString();
39 if (!jsonRegionId.equals(regionId)) {
40 description.appendText("region id was " + jsonRegionId);
41 return false;
42 }
43
44 // check type
45 String jsonType = jsonRegion.get("type").asText();
46 String type = region.type().toString();
47 if (!jsonType.equals(type)) {
48 description.appendText("type was " + jsonType);
49 return false;
50 }
51
52 // check name
53 String jsonName = jsonRegion.get("name").asText();
54 String name = region.name();
55 if (!jsonName.equals(name)) {
56 description.appendText("name was " + jsonName);
57 return false;
58 }
59
60 // check size of master array
61 JsonNode jsonMasters = jsonRegion.get("masters");
62 if (jsonMasters.size() != region.masters().size()) {
63 description.appendText("masters size was " + jsonMasters.size());
64 return false;
65 }
66
67 // TODO: check the content inside masters
68
69 return true;
70 }
71
72 @Override
73 public void describeTo(Description description) {
74 description.appendText(region.toString());
75 }
76
77 /**
78 * Factory to allocate a region matcher.
79 *
80 * @param region region object we are looking for
81 * @return matcher
82 */
83 public static RegionJsonMatcher matchesRegion(Region region) {
84 return new RegionJsonMatcher(region);
85 }
86}