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