alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.apache.commons.collections4.CollectionUtils; |
| 22 | import org.easymock.IArgumentMatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.intent.Intent; |
| 24 | import org.onosproject.net.intent.IntentId; |
| 25 | import org.onosproject.net.intent.IntentOperation; |
| 26 | import org.onosproject.net.intent.IntentOperations; |
| 27 | import org.onosproject.sdnip.IntentSynchronizer.IntentKey; |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 28 | |
| 29 | import static org.easymock.EasyMock.reportMatcher; |
| 30 | |
| 31 | /** |
| 32 | * Helper class for testing operations submitted to the IntentService. |
| 33 | */ |
| 34 | public final class TestIntentServiceHelper { |
| 35 | /** |
| 36 | * Default constructor to prevent instantiation. |
| 37 | */ |
| 38 | private TestIntentServiceHelper() { |
| 39 | } |
| 40 | |
| 41 | /** |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 42 | * Matcher method to set the expected intent to match against |
| 43 | * (ignoring the intent ID for the intent). |
| 44 | * |
| 45 | * @param intent the expected Intent |
| 46 | * @return the submitted Intent |
| 47 | */ |
| 48 | static Intent eqExceptId(Intent intent) { |
| 49 | reportMatcher(new IdAgnosticIntentMatcher(intent)); |
| 50 | return intent; |
| 51 | } |
| 52 | |
| 53 | /** |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 54 | * Matcher method to set the expected intent operations to match against |
| 55 | * (ignoring the intent ID for each intent). |
| 56 | * |
| 57 | * @param intentOperations the expected Intent Operations |
| 58 | * @return the submitted Intent Operations |
| 59 | */ |
| 60 | static IntentOperations eqExceptId(IntentOperations intentOperations) { |
| 61 | reportMatcher(new IdAgnosticIntentOperationsMatcher(intentOperations)); |
| 62 | return intentOperations; |
| 63 | } |
| 64 | |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 65 | /* |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 66 | * EasyMock matcher that matches {@link Intent} but |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 67 | * ignores the {@link IntentId} when matching. |
| 68 | * <p/> |
| 69 | * The normal intent equals method tests that the intent IDs are equal, |
| 70 | * however in these tests we can't know what the intent IDs will be in |
| 71 | * advance, so we can't set up expected intents with the correct IDs. Thus, |
| 72 | * the solution is to use an EasyMock matcher that verifies that all the |
| 73 | * value properties of the provided intent match the expected values, but |
| 74 | * ignores the intent ID when testing equality. |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 75 | */ |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 76 | private static final class IdAgnosticIntentMatcher implements |
| 77 | IArgumentMatcher { |
| 78 | |
| 79 | private final Intent intent; |
| 80 | private String providedString; |
| 81 | |
| 82 | /** |
| 83 | * Constructor taking the expected intent to match against. |
| 84 | * |
| 85 | * @param intent the expected intent |
| 86 | */ |
| 87 | public IdAgnosticIntentMatcher(Intent intent) { |
| 88 | this.intent = intent; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void appendTo(StringBuffer strBuffer) { |
| 93 | strBuffer.append("IntentMatcher unable to match: " |
| 94 | + providedString); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean matches(Object object) { |
| 99 | if (!(object instanceof Intent)) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | Intent providedIntent = (Intent) object; |
| 104 | providedString = providedIntent.toString(); |
| 105 | |
| 106 | IntentKey thisIntentKey = new IntentKey(intent); |
| 107 | IntentKey providedIntentKey = new IntentKey(providedIntent); |
| 108 | return thisIntentKey.equals(providedIntentKey); |
| 109 | } |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * EasyMock matcher that matches {@link IntenOperations} but |
| 114 | * ignores the {@link IntentId} when matching. |
| 115 | * <p/> |
| 116 | * The normal intent equals method tests that the intent IDs are equal, |
| 117 | * however in these tests we can't know what the intent IDs will be in |
| 118 | * advance, so we can't set up expected intents with the correct IDs. Thus, |
| 119 | * the solution is to use an EasyMock matcher that verifies that all the |
| 120 | * value properties of the provided intent match the expected values, but |
| 121 | * ignores the intent ID when testing equality. |
| 122 | */ |
| 123 | private static final class IdAgnosticIntentOperationsMatcher implements |
| 124 | IArgumentMatcher { |
| 125 | |
| 126 | private final IntentOperations intentOperations; |
| 127 | private String providedString; |
| 128 | |
| 129 | /** |
| 130 | * Constructor taking the expected intent operations to match against. |
| 131 | * |
| 132 | * @param intentOperations the expected intent operations |
| 133 | */ |
| 134 | public IdAgnosticIntentOperationsMatcher( |
| 135 | IntentOperations intentOperations) { |
| 136 | this.intentOperations = intentOperations; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void appendTo(StringBuffer strBuffer) { |
| 141 | strBuffer.append("IntentOperationsMatcher unable to match: " |
| 142 | + providedString); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean matches(Object object) { |
| 147 | if (!(object instanceof IntentOperations)) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | IntentOperations providedIntentOperations = |
| 152 | (IntentOperations) object; |
| 153 | providedString = providedIntentOperations.toString(); |
| 154 | |
| 155 | List<IntentKey> thisSubmitIntents = new LinkedList<>(); |
| 156 | List<IntentId> thisWithdrawIntentIds = new LinkedList<>(); |
| 157 | List<IntentKey> thisReplaceIntents = new LinkedList<>(); |
| 158 | List<IntentKey> thisUpdateIntents = new LinkedList<>(); |
| 159 | List<IntentKey> providedSubmitIntents = new LinkedList<>(); |
| 160 | List<IntentId> providedWithdrawIntentIds = new LinkedList<>(); |
| 161 | List<IntentKey> providedReplaceIntents = new LinkedList<>(); |
| 162 | List<IntentKey> providedUpdateIntents = new LinkedList<>(); |
| 163 | |
| 164 | extractIntents(intentOperations, thisSubmitIntents, |
| 165 | thisWithdrawIntentIds, thisReplaceIntents, |
| 166 | thisUpdateIntents); |
| 167 | extractIntents(providedIntentOperations, providedSubmitIntents, |
| 168 | providedWithdrawIntentIds, providedReplaceIntents, |
| 169 | providedUpdateIntents); |
| 170 | |
| 171 | return CollectionUtils.isEqualCollection(thisSubmitIntents, |
| 172 | providedSubmitIntents) && |
| 173 | CollectionUtils.isEqualCollection(thisWithdrawIntentIds, |
| 174 | providedWithdrawIntentIds) && |
| 175 | CollectionUtils.isEqualCollection(thisUpdateIntents, |
| 176 | providedUpdateIntents) && |
| 177 | CollectionUtils.isEqualCollection(thisReplaceIntents, |
| 178 | providedReplaceIntents); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Extracts the intents per operation type. Each intent is encapsulated |
| 183 | * in IntentKey so it can be compared by excluding the Intent ID. |
| 184 | * |
| 185 | * @param intentOperations the container with the intent operations |
| 186 | * to extract the intents from |
| 187 | * @param submitIntents the SUBMIT intents |
| 188 | * @param withdrawIntentIds the WITHDRAW intents IDs |
| 189 | * @param replaceIntents the REPLACE intents |
| 190 | * @param updateIntents the UPDATE intens |
| 191 | */ |
| 192 | private void extractIntents(IntentOperations intentOperations, |
| 193 | List<IntentKey> submitIntents, |
| 194 | List<IntentId> withdrawIntentIds, |
| 195 | List<IntentKey> replaceIntents, |
| 196 | List<IntentKey> updateIntents) { |
| 197 | for (IntentOperation oper : intentOperations.operations()) { |
| 198 | IntentId intentId; |
| 199 | IntentKey intentKey; |
| 200 | switch (oper.type()) { |
| 201 | case SUBMIT: |
| 202 | intentKey = new IntentKey(oper.intent()); |
| 203 | submitIntents.add(intentKey); |
| 204 | break; |
| 205 | case WITHDRAW: |
| 206 | intentId = oper.intentId(); |
| 207 | withdrawIntentIds.add(intentId); |
| 208 | break; |
| 209 | case REPLACE: |
| 210 | intentKey = new IntentKey(oper.intent()); |
| 211 | replaceIntents.add(intentKey); |
| 212 | break; |
| 213 | case UPDATE: |
| 214 | intentKey = new IntentKey(oper.intent()); |
| 215 | updateIntents.add(intentKey); |
| 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 223 | } |