blob: 98ce292747ff0c56e72543873e8eda230e9c9b06 [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
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080018import org.easymock.IArgumentMatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080021
Sho SHIMIZU4931ee52015-02-03 21:09:28 -080022import static org.easymock.EasyMock.reportMatcher;
23
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080024/**
25 * Helper class for testing operations submitted to the IntentService.
26 */
27public final class TestIntentServiceHelper {
28 /**
29 * Default constructor to prevent instantiation.
30 */
31 private TestIntentServiceHelper() {
32 }
33
34 /**
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080035 * 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 Radoslavovdde22ae2014-11-24 11:47:17 -080047 * Matcher method to set the expected intent operations to match against
48 * (ignoring the intent ID for each intent).
49 *
Sho SHIMIZU4931ee52015-02-03 21:09:28 -080050 * param intentOperations the expected Intent Operations
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080051 * @return the submitted Intent Operations
52 */
Sho SHIMIZU4931ee52015-02-03 21:09:28 -080053 /*
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080054 static IntentOperations eqExceptId(IntentOperations intentOperations) {
55 reportMatcher(new IdAgnosticIntentOperationsMatcher(intentOperations));
56 return intentOperations;
57 }
Sho SHIMIZU4931ee52015-02-03 21:09:28 -080058 */
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -080059
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080060 /*
Pavlin Radoslavovcaf63372014-11-26 11:59:11 -080061 * EasyMock matcher that matches {@link Intent} but
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080062 * 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 Radoslavovdde22ae2014-11-24 11:47:17 -080070 */
Pavlin Radoslavov97e8a8b2014-11-24 17:51:28 -080071 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 Radoslavovdde22ae2014-11-24 11:47:17 -0800105 }
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 SHIMIZU4931ee52015-02-03 21:09:28 -0800118 /*
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800119 private static final class IdAgnosticIntentOperationsMatcher implements
120 IArgumentMatcher {
121
Sho SHIMIZU4931ee52015-02-03 21:09:28 -0800122 //private final IntentOperations intentOperations;
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800123 private String providedString;
124
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800125 @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 SHIMIZU4931ee52015-02-03 21:09:28 -0800167
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800168 /**
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 Hart6cd2f352015-01-13 17:44:45 -0800177 * @param updateIntents the UPDATE intents
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800178 */
Sho SHIMIZU4931ee52015-02-03 21:09:28 -0800179 /*
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800180 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 SHIMIZU4931ee52015-02-03 21:09:28 -0800211 */
Pavlin Radoslavovdde22ae2014-11-24 11:47:17 -0800212}