public static <T extends ObjectType> Collection<? extends ItemDelta<?, ?>> computeReencryptModifications(Protector protector, PrismObject<T> object) throws EncryptionException { PrismObject<T> reencrypted = object.clone(); int changes = reencryptValues(protector, reencrypted); if (changes == 0) { return Collections.emptySet(); } ObjectDelta<T> diff = object.diff(reencrypted, EquivalenceStrategy.LITERAL); if (!diff.isModify()) { throw new AssertionError("Expected MODIFY delta, got " + diff); } return diff.getModifications(); }
public static <T extends Objectable> ObjectDelta<T> diff(PrismObject<T> oldObject, PrismObject<T> newObject) { if (oldObject == null) { if (newObject == null) { return null; } PrismContext prismContext = getPrismContext(oldObject, newObject); if (prismContext == null) { throw new IllegalStateException("No prismContext in DiffUtil.diff!"); } ObjectDelta<T> objectDelta = prismContext.deltaFactory().object().create(newObject.getCompileTimeClass(), ChangeType.ADD); objectDelta.setOid(newObject.getOid()); objectDelta.setObjectToAdd(newObject); return objectDelta; } else { return oldObject.diff(newObject); } }
@Test public void diffRoles() throws Exception { PrismObject<RoleType> role1 = PrismTestUtil.parseObject(ROLE_1); PrismObject<RoleType> role2 = PrismTestUtil.parseObject(ROLE_2); ObjectDelta<RoleType> delta = role1.diff(role2, EquivalenceStrategy.LITERAL_IGNORE_METADATA); assertFalse(delta.isEmpty()); }
private <O extends ObjectType> void serializationRoundTripPrismObject(PrismObject<O> parsedObject) throws Exception { // WHEN String serializedObject = SerializationUtil.toString(parsedObject); // THEN System.out.println("\nSerialized object:"); System.out.println(serializedObject); PrismObject<O> deserializedObject = SerializationUtil.fromString(serializedObject); System.out.println("\nDeserialized object (PrismObject):"); System.out.println(deserializedObject.debugDump()); deserializedObject.revive(getPrismContext()); ObjectDelta<O> diff = parsedObject.diff(deserializedObject); assertTrue("Something changed in serialization of "+parsedObject+" (PrismObject): "+diff, diff.isEmpty()); }
@Test public void testResourceNsChangeLiteral() throws SchemaException, SAXException, IOException, JAXBException { System.out.println("===[ testResourceNsChangeLiteral ]==="); PrismObject<ResourceType> resourceBefore = PrismTestUtil.parseObject(RESOURCE_BEFORE_FILE); PrismObject<ResourceType> resourceAfter = PrismTestUtil.parseObject(RESOURCE_AFTER_NS_CHANGE_FILE); resourceBefore.checkConsistence(); resourceAfter.checkConsistence(); // WHEN ObjectDelta<ResourceType> resourceDelta = resourceBefore.diff(resourceAfter, EquivalenceStrategy.LITERAL_IGNORE_METADATA); // THEN System.out.println("DELTA:"); System.out.println(resourceDelta.debugDump()); resourceDelta.checkConsistence(); resourceDelta.assertDefinitions(true); resourceBefore.checkConsistence(); resourceAfter.checkConsistence(); assertFalse("The delta is empty", resourceDelta.isEmpty()); }
private MidPointPrincipal save(MidPointPrincipal person, OperationResult result) throws ObjectNotFoundException, SchemaException, ObjectAlreadyExistsException { UserType oldUserType = getUserByOid(person.getOid(), result); PrismObject<UserType> oldUser = oldUserType.asPrismObject(); PrismObject<UserType> newUser = person.getUser().asPrismObject(); ObjectDelta<UserType> delta = oldUser.diff(newUser); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Updating user {} with delta:\n{}", newUser, delta.debugDump()); } repositoryService.modifyObject(UserType.class, delta.getOid(), delta.getModifications(), new OperationResult(OPERATION_UPDATE_USER)); return person; }
private void checkObject(String oid, PrismObject<UserType> expected, boolean loadPhoto, OperationResult result) throws ObjectNotFoundException, SchemaException { Collection<SelectorOptions<GetOperationOptions>> options; if (loadPhoto) { options = Collections.singletonList( SelectorOptions.create(prismContext.toUniformPath(UserType.F_JPEG_PHOTO), GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE))); } else { options = null; } PrismObject<UserType> real = repositoryService.getObject(UserType.class, oid, options, result); ObjectDelta<UserType> delta = expected.diff(real); System.out.println("Expected object = \n" + expected.debugDump()); System.out.println("Real object in repo = \n" + real.debugDump()); System.out.println("Difference = \n" + delta.debugDump()); if (!delta.isEmpty()) { fail("Objects are not equal.\n*** Expected:\n" + expected.debugDump() + "\n*** Got:\n" + real.debugDump() + "\n*** Delta:\n" + delta.debugDump()); } }
private <O extends ObjectType> void serializationRoundTripObjectType(O parsedObject) throws Exception { // WHEN String serializedObject = SerializationUtil.toString(parsedObject); // THEN O deserializedObject = SerializationUtil.fromString(serializedObject); deserializedObject.asPrismObject().revive(getPrismContext()); System.out.println("Deserialized object (ObjectType):"); System.out.println(deserializedObject.asPrismObject().debugDump()); ObjectDelta<O> diff = parsedObject.asPrismObject().diff((PrismObject) deserializedObject.asPrismObject()); assertTrue("Something changed in serializetion of "+parsedObject+" (ObjectType): "+diff, diff.isEmpty()); }
private void checkObject(String oid, PrismObject<OrgType> expected, boolean loadPhoto, OperationResult result) throws ObjectNotFoundException, SchemaException { Collection<SelectorOptions<GetOperationOptions>> options; if (loadPhoto) { options = Collections.singletonList( SelectorOptions.create(prismContext.toUniformPath(FocusType.F_JPEG_PHOTO), GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE))); } else { options = null; } PrismObject<OrgType> real = repositoryService.getObject(OrgType.class, oid, options, result); ObjectDelta<OrgType> delta = expected.diff(real); System.out.println("Expected object = \n" + expected.debugDump()); System.out.println("Real object in repo = \n" + real.debugDump()); System.out.println("Difference = \n" + delta.debugDump()); if (!delta.isEmpty()) { fail("Objects are not equal.\n*** Expected:\n" + expected.debugDump() + "\n*** Got:\n" + real.debugDump() + "\n*** Delta:\n" + delta.debugDump()); } }
@Test public void testUserListSimpleDiff() throws SchemaException { System.out.println("\n\n===[ testUserListSimpleDiff ]===\n"); UserType u1 = new UserType(); u1.setName(PrismTestUtil.createPolyStringType("test name")); UserType u2 = new UserType(); u2.setName(PrismTestUtil.createPolyStringType("test name")); PrismTestUtil.getPrismContext().adopt(u1); PrismTestUtil.getPrismContext().adopt(u2); ObjectDelta delta = u1.asPrismObject().diff(u2.asPrismObject()); assertNotNull(delta); assertEquals(0, delta.getModifications().size()); u2.getAdditionalName(); delta = u1.asPrismObject().diff(u2.asPrismObject()); assertNotNull(delta); assertEquals("Delta should be empty, nothing changed.", 0, delta.getModifications().size()); }
@Test public void testUserSimplePropertyDiff() throws SchemaException { System.out.println("\n\n===[ testUserSimplePropertyDiff ]===\n"); UserType userType1 = new UserType(); userType1.setName(PrismTestUtil.createPolyStringType("test name")); UserType userType2 = new UserType(); userType2.setName(PrismTestUtil.createPolyStringType("test name")); PrismTestUtil.getPrismContext().adopt(userType1); PrismTestUtil.getPrismContext().adopt(userType2); ObjectDelta delta = userType1.asPrismObject().diff(userType2.asPrismObject()); assertNotNull(delta); assertEquals(0, delta.getModifications().size()); userType2.setDescription(null); delta = userType1.asPrismObject().diff(userType2.asPrismObject()); assertNotNull(delta); assertEquals("Delta should be empty, nothing changed.", 0, delta.getModifications().size()); }
@Test public void test102ApplyPasswordValueDelta() throws Exception { System.out.println("===[ test102ApplyPasswordValueDelta ]==="); PrismObject<UserType> jack = getJack(); PrismObject<UserType> temp = jack.clone(); ObjectDelta<UserType> delta = getPrismContext().deltaFor(UserType.class) .item(UserType.F_CREDENTIALS, CredentialsType.F_PASSWORD, PasswordType.F_VALUE).replace("cleartext") .asObjectDeltaCast(temp.getOid()); measure("delta.applyTo(jack)", () -> { delta.applyTo(temp); return true; }); PrismObject<UserType> jack2 = jack.clone(); delta.applyTo(jack2); measure("jack2.diff(jack)", () -> jack2.diff(jack)); }
@Test public void test100ApplyNameDelta() throws Exception { System.out.println("===[ test100ApplyNameDelta ]==="); PrismObject<UserType> jack = getJack(); PrismObject<UserType> temp = jack.clone(); ObjectDelta<UserType> delta = getPrismContext().deltaFor(UserType.class) .item(UserType.F_NAME).replace("jjj") .asObjectDeltaCast(temp.getOid()); measure("delta.applyTo(jack)", () -> { delta.applyTo(temp); return true; }); PrismObject<UserType> jack2 = jack.clone(); delta.applyTo(jack2); measure("jack2.diff(jack)", () -> jack2.diff(jack)); }
@Test public void addUserWithAssignmentExtension() throws Exception { LOGGER.info("===[ addUserWithAssignmentExtension ]==="); File file = new File(FOLDER_BASIC, "user-assignment-extension.xml"); List<PrismObject<? extends Objectable>> elements = prismContext.parserFor(file).parseObjects(); OperationResult result = new OperationResult("ADD"); String oid = repositoryService.addObject((PrismObject) elements.get(0), null, result); PrismObject<UserType> fileUser = (PrismObject<UserType>) prismContext.parserFor(file).parseObjects().get(0); long id = 1; for (AssignmentType assignment : fileUser.asObjectable().getAssignment()) { assignment.setId(id); id++; } PrismObject<UserType> repoUser = repositoryService.getObject(UserType.class, oid, null, result); ObjectDelta<UserType> delta = fileUser.diff(repoUser); AssertJUnit.assertNotNull(delta); LOGGER.info("delta\n{}", delta.debugDump(3)); assertTrue(delta.isEmpty()); }
@Test public void test104ApplyExtensionItemDelta() throws Exception { System.out.println("===[ test104ApplyExtensionItemDelta ]==="); PrismObject<UserType> jack = getJack(); PrismObject<UserType> temp = jack.clone(); ObjectDelta<UserType> delta = getPrismContext().deltaFor(UserType.class) .item(ItemPath.create(UserType.F_EXTENSION, "bar23"), def("bar23")).replace("bbb") .asObjectDeltaCast(temp.getOid()); measure("delta.applyTo(jack)", () -> { delta.applyTo(temp); return true; }); PrismObject<UserType> jack2 = jack.clone(); delta.applyTo(jack2); measure("jack2.diff(jack)", () -> jack2.diff(jack)); }
@Test public void testUserSimplePropertyDiffReplace() throws Exception { System.out.println("\n\n===[ testUserSimplePropertyDiffReplace ]===\n"); // GIVEN PrismObjectDefinition<UserType> userDef = getUserTypeDefinition(); PrismObject<UserType> user1 = userDef.instantiate(); user1.setOid(USER_JACK_OID); user1.setPropertyRealValue(UserType.F_NAME, PrismTestUtil.createPolyString("test name")); PrismObject<UserType> user2 = userDef.instantiate(); user2.setOid(USER_JACK_OID); user2.setPropertyRealValue(UserType.F_NAME, PrismTestUtil.createPolyString("other name")); // WHEN ObjectDelta<UserType> delta = user1.diff(user2); // THEN assertNotNull(delta); System.out.println(delta.debugDump()); assertEquals("Unexpected number of midifications", 1, delta.getModifications().size()); PrismAsserts.assertPropertyReplace(delta, UserType.F_NAME, PrismTestUtil.createPolyString("other name")); assertEquals("Wrong OID", USER_JACK_OID, delta.getOid()); delta.checkConsistence(); }
@Test public void testUserSimplePropertyDiffNoChange() throws Exception { System.out.println("\n\n===[ testUserSimplePropertyDiffNoChange ]===\n"); // GIVEN PrismObjectDefinition<UserType> userDef = getUserTypeDefinition(); PrismObject<UserType> user1 = userDef.instantiate(); user1.setOid(USER_JACK_OID); user1.setPropertyRealValue(UserType.F_NAME, PrismTestUtil.createPolyString("test name")); PrismObject<UserType> user2 = userDef.instantiate(); user2.setOid(USER_JACK_OID); user2.setPropertyRealValue(UserType.F_NAME, PrismTestUtil.createPolyString("test name")); // WHEN ObjectDelta<UserType> delta = user1.diff(user2); // THEN assertNotNull(delta); assertEquals("Unexpected number of midifications", 0, delta.getModifications().size()); assertEquals("Wrong OID", USER_JACK_OID, delta.getOid()); delta.checkConsistence(); }
private MidPointPrincipal save(MidPointPrincipal person, OperationResult result) throws ObjectNotFoundException, SchemaException, ObjectAlreadyExistsException { UserType oldUserType = getUserByOid(person.getOid(), result); PrismObject<UserType> oldUser = oldUserType.asPrismObject(); PrismObject<UserType> newUser = person.getUser().asPrismObject(); ObjectDelta<UserType> delta = oldUser.diff(newUser); repositoryService.modifyObject(UserType.class, delta.getOid(), delta.getModifications(), new OperationResult(OPERATION_UPDATE_USER)); return person; }
@Test public void testDiffPatchRoundTrip() throws SchemaException, SAXException, IOException { final String TEST_NAME="testDiffPatchRoundTrip"; displayTestTitle(TEST_NAME); // GIVEN PrismContext prismContext = constructInitializedPrismContext(); PrismObject<UserType> jackOriginal = prismContext.parseObject(getFile(USER_JACK_FILE_BASENAME)); PrismObject<UserType> jackModified = prismContext.parseObject(getFile(USER_JACK_MODIFIED_FILE_BASENAME)); ObjectDelta<UserType> jackDelta = jackOriginal.diff(jackModified); // System.out.println("jackOriginal:\n" + prismContext.getXnodeProcessor().serializeObject(jackOriginal).debugDump(1)); // System.out.println("jackModified:\n" + prismContext.getXnodeProcessor().serializeObject(jackModified).debugDump(1)); // System.out.println("jackDelta:\n" + jackDelta.debugDump()); jackDelta.assertDefinitions(); jackDelta.checkConsistence(true, true, true); // WHEN jackDelta.applyTo(jackOriginal); // System.out.println("jackOriginal after applying delta:\n" + prismContext.getXnodeProcessor().serializeObject(jackOriginal).debugDump(1)); // THEN assertTrue("Roundtrip failed", jackOriginal.equivalent(jackModified)); }
@Test public void test110ApplyDeltaWith5Modifications() throws Exception { System.out.println("===[ test110ApplyDeltaWith5Modifications ]==="); PrismObject<UserType> jack = getJack(); PrismObject<UserType> temp = jack.clone(); ObjectDelta<UserType> delta = getPrismContext().deltaFor(UserType.class) .item(UserType.F_NAME).replace("jjj") .item(UserType.F_HONORIFIC_SUFFIX).replace("sss") .item(ItemPath.create(UserType.F_EXTENSION, "bar23"), def("bar23")).replace("bbb") .item(UserType.F_CREDENTIALS, CredentialsType.F_PASSWORD, PasswordType.F_VALUE).replace("cleartext") .item(UserType.F_ADMIN_GUI_CONFIGURATION).replace(new AdminGuiConfigurationType(getPrismContext())) .asObjectDeltaCast(temp.getOid()); measure("delta.applyTo(jack)", () -> { delta.applyTo(temp); return true; }); PrismObject<UserType> jack2 = jack.clone(); delta.applyTo(jack2); measure("jack2.diff(jack)", () -> jack2.diff(jack)); }