Skip to content

Commit

Permalink
Fixed result handling in web service testResource() (+test)
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Apr 10, 2015
1 parent 8fa33a8 commit 4fccfb1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
Expand Up @@ -238,7 +238,9 @@ public OperationResultType testResource(String resourceOid) throws FaultMessage
return handleOperationResult(testResult);
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL testResource() failed", ex);
throwFault(ex, null);
OperationResult faultResult = new OperationResult(TEST_RESOURCE);
faultResult.recordFatalError(ex);
throwFault(ex, faultResult);
// notreached
return null;
} finally {
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.evolveum.midpoint.testing.wstest;

import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

Expand Down Expand Up @@ -320,31 +321,6 @@ protected static <T> JAXBElement<T> toJaxbElement(QName name, T value){
protected static Element parseElement(String stringXml) throws SAXException, IOException {
return DOMUtil.getFirstChildElement(DOMUtil.parseDocument(stringXml));
}

/**
* Clean the repository after tests. Preserves user administrator
* */
protected void cleanRepository() throws FaultMessage {
cleanObjects(UserType.class, SystemObjectsType.USER_ADMINISTRATOR.value());
cleanObjects(RoleType.class, SystemObjectsType.ROLE_SUPERUSER.value(), SystemObjectsType.ROLE_END_USER.value());
}

private <O extends ObjectType> void cleanObjects(Class<O> type, String... protectedOids) throws FaultMessage {
Holder<OperationResultType> resultHolder = new Holder<OperationResultType>();
Holder<ObjectListType> objectListHolder = new Holder<ObjectListType>();
PagingType paging = new PagingType();

modelPort.searchObjects(getTypeQName(type), null, null, objectListHolder, resultHolder);

List<String> protectedOidList = Arrays.asList(protectedOids);
ObjectListType objectList = objectListHolder.value;
for (ObjectType object: objectList.getObject()) {
if (!protectedOidList.contains(object.getOid())) {
display("Deleting "+type.getSimpleName()+" "+ModelClientUtil.toString(object));
deleteObject(type, object.getOid());
}
}
}

protected <O extends ObjectType> void deleteObject(Class<O> type, String oid) throws FaultMessage {
ObjectDeltaListType deltaList = new ObjectDeltaListType();
Expand Down Expand Up @@ -418,6 +394,7 @@ protected void assertSuccess(OperationResultType result) {

protected <F extends FaultType> void assertFaultMessage(FaultMessage fault, Class<F> expectedFaultInfoClass, String expectedMessage) {
FaultType faultInfo = fault.getFaultInfo();
assertNotNull("No fault info in "+fault);
if (expectedFaultInfoClass != null && !expectedFaultInfoClass.isAssignableFrom(faultInfo.getClass())) {
AssertJUnit.fail("Expected that faultInfo will be of type "+expectedFaultInfoClass+", but it was "+faultInfo.getClass());
}
Expand All @@ -426,6 +403,7 @@ protected <F extends FaultType> void assertFaultMessage(FaultMessage fault, Clas
assertTrue("Wrong message in fault info: "+faultInfo.getMessage(), faultInfo.getMessage().contains(expectedMessage));
}
OperationResultType result = faultInfo.getOperationResult();
assertNotNull("No result in faultInfo in "+fault, result);
assertEquals("Expected that resut in FaultInfo will be fatal error, but it was "+result.getStatus(),
OperationResultStatusType.FATAL_ERROR, result.getStatus());
}
Expand Down Expand Up @@ -624,6 +602,33 @@ protected void assertPasswordCreateMetadata(UserType user, String actorOid, XMLG
TestUtil.assertBetween("Wrong password createTimestamp", startTs, endTs, passwordMetadata.getCreateTimestamp());
}


/**
* Clean the repository after tests. Preserves user administrator
* */
protected void cleanRepository() throws FaultMessage {
cleanObjects(UserType.class, SystemObjectsType.USER_ADMINISTRATOR.value());
cleanObjects(RoleType.class, SystemObjectsType.ROLE_SUPERUSER.value(), SystemObjectsType.ROLE_END_USER.value());
cleanObjects(ResourceType.class);
}

private <O extends ObjectType> void cleanObjects(Class<O> type, String... protectedOids) throws FaultMessage {
Holder<OperationResultType> resultHolder = new Holder<OperationResultType>();
Holder<ObjectListType> objectListHolder = new Holder<ObjectListType>();
PagingType paging = new PagingType();

modelPort.searchObjects(getTypeQName(type), null, null, objectListHolder, resultHolder);

List<String> protectedOidList = Arrays.asList(protectedOids);
ObjectListType objectList = objectListHolder.value;
for (ObjectType object: objectList.getObject()) {
if (!protectedOidList.contains(object.getOid())) {
display("Deleting "+type.getSimpleName()+" "+ModelClientUtil.toString(object));
deleteObject(type, object.getOid());
}
}
}

@Test
public void test000SanityAndCleanup() throws Exception {
final String TEST_NAME = "test000SanityAndCleanup";
Expand All @@ -633,7 +638,6 @@ public void test000SanityAndCleanup() throws Exception {
configurationType = getConfiguration();
checkAuditEnabled(configurationType);


cleanRepository();
}

Expand Down
Expand Up @@ -50,6 +50,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MetadataType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ModelExecuteOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultStatusType;
Expand All @@ -62,11 +63,14 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.FaultMessage;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.FaultType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.ObjectNotFoundFaultType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.PolicyViolationFaultType;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ModelPortType;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ModelService;
import com.evolveum.prism.xml.ns._public.query_3.QueryType;
import com.evolveum.prism.xml.ns._public.types_3.ChangeTypeType;
import com.evolveum.prism.xml.ns._public.types_3.ModificationTypeType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;

Expand Down Expand Up @@ -152,14 +156,26 @@ public void test020AddResourceOpenDj() throws Exception {

LogfileTestTailer tailer = createLogTailer();
ResourceType resource = ModelClientUtil.unmarshallFile(RESOURCE_OPENDJ_FILE);
resource.getConnectorRef().setOid(connectorLdapOid);


ObjectDeltaListType deltaList = new ObjectDeltaListType();
ObjectDeltaType delta = new ObjectDeltaType();
delta.setObjectType(getTypeQName(ResourceType.class));
delta.setChangeType(ChangeTypeType.ADD);
delta.setObjectToAdd(resource);
deltaList.getDelta().add(delta);

ModelExecuteOptionsType options = new ModelExecuteOptionsType();
options.setIsImport(Boolean.TRUE);

XMLGregorianCalendar startTs = TestUtil.currentTime();

// WHEN
String oid = addObject(resource);

// WHEN
ObjectDeltaOperationListType deltaOpList = modelPort.executeChanges(deltaList, options);

// THEN
assertSuccess(deltaOpList);
String oid = deltaOpList.getDeltaOperation().get(0).getObjectDelta().getOid();

// THEN
XMLGregorianCalendar endTs = TestUtil.currentTime();

tailer.tail();
Expand All @@ -174,6 +190,8 @@ public void test020AddResourceOpenDj() throws Exception {
ResourceType resourceAfter = getObject(ResourceType.class, RESOURCE_OPENDJ_OID);
display(resourceAfter);

assertEquals("Wrong connector OID", connectorLdapOid, resourceAfter.getConnectorRef().getOid());

assertCreateMetadata(resourceAfter, USER_ADMINISTRATOR_OID, startTs, endTs);
}

Expand All @@ -192,7 +210,24 @@ public void test030ResourceOpenDjTestConnection() throws Exception {
assertSuccess(testResult);
}

// TODO: test non-existing resource

@Test
public void test032ResourceNonexistingTestConnection() throws Exception {
final String TEST_NAME = "test032ResourceNonexistingTestConnection";
displayTestTitle(TEST_NAME);

try {
// WHEN
OperationResultType testResult = modelPort.testResource("56b53914-df90-11e4-8c8c-001e8c717e5b");

AssertJUnit.fail("Unexpected success");
} catch (FaultMessage f) {
assertFaultMessage(f, ObjectNotFoundFaultType.class, "was not found");
}

}

// TODO: test unreachable resource


}

0 comments on commit 4fccfb1

Please sign in to comment.