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