From 115da97223e9652aa2742932bf398ec2ecf12f63 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Fri, 3 Feb 2017 16:32:55 +0100 Subject: [PATCH] Fixed missing xsi:type for polymorphic (multivalued?) properties. --- .../prism/marshaller/PrismMarshaller.java | 17 ++-- .../parser/TestParseWorkflowContext.java | 78 +++++++++++++++++++ .../resources/common/xml/ns/wf-context.xml | 40 ++++++++++ infra/schema/testng-unit.xml | 1 + 4 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 infra/schema/src/test/java/com/evolveum/midpoint/schema/parser/TestParseWorkflowContext.java create mode 100644 infra/schema/src/test/resources/common/xml/ns/wf-context.xml diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java index 216dacbb6a9..1b336a840c8 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java @@ -22,6 +22,7 @@ import com.evolveum.midpoint.prism.xnode.*; import com.evolveum.midpoint.util.DOMUtil; import com.evolveum.midpoint.util.JAXBUtil; +import com.evolveum.midpoint.util.QNameUtil; import com.evolveum.midpoint.util.exception.SchemaException; import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType; import com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType; @@ -192,9 +193,9 @@ private XNode marshalItemValue(@NotNull PrismValue itemValue, @Nullable ItemDefi } else if (itemValue instanceof PrismReferenceValue) { xnode = serializeReferenceValue((PrismReferenceValue)itemValue, (PrismReferenceDefinition) definition, ctx); } else if (itemValue instanceof PrismPropertyValue) { - xnode = serializePropertyValue((PrismPropertyValue)itemValue, (PrismPropertyDefinition)definition, typeName); + xnode = serializePropertyValue((PrismPropertyValue)itemValue, (PrismPropertyDefinition) definition, typeName); } else if (itemValue instanceof PrismContainerValue) { - xnode = marshalContainerValue((PrismContainerValue)itemValue, (PrismContainerDefinition)definition, ctx); + xnode = marshalContainerValue((PrismContainerValue)itemValue, (PrismContainerDefinition) definition, ctx); } else { throw new IllegalArgumentException("Unsupported value type "+itemValue.getClass()); } @@ -392,12 +393,12 @@ private XNode serializePropertyValue(@NotNull PrismPropertyValue value, P return serializePolyString((PolyString) realValue); } else if (beanMarshaller.canProcess(typeName)) { XNode xnode = beanMarshaller.marshall(realValue); -// // why is this? -// if (realValue instanceof ProtectedDataType && (definition == null || definition.isDynamic())) { -// xnode.setExplicitTypeDeclaration(true); -// xnode.setTypeQName(typeName); -// } - return xnode; + TypeDefinition typeDef = getSchemaRegistry().findTypeDefinitionByCompileTimeClass(realValue.getClass(), TypeDefinition.class); + if (xnode != null && typeDef != null && !QNameUtil.match(typeDef.getTypeName(), typeName)) { + xnode.setTypeQName(typeDef.getTypeName()); + xnode.setExplicitTypeDeclaration(true); + } + return xnode; } else { // primitive value return createPrimitiveXNode(realValue, typeName); diff --git a/infra/schema/src/test/java/com/evolveum/midpoint/schema/parser/TestParseWorkflowContext.java b/infra/schema/src/test/java/com/evolveum/midpoint/schema/parser/TestParseWorkflowContext.java new file mode 100644 index 00000000000..eaa4456ec74 --- /dev/null +++ b/infra/schema/src/test/java/com/evolveum/midpoint/schema/parser/TestParseWorkflowContext.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2010-2017 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.schema.parser; + +import com.evolveum.midpoint.prism.PrismContainerValue; +import com.evolveum.midpoint.schema.SchemaConstantsGenerated; +import com.evolveum.midpoint.util.exception.SchemaException; +import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingsType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.WfContextType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.WfProcessEventType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemCompletionEventType; +import org.testng.annotations.Test; + +import javax.xml.namespace.QName; +import java.io.File; + +import static org.testng.AssertJUnit.assertEquals; + +/** + * TODO finish + * @author mederly + * + */ +@SuppressWarnings({ "Convert2MethodRef", "Duplicates" }) +public class TestParseWorkflowContext extends AbstractContainerValueParserTest { + + @Override + protected File getFile() { + return getFile("wf-context"); + } + + @Test + public void testParseFile() throws Exception { + displayTestTitle("testParseFile"); + processParsings(null, null); + } + + @Test + public void testParseRoundTrip() throws Exception{ + displayTestTitle("testParseRoundTrip"); + + //processParsings(v -> getPrismContext().serializerFor(language).serialize(v)); // no item name nor definition => cannot serialize + processParsings(v -> getPrismContext().serializerFor(language).root(new QName("dummy")).serialize(v), "s1"); + processParsings(v -> getPrismContext().serializerFor(language).root(SchemaConstantsGenerated.C_USER).serialize(v), "s2"); // misleading item name + processParsings(v -> getPrismContext().serializerFor(language).root(new QName("dummy")).serializeRealValue(v.asContainerable()), "s3"); + processParsings(v -> getPrismContext().serializerFor(language).root(new QName("dummy")).serializeAnyData(v.asContainerable()), "s4"); + } + + private void processParsings(SerializingFunction> serializer, String serId) throws Exception { + processParsings(WfContextType.class, null, WfContextType.COMPLEX_TYPE, null, serializer, serId); + } + + @Override + public void assertPrismContainerValueLocal(PrismContainerValue value) throws SchemaException { + WfContextType wfc = value.asContainerable(); + assertEquals("Wrong # of events", 1, wfc.getEvent().size()); + assertEquals("Wrong type of first event", WorkItemCompletionEventType.class, wfc.getEvent().get(0).getClass()); + + } + + @Override + protected boolean isContainer() { + return false; + } +} diff --git a/infra/schema/src/test/resources/common/xml/ns/wf-context.xml b/infra/schema/src/test/resources/common/xml/ns/wf-context.xml new file mode 100644 index 00000000000..e6132a73694 --- /dev/null +++ b/infra/schema/src/test/resources/common/xml/ns/wf-context.xml @@ -0,0 +1,40 @@ + + + + 1 + a + b + + 2017-02-03T15:59:40.015+01:00 + + + test + test + + + 1 + 116 + + approve + __APPROVED__ + + + \ No newline at end of file diff --git a/infra/schema/testng-unit.xml b/infra/schema/testng-unit.xml index 132750ee1f9..0d2b3d7506b 100644 --- a/infra/schema/testng-unit.xml +++ b/infra/schema/testng-unit.xml @@ -29,6 +29,7 @@ +