Search in sources :

Example 51 with SelectorOptions

use of com.evolveum.midpoint.schema.SelectorOptions in project midpoint by Evolveum.

the class AbstractLdapTest method doSearch.

protected SearchResultList<PrismObject<ShadowType>> doSearch(final String TEST_NAME, ObjectQuery query, GetOperationOptions rootOptions, int expectedSize, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    final List<PrismObject<ShadowType>> foundObjects = new ArrayList<PrismObject<ShadowType>>(expectedSize);
    ResultHandler<ShadowType> handler = new ResultHandler<ShadowType>() {

        @Override
        public boolean handle(PrismObject<ShadowType> object, OperationResult parentResult) {
            //				LOGGER.trace("Found {}", object);
            String name = object.asObjectable().getName().getOrig();
            for (PrismObject<ShadowType> foundShadow : foundObjects) {
                if (!allowDuplicateSearchResults() && foundShadow.asObjectable().getName().getOrig().equals(name)) {
                    AssertJUnit.fail("Duplicate name " + name);
                }
            }
            foundObjects.add(object);
            return true;
        }
    };
    Collection<SelectorOptions<GetOperationOptions>> options = null;
    if (rootOptions != null) {
        options = SelectorOptions.createCollection(rootOptions);
    }
    rememberConnectorOperationCount();
    rememberConnectorSimulatedPagingSearchCount();
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    display("Searching shadows, options=" + options + ", query", query);
    SearchResultMetadata searchResultMetadata = modelService.searchObjectsIterative(ShadowType.class, query, handler, options, task, result);
    // THEN
    result.computeStatus();
    TestUtil.assertSuccess(result);
    if (expectedSize != foundObjects.size()) {
        if (foundObjects.size() < 10) {
            display("Found objects", foundObjects);
            AssertJUnit.fail("Unexpected number of accounts. Expected " + expectedSize + ", found " + foundObjects.size() + ": " + foundObjects);
        } else {
            AssertJUnit.fail("Unexpected number of accounts. Expected " + expectedSize + ", found " + foundObjects.size() + " (too many to display)");
        }
    }
    SearchResultList<PrismObject<ShadowType>> resultList = new SearchResultList<>(foundObjects, searchResultMetadata);
    return resultList;
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ArrayList(java.util.ArrayList) SearchResultMetadata(com.evolveum.midpoint.schema.SearchResultMetadata) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismObject(com.evolveum.midpoint.prism.PrismObject) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions)

Example 52 with SelectorOptions

use of com.evolveum.midpoint.schema.SelectorOptions in project midpoint by Evolveum.

the class ObjectNotFoundHandler method handleError.

