Search in sources :

Example 6 with ImportOptionsType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType in project midpoint by Evolveum.

the class ImportTest method test006ImportUsersWithOverwriteKeepOid.

// Import the same thing again, with overwrite and also while keeping OIDs
@Test
public void test006ImportUsersWithOverwriteKeepOid() throws Exception {
    TestUtil.displayTestTile(this, "test006ImportUsersWithOverwriteKeepOid");
    // GIVEN
    Task task = taskManager.createTaskInstance();
    OperationResult result = new OperationResult(ImportTest.class.getName() + "test005ImportUsersWithOverwrite");
    FileInputStream stream = new FileInputStream(IMPORT_USERS_OVERWRITE_FILE);
    ImportOptionsType options = getDefaultImportOptions();
    options.setOverwrite(true);
    options.setKeepOid(true);
    dummyAuditService.clear();
    // WHEN
    modelService.importObjectsFromStream(stream, options, task, result);
    // THEN
    result.computeStatus();
    display("Result after import with overwrite", result);
    TestUtil.assertSuccess("Import failed (result)", result, 1);
    // list all users
    List<PrismObject<UserType>> users = modelService.searchObjects(UserType.class, new ObjectQuery(), null, task, result);
    // Three old users, one new
    assertEquals(5, users.size());
    for (PrismObject<UserType> user : users) {
        UserType userType = user.asObjectable();
        if (userType.getName().equals("jack")) {
            // OID and all the attributes should be the same
            assertEquals(USER_JACK_OID, userType.getOid());
            PrismAsserts.assertEqualsPolyString("wrong givenName", "Jack", userType.getGivenName());
            PrismAsserts.assertEqualsPolyString("wrong familyName", "Sparrow", userType.getFamilyName());
            PrismAsserts.assertEqualsPolyString("wrong fullName", "Cpt. Jack Sparrow", userType.getFullName());
        }
        if (userType.getName().equals("will")) {
            // OID should be the same, and there should be an employee type
            assertEquals(USER_WILL_OID, userType.getOid());
            assertTrue("Wrong Will's employee type", userType.getEmployeeType().contains("legendary"));
        }
        if (userType.getName().equals("guybrush")) {
            // OID should be the same, there should be a locality attribute
            assertEquals("Guybrush's OID went leeway", guybrushOid, userType.getOid());
            PrismAsserts.assertEqualsPolyString("Guybrush is not in the Caribbean", "Deep in the Caribbean", userType.getLocality());
        }
        if (userType.getName().equals("ht")) {
            // Herman should still be here
            assertEquals("Herman's OID went leeway", hermanOid, userType.getOid());
            PrismAsserts.assertEqualsPolyString("Herman is confused", "Herman Toothrot", userType.getFullName());
            PrismAsserts.assertEqualsPolyString("Herman is confused", "Herman", userType.getGivenName());
            PrismAsserts.assertEqualsPolyString("Herman is confused", "Toothrot", userType.getFamilyName());
        }
    }
    assertUsers(5);
    // Check audit
    display("Audit", dummyAuditService);
    dummyAuditService.assertRecords(6);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ImportOptionsType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test) AbstractConfiguredModelIntegrationTest(com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest)

Example 7 with ImportOptionsType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType in project midpoint by Evolveum.

the class ObjectImporter method importObjectToRepository.

private <T extends ObjectType> void importObjectToRepository(PrismObject<T> object, ImportOptionsType options, boolean raw, Task task, OperationResult objectResult) throws ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, PolicyViolationException, SecurityViolationException, SchemaException, ObjectAlreadyExistsException {
    OperationResult result = objectResult.createSubresult(ObjectImporter.class.getName() + ".importObjectToRepository");
    if (options == null) {
        options = new ImportOptionsType();
    }
    if (BooleanUtils.isTrue(options.isKeepOid()) && object.getOid() == null) {
        // Try to check if there is existing object with the same type and name
        ObjectQuery query = ObjectQueryUtil.createNameQuery(object);
        List<PrismObject<T>> foundObjects = repository.searchObjects(object.getCompileTimeClass(), query, null, result);
        if (foundObjects.size() == 1) {
            String oid = foundObjects.iterator().next().getOid();
            object.setOid(oid);
        }
    }
    try {
        String oid = addObject(object, BooleanUtils.isTrue(options.isOverwrite()), BooleanUtils.isFalse(options.isEncryptProtectedValues()), raw, task, result);
        if (object.canRepresent(TaskType.class)) {
            taskManager.onTaskCreate(oid, result);
        }
        result.recordSuccess();
    } catch (ObjectAlreadyExistsException e) {
        if (BooleanUtils.isTrue(options.isOverwrite() && BooleanUtils.isNotTrue(options.isKeepOid()) && object.getOid() == null)) {
            // This is overwrite, without keep oid, therefore we do not have conflict on OID
            // this has to be conflict on name. So try to delete the conflicting object and create new one (with a new OID).
            result.muteLastSubresultError();
            ObjectQuery query = ObjectQueryUtil.createNameQuery(object);
            List<PrismObject<T>> foundObjects = repository.searchObjects(object.getCompileTimeClass(), query, null, result);
            if (foundObjects.size() == 1) {
                PrismObject<T> foundObject = foundObjects.iterator().next();
                String deletedOid = deleteObject(foundObject, repository, result);
                if (deletedOid != null) {
                    if (object.canRepresent(TaskType.class)) {
                        taskManager.onTaskDelete(deletedOid, result);
                    }
                    if (BooleanUtils.isTrue(options.isKeepOid())) {
                        object.setOid(deletedOid);
                    }
                    addObject(object, false, BooleanUtils.isFalse(options.isEncryptProtectedValues()), raw, task, result);
                    if (object.canRepresent(TaskType.class)) {
                        taskManager.onTaskCreate(object.getOid(), result);
                    }
                    result.recordSuccess();
                } else {
                    // cannot delete, throw original exception
                    result.recordFatalError("Object already exists, cannot overwrite", e);
                    throw e;
                }
            } else {
                // Cannot locate conflicting object
                String message = "Conflicting object already exists but it was not possible to precisely locate it, " + foundObjects.size() + " objects with same name exist";
                result.recordFatalError(message, e);
                throw new ObjectAlreadyExistsException(message, e);
            }
        } else {
            result.recordFatalError(e);
            throw e;
        }
    } catch (ObjectNotFoundException | ExpressionEvaluationException | CommunicationException | ConfigurationException | PolicyViolationException | SecurityViolationException | SchemaException e) {
        result.recordFatalError("Cannot import " + object + ": " + e.getMessage(), e);
        throw e;
    } catch (RuntimeException ex) {
        result.recordFatalError("Couldn't import object: " + object + ". Reason: " + ex.getMessage(), ex);
        throw ex;
    }
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) ImportOptionsType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType) List(java.util.List)

Aggregations

ImportOptionsType (com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType)7 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 Task (com.evolveum.midpoint.task.api.Task)5 Test (org.testng.annotations.Test)5 AbstractConfiguredModelIntegrationTest (com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest)4 FileInputStream (java.io.FileInputStream)4 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 LookupTableType (com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType)1 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)1 List (java.util.List)1