Skip to content

Commit

Permalink
Merge pull request #16041 from vinumaddumage/fix-intermitant-test-fail
Browse files Browse the repository at this point in the history
Fix workflow deployment verification and add logs
  • Loading branch information
vinumaddumage committed Jun 20, 2023
2 parents 4fb5589 + 386a7af commit 5676e6b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
import org.wso2.identity.integration.test.utils.WorkflowConstants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;

public class UserApprovalTestBase extends RESTAPIUserTestBase {

private static final Log log = LogFactory.getLog(UserApprovalTestBase.class);
private static final int MAX_WAIT_ITERATIONS_TILL_WORKFLOW_DEPLOYMENT = 100;

protected static String templateId = "MultiStepApprovalTemplate";
protected static String workflowImplId = "ApprovalWorkflow";
Expand Down Expand Up @@ -200,50 +202,50 @@ protected void addAssociationForMatch() throws Exception {

protected void waitForWorkflowToDeploy() throws Exception {

// We have to check whether the service is up and running by calling the generated endpoint.
String url = super.getBackendURL() + addUserWorkflowName + "Service";
HttpClient client = new HttpClientFactory().getHttpClient();
HttpGet request = new HttpGet(url);

boolean runLoop;
int count = 1;

do {
runLoop = false;
HttpResponse httpResponse = client.execute(request);

// If the server response is 500 or it contains "service not available" text or "Operation not found" text,
// then we have to assume that the service is still not available. So we have to recheck after
// brief time period.
if (httpResponse.getStatusLine().getStatusCode() == 500) {
runLoop = true;
} else {
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("The service cannot be found for the endpoint reference") ||
line.contains("The endpoint reference (EPR) for the Operation not found")) {
runLoop = true;
break;
}
boolean isBpelDeployed = false;
boolean isHumanTaskDeployed = false;
String url;
for (int count = 1; count <= MAX_WAIT_ITERATIONS_TILL_WORKFLOW_DEPLOYMENT; count++) {
log.info("Verifying workflow deployment on " + count + " of " +
MAX_WAIT_ITERATIONS_TILL_WORKFLOW_DEPLOYMENT + " attempts.");
if (!isBpelDeployed) {
log.info("Verifying BPEL deployment.");
url = identityContextUrls.getSecureServiceUrl() + "/" + addUserWorkflowName + "Service?wsdl";
if (isServiceDeployed(url)) {
isBpelDeployed = true;
log.info("Verified BPEL Workflow deployment successfully in " + count + " attempt(s).");
}
}

// Wait 20 times for 5 seconds intervals until the workflow is properly deployed. This is for server based
// test runners like Jenkins.
if (count < 20) {
log.info("Still no luck :(. So going to wait 5 seconds and try " + (20 - count) + " more time(s).");
Thread.sleep(5000);
if (isBpelDeployed) {
log.info("Verifying Human Task deployment.");
url = identityContextUrls.getSecureServiceUrl() + "/" + addUserWorkflowName + "TaskService?wsdl";
if (isServiceDeployed(url)) {
isHumanTaskDeployed = true;
log.info("Verified Human Task Workflow deployment successfully in " + count + " attempt(s).");
log.info("Workflow deployment successfully Verified.");
break;
}
}
log.info("Still no luck :(. So going to wait 1 seconds and retry");
Thread.sleep(1000);
}

// Give up after 100 seconds or this will be a forever loop.
if (count >= 20) {
log.info("No luck. Going to give up. Test will most probably fail.");
runLoop = false;
}
count++;
} while (runLoop);
// Give up after 100 seconds.
if (!isBpelDeployed || !isHumanTaskDeployed) {
log.warn("No luck. Going to give up. Test will most probably fail.");
}
}

private boolean isServiceDeployed(String url) throws IOException {

// We have to check whether the service is up and running by calling the generated endpoint.
HttpClient client = new HttpClientFactory().getHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse httpResponse = client.execute(request);

log.info("Response code: " + httpResponse.getStatusLine().getStatusCode());
return (httpResponse.getStatusLine().getStatusCode() == 200);
}

protected void setUpWorkFlowAssociation() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.um.ws.api.stub.ClaimValue;
import org.wso2.identity.integration.test.rest.api.user.approval.common.UserApprovalTestBase;

import java.io.IOException;
import java.util.Arrays;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
Expand All @@ -51,7 +53,7 @@ public class UserMeApprovalTest extends UserApprovalTestBase {
private static final String TEST_WORKFLOW_ADD_USER_FOR_REST_TASK = addUserWorkflowName + "Task";
private static final String JSON_PATH_MATCHING_REST_API_TEST_APPROVAL_TASK = "findAll{ it.presentationName == '"
+ TEST_WORKFLOW_ADD_USER_FOR_REST_TASK + "' }";
private static final int MAX_WAIT_ITERATIONS_TILL_WORKFLOW_DEPLOYMENT = 18;
private static final int MAX_WAIT_ITERATIONS_TILL_TASK_CREATION = 10;

private static String swaggerDefinition;
private String taskIdToApprove;
Expand Down Expand Up @@ -117,20 +119,11 @@ public void testListTasksWhenEmpty() {
@Test(dependsOnMethods = {"testListTasksWhenEmpty"})
public void testListTasksWhenAvailable() throws Exception {

addAssociationForMatch();
for (int i = 1; i <= MAX_WAIT_ITERATIONS_TILL_WORKFLOW_DEPLOYMENT; i++) {
int numOfTasks = getResponseOfGet(ME_APPROVAL_TASKS_ENDPOINT_URI)
.then()
.extract()
.path("findAll{ it.presentationName == '"
+ TEST_WORKFLOW_ADD_USER_FOR_REST_TASK + "' }.size()");
if (numOfTasks == 3) {
break;
}

// Wait till workflow is applied.
log.info("Waiting 5 seconds till the workflow is applied for association, iteration " + i + " of 3");
Thread.sleep(5000);
log.info("Adding users matching the workflow engagement " + addUserWorkflowName + " to tenant " + tenant);
for (int taskCount = 1; taskCount <= userToAdd.length; taskCount++) {
usmClient.addUser(userToAdd[taskCount-1], "test12345", Arrays.copyOfRange(rolesToAdd,0, taskCount),
new ClaimValue[0],null, false);
verifyTaskCreation(taskCount);
}

getResponseOfGet(ME_APPROVAL_TASKS_ENDPOINT_URI)
Expand All @@ -139,7 +132,7 @@ public void testListTasksWhenAvailable() throws Exception {
.statusCode(HttpStatus.SC_OK)
.log().ifValidationFails()
.body("findAll{ it.presentationName == " +
"'" + TEST_WORKFLOW_ADD_USER_FOR_REST_TASK + "' }.size()", is(3))
"'" + TEST_WORKFLOW_ADD_USER_FOR_REST_TASK + "' }.size()", is(userToAdd.length))
.body("findAll{ it.presentationName == " +
"'" + TEST_WORKFLOW_ADD_USER_FOR_REST_TASK + "' }[0].name", containsString(addUserWorkflowName))
.body("findAll{ it.presentationName == '" + TEST_WORKFLOW_ADD_USER_FOR_REST_TASK +
Expand Down Expand Up @@ -339,6 +332,30 @@ private String getPayLoadForReleaseTask() throws IOException {
return getPayLoad(APPROVAL_ACTION.RELEASE);
}

private void verifyTaskCreation(int taskCount) throws InterruptedException {

// Verifying task creation in a 10 sec window.
for (int i = 0; i < MAX_WAIT_ITERATIONS_TILL_TASK_CREATION; i++) {
// Wait till workflow is applied.
log.info("Waiting 1 seconds before checking whether workflow is applied for association.");
Thread.sleep(1000);
int numOfTasks = getResponseOfGet(ME_APPROVAL_TASKS_ENDPOINT_URI)
.then()
.extract()
.path("findAll{ it.presentationName == '"
+ TEST_WORKFLOW_ADD_USER_FOR_REST_TASK + "' }.size()");
if (numOfTasks == taskCount) {
log.info("Tasks creation verified successfully in " + i + " attempt(s). Number of tasks created : "
+ numOfTasks + " of " + userToAdd.length);
break;
} else {
log.info("Tasks " + taskCount + " creation incomplete after " + i + "of "
+ MAX_WAIT_ITERATIONS_TILL_TASK_CREATION + " attempt(s)");
}
}

}

private void validateTaskListFilterResponse(Response response, String taskId, int size, STATE state) {

validateResponseElement(response, "size()", is(size));
Expand Down

0 comments on commit 5676e6b

Please sign in to comment.