blob: 3ea8b02d5adddb3f4cf52ff251d2c040c7b7d518 [file] [log] [blame]
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -08001package org.onlab.onos.sdnip;
2
3import java.util.LinkedList;
4import java.util.List;
5
6import org.apache.commons.collections4.CollectionUtils;
7import org.easymock.IArgumentMatcher;
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -08008import org.onlab.onos.net.intent.Intent;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -08009import org.onlab.onos.net.intent.IntentId;
10import org.onlab.onos.net.intent.IntentOperation;
11import org.onlab.onos.net.intent.IntentOperations;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080012import org.onlab.onos.sdnip.IntentSynchronizer.IntentKey;
13
14import static org.easymock.EasyMock.reportMatcher;
15
16/**
17 * Helper class for testing operations submitted to the IntentService.
18 */
19public final class TestIntentServiceHelper {
20 /**
21 * Default constructor to prevent instantiation.
22 */
23 private TestIntentServiceHelper() {
24 }
25
26 /**
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080027 * 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 Radoslavovdde22ae2014-11-24 11:47:17 -080039 * 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 Radoslavov97e8a8b2014-11-24 17:51:28 -080050 /*
51 * EasyMock matcher that matches {@link Inten} but
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 Radoslavovdde22ae2014-11-24 11:47:17 -080060 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080061 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 Radoslavovdde22ae2014-11-24 11:47:17 -080095 }
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 Radoslavovdde22ae2014-11-24 11:47:17 -0800208}