All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.identityconnectors.contract.test.CreateApiOpTests Maven / Gradle / Ivy

The newest version!
/*
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 * Portions Copyrighted 2010-2013 ForgeRock AS.
 * Portions Copyrighted 2018 ConnId
 */
package org.identityconnectors.contract.test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.HashSet;
import java.util.Set;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.contract.exceptions.ObjectNotFoundException;
import org.identityconnectors.framework.api.operations.APIOperation;
import org.identityconnectors.framework.api.operations.CreateApiOp;
import org.identityconnectors.framework.api.operations.DeleteApiOp;
import org.identityconnectors.framework.api.operations.GetApiOp;
import org.identityconnectors.framework.common.objects.Attribute;
import org.identityconnectors.framework.common.objects.AttributeBuilder;
import org.identityconnectors.framework.common.objects.ConnectorObject;
import org.identityconnectors.framework.common.objects.ObjectClass;
import org.identityconnectors.framework.common.objects.Uid;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
 * Contract test of {@link CreateApiOp} operation.
 */
public class CreateApiOpTests extends ObjectClassRunner {

    private static final Log LOG = Log.getLog(CreateApiOpTests.class);

    public static final String TEST_NAME = "Create";

    private static final String NON_EXISTING_PROP_NAME = "unsupportedAttributeName";

    /**
     * {@inheritDoc}
     */
    @Override
    public Set> getAPIOperations() {
        Set> requiredOps = new HashSet<>();
        // list of required operations by this test:
        requiredOps.add(CreateApiOp.class);
        requiredOps.add(GetApiOp.class);
        return requiredOps;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void testRun(ObjectClass objectClass) {
        Uid uid = null;

        try {
            Set attrs = ConnectorHelper.getCreateableAttributes(getDataProvider(),
                    getObjectClassInfo(objectClass), getTestName(), 0, true, false);

            // should throw UnsupportedObjectClass if not supported
            uid = getConnectorFacade().create(objectClass, attrs,
                    getOperationOptionsByOp(objectClass, CreateApiOp.class));

            // get the user to make sure it exists now
            ConnectorObject obj = getConnectorFacade().getObject(objectClass, uid,
                    getOperationOptionsByOp(objectClass, GetApiOp.class));

            assertNotNull(obj, "Unable to retrieve newly created object");

            // compare requested attributes to retrieved attributes
            ConnectorHelper.checkObject(getObjectClassInfo(objectClass), obj, attrs);
        } finally {
            if (uid != null) {
                // delete the object
                getConnectorFacade().delete(objectClass, uid,
                        getOperationOptionsByOp(objectClass, DeleteApiOp.class));
            }
        }
    }

    /**
     * Tests create method with invalid Attribute, RuntimeException is expected
     *
     * connector developers can set the value of unsupported attribute
     * using test property: testsuite.Create.unsupportedAttributeName
     */
    @ParameterizedTest
    @MethodSource("objectClasses")
    public void testCreateFailUnsupportedAttribute(ObjectClass objectClass) {
        // run the contract test only if create is supported by tested object class
        if (ConnectorHelper.operationsSupported(getConnectorFacade(), objectClass,
                getAPIOperations())) {
            // create not supported Attribute Set
            Set attrs = ConnectorHelper.getCreateableAttributes(getDataProvider(),
                    getObjectClassInfo(objectClass), getTestName(), 0, true, false);

            String unsupportedAttribute = null;
            try {
                unsupportedAttribute = (String) getDataProvider().getTestSuiteAttribute(NON_EXISTING_PROP_NAME,
                        TEST_NAME);
            } catch (ObjectNotFoundException ex) {
                unsupportedAttribute = "nonExistingAndUnlikelyAttrName";
            }

            attrs.add(AttributeBuilder.build(unsupportedAttribute));

            Uid uid = null;
            try {
                // do the create call
                // note - the ObjectClassInfo is always supported
                uid = getConnectorFacade().create(objectClass, attrs, null);
                fail("'testCreateFailUnsupportedAttribute': NONEXISTING attribute accepted without throwing a "
                        + "RuntimeException.");
            } catch (RuntimeException ex) {
                // ok
            } finally {
                if (uid != null) {
                    // delete the created the object
                    ConnectorHelper.deleteObject(getConnectorFacade(), objectClass, uid,
                            false, getOperationOptionsByOp(objectClass, DeleteApiOp.class));
                }
            }
        } else {
            LOG.info("----------------------------------------------------------------------------------------");
            LOG.
                    info("Skipping test ''testCreateFailUnsupportedAttribute'' for object class ''" + objectClass
                            + "''.");
            LOG.info("----------------------------------------------------------------------------------------");
        }
    }

    /**
     * Tests create twice with the same attributes. It should return different Uids.
     */
    @ParameterizedTest
    @MethodSource("objectClasses")
    public void testCreateWithSameAttributes(ObjectClass objectClass) {
        // run the contract test only if create is supported by tested object class
        if (ConnectorHelper.operationsSupported(getConnectorFacade(), objectClass, getAPIOperations())) {
            Uid uid1 = null;
            Uid uid2 = null;

            try {
                Set attrs = ConnectorHelper.getCreateableAttributes(getDataProvider(),
                        getObjectClassInfo(objectClass), getTestName(), 1, true, false);

                // ObjectClassInfo is always supported
                uid1 = getConnectorFacade().create(objectClass, attrs,
                        getOperationOptionsByOp(objectClass, CreateApiOp.class));

                // get the object to make sure it exist now
                ConnectorObject obj1 = getConnectorFacade().getObject(objectClass,
                        uid1, getOperationOptionsByOp(objectClass, GetApiOp.class));
                assertNotNull(obj1, "Unable to retrieve newly created object");

                // compare requested attributes to retrieved attributes
                ConnectorHelper.checkObject(getObjectClassInfo(objectClass), obj1, attrs);

                /* SECOND CREATE: */
                // should return different uid or throw
                uid2 = getConnectorFacade().create(objectClass, attrs, getOperationOptionsByOp(objectClass,
                        CreateApiOp.class));
                assertFalse(uid1
                        .equals(uid2), "Create returned the same Uid as by previous create.");

                // get the object to make sure it exists now
                ConnectorObject obj2 = getConnectorFacade().getObject(objectClass,
                        uid2, getOperationOptionsByOp(objectClass, GetApiOp.class));
                assertNotNull(obj2, "Unable to retrieve newly created object");

                // compare requested attributes to retrieved attributes
                ConnectorHelper.checkObject(getObjectClassInfo(objectClass), obj2, attrs);
            } catch (RuntimeException ex) {
                // ok - second create could throw this exception
            } finally {
                if (uid1 != null) {
                    // delete the object
                    ConnectorHelper.deleteObject(getConnectorFacade(), objectClass, uid1,
                            false, getOperationOptionsByOp(objectClass, DeleteApiOp.class));
                }
                if (uid2 != null) {
                    // delete the object
                    ConnectorHelper.deleteObject(getConnectorFacade(), objectClass, uid2,
                            false, getOperationOptionsByOp(objectClass, DeleteApiOp.class));
                }
            }
        } else {
            LOG.info("----------------------------------------------------------------------------------------");
            LOG.info("Skipping test ''testCreateWithSameAttributes'' for object class ''" + objectClass + "''.");
            LOG.info("----------------------------------------------------------------------------------------");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getTestName() {
        return TEST_NAME;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy