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