blob: 6acbd6d919f9e86487293f3ae0f869431cf8a1df [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li8bcef8b2016-01-06 11:35:53 -08003 *
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.flow.criteria.Criterion;
22import org.onosproject.net.flowobjective.FilteringObjective;
23
24/**
25 * Hamcrest matcher for filteringObjective.
26 */
27public final class FilteringObjectiveJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 final FilteringObjective filteringObj;
30
31 private FilteringObjectiveJsonMatcher(FilteringObjective filteringObjective) {
32 this.filteringObj = filteringObjective;
33 }
34
35 @Override
36 protected boolean matchesSafely(JsonNode jsonFilteringObj, Description description) {
37
38 ObjectiveJsonMatcher.matchesObjective(filteringObj).matchesSafely(jsonFilteringObj);
39
40 // check id
41 int jsonId = jsonFilteringObj.get("id").asInt();
42 int id = filteringObj.id();
43 if (jsonId != id) {
44 description.appendText("id was " + jsonId);
45 return false;
46 }
47
48 // check type
49 String jsonType = jsonFilteringObj.get("type").asText();
50 String type = filteringObj.type().toString();
51 if (!jsonType.equals(type)) {
52 description.appendText("type was " + jsonType);
53 return false;
54 }
55
56 // check size of condition array
57 JsonNode jsonConditions = jsonFilteringObj.get("conditions");
58 if (jsonConditions.size() != filteringObj.conditions().size()) {
59 description.appendText("conditions size was " + jsonConditions.size());
60 return false;
61 }
62
63 // check conditions
64 for (Criterion c : filteringObj.conditions()) {
65 boolean conditionFound = false;
66 for (int cIndex = 0; cIndex < jsonConditions.size(); cIndex++) {
67 CriterionJsonMatcher criterionMatcher = CriterionJsonMatcher.matchesCriterion(c);
68 if (criterionMatcher.matches(jsonConditions.get(cIndex))) {
69 conditionFound = true;
70 break;
71 }
72 }
73 if (!conditionFound) {
74 description.appendText("condition not found " + c.toString());
75 return false;
76 }
77 }
78
79 return true;
80 }
81
82 @Override
83 public void describeTo(Description description) {
84 description.appendText(filteringObj.toString());
85 }
86
87 /**
88 * Factory to allocate a filteringObjective matcher.
89 *
90 * @param filteringObj filteringObjective object we are looking for
91 * @return matcher
92 */
93 public static FilteringObjectiveJsonMatcher matchesFilteringObjective(FilteringObjective filteringObj) {
94 return new FilteringObjectiveJsonMatcher(filteringObj);
95 }
96}