Search in sources :

Example 66 with ObjectNotFoundException

use of com.evolveum.midpoint.util.exception.ObjectNotFoundException in project midpoint by Evolveum.

the class SequenceHelper method returnUnusedValuesToSequenceAttempt.

public void returnUnusedValuesToSequenceAttempt(String oid, Collection<Long> unusedValues, OperationResult result) throws ObjectNotFoundException, SchemaException, SerializationRelatedException {
    LOGGER.debug("Returning unused values of {} to a sequence with oid '{}'.", unusedValues, oid);
    LOGGER_PERFORMANCE.debug("> return unused values, oid={}, values={}", oid, unusedValues);
    Session session = null;
    try {
        session = baseHelper.beginTransaction();
        PrismObject<SequenceType> prismObject = objectRetriever.getObjectInternal(session, SequenceType.class, oid, null, true, result);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("OBJECT before:\n{}", prismObject.debugDump());
        }
        SequenceType sequence = prismObject.asObjectable();
        int maxUnusedValues = sequence.getMaxUnusedValues() != null ? sequence.getMaxUnusedValues() : 0;
        Iterator<Long> valuesToReturnIterator = unusedValues.iterator();
        while (valuesToReturnIterator.hasNext() && sequence.getUnusedValues().size() < maxUnusedValues) {
            Long valueToReturn = valuesToReturnIterator.next();
            if (valueToReturn == null) {
                // sanity check
                continue;
            }
            if (!sequence.getUnusedValues().contains(valueToReturn)) {
                sequence.getUnusedValues().add(valueToReturn);
            } else {
                LOGGER.warn("UnusedValues in sequence {} already contains value of {} - ignoring the return request", oid, valueToReturn);
            }
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("OBJECT after:\n{}", prismObject.debugDump());
        }
        // merge and update object
        LOGGER.trace("Translating JAXB to data type.");
        RObject rObject = objectUpdater.createDataObjectFromJAXB(prismObject, PrismIdentifierGenerator.Operation.MODIFY);
        rObject.setVersion(rObject.getVersion() + 1);
        objectUpdater.updateFullObject(rObject, prismObject);
        session.merge(rObject);
        LOGGER.trace("Before commit...");
        session.getTransaction().commit();
        LOGGER.trace("Committed!");
    } catch (ObjectNotFoundException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (SchemaException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (DtoTranslationException | RuntimeException ex) {
        // should always throw an exception
        baseHelper.handleGeneralException(ex, session, result);
        // ...so this shouldn't occur at all
        throw new SystemException("Exception " + ex + " was not handled correctly", ex);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
        LOGGER.trace("Session cleaned up.");
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SequenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.SequenceType) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SystemException(com.evolveum.midpoint.util.exception.SystemException) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Session(org.hibernate.Session)

Example 67 with ObjectNotFoundException

use of com.evolveum.midpoint.util.exception.ObjectNotFoundException in project midpoint by Evolveum.

the class SequenceHelper method advanceSequenceAttempt.

public long advanceSequenceAttempt(String oid, OperationResult result) throws ObjectNotFoundException, SchemaException, SerializationRelatedException {
    long returnValue;
    LOGGER.debug("Advancing sequence with oid '{}'.", oid);
    LOGGER_PERFORMANCE.debug("> advance sequence, oid={}", oid);
    Session session = null;
    try {
        session = baseHelper.beginTransaction();
        PrismObject<SequenceType> prismObject = objectRetriever.getObjectInternal(session, SequenceType.class, oid, null, true, result);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("OBJECT before:\n{}", prismObject.debugDump());
        }
        SequenceType sequence = prismObject.asObjectable();
        if (!sequence.getUnusedValues().isEmpty()) {
            returnValue = sequence.getUnusedValues().remove(0);
        } else {
            long counter = sequence.getCounter() != null ? sequence.getCounter() : 0L;
            long maxCounter = sequence.getMaxCounter() != null ? sequence.getMaxCounter() : Long.MAX_VALUE;
            boolean allowRewind = Boolean.TRUE.equals(sequence.isAllowRewind());
            if (counter < maxCounter) {
                returnValue = counter;
                sequence.setCounter(counter + 1);
            } else if (counter == maxCounter) {
                returnValue = counter;
                if (allowRewind) {
                    sequence.setCounter(0L);
                } else {
                    // will produce exception during next run
                    sequence.setCounter(counter + 1);
                }
            } else {
                // i.e. counter > maxCounter
                if (allowRewind) {
                    // shouldn't occur but...
                    LOGGER.warn("Sequence {} overflown with allowRewind set to true. Rewinding.", oid);
                    returnValue = 0;
                    sequence.setCounter(1L);
                } else {
                    // TODO some better exception...
                    throw new SystemException("No (next) value available from sequence " + oid + ". Current counter = " + sequence.getCounter() + ", max value = " + sequence.getMaxCounter());
                }
            }
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Return value = {}, OBJECT after:\n{}", returnValue, prismObject.debugDump());
        }
        // merge and update object
        LOGGER.trace("Translating JAXB to data type.");
        RObject rObject = objectUpdater.createDataObjectFromJAXB(prismObject, PrismIdentifierGenerator.Operation.MODIFY);
        rObject.setVersion(rObject.getVersion() + 1);
        objectUpdater.updateFullObject(rObject, prismObject);
        session.merge(rObject);
        LOGGER.trace("Before commit...");
        session.getTransaction().commit();
        LOGGER.trace("Committed!");
        return returnValue;
    } catch (ObjectNotFoundException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (SchemaException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (DtoTranslationException | RuntimeException ex) {
        // should always throw an exception
        baseHelper.handleGeneralException(ex, session, result);
        // ...so this shouldn't occur at all
        throw new SystemException("Exception " + ex + " was not handled correctly", ex);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
        LOGGER.trace("Session cleaned up.");
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SystemException(com.evolveum.midpoint.util.exception.SystemException) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SequenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.SequenceType) Session(org.hibernate.Session)

Example 68 with ObjectNotFoundException

use of com.evolveum.midpoint.util.exception.ObjectNotFoundException in project midpoint by Evolveum.

the class TestNotifications method test119ModifyUserDeleteAccount.

@Test
public void test119ModifyUserDeleteAccount() throws Exception {
    final String TEST_NAME = "test119ModifyUserDeleteAccount";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(TestNotifications.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    preTestCleanup(AssignmentPolicyEnforcementType.POSITIVE);
    PrismObject<ShadowType> account = PrismTestUtil.parseObject(ACCOUNT_JACK_DUMMY_FILE);
    account.setOid(accountJackOid);
    ObjectDelta<UserType> userDelta = ObjectDelta.createEmptyModifyDelta(UserType.class, USER_JACK_OID, prismContext);
    ReferenceDelta accountDelta = ReferenceDelta.createModificationDelete(UserType.F_LINK_REF, getUserDefinition(), account);
    userDelta.addModification(accountDelta);
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(userDelta);
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    modelService.executeChanges(deltas, null, task, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    result.computeStatus();
    TestUtil.assertSuccess("executeChanges result", result, 2);
    assertShadowFetchOperationCountIncrement(0);
    // Check accountRef
    PrismObject<UserType> userJack = modelService.getObject(UserType.class, USER_JACK_OID, null, task, result);
    assertUserJack(userJack);
    UserType userJackType = userJack.asObjectable();
    assertEquals("Unexpected number of linkRefs", 0, userJackType.getLinkRef().size());
    // Check is shadow is gone
    try {
        PrismObject<ShadowType> accountShadow = repositoryService.getObject(ShadowType.class, accountJackOid, null, result);
        AssertJUnit.fail("Shadow " + accountJackOid + " still exists");
    } catch (ObjectNotFoundException e) {
    // This is OK
    }
    // Check if dummy resource account is gone
    assertNoDummyAccount("jack");
    assertDummyScriptsDelete();
    // Check notifications
    display("Notifications", dummyTransport);
    notificationManager.setDisabled(true);
    checkDummyTransportMessages("accountPasswordNotifier", 0);
    checkDummyTransportMessages("userPasswordNotifier", 0);
    checkDummyTransportMessages("simpleAccountNotifier-SUCCESS", 1);
    checkDummyTransportMessages("simpleAccountNotifier-FAILURE", 0);
    checkDummyTransportMessages("simpleAccountNotifier-ADD-SUCCESS", 0);
    checkDummyTransportMessages("simpleAccountNotifier-DELETE-SUCCESS", 1);
    checkDummyTransportMessages("simpleUserNotifier", 0);
    checkDummyTransportMessages("simpleUserNotifier-ADD", 0);
    String expected = "Notification about account-related operation\n" + "\n" + "Owner: Jack Sparrow (jack, oid c0c010c0-d34d-b33f-f00d-111111111111)\n" + "Resource: Dummy Resource (oid 10000000-0000-0000-0000-000000000004)\n" + "Account: jack\n" + "\n" + "The account has been successfully removed from the resource.\n" + "\n" + "Channel: ";
    assertEquals("Wrong message body", expected, dummyTransport.getMessages("dummy:simpleAccountNotifier-DELETE-SUCCESS").get(0).getBody());
    assertSteadyResources();
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ReferenceDelta(com.evolveum.midpoint.prism.delta.ReferenceDelta) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Test(org.testng.annotations.Test)

Example 69 with ObjectNotFoundException

use of com.evolveum.midpoint.util.exception.ObjectNotFoundException in project midpoint by Evolveum.

the class BadImportTest method test001BadImport.

@Test
public void test001BadImport() throws FileNotFoundException, SchemaException {
    TestUtil.displayTestTile(this, "test001BadImport");
    // GIVEN
    Task task = taskManager.createTaskInstance();
    OperationResult result = new OperationResult(ImportTest.class.getName() + "test001GoodImport");
    FileInputStream stream = new FileInputStream(BAD_IMPORT_FILE_NAME);
    // WHEN
    modelService.importObjectsFromStream(stream, getDefaultImportOptions(), task, result);
    // THEN
    result.computeStatus("Failed import.");
    display("Result after bad import", result);
    // Jack is OK in the import file, he should be imported
    try {
        UserType jack = repositoryService.getObject(UserType.class, USER_JACK_OID, null, result).asObjectable();
        AssertJUnit.assertNotNull("Jack is null", jack);
    } catch (ObjectNotFoundException e) {
        AssertJUnit.fail("Jack was not imported");
    }
    List<PrismObject<UserType>> users = repositoryService.searchObjects(UserType.class, null, null, result);
    AssertJUnit.assertNotNull(users);
    AssertJUnit.assertEquals("Search retuned unexpected results: " + users, 3, users.size());
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test) AbstractConfiguredModelIntegrationTest(com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest)

Example 70 with ObjectNotFoundException

use of com.evolveum.midpoint.util.exception.ObjectNotFoundException in project midpoint by Evolveum.

the class TestBrokenResources method test350AddResourceWrongConnectorOid.

@Test
public void test350AddResourceWrongConnectorOid() throws Exception {
    final String TEST_NAME = "test350AddResourceWrongConnectorOid";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(TestBrokenResources.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    PrismObject<ResourceType> resource = PrismTestUtil.parseObject(RESOURCE_DUMMY_WRONG_CONNECTOR_OID_FILE);
    ObjectDelta<ResourceType> delta = ObjectDelta.createAddDelta(resource);
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(delta);
    try {
        // WHEN
        modelService.executeChanges(deltas, null, task, result);
        AssertJUnit.fail("Unexpected success");
    } catch (ObjectNotFoundException e) {
    // This is expected
    }
    // THEN
    result.computeStatus();
    display(result);
    TestUtil.assertFailure(result);
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) Task(com.evolveum.midpoint.task.api.Task) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Test(org.testng.annotations.Test) AbstractConfiguredModelIntegrationTest(com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest)

Aggregations

ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)291 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)214 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)200 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)100 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)93 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)90 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)88 Task (com.evolveum.midpoint.task.api.Task)75 SystemException (com.evolveum.midpoint.util.exception.SystemException)71 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)64 PrismObject (com.evolveum.midpoint.prism.PrismObject)52 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)42 Test (org.testng.annotations.Test)40 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)38 ArrayList (java.util.ArrayList)35 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)33 PolicyViolationException (com.evolveum.midpoint.util.exception.PolicyViolationException)32 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)30 QName (javax.xml.namespace.QName)29 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)28