Skip to content

Commit

Permalink
Fixing matching rules in midpoint.isUniquePropertyValue() library fun…
Browse files Browse the repository at this point in the history
…ction (MID-2887)
  • Loading branch information
semancik committed May 16, 2016
1 parent 5b9abba commit cc5f015
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
Expand Up @@ -27,6 +27,7 @@
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.match.DefaultMatchingRule;
import com.evolveum.midpoint.prism.match.PolyStringOrigMatchingRule;
import com.evolveum.midpoint.prism.parser.XPathHolder;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
Expand Down Expand Up @@ -526,7 +527,7 @@ public <T> boolean isUniquePropertyValue(ObjectType objectType, String propertyP
private <T> boolean isUniquePropertyValue(final ObjectType objectType, ItemPath propertyPath, T propertyValue, Task task, OperationResult result)
throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException,
SecurityViolationException {
List<? extends ObjectType> conflictingObjects = getObjectsInConflictOnPropertyValue(objectType, propertyPath, propertyValue, DefaultMatchingRule.NAME, false, task, result);
List<? extends ObjectType> conflictingObjects = getObjectsInConflictOnPropertyValue(objectType, propertyPath, propertyValue, null, false, task, result);
return conflictingObjects.isEmpty();
}

Expand All @@ -549,6 +550,13 @@ private <O extends ObjectType, T> List<O> getObjectsInConflictOnPropertyValue(fi
Validate.notNull(propertyPath, "Null property path");
Validate.notNull(propertyValue, "Null property value");
PrismPropertyDefinition<T> propertyDefinition = objectType.asPrismObject().getDefinition().findPropertyDefinition(propertyPath);
if (matchingRule == null) {
if (propertyDefinition != null && PolyStringType.COMPLEX_TYPE.equals(propertyDefinition.getTypeName())) {
matchingRule = PolyStringOrigMatchingRule.NAME;
} else {
matchingRule = DefaultMatchingRule.NAME;
}
}
EqualFilter<T> filter = EqualFilter.createEqual(propertyPath, propertyDefinition, matchingRule, propertyValue);
ObjectQuery query = ObjectQuery.createObjectQuery(filter);
if (LOGGER.isTraceEnabled()) {
Expand Down
Expand Up @@ -151,7 +151,7 @@ public void testGetManagersOids() throws Exception {
PrismObject<UserType> chef = repositoryService.getObject(UserType.class, CHEF_OID, null, result);

ScriptExpressionEvaluatorType scriptType = parseScriptType("expression-" + TEST_NAME + ".xml");
ItemDefinition outputDefinition = new PrismPropertyDefinition(PROPERTY_NAME, DOMUtil.XSD_STRING, PrismTestUtil.getPrismContext());
PrismPropertyDefinition<String> outputDefinition = new PrismPropertyDefinition<>(PROPERTY_NAME, DOMUtil.XSD_STRING, PrismTestUtil.getPrismContext());
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, TEST_NAME);
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinition(new QName(SchemaConstants.NS_C, "user"), chef);
Expand All @@ -169,6 +169,36 @@ public void testGetManagersOids() throws Exception {
Set<String> expectedOids = new HashSet<String>(Arrays.asList(new String[] { CHEESE_OID, CHEESE_JR_OID, LECHUCK_OID }));
assertEquals("Unexpected script output", expectedOids, oids);
}

/**
* MID-2887
*/
@Test
public void testIsUniquePropertyValue() throws Exception {
final String TEST_NAME = "testIsUniquePropertyValue";
TestUtil.displayTestTile(this, TEST_NAME);

// GIVEN
OperationResult result = new OperationResult(TestModelExpressions.class.getName() + "." + TEST_NAME);

PrismObject<UserType> chef = repositoryService.getObject(UserType.class, CHEF_OID, null, result);

ScriptExpressionEvaluatorType scriptType = parseScriptType("expression-" + TEST_NAME + ".xml");
PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinition<>(PROPERTY_NAME, DOMUtil.XSD_BOOLEAN, PrismTestUtil.getPrismContext());
ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, TEST_NAME);
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinition(new QName(SchemaConstants.NS_C, "user"), chef);
variables.addVariableDefinition(new QName(SchemaConstants.NS_C, "value"), "Scumm Bar Chef");

// WHEN
List<PrismPropertyValue<Boolean>> scriptOutputs = evaluate(scriptExpression, variables, false, TEST_NAME, null, result);

// THEN
display("Script output", scriptOutputs);
assertEquals("Unexpected number of script outputs", 1, scriptOutputs.size());
Boolean scriptOutput = scriptOutputs.get(0).getValue();
assertEquals("Unexpected script output", Boolean.TRUE, scriptOutput);
}

@Test
public void testGetOrgByName() throws Exception {
Expand Down Expand Up @@ -297,7 +327,7 @@ private String executeScriptExpressionString(final String TEST_NAME, ExpressionV

}

private List<PrismPropertyValue<String>> evaluate(ScriptExpression scriptExpression, ExpressionVariables variables, boolean useNew,
private <T> List<PrismPropertyValue<T>> evaluate(ScriptExpression scriptExpression, ExpressionVariables variables, boolean useNew,
String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
if (task == null) {
task = taskManager.createTaskInstance();
Expand Down
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<script xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3">
<language>http://midpoint.evolveum.com/xml/ns/public/expression/language#Groovy</language>
<code>
midpoint.isUniquePropertyValue(user, 'fullName', value)
</code>
</script>

0 comments on commit cc5f015

Please sign in to comment.