/
TestExpressionUtil.java
252 lines (200 loc) · 10.9 KB
/
TestExpressionUtil.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
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.model.common.expression;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.assertEquals;
import com.evolveum.midpoint.prism.impl.marshaller.ItemPathParserTemp;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.util.ItemDeltaItem;
import com.evolveum.midpoint.prism.util.ObjectDeltaObject;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.repo.common.DirectoryFileObjectResolver;
import com.evolveum.midpoint.repo.common.ObjectResolver;
import com.evolveum.midpoint.task.api.test.NullTaskImpl;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import org.testng.annotations.BeforeSuite;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.evolveum.midpoint.repo.common.expression.ExpressionUtil;
import com.evolveum.midpoint.repo.common.expression.ExpressionVariables;
import com.evolveum.midpoint.model.common.AbstractModelCommonTest;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.UniformItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.ExpressionConstants;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.test.IntegrationTestTools;
import com.evolveum.midpoint.test.util.MidPointTestConstants;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
/**
* @author semancik
*
*/
public class TestExpressionUtil extends AbstractModelCommonTest {
public static final String USER_JACK_OID = "c0c010c0-d34d-b33f-f00d-111111111111";
public static final File USER_JACK_FILE = new File(MidPointTestConstants.OBJECTS_DIR, USER_JACK_OID + ".xml");
@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(MidPointConstants.NS_MIDPOINT_PUBLIC_PREFIX);
PrismTestUtil.resetPrismContext(MidPointPrismContextFactory.FACTORY);
}
@Test
public void testResolvePathStringProperty() throws Exception {
final String TEST_NAME = "testResolvePathStringProperty";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
PrismProperty<String> resolvedProperty = resolvePath("$user/description", TEST_NAME);
// THEN
assertEquals("Wrong resolved property value", "jack", resolvedProperty.getRealValue());
}
@Test
public void testResolvePathPolyStringProperty() throws Exception {
final String TEST_NAME = "testResolvePathPolyStringProperty";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
PrismProperty<PolyString> resolvedProperty = resolvePath("$user/fullName", TEST_NAME);
// THEN
assertEquals("Wrong resolved property value", PrismTestUtil.createPolyString("Jack Sparrow"),
resolvedProperty.getRealValue());
}
@Test
public void testResolvePathPolyStringOrig() throws Exception {
final String TEST_NAME = "testResolvePathPolyStringOrig";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
String resolved = resolvePath("$user/fullName/t:orig", TEST_NAME);
// THEN
assertEquals("Wrong resolved property value", "Jack Sparrow", resolved);
}
@Test
public void testResolvePathPolyStringNorm() throws Exception {
final String TEST_NAME = "testResolvePathPolyStringNorm";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
String resolved = resolvePath("$user/fullName/t:norm", TEST_NAME);
// THEN
assertEquals("Wrong resolved property value", "jack sparrow", resolved);
}
@Test
public void testResolvePathPolyStringOdo() throws Exception {
final String TEST_NAME = "testResolvePathPolyStringOdo";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
ItemDeltaItem<PrismPropertyValue<PolyString>,PrismPropertyDefinition<PolyString>> idi = resolvePathOdo("$user/fullName", TEST_NAME);
// THEN
assertEquals("Wrong resolved idi old value", PrismTestUtil.createPolyString("Jack Sparrow"),
((PrismProperty<PolyString>)idi.getItemOld()).getRealValue());
assertEquals("Wrong resolved idi new value", PrismTestUtil.createPolyString("Captain Jack Sparrow"),
((PrismProperty<PolyString>)idi.getItemNew()).getRealValue());
assertTrue("Wrong residual path: "+idi.getResidualPath(),
idi.getResidualPath() == null || idi.getResidualPath().isEmpty());
}
@Test
public void testResolvePathPolyStringOdoOrig() throws Exception {
final String TEST_NAME = "testResolvePathPolyStringOdoOrig";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
ItemDeltaItem<PrismPropertyValue<PolyString>,PrismPropertyDefinition<PolyString>> idi = resolvePathOdo("$user/fullName/t:orig", TEST_NAME);
// THEN
assertEquals("Wrong resolved idi old value", PrismTestUtil.createPolyString("Jack Sparrow"),
((PrismProperty<PolyString>)idi.getItemOld()).getRealValue());
assertEquals("Wrong resolved idi new value", PrismTestUtil.createPolyString("Captain Jack Sparrow"),
((PrismProperty<PolyString>)idi.getItemNew()).getRealValue());
PrismAsserts.assertPathEquivalent("Wrong residual path", PolyString.F_ORIG, idi.getResidualPath());
}
@Test
public void testResolvePathPolyStringOdoNorm() throws Exception {
final String TEST_NAME = "testResolvePathPolyStringOdoNorm";
displayTestTitle(TEST_NAME);
// GIVEN
// WHEN
ItemDeltaItem<PrismPropertyValue<PolyString>,PrismPropertyDefinition<PolyString>> idi = resolvePathOdo("$user/fullName/t:norm", TEST_NAME);
// THEN
assertEquals("Wrong resolved idi old value", PrismTestUtil.createPolyString("Jack Sparrow"),
((PrismProperty<PolyString>)idi.getItemOld()).getRealValue());
assertEquals("Wrong resolved idi new value", PrismTestUtil.createPolyString("Captain Jack Sparrow"),
((PrismProperty<PolyString>)idi.getItemNew()).getRealValue());
PrismAsserts.assertPathEquivalent("Wrong residual path", PolyString.F_NORM, idi.getResidualPath());
}
private <T> T resolvePath(String path, final String TEST_NAME) throws SchemaException, ObjectNotFoundException, IOException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
ExpressionVariables variables = createVariables();
return resolvePath(path, variables, TEST_NAME);
}
private <T> T resolvePathOdo(String path, final String TEST_NAME) throws SchemaException, ObjectNotFoundException, IOException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
ExpressionVariables variables = createVariablesOdo();
return resolvePath(path, variables, TEST_NAME);
}
private <T> T resolvePath(String path, ExpressionVariables variables, final String TEST_NAME) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
OperationResult result = new OperationResult(TestExpressionUtil.class.getName() + "." + TEST_NAME);
ItemPath itemPath = toItemPath(path);
// WHEN
ObjectResolver objectResolver = new DirectoryFileObjectResolver(MidPointTestConstants.OBJECTS_DIR);
Object resolved = ExpressionUtil.resolvePathGetValue(itemPath, variables, false, null, objectResolver,
PrismTestUtil.getPrismContext(), TEST_NAME, new NullTaskImpl(), result);
// THEN
IntegrationTestTools.display("Resolved", resolved);
//noinspection unchecked
return (T) resolved;
}
private ExpressionVariables createVariables() throws SchemaException, IOException {
ExpressionVariables variables = new ExpressionVariables();
PrismObject<UserType> user = createUser();
variables.addVariableDefinition(ExpressionConstants.VAR_USER, user, user.getDefinition());
variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, user, user.getDefinition());
return variables;
}
private ExpressionVariables createVariablesOdo() throws SchemaException, IOException {
ExpressionVariables variables = new ExpressionVariables();
PrismObject<UserType> userOld = createUser();
ObjectDelta<UserType> delta = PrismTestUtil.getPrismContext().deltaFactory().object().createModificationReplaceProperty(UserType.class,
userOld.getOid(), UserType.F_FULL_NAME,
PrismTestUtil.createPolyString("Captain Jack Sparrow"));
ObjectDeltaObject<UserType> odo = new ObjectDeltaObject<>(userOld, delta, null, userOld.getDefinition());
odo.recompute();
variables.addVariableDefinition(ExpressionConstants.VAR_USER, odo, odo.getDefinition());
variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, odo, odo.getDefinition());
return variables;
}
private PrismObject<UserType> createUser() throws SchemaException, IOException {
return PrismTestUtil.parseObject(USER_JACK_FILE);
}
// the path can contain t:xyz elements
private UniformItemPath toItemPath(String stringPath) {
String xml = "<path " +
"xmlns='"+SchemaConstants.NS_C+"' " +
"xmlns:t='"+SchemaConstants.NS_TYPES+"'>" +
stringPath + "</path>";
Document doc = DOMUtil.parseDocument(xml);
Element element = DOMUtil.getFirstChildElement(doc);
return ItemPathParserTemp.parseFromElement(element);
}
}