/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
package com.evolveum.midpoint.provisioning.impl.dummy;
import static com.evolveum.midpoint.test.IntegrationTestTools.display;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.schema.processor.ResourceSchemaImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import org.w3c.dom.Element;
import com.evolveum.icf.dummy.resource.DummyObjectClass;
import com.evolveum.icf.dummy.resource.DummyResource;
import com.evolveum.midpoint.prism.ComplexTypeDefinition;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.provisioning.impl.ConnectorManager;
import com.evolveum.midpoint.provisioning.impl.ProvisioningTestUtil;
import com.evolveum.midpoint.provisioning.impl.mock.SynchornizationServiceMock;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.processor.ResourceSchema;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.AbstractIntegrationTest;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CachingMetadataType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType;
/**
* The test of Provisioning service on the API level. The test is using dummy resource for speed and flexibility.
*
* @author Radovan Semancik
*
*/
@ContextConfiguration(locations = "classpath:ctx-provisioning-test-main.xml")
@DirtiesContext
public class TestDummyHacks extends AbstractIntegrationTest {
private static final String TEST_DIR = "src/test/resources/impl/dummy-hacks/";
private static final String CONNECTOR_DUMMY_FILENAME = TEST_DIR + "connector-dummy.xml";
private static final String RESOURCE_DUMMY_FILENAME = TEST_DIR + "resource-dummy.xml";
private static final String RESOURCE_DUMMY_OID = "ef2bc95b-76e0-59e2-86d6-9999dddddddd";
private static final Trace LOGGER = TraceManager.getTrace(TestDummyHacks.class);
private PrismObject<ConnectorType> connector;
private PrismObject<ResourceType> resource;
private ResourceType resourceType;
private static DummyResource dummyResource;
private static Task syncTask;
@Autowired(required=true)
private ProvisioningService provisioningService;
// Used to make sure that the connector is cached
@Autowired(required=true)
private ConnectorManager connectorManager;
@Autowired(required=true)
private SynchornizationServiceMock syncServiceMock;
public TestDummyHacks() {
super();
}
/*
* (non-Javadoc)
*
* @see com.evolveum.midpoint.test.AbstractIntegrationTest#initSystem()
*/
@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
// DO NOT DO provisioningService.postInit(..)
// We want to avoid connector discovery and insert our own connector object
// provisioningService.postInit(initResult);
connector = repoAddObjectFromFile(CONNECTOR_DUMMY_FILENAME, initResult);
resource = repoAddObjectFromFile(RESOURCE_DUMMY_FILENAME, initResult);
resourceType = resource.asObjectable();
dummyResource = DummyResource.getInstance();
dummyResource.reset();
dummyResource.populateWithDefaultSchema();
DummyObjectClass accountObjectClass = dummyResource.getAccountObjectClass();
accountObjectClass.addAttributeDefinition(ProvisioningTestUtil.CONNID_DESCRIPTION_NAME, String.class);
}
/**
* This should be the very first test that works with the resource.
*
* The original repository object does not have resource schema. The schema
* should be generated from the resource on the first use. This is the test
* that executes testResource and checks whether the schema was generated.
*/
@Test
public void test003Connection() throws Exception {
final String TEST_NAME = "test003Connection";
TestUtil.displayTestTile(TEST_NAME);
// GIVEN
OperationResult result = new OperationResult(TestDummyHacks.class.getName()
+ "." + TEST_NAME);
// Check that there is no schema before test (pre-condition)
ResourceType resourceBefore = repositoryService.getObject(ResourceType.class, RESOURCE_DUMMY_OID,
null, result).asObjectable();
assertNotNull("No connector ref", resourceBefore.getConnectorRef());
assertNotNull("No connector ref OID", resourceBefore.getConnectorRef().getOid());
ConnectorType connector = repositoryService.getObject(ConnectorType.class, resourceBefore
.getConnectorRef().getOid(), null, result).asObjectable();
assertNotNull(connector);
XmlSchemaType xmlSchemaTypeBefore = resourceBefore.getSchema();
Element resourceXsdSchemaElementBefore = ResourceTypeUtil.getResourceXsdSchema(resourceBefore);
AssertJUnit.assertNull("Found schema before test connection. Bad test setup?", resourceXsdSchemaElementBefore);
// WHEN
OperationResult testResult = provisioningService.testResource(RESOURCE_DUMMY_OID);
// THEN
display("Test result", testResult);
TestUtil.assertSuccess("Test resource failed (result)", testResult);
PrismObject<ResourceType> resourceRepoAfter = repositoryService.getObject(ResourceType.class, RESOURCE_DUMMY_OID, null, result);
ResourceType resourceTypeRepoAfter = resourceRepoAfter.asObjectable();
display("Resource after test", resourceTypeRepoAfter);
XmlSchemaType xmlSchemaTypeAfter = resourceTypeRepoAfter.getSchema();
assertNotNull("No schema after test connection", xmlSchemaTypeAfter);
Element resourceXsdSchemaElementAfter = ResourceTypeUtil.getResourceXsdSchema(resourceTypeRepoAfter);
assertNotNull("No schema after test connection", resourceXsdSchemaElementAfter);
String resourceXml = prismContext.serializeObjectToString(resourceRepoAfter, PrismContext.LANG_XML);
display("Resource XML", resourceXml);
CachingMetadataType cachingMetadata = xmlSchemaTypeAfter.getCachingMetadata();
assertNotNull("No caching metadata", cachingMetadata);
assertNotNull("No retrievalTimestamp", cachingMetadata.getRetrievalTimestamp());
assertNotNull("No serialNumber", cachingMetadata.getSerialNumber());
Element xsdElement = ObjectTypeUtil.findXsdElement(xmlSchemaTypeAfter);
ResourceSchema parsedSchema = ResourceSchemaImpl.parse(xsdElement, resourceBefore.toString(), prismContext);
assertNotNull("No schema after parsing", parsedSchema);
display("Parsed schema", parsedSchema);
ComplexTypeDefinition accountDef = parsedSchema.findComplexTypeDefinition(
new QName(parsedSchema.getNamespace(),SchemaConstants.ACCOUNT_OBJECT_CLASS_LOCAL_NAME));
assertNotNull("No account definition in schema after parsing", accountDef);
PrismAsserts.assertPropertyDefinition(accountDef,
SchemaConstants.ICFS_NAME, DOMUtil.XSD_STRING, 1, 1);
PrismAsserts.assertPropertyDefinition(accountDef,
new QName(SchemaConstants.NS_ICF_SCHEMA, "description"),
DOMUtil.XSD_STRING, 0, 1);
// The useless configuration variables should be reflected to the resource now
assertEquals("Wrong useless string", "Shiver me timbers!", dummyResource.getUselessString());
assertEquals("Wrong guarded useless string", "Dead men tell no tales", dummyResource.getUselessGuardedString());
}
}