Skip to content

Commit

Permalink
Fixing expression support for prism container without compile-time cl…
Browse files Browse the repository at this point in the history
…asses (MID-5264) +tests
  • Loading branch information
semancik committed Apr 9, 2019
1 parent 5f064f5 commit 670d524
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 26 deletions.
Expand Up @@ -736,6 +736,10 @@ public static void assertNoAttribute(Entry response, String name) {
public static void assertAttributeLang(Entry entry, String attributeName, String expectedOrigValue, String... params) {
List<Attribute> attrs = entry.getAttribute(attributeName.toLowerCase());
if (attrs == null || attrs.size() == 0) {
if (expectedOrigValue == null && params.length == 0) {
// everything is as it should be
return;
}
AssertJUnit.fail("Attribute "+attributeName+" does not have any value");
}
Map<String,String> expectedLangs = MiscUtil.paramsToMap(params);
Expand Down
Expand Up @@ -437,43 +437,50 @@ public static <V extends PrismValue, F extends FocusType> Collection<V> computeT
}

// TODO what about collections of values?
public static <T> TypedValue<T> convertVariableValue(TypedValue<T> originalValueAndDefinition, String variableName, ObjectResolver objectResolver,
public static <T> TypedValue<T> convertVariableValue(TypedValue<T> originalTypedValue, String variableName, ObjectResolver objectResolver,
String contextDescription, PrismContext prismContext, Task task, OperationResult result) throws ExpressionSyntaxException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
Object valueToConvert = originalValueAndDefinition.getValue();
Object valueToConvert = originalTypedValue.getValue();
if (valueToConvert == null) {
return originalValueAndDefinition;
return originalTypedValue;
}
TypedValue<T> convertedValueAndDefinition = new TypedValue<T>(valueToConvert, (ItemDefinition)originalValueAndDefinition.getDefinition());
TypedValue<T> convertedTypeValue = originalTypedValue.createTransformed(valueToConvert);
if (valueToConvert instanceof PrismValue) {
((PrismValue) valueToConvert).setPrismContext(prismContext); // TODO - or revive? Or make sure prismContext is set here?
} else if (valueToConvert instanceof Item) {
((Item) valueToConvert).setPrismContext(prismContext); // TODO - or revive? Or make sure prismContext is set here?
}
if (valueToConvert instanceof ObjectReferenceType) {
try {
convertedValueAndDefinition = (TypedValue<T>) resolveReference((TypedValue<ObjectReferenceType>) originalValueAndDefinition, objectResolver, variableName,
convertedTypeValue = (TypedValue<T>) resolveReference((TypedValue<ObjectReferenceType>) originalTypedValue, objectResolver, variableName,
contextDescription, task, result);
valueToConvert = convertedValueAndDefinition.getValue();
valueToConvert = convertedTypeValue.getValue();
} catch (SchemaException e) {
throw new ExpressionSyntaxException("Schema error during variable "+variableName+" resolution in "+contextDescription+": "+e.getMessage(), e);
}
}
if (valueToConvert instanceof PrismObject<?>) {
convertedValueAndDefinition.setValue(((PrismObject<?>)valueToConvert).asObjectable());
return convertedValueAndDefinition;
convertedTypeValue.setValue(((PrismObject<?>)valueToConvert).asObjectable());
return convertedTypeValue;
}
if (valueToConvert instanceof PrismContainerValue<?>) {
convertedValueAndDefinition.setValue(((PrismContainerValue<?>)valueToConvert).asContainerable());
return convertedValueAndDefinition;
PrismContainerValue<?> cval = ((PrismContainerValue<?>)valueToConvert);
Class<?> containerCompileTimeClass = cval.getCompileTimeClass();
if (containerCompileTimeClass == null) {
// Dynamic schema. We do not have anything to convert to. Leave it as PrismContainerValue
convertedTypeValue.setValue(valueToConvert);
} else {
convertedTypeValue.setValue(cval.asContainerable());
}
return convertedTypeValue;
}
if (valueToConvert instanceof PrismPropertyValue<?>) {
convertedValueAndDefinition.setValue(((PrismPropertyValue<?>)valueToConvert).getValue());
return convertedValueAndDefinition;
convertedTypeValue.setValue(((PrismPropertyValue<?>)valueToConvert).getValue());
return convertedTypeValue;
}
if (valueToConvert instanceof PrismReferenceValue) {
if (((PrismReferenceValue) valueToConvert).getDefinition() != null) {
convertedValueAndDefinition.setValue(((PrismReferenceValue) valueToConvert).asReferencable());
return convertedValueAndDefinition;
convertedTypeValue.setValue(((PrismReferenceValue) valueToConvert).asReferencable());
return convertedTypeValue;
}
}
if (valueToConvert instanceof PrismProperty<?>) {
Expand Down Expand Up @@ -508,26 +515,36 @@ public static <T> TypedValue<T> convertVariableValue(TypedValue<T> originalValue
if (valueToConvert instanceof PrismContainer<?>) {
PrismContainer<?> container = (PrismContainer<?>)valueToConvert;
PrismContainerDefinition<?> def = container.getDefinition();
if (def != null) {
if (def.isSingleValue()) {
return new TypedValue(container.getRealValue(), def);
Class<?> containerCompileTimeClass = container.getCompileTimeClass();
if (containerCompileTimeClass == null) {
// Dynamic schema. We do not have anything to convert to. Leave it as PrismContainer
if (def != null) {
return new TypedValue(container, def);
} else {
return new TypedValue(container.getRealValues(), def);

return new TypedValue(container, PrismContainer.class);
}
} else {
PrismContainerValue<?> cval = container.getValue();
if (cval != null) {
Containerable containerable = cval.asContainerable();
if (containerable != null) {
return new TypedValue(container.getRealValues(), containerable.getClass());
if (def != null) {
if (def.isSingleValue()) {
return new TypedValue(container.getRealValue(), def);
} else {
return new TypedValue(container.getRealValues(), def);

}
} else {
PrismContainerValue<?> cval = container.getValue();
if (cval != null) {
Containerable containerable = cval.asContainerable();
if (containerable != null) {
return new TypedValue(container.getRealValues(), containerable.getClass());
}
}
return new TypedValue(container.getRealValues(), Object.class);
}
return new TypedValue(container.getRealValues(), Object.class);
}
}

return convertedValueAndDefinition;
return convertedTypeValue;
}

private static TypedValue<PrismObject<?>> resolveReference(TypedValue<ObjectReferenceType> refAndDef, ObjectResolver objectResolver,
Expand Down
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2019 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.asserter.prism;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

import java.util.Iterator;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismReference;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.prism.xml.ns._public.types_3.RawType;

/**
* @author semancik
*
*/
public class PrismContainerAsserter<C extends Containerable, RA> extends PrismItemAsserter<PrismContainer<C>, RA> {

public PrismContainerAsserter(PrismContainer<C> container) {
super(container);
}

public PrismContainerAsserter(PrismContainer<C> container, String detail) {
super(container, detail);
}

public PrismContainerAsserter(PrismContainer<C> container, RA returnAsserter, String detail) {
super(container, returnAsserter, detail);
}

@Override
public PrismContainerAsserter<C,RA> assertSize(int expected) {
super.assertSize(expected);
return this;
}

// TODO

protected String desc() {
return getDetails();
}

}
Expand Up @@ -190,6 +190,14 @@ public <CC extends Containerable> PrismContainerValueAsserter<CC,? extends Prism
return asserter;
}

public <CC extends Containerable> PrismContainerAsserter<CC,? extends PrismContainerValueAsserter<C,RA>> container(QName subcontainerQName) {
PrismContainer<CC> container = findContainer(subcontainerQName);
assertNotNull("No container "+subcontainerQName+" in "+desc(), container);
PrismContainerAsserter<CC,PrismContainerValueAsserter<C,RA>> asserter = new PrismContainerAsserter<>(container, this, subcontainerQName.getLocalPart() + " in " + desc());
copySetupTo(asserter);
return asserter;
}

// TODO

protected String desc() {
Expand Down
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2019 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.asserter.prism;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.test.asserter.AbstractAsserter;


/**
* @author semancik
*
*/
public abstract class PrismItemAsserter<I extends Item, RA> extends AbstractAsserter<RA> {

private I item;

public PrismItemAsserter(I item) {
super();
this.item = item;
}

public PrismItemAsserter(I item, String detail) {
super(detail);
this.item = item;
}

public PrismItemAsserter(I item, RA returnAsserter, String detail) {
super(returnAsserter, detail);
this.item = item;
}

public I getItem() {
return item;
}

public PrismItemAsserter<I,RA> assertSize(int expected) {
assertEquals("Wrong number of values in "+desc(), expected, item.size());
return this;
}

// TODO

protected String desc() {
return getDetails();
}

}

0 comments on commit 670d524

Please sign in to comment.