-
Notifications
You must be signed in to change notification settings - Fork 188
/
MidPointAsserts.java
303 lines (264 loc) · 12.7 KB
/
MidPointAsserts.java
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright (c) 2010-2016 Evolveum
*
* 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 com.evolveum.midpoint.test.util;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.assertNotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.util.QNameUtil;
import org.apache.commons.lang.StringUtils;
import org.testng.AssertJUnit;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
/**
* @author semancik
*
*/
public class MidPointAsserts {
public static <F extends FocusType> void assertAssigned(PrismObject<F> user, String targetOid, QName refType) {
F userType = user.asObjectable();
for (AssignmentType assignmentType: userType.getAssignment()) {
ObjectReferenceType targetRef = assignmentType.getTargetRef();
if (targetRef != null) {
if (refType.equals(targetRef.getType())) {
if (targetOid.equals(targetRef.getOid())) {
return;
}
}
}
}
AssertJUnit.fail(user + " does not have assigned "+refType.getLocalPart()+" "+targetOid);
}
public static void assertAssigned(PrismObject<? extends FocusType> focus, String targetOid, QName refType, QName relation) {
FocusType focusType = focus.asObjectable();
for (AssignmentType assignmentType: focusType.getAssignment()) {
ObjectReferenceType targetRef = assignmentType.getTargetRef();
if (targetRef != null) {
if (refType.equals(targetRef.getType())) {
if (targetOid.equals(targetRef.getOid()) &&
MiscSchemaUtil.compareRelation(targetRef.getRelation(), relation)) {
return;
}
}
}
}
AssertJUnit.fail(focus + " does not have assigned "+refType.getLocalPart()+" "+targetOid+ ", relation "+relation);
}
public static <F extends FocusType> void assertNotAssigned(PrismObject<F> user, String targetOid, QName refType) {
F userType = user.asObjectable();
for (AssignmentType assignmentType: userType.getAssignment()) {
ObjectReferenceType targetRef = assignmentType.getTargetRef();
if (targetRef != null) {
if (refType.equals(targetRef.getType())) {
if (targetOid.equals(targetRef.getOid())) {
AssertJUnit.fail(user + " does have assigned "+refType.getLocalPart()+" "+targetOid+" while not expecting it");
}
}
}
}
}
public static <F extends FocusType> void assertAssignments(PrismObject<F> user, int expectedNumber) {
F userType = user.asObjectable();
assertEquals("Unexepected number of assignments in "+user+": "+userType.getAssignment(), expectedNumber, userType.getAssignment().size());
}
public static <F extends FocusType> void assertAssignments(PrismObject<F> user, Class expectedType, int expectedNumber) {
F userType = user.asObjectable();
int actualAssignments = 0;
List<AssignmentType> assignments = userType.getAssignment();
for (AssignmentType assignment: assignments) {
ObjectReferenceType targetRef = assignment.getTargetRef();
if (targetRef != null) {
QName type = targetRef.getType();
if (type != null) {
Class<? extends ObjectType> assignmentTargetClass = ObjectTypes.getObjectTypeFromTypeQName(type).getClassDefinition();
if (expectedType.isAssignableFrom(assignmentTargetClass)) {
actualAssignments++;
}
}
}
}
assertEquals("Unexepected number of assignments of type "+expectedType+" in "+user+": "+userType.getAssignment(), expectedNumber, actualAssignments);
}
public static <F extends FocusType> void assertNoAssignments(PrismObject<F> user) {
F userType = user.asObjectable();
List<AssignmentType> assignments = userType.getAssignment();
assertTrue(user + " does have assignments "+assignments+" while not expecting it", assignments.isEmpty());
}
public static <F extends FocusType> void assertAssignedRole(PrismObject<F> user, String roleOid) {
assertAssigned(user, roleOid, RoleType.COMPLEX_TYPE);
}
public static <F extends FocusType> void assertNotAssignedRole(PrismObject<F> user, String roleOid) {
assertNotAssigned(user, roleOid, RoleType.COMPLEX_TYPE);
}
public static <F extends FocusType> void assertAssignedRoles(PrismObject<F> user, String... roleOids) {
assertAssignedTargets(user, "roles", RoleType.COMPLEX_TYPE, roleOids);
}
public static <F extends FocusType> void assertAssignedOrgs(PrismObject<F> user, String... orgOids) {
assertAssignedTargets(user, "orgs", OrgType.COMPLEX_TYPE, orgOids);
}
public static <F extends FocusType> void assertAssignedTargets(PrismObject<F> user, String typeDesc, QName type, String... expectedTargetOids) {
F userType = user.asObjectable();
List<String> haveTagetOids = new ArrayList<>();
for (AssignmentType assignmentType: userType.getAssignment()) {
ObjectReferenceType targetRef = assignmentType.getTargetRef();
if (targetRef != null) {
if (type.equals(targetRef.getType())) {
haveTagetOids.add(targetRef.getOid());
}
}
}
PrismAsserts.assertSets("Wrong "+typeDesc+" in "+user, haveTagetOids, expectedTargetOids);
}
public static <F extends FocusType> void assertNotAssignedResource(PrismObject<F> user, String resourceOid) {
F userType = user.asObjectable();
for (AssignmentType assignmentType: userType.getAssignment()) {
if (assignmentType.getConstruction() == null) {
continue;
}
ObjectReferenceType targetRef = assignmentType.getConstruction().getResourceRef();
if (targetRef != null) {
if (resourceOid.equals(targetRef.getOid())) {
AssertJUnit.fail(user + " does have assigned resource "+resourceOid+" while not expecting it");
}
}
}
}
public static <F extends FocusType> void assertAssignedResource(PrismObject<F> user, String resourceOid) {
F userType = user.asObjectable();
for (AssignmentType assignmentType: userType.getAssignment()) {
if (assignmentType.getConstruction() == null) {
continue;
}
ObjectReferenceType targetRef = assignmentType.getConstruction().getResourceRef();
if (targetRef != null) {
if (resourceOid.equals(targetRef.getOid())) {
return;
}
}
}
AssertJUnit.fail(user + " does NOT have assigned resource "+resourceOid+" while expecting it");
}
public static <F extends FocusType> void assertNotAssignedOrg(PrismObject<F> user, String orgOid, QName relation) {
F userType = user.asObjectable();
for (AssignmentType assignmentType: userType.getAssignment()) {
ObjectReferenceType targetRef = assignmentType.getTargetRef();
if (targetRef != null) {
if (OrgType.COMPLEX_TYPE.equals(targetRef.getType())) {
if (orgOid.equals(targetRef.getOid()) && QNameUtil.match(targetRef.getRelation(), relation)) {
AssertJUnit.fail(user + " does have assigned OrgType "+orgOid+" with relation "+relation+" while not expecting it");
}
}
}
}
}
public static void assertAssignedOrg(PrismObject<? extends FocusType> focus, String orgOid) {
assertAssigned(focus, orgOid, OrgType.COMPLEX_TYPE);
}
public static void assertAssignedOrg(PrismObject<? extends FocusType> focus, String orgOid, QName relation) {
assertAssigned(focus, orgOid, OrgType.COMPLEX_TYPE, relation);
}
public static <O extends ObjectType> void assertHasOrg(PrismObject<O> object, String orgOid) {
for (ObjectReferenceType orgRef: object.asObjectable().getParentOrgRef()) {
if (orgOid.equals(orgRef.getOid())) {
return;
}
}
AssertJUnit.fail(object + " does not have org " + orgOid);
}
public static <O extends ObjectType> boolean hasOrg(PrismObject<O> user, String orgOid, QName relation) {
for (ObjectReferenceType orgRef: user.asObjectable().getParentOrgRef()) {
if (orgOid.equals(orgRef.getOid()) &&
MiscSchemaUtil.compareRelation(orgRef.getRelation(), relation)) {
return true;
}
}
return false;
}
public static <O extends ObjectType> void assertHasOrg(PrismObject<O> user, String orgOid, QName relation) {
AssertJUnit.assertTrue(user + " does not have org " + orgOid + ", relation "+relation, hasOrg(user, orgOid, relation));
}
public static <O extends ObjectType> void assertHasNoOrg(PrismObject<O> user, String orgOid, QName relation) {
AssertJUnit.assertFalse(user + " has org " + orgOid + ", relation "+relation+ " even if should NOT have it", hasOrg(user, orgOid, relation));
}
public static <O extends ObjectType> void assertHasNoOrg(PrismObject<O> user) {
assertTrue(user + " does have orgs "+user.asObjectable().getParentOrgRef()+" while not expecting them", user.asObjectable().getParentOrgRef().isEmpty());
}
public static <O extends ObjectType> void assertHasOrgs(PrismObject<O> user, int expectedNumber) {
O userType = user.asObjectable();
assertEquals("Unexepected number of orgs in "+user+": "+userType.getParentOrgRef(), expectedNumber, userType.getParentOrgRef().size());
}
public static <O extends ObjectType> void assertVersionIncrease(PrismObject<O> objectOld, PrismObject<O> objectNew) {
Long versionOld = parseVersion(objectOld);
Long versionNew = parseVersion(objectNew);
assertTrue("Version not increased (from "+versionOld+" to "+versionNew+")", versionOld < versionNew);
}
public static <O extends ObjectType> Long parseVersion(PrismObject<O> object) {
String version = object.getVersion();
if (version == null) {
return null;
}
return Long.valueOf(version);
}
public static <O extends ObjectType> void assertVersion(PrismObject<O> object, int expectedVersion) {
assertVersion(object, Integer.toString(expectedVersion));
}
public static <O extends ObjectType> void assertVersion(PrismObject<O> object, String expectedVersion) {
assertEquals("Wrong version for "+object, expectedVersion, object.getVersion());
}
public static <O extends ObjectType> void assertOid(PrismObject<O> object, String expectedOid) {
assertEquals("Wrong OID for "+object, expectedOid, object.getOid());
}
public static void assertContainsCaseIgnore(String message, Collection<String> actualValues, String expectedValue) {
AssertJUnit.assertNotNull(message+", expected "+expectedValue+", got null", actualValues);
for (String actualValue: actualValues) {
if (StringUtils.equalsIgnoreCase(actualValue, expectedValue)) {
return;
}
}
AssertJUnit.fail(message+", expected "+expectedValue+", got "+actualValues);
}
public static void assertNotContainsCaseIgnore(String message, Collection<String> actualValues, String expectedValue) {
if (actualValues == null) {
return;
}
for (String actualValue: actualValues) {
if (StringUtils.equalsIgnoreCase(actualValue, expectedValue)) {
AssertJUnit.fail(message+", expected that value "+expectedValue+" will not be present but it is");
}
}
}
public static void assertObjectClass(ShadowType shadow, QName expectedStructuralObjectClass, QName... expectedAuxiliaryObjectClasses) {
assertEquals("Wrong object class in "+shadow, expectedStructuralObjectClass, shadow.getObjectClass());
PrismAsserts.assertEqualsCollectionUnordered("Wrong auxiliary object classes in "+shadow, shadow.getAuxiliaryObjectClass(), expectedAuxiliaryObjectClasses);
}
public static void assertInstanceOf(String message, Object object, Class<?> expectedClass) {
assertNotNull(message+" is null", object);
assertTrue(message+" is not instance of "+expectedClass+", it is "+object.getClass(),
expectedClass.isAssignableFrom(object.getClass()));
}
}