Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix workflow deployment verification and add logs #16041

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,67 @@ 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.
if (!isBpelDeployed || !isHumanTaskDeployed) {
log.warn("No luck. Going to give up. Test will most probably fail.");
}
}

// 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;
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());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return true;
}

// 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.
// Log response to get more context.
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String line;
while ((line = bufferedReader.readLine()) != null) {
log.info("Response content: " + line);
if (line.contains("The service cannot be found for the endpoint reference") ||
line.contains("The endpoint reference (EPR) for the Operation not found")) {
break;
}
count++;
} while (runLoop);
}
return false;
}

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