blob: 3d7af5883de1c30248b71f89a78cd7c5df8f50d7 [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.flowobjective.ForwardingObjective;
22
23/**
24 * Hamcrest matcher for forwardingObjective.
25 */
26public final class ForwardingObjectiveJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 final ForwardingObjective forwardingObjective;
29
30 private ForwardingObjectiveJsonMatcher(ForwardingObjective forwardingObjective) {
31 this.forwardingObjective = forwardingObjective;
32 }
33
34 @Override
35 protected boolean matchesSafely(JsonNode jsonForwardingObj, Description description) {
36
37 ObjectiveJsonMatcher.matchesObjective(forwardingObjective).matchesSafely(jsonForwardingObj);
38
39 // check id
40 int jsonId = jsonForwardingObj.get("id").asInt();
41 int id = forwardingObjective.id();
42 if (jsonId != id) {
43 description.appendText("id was " + jsonId);
44 return false;
45 }
46
47 // check nextId
48 int jsonNextId = jsonForwardingObj.get("nextId").asInt();
49 int nextId = forwardingObjective.nextId();
50 if (jsonNextId != nextId) {
51 description.appendText("nextId was " + jsonNextId);
52 return false;
53 }
54
55 // check flag
56 String jsonFlag = jsonForwardingObj.get("flag").asText();
57 String flag = forwardingObjective.flag().toString();
58 if (!jsonFlag.equals(flag)) {
59 description.appendText("flag was " + jsonFlag);
60 return false;
61 }
62
63 return true;
64 }
65
66 @Override
67 public void describeTo(Description description) {
68 description.appendText(forwardingObjective.toString());
69 }
70
71 /**
72 * Factory to allocate a forwardingObjective matcher.
73 *
74 * @param forwardingObjective forwardingObjective object we are looking for
75 * @return matcher
76 */
77 public static ForwardingObjectiveJsonMatcher matchesForwardingObjective(ForwardingObjective forwardingObjective) {
78 return new ForwardingObjectiveJsonMatcher(forwardingObjective);
79 }
80}