Skip to content

Commit

Permalink
MID-7723: fix convert non exist attribute with non exists parent to n…
Browse files Browse the repository at this point in the history
…ative item delta
  • Loading branch information
skublik committed Mar 16, 2022
1 parent f05913a commit c0d817b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.ModificationTypeType;

Expand All @@ -35,6 +37,8 @@
*/
class ItemDeltaBeanToNativeConversion<IV extends PrismValue, ID extends ItemDefinition<?>> {

private static final Trace LOGGER = TraceManager.getTrace(ItemDeltaBeanToNativeConversion.class);

/**
* Delta to be converted ("XML").
*/
Expand Down Expand Up @@ -104,7 +108,7 @@ public ItemDelta<IV, ID> convert() throws SchemaException {
//noinspection unchecked
return (ItemDelta<IV, ID>) itemDefinition.createEmptyDelta(itemPath);
} else {
PrismProperty<?> property = new PrismPropertyImpl<>(itemName, parentDefinition.getPrismContext());
PrismProperty<?> property = new PrismPropertyImpl<>(itemName, PrismContext.get());
//noinspection unchecked
return (ItemDelta<IV, ID>) property.createDelta(itemPath);
}
Expand All @@ -114,7 +118,7 @@ private void findParentDefinitionIfNeeded() throws SchemaException {
if (itemDefinition == null) {
parentDefinition = rootContainerDef.findContainerDefinition(itemPath.allUpToLastName());
if (parentDefinition == null) {
throw new SchemaException("No definition for " + itemPath.allUpToLastName().lastName() +
LOGGER.debug("No definition for " + itemPath.allUpToLastName().lastName() +
" (while creating delta for " + rootContainerDef + ")");
}
}
Expand All @@ -125,10 +129,9 @@ private Collection<IV> getParsedValues(List<RawType> values)

List<IV> parsedValues = new ArrayList<>();
for (RawType rawValue : values) {
if (itemDefinition == null) {
assert parentDefinition != null;
if (itemDefinition == null && parentDefinition != null) {
//noinspection unchecked
itemDefinition = (ID) ((PrismContextImpl) parentDefinition.getPrismContext()).getPrismUnmarshaller()
itemDefinition = (ID) ((PrismContextImpl) PrismContext.get()).getPrismUnmarshaller()
.locateItemDefinition(parentDefinition, itemName, rawValue.getXnode());
}
// Note this can be a slight problem if itemDefinition is PRD and the value is a full object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,31 @@ public void testModifyInducement() throws Exception {
assertEquals("wrong target type in targetRef", RoleType.COMPLEX_TYPE, targetRefVal.getTargetType());
}

@Test
public void testModifyNonExistsAttribute() throws Exception {
System.out.println("===[ testModifyNonExistsAttribute ]====");

ObjectModificationType objectChange = PrismTestUtil.parseAtomicValue(new File(TEST_DIR, "resource-modify-non-exists-attribute.xml"),
ObjectModificationType.COMPLEX_TYPE);

// WHEN
ObjectDelta<ResourceType> objectDelta = DeltaConvertor.createObjectDelta(objectChange, ResourceType.class,
getPrismContext());

System.out.println("Delta:");
System.out.println(objectDelta.debugDump());

// THEN
assertNotNull("No object delta", objectDelta);
objectDelta.checkConsistence();
assertEquals("Wrong OID", "00000000-8888-6666-0000-100000000775", objectDelta.getOid());
PropertyDelta<Object> propertyDelta = objectDelta.findPropertyDelta(ItemPath.create("nonExistsParent", "nonExistsAttribute"));
assertNotNull("No targetRef delta", propertyDelta);
Collection<PrismPropertyValue<Object>> valuesToReplace = propertyDelta.getValuesToReplace();
assertEquals("Wrong number of values to replace", 1, valuesToReplace.size());
assertTrue("wrong target type in targetRef", valuesToReplace.iterator().next().getValue() instanceof RawType);
}

@Test
public void test100ObjectAdd() throws Exception {
System.out.println("===[ test100ObjectAdd ]====");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright (c) 2010-2017 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<objectModification
xmlns='http://midpoint.evolveum.com/xml/ns/public/common/api-types-3'
xmlns:c='http://midpoint.evolveum.com/xml/ns/public/common/common-3'
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3">
<oid>00000000-8888-6666-0000-100000000775</oid>
<itemDelta>
<t:modificationType>replace</t:modificationType>
<t:path>c:nonExistsParent/c:nonExistsAttribute</t:path>
<t:value>
<c:objectActionsEntry>
<c:objectType>c:ResourceType</c:objectType>
<c:operation>modify</c:operation>
<c:totalSuccessCount>1</c:totalSuccessCount>
<c:lastSuccessObjectName>RTISZ_T</c:lastSuccessObjectName>
<c:lastSuccessObjectOid>cececece-44c9-492b-8fea-619591591097</c:lastSuccessObjectOid>
<c:lastSuccessTimestamp>2021-11-02T07:24:47.291+01:00</c:lastSuccessTimestamp>
<c:totalFailureCount>0</c:totalFailureCount>
</c:objectActionsEntry>
<c:resultingObjectActionsEntry>
<c:objectType>c:ResourceType</c:objectType>
<c:operation>modify</c:operation>
<c:totalSuccessCount>1</c:totalSuccessCount>
<c:lastSuccessObjectName>RTISZ_T</c:lastSuccessObjectName>
<c:lastSuccessObjectOid>cececece-44c9-492b-8fea-619591591097</c:lastSuccessObjectOid>
<c:lastSuccessTimestamp>2021-11-02T07:24:47.291+01:00</c:lastSuccessTimestamp>
<c:totalFailureCount>0</c:totalFailureCount>
</c:resultingObjectActionsEntry>
</t:value>
</itemDelta>
</objectModification>

0 comments on commit c0d817b

Please sign in to comment.