Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | ee65a23 | 2017-03-29 14:15:30 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.mapping.actions.MappingAction; |
| 22 | import org.onosproject.mapping.actions.NoMappingAction; |
| 23 | import org.onosproject.mapping.actions.DropMappingAction; |
| 24 | import org.onosproject.mapping.actions.ForwardMappingAction; |
| 25 | import org.onosproject.mapping.actions.NativeForwardMappingAction; |
| 26 | /** |
| 27 | * Hamcrest matcher for mapping actions. |
| 28 | */ |
| 29 | public final class MappingActionJsonMatcher |
| 30 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 31 | |
| 32 | private final MappingAction action; |
| 33 | |
| 34 | /** |
| 35 | * A default constructor. |
| 36 | * |
| 37 | * @param action mapping action |
| 38 | */ |
| 39 | private MappingActionJsonMatcher(MappingAction action) { |
| 40 | this.action = action; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Matches the contents of a no mapping action. |
| 45 | * |
| 46 | * @param node JSON action to match |
| 47 | * @param description object used for recording errors |
| 48 | * @return true if contents match, false otherwise |
| 49 | */ |
| 50 | private boolean matchNoAction(JsonNode node, Description description) { |
| 51 | NoMappingAction actionToMatch = (NoMappingAction) action; |
| 52 | final String jsonType = node.get(MappingActionCodec.TYPE).textValue(); |
| 53 | if (!actionToMatch.type().name().equals(jsonType)) { |
| 54 | description.appendText("type was " + jsonType); |
| 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Matches the contents of a drop mapping action. |
| 62 | * |
| 63 | * @param node JSON action to match |
| 64 | * @param description object used for recording errors |
| 65 | * @return true if the contents match, false otherwise |
| 66 | */ |
| 67 | private boolean matchDropAction(JsonNode node, Description description) { |
| 68 | DropMappingAction actionToMatch = (DropMappingAction) action; |
| 69 | final String jsonType = node.get(MappingActionCodec.TYPE).textValue(); |
| 70 | if (!actionToMatch.type().name().equals(jsonType)) { |
| 71 | description.appendText("type was " + jsonType); |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Matches the contents of a forward mapping action. |
| 79 | * |
| 80 | * @param node JSON action to match |
| 81 | * @param description object used for recording errors |
| 82 | * @return true if the contents match, false otherwise |
| 83 | */ |
| 84 | private boolean matchForwardAction(JsonNode node, Description description) { |
| 85 | ForwardMappingAction actionToMatch = (ForwardMappingAction) action; |
| 86 | final String jsonType = node.get(MappingActionCodec.TYPE).textValue(); |
| 87 | if (!actionToMatch.type().name().equals(jsonType)) { |
| 88 | description.appendText("type was " + jsonType); |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Matches the contents of a native forward mapping action. |
| 96 | * |
| 97 | * @param node JSON action to match |
| 98 | * @param description object used for recording errors |
| 99 | * @return true if the contents match, false otherwise |
| 100 | */ |
| 101 | private boolean matchNativeForwardAction(JsonNode node, Description description) { |
| 102 | NativeForwardMappingAction actionToMatch = (NativeForwardMappingAction) action; |
| 103 | final String jsonType = node.get(MappingActionCodec.TYPE).textValue(); |
| 104 | if (!actionToMatch.type().name().equals(jsonType)) { |
| 105 | description.appendText("type was " + jsonType); |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 113 | |
| 114 | // check type |
| 115 | final JsonNode jsonTypeNode = jsonNode.get(MappingActionCodec.TYPE); |
| 116 | final String jsonType = jsonTypeNode.textValue(); |
| 117 | final String type = action.type().name(); |
| 118 | if (!jsonType.equals(type)) { |
| 119 | description.appendText("type was " + type); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (action instanceof NoMappingAction) { |
| 124 | return matchNoAction(jsonNode, description); |
| 125 | } else if (action instanceof DropMappingAction) { |
| 126 | return matchDropAction(jsonNode, description); |
| 127 | } else if (action instanceof ForwardMappingAction) { |
| 128 | return matchForwardAction(jsonNode, description); |
| 129 | } else if (action instanceof NativeForwardMappingAction) { |
| 130 | return matchNativeForwardAction(jsonNode, description); |
| 131 | } |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void describeTo(Description description) { |
| 138 | description.appendText(action.toString()); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Factory to allocate a mapping action matcher. |
| 143 | * |
| 144 | * @param action action object we are looking for |
| 145 | * @return matcher |
| 146 | */ |
| 147 | public static MappingActionJsonMatcher matchesAction(MappingAction action) { |
| 148 | return new MappingActionJsonMatcher(action); |
| 149 | } |
| 150 | } |