blob: cdf4da92790b87651b90c474505f5c6dc654dbe1 [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li8bcef8b2016-01-06 11:35:53 -080020import org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
22import org.onosproject.net.flowobjective.NextObjective;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070023import org.onosproject.net.flowobjective.NextTreatment;
24import java.util.ArrayList;
25import java.util.List;
Jian Li8bcef8b2016-01-06 11:35:53 -080026
27/**
28 * Hamcrest matcher for nextObjective.
29 */
30public final class NextObjectiveJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
31
32 final NextObjective nextObjective;
33
34 private NextObjectiveJsonMatcher(NextObjective nextObjective) {
35 this.nextObjective = nextObjective;
36 }
37
38 @Override
39 protected boolean matchesSafely(JsonNode jsonNextObj, Description description) {
40 ObjectiveJsonMatcher.matchesObjective(nextObjective).matchesSafely(jsonNextObj);
41
42 // check id
43 int jsonId = jsonNextObj.get("id").asInt();
44 int id = nextObjective.id();
45 if (jsonId != id) {
46 description.appendText("id was " + jsonId);
47 return false;
48 }
49
50 // check type
51 String jsonType = jsonNextObj.get("type").asText();
52 String type = nextObjective.type().toString();
53 if (!jsonType.equals(type)) {
54 description.appendText("type was " + jsonType);
55 return false;
56 }
57
58 // check size of treatment array
59 JsonNode jsonTreatments = jsonNextObj.get("treatments");
60 if (jsonTreatments.size() != nextObjective.next().size()) {
61 description.appendText("treatments size was " + jsonTreatments.size());
62 return false;
63 }
64
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070065 // check the weight
66 boolean result = true;
67 List<NextTreatment> nt = new ArrayList(nextObjective.nextTreatments());
68 for (int i = 0; i < jsonTreatments.size(); i++) {
69 ObjectNode jsonTreatment = jsonTreatments.path(i).isObject() &&
70 !jsonTreatments.path(i).isNull() ? (ObjectNode) jsonTreatments.path(i) : null;
71 int jsonWeight = jsonTreatment.get("weight").asInt();
72 if (jsonWeight != nt.get(i).weight()) {
73 description.appendText("weight of NextTreatment with index " + i + " was " + jsonWeight);
74 result = false;
75 }
76 }
77 if (!result) {
78 return false;
79 }
80
Jian Li8bcef8b2016-01-06 11:35:53 -080081 // TODO: need to check the content of treatment collection
82
83 // TODO: need to check the content of selector instance
84
85 return true;
86 }
87
88 @Override
89 public void describeTo(Description description) {
90 description.appendText(nextObjective.toString());
91 }
92
93 /**
94 * Factory to allocate a nextObjective matcher.
95 *
96 * @param nextObjective nextObjective object we are looking for
97 * @return matcher
98 */
99 public static NextObjectiveJsonMatcher matchesNextObjective(NextObjective nextObjective) {
100 return new NextObjectiveJsonMatcher(nextObjective);
101 }
102}