blob: 9aaa6fbdeb5211c105812e3531112756a222fde7 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080017
18import java.util.LinkedList;
19import java.util.List;
20
21import org.apache.commons.collections4.CollectionUtils;
22import org.easymock.IArgumentMatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.intent.Intent;
24import org.onosproject.net.intent.IntentId;
25import org.onosproject.net.intent.IntentOperation;
26import org.onosproject.net.intent.IntentOperations;
27import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080028
29import static org.easymock.EasyMock.reportMatcher;
30
31/**
32 * Helper class for testing operations submitted to the IntentService.
33 */
34public final class TestIntentServiceHelper {
35 /**
36 * Default constructor to prevent instantiation.
37 */
38 private TestIntentServiceHelper() {
39 }
40
41 /**
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080042 * 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 Radoslavovdde22ae2014-11-24 11:47:17 -080054 * 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 Radoslavov97e8a8b2014-11-24 17:51:28 -080065 /*
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -080066 * EasyMock matcher that matches {@link Intent} but
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080067 * 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 Radoslavovdde22ae2014-11-24 11:47:17 -080075 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080076 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 Radoslavovdde22ae2014-11-24 11:47:17 -0800110 }
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 Radoslavovdde22ae2014-11-24 11:47:17 -0800223}