@Override
public <T extends ShadowType> T handleError(T shadow, FailedOperation op, Exception ex, boolean doDiscovery, boolean compensate, Task task, OperationResult parentResult) throws SchemaException, GenericFrameworkException, CommunicationException, ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    if (!doDiscovery) {
        parentResult.recordFatalError(ex);
        if (ex instanceof ObjectNotFoundException) {
            throw (ObjectNotFoundException) ex;
        } else {
            throw new ObjectNotFoundException(ex.getMessage(), ex);
        }
    }
    OperationResult result = parentResult.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ObjectNotFoundHandler.handleError." + op.name());
    result.addParam("shadow", shadow);
    result.addParam("currentOperation", op);
    if (ex.getMessage() != null) {
        result.addParam("exception", ex.getMessage());
    }
    LOGGER.trace("Start compensating object not found situation while execution operation: {}", op.name(), ex);
    //		Task task = taskManager.createTaskInstance();
    ObjectDelta delta = null;
    switch(op) {
        case DELETE:
            LOGGER.debug("DISCOVERY: cannot find object {}. The operation in progress is DELETE, therefore just deleting the shadow", shadow);
            LOGGER.trace("Deleting shadow from the repository.");
            for (OperationResult subResult : parentResult.getSubresults()) {
                subResult.muteError();
            }
            try {
                cacheRepositoryService.deleteObject(ShadowType.class, shadow.getOid(), result);
            } catch (ObjectNotFoundException e) {
                LOGGER.debug("Cannot delete {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
            }
            String message = "Object was not found on the " + ObjectTypeUtil.toShortString(shadow.getResource()) + ". Shadow deleted from the repository to equalize the state on the resource and in the repository.";
            parentResult.recordHandledError(message);
            LOGGER.trace("Shadow deleted from the repository. Inconsistencies are now removed.");
            result.computeStatus();
            // parentResult status can be recomputed by the caller method
            result.setStatus(OperationResultStatus.HANDLED_ERROR);
            delta = ObjectDelta.createDeleteDelta(shadow.getClass(), shadow.getOid(), prismContext);
            ResourceOperationDescription operationDescritpion = createOperationDescription(shadow, ex, shadow.getResource(), delta, task, result);
            changeNotificationDispatcher.notifySuccess(operationDescritpion, task, result);
            LOGGER.debug("DISCOVERY: cannot find object {}: DELETE operation handler done", shadow);
            return shadow;
        case MODIFY:
            LOGGER.debug("DISCOVERY: cannot find object {}. The operation in progress is MODIFY, therefore initiating synchronization", shadow);
            LOGGER.trace("Starting discovery to find out if the object should exist or not.");
            OperationResult handleErrorResult = result.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ObjectNotFoundHandler.handleError.discovery");
            handleErrorResult.addContext(OperationResult.CONTEXT_RESOURCE, shadow.getResource());
            ObjectDeltaType shadowModifications = shadow.getObjectChange();
            Collection<? extends ItemDelta> modifications = DeltaConvertor.toModifications(shadowModifications.getItemDelta(), shadow.asPrismObject().getDefinition());
            shadow.setDead(true);
            Collection<? extends ItemDelta> deadDeltas = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, shadow.asPrismObject().getDefinition(), true);
            ConstraintsChecker.onShadowModifyOperation(deadDeltas);
            cacheRepositoryService.modifyObject(ShadowType.class, shadow.getOid(), deadDeltas, result);
            ResourceObjectShadowChangeDescription change = createResourceObjectShadowChangeDescription(shadow, result);
            try {
                changeNotificationDispatcher.notifyChange(change, task, handleErrorResult);
            } catch (RuntimeException e) {
                handleErrorResult.recordFatalError(e);
                result.computeStatus();
                throw e;
            }
            handleErrorResult.computeStatus();
            String oidVal = null;
            String oid = findCreatedAccountOid(handleErrorResult, oidVal);
            if (oid != null) {
                LOGGER.trace("Found new oid {} as a return param from model. Probably the new shadow was created.", oid);
                LOGGER.debug("DISCOVERY: object {} re-created, applying pending changes", shadow);
                LOGGER.trace("Modifying re-created object by applying pending changes:\n{}", DebugUtil.debugDump(modifications));
                try {
                    ProvisioningOperationOptions options = new ProvisioningOperationOptions();
                    options.setCompletePostponed(false);
                    options.setDoNotDiscovery(true);
                    provisioningService.modifyObject(ShadowType.class, oid, modifications, null, options, task, result);
                    parentResult.recordHandledError("Object was recreated and modifications were applied to newly created object.");
                } catch (ObjectNotFoundException e) {
                    parentResult.recordHandledError("Modifications were not applied, because shadow was deleted by discovery. Repository state were refreshed and unused shadow was deleted.");
                }
            } else {
                LOGGER.debug("DISCOVERY: object {} deleted, application of pending changes skipped", shadow);
                parentResult.recordHandledError("Object was deleted by discovery. Modification were not applied.");
            }
            // We do not need the old shadow any more. Even if the object was re-created it has a new shadow now.
            try {
                cacheRepositoryService.deleteObject(ShadowType.class, shadow.getOid(), parentResult);
            } catch (ObjectNotFoundException e) {
                // delete the old shadow that was probably deleted from the
                // user, or the new one was assigned
                LOGGER.debug("Cannot delete {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
            }
            result.computeStatus();
            if (parentResult.isHandledError()) {
                // Ugly hack. We shouldn't set parentResult status in the first place, as it can be overriden by computeStatus/recomputeStatus called in the parent.
                result.setStatus(OperationResultStatus.HANDLED_ERROR);
            }
            if (oid != null) {
                shadowModifications.setOid(oid);
                shadow.setOid(oid);
            }
            LOGGER.debug("DISCOVERY: cannot find object {}: MODIFY operation handler done", shadow);
            return shadow;
        case GET:
            if (!compensate) {
                LOGGER.trace("DISCOVERY: cannot find object {}, GET operation: handling skipped because discovery is disabled", shadow);
                result.recordFatalError(ex.getMessage(), ex);
                throw new ObjectNotFoundException(ex.getMessage(), ex);
            }
            LOGGER.debug("DISCOVERY: cannot find object {}. The operation in progress is GET, therefore initiating synchronization", shadow);
            OperationResult handleGetErrorResult = result.createSubresult("com.evolveum.midpoint.provisioning.consistency.impl.ObjectNotFoundHandler.handleError.discovery");
            handleGetErrorResult.addContext(OperationResult.CONTEXT_RESOURCE, shadow.getResource());
            Collection<? extends ItemDelta> deadModification = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, shadow.asPrismObject().getDefinition(), true);
            ConstraintsChecker.onShadowModifyOperation(deadModification);
            try {
                cacheRepositoryService.modifyObject(ShadowType.class, shadow.getOid(), deadModification, result);
            } catch (ObjectNotFoundException e) {
                // The shadow is not there. So we cannot mark it as dead.
                LOGGER.debug("Cannot modify shadow {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
            }
            shadow.setDead(true);
            ResourceObjectShadowChangeDescription getChange = createResourceObjectShadowChangeDescription(shadow, result);
            if (task == null) {
                task = taskManager.createTaskInstance();
            }
            try {
                changeNotificationDispatcher.notifyChange(getChange, task, handleGetErrorResult);
            } catch (RuntimeException e) {
                LOGGER.trace("DISCOVERY: synchronization invoked for  {} ended with an error {}", shadow, e);
                handleGetErrorResult.recordFatalError(e);
                result.computeStatus();
                throw e;
            }
            // String oidVal = null;
            handleGetErrorResult.computeStatus();
            LOGGER.trace("DISCOVERY: synchronization invoked for  {} finished with status {}", shadow, handleGetErrorResult.getStatus());
            oid = findCreatedAccountOid(handleGetErrorResult, null);
            try {
                LOGGER.trace("DISCOVERY: deleting  {}", shadow);
                cacheRepositoryService.deleteObject(ShadowType.class, shadow.getOid(), result);
            } catch (ObjectNotFoundException e) {
                // delete the old shadow that was probably deleted from the
                // user, or the new one was assigned
                LOGGER.debug("Cannot delete {} in consistency compensation (discovery): {} - this is probably harmless", shadow, e.getMessage());
            }
            for (OperationResult subResult : parentResult.getSubresults()) {
                subResult.muteError();
            }
            if (oid != null) {
                PrismObject newShadow;
                try {
                    LOGGER.trace("DISCOVERY: retrieving shadow  {}", oid);
                    Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createDoNotDiscovery());
                    newShadow = provisioningService.getObject(shadow.getClass(), oid, options, task, result);
                    LOGGER.trace("DISCOVERY: retrieved {}", newShadow);
                } catch (ObjectNotFoundException e) {
                    String msg = "Strange thing did happen: new shadow (" + oid + ") was supposedly created for old shadow " + shadow + ", however the new shadow was not found: " + e.getMessage();
                    LOGGER.error(msg);
                    result.recordFatalError(msg, e);
                    parentResult.recordFatalError(msg);
                    throw new ObjectNotFoundException(msg, ex);
                } finally {
                    result.computeStatus();
                }
                LOGGER.debug("DISCOVERY: object {} re-created as {}. GET operation handler done: returning new shadow", shadow, newShadow);
                shadow = (T) newShadow.asObjectable();
                parentResult.recordHandledError("Object was re-created by the discovery.");
                // parentResult status can be recomputed by the caller method
                result.setStatus(OperationResultStatus.HANDLED_ERROR);
                return shadow;
            } else {
                parentResult.recordHandledError("Object was deleted by the discovery and the invalid link was removed from the user.");
                result.computeStatus();
                // parentResult status can be recomputed by the caller method
                result.setStatus(OperationResultStatus.HANDLED_ERROR);
                LOGGER.debug("DISCOVERY: object {} was deleted. GET operation handler done: throwing ObjectNotFoundException", shadow);
                throw new ObjectNotFoundException(ex.getMessage(), ex);
            }
        default:
            throw new ObjectNotFoundException(ex.getMessage(), ex);
    }
}
Also used : ProvisioningOperationOptions(com.evolveum.midpoint.provisioning.api.ProvisioningOperationOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) PrismObject(com.evolveum.midpoint.prism.PrismObject) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ResourceObjectShadowChangeDescription(com.evolveum.midpoint.provisioning.api.ResourceObjectShadowChangeDescription) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ResourceOperationDescription(com.evolveum.midpoint.provisioning.api.ResourceOperationDescription) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta)

Example 53 with SelectorOptions

use of com.evolveum.midpoint.schema.SelectorOptions in project midpoint by Evolveum.

the class TestLdap method assertOpenDjAccountShadows.

private void assertOpenDjAccountShadows(int expected, boolean raw, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    ObjectQuery query = ObjectQueryUtil.createResourceAndObjectClassQuery(RESOURCE_OPENDJ_OID, new QName(RESOURCE_OPENDJ_NAMESPACE, "inetOrgPerson"), prismContext);
    final MutableInt count = new MutableInt(0);
    ResultHandler<ShadowType> handler = new ResultHandler<ShadowType>() {

        @Override
        public boolean handle(PrismObject<ShadowType> shadow, OperationResult parentResult) {
            count.increment();
            display("Found", shadow);
            return true;
        }
    };
    Collection<SelectorOptions<GetOperationOptions>> options = null;
    if (raw) {
        options = SelectorOptions.createCollection(GetOperationOptions.createRaw());
    }
    modelService.searchObjectsIterative(ShadowType.class, query, handler, options, task, result);
    assertEquals("Unexpected number of search results (raw=" + raw + ")", expected, count.getValue());
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) QName(javax.xml.namespace.QName) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) MutableInt(org.apache.commons.lang.mutable.MutableInt) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery)

Example 54 with SelectorOptions

use of com.evolveum.midpoint.schema.SelectorOptions in project midpoint by Evolveum.

the class TestResources method test100SearchResourcesNoFetch.

/**
	 * MID-3424
	 */
@Test
public void test100SearchResourcesNoFetch() throws Exception {
    final String TEST_NAME = "test100SearchResourcesNoFetch";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = createTask(TEST_NAME);
    OperationResult result = task.getResult();
    preTestCleanup(AssignmentPolicyEnforcementType.POSITIVE);
    // precondition
    assertSteadyResources();
    rememberPrismObjectCloneCount();
    Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createNoFetch());
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    List<PrismObject<ResourceType>> resources = modelService.searchObjects(ResourceType.class, null, options, task, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    assertNotNull("null search return", resources);
    assertFalse("Empty search return", resources.isEmpty());
    assertEquals("Unexpected number of resources found", 2, resources.size());
    result.computeStatus();
    TestUtil.assertSuccess("searchObjects result", result);
    assertPrismObjectCloneIncrement(2);
    for (PrismObject<ResourceType> resource : resources) {
        assertResource(resource, false);
    }
    assertResourceSchemaFetchIncrement(0);
    assertResourceSchemaParseCountIncrement(0);
    assertConnectorCapabilitiesFetchIncrement(0);
    assertConnectorInitializationCountIncrement(0);
    assertConnectorSchemaParseIncrement(0);
    assertSteadyResources();
}
Also used : Task(com.evolveum.midpoint.task.api.Task) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) Test(org.testng.annotations.Test)

Example 55 with SelectorOptions

use of com.evolveum.midpoint.schema.SelectorOptions in project midpoint by Evolveum.

the class TestResources method test112GetResourceDummyReadOnly.

@Test
public void test112GetResourceDummyReadOnly() throws Exception {
    final String TEST_NAME = "test112GetResourceDummyReadOnly";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = createTask(TEST_NAME);
    OperationResult result = task.getResult();
    preTestCleanup(AssignmentPolicyEnforcementType.POSITIVE);
    rememberPrismObjectCloneCount();
    Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createReadOnly());
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    PrismObject<ResourceType> resource = modelService.getObject(ResourceType.class, RESOURCE_DUMMY_OID, options, task, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    result.computeStatus();
    TestUtil.assertSuccess("getObject result", result);
    assertPrismObjectCloneIncrement(1);
    assertResourceDummy(resource, true);
    assertResourceSchemaFetchIncrement(0);
    assertResourceSchemaParseCountIncrement(0);
    assertConnectorCapabilitiesFetchIncrement(0);
    assertConnectorInitializationCountIncrement(0);
    assertConnectorSchemaParseIncrement(0);
    IntegrationTestTools.displayXml("Initialized dummy resource", resource);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) Test(org.testng.annotations.Test)

Aggregations

SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)127 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)106 Task (com.evolveum.midpoint.task.api.Task)82 Test (org.testng.annotations.Test)47 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)34 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)31 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)31 PrismObject (com.evolveum.midpoint.prism.PrismObject)28 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)28 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)28 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)23 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)21 ArrayList (java.util.ArrayList)19 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)17 QName (javax.xml.namespace.QName)17 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)16 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)16 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)14 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)14 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)12