Skip to content

Commit

Permalink
Retry the application creation when 401 error
Browse files Browse the repository at this point in the history
  • Loading branch information
madurangasiriwardena committed Jan 17, 2025
1 parent a327422 commit 656315b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OpenIDConnectConfiguration;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.SAML2ServiceProvider;
import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.RoleV2;
import org.wso2.identity.integration.test.restclients.exceptions.AuthenticationException;
import org.wso2.identity.integration.test.utils.OAuth2Constant;

import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -104,7 +105,10 @@ public String createApplication(ApplicationModel application) throws IOException
try (CloseableHttpResponse response = getResponseOfHttpPost(applicationManagementApiBasePath, jsonRequest,
getHeaders())) {

if (response.getStatusLine().getStatusCode() >= 400) {
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_UNAUTHORIZED) {
String responseBody = EntityUtils.toString(response.getEntity());
throw new AuthenticationException("Error occurred while creating the application. Response: " + responseBody);
} else if (response.getStatusLine().getStatusCode() >= HttpServletResponse.SC_BAD_REQUEST) {
String responseBody = EntityUtils.toString(response.getEntity());
throw new RuntimeException("Error occurred while creating the application. Response: " + responseBody);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@

package org.wso2.identity.integration.test.restclients;

import com.google.gson.Gson;
import io.restassured.http.ContentType;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.wso2.carbon.automation.engine.context.beans.Tenant;
import org.wso2.identity.integration.test.rest.api.server.tenant.management.v1.model.TenantModel;
import org.wso2.identity.integration.test.rest.api.user.common.model.UserObject;
import org.wso2.identity.integration.test.utils.OAuth2Constant;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
Expand Down Expand Up @@ -70,24 +65,6 @@ public String addTenant(TenantModel TenantReqModel) throws Exception {
}
}

/**
* Create a tenant
*
* @param tenantId Id of the tenant.
* @return TenantModel object with tenant details.
* @throws Exception If an error occurred while adding a tenant.
*/
public TenantModel getTenantById(String tenantId) throws Exception {
String endPoint = serverUrl + TENANT_MGT_BASE_PATH + PATH_SEPARATOR + tenantId;

try (CloseableHttpResponse response = getResponseOfHttpGet(endPoint, getHeaders())) {
Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpServletResponse.SC_OK,
"Tenant retrieval failed");
//convert the response to a TenantModel object
return new Gson().fromJson(EntityUtils.toString(response.getEntity()), TenantModel.class);
}
}

private Header[] getHeaders() {
Header[] headerList = new Header[2];
headerList[0] = new BasicHeader(AUTHORIZATION_ATTRIBUTE, BASIC_AUTHORIZATION_ATTRIBUTE +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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 org.wso2.identity.integration.test.restclients.exceptions;

public class AuthenticationException extends RuntimeException {

public AuthenticationException(String message) {
super(message);
}

public AuthenticationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.wso2.identity.integration.test.rest.api.server.tenant.management.v1.model.TenantModel;
import org.wso2.identity.integration.test.restclients.OAuth2RestClient;
import org.wso2.identity.integration.test.restclients.TenantMgtRestClient;
import org.wso2.identity.integration.test.restclients.exceptions.AuthenticationException;
import org.wso2.identity.integration.test.util.Utils;
import org.wso2.identity.integration.test.utils.UserUtil;

Expand Down Expand Up @@ -271,17 +272,7 @@ private void addRegistryMountTenant() throws Exception {
tenantReqModel.setDomain(TENANT_DOMAIN);
tenantReqModel.addOwnersItem(tenantAdminUser);

String tenantId = tenantMgtRestClient.addTenant(tenantReqModel);

// Check if the tenant is created successfully iteratively for 10 seconds
for (int i = 0; i < 10; i++) {
TenantModel tenant = tenantMgtRestClient.getTenantById(tenantId);
if (tenant != null) {
break;
}
log.info("Tenant is not created yet. Waiting for tenant creation...");
Thread.sleep(1000);
}
tenantMgtRestClient.addTenant(tenantReqModel);
}

private Tenant getRegistryMountTenantInfo() {
Expand All @@ -304,11 +295,14 @@ private void createApplication() throws Exception {
.saml(getSAMLConfigurations()))
.claimConfiguration(getClaimConfiguration());

try {
appId = applicationMgtRestClient.createApplication(applicationCreationModel);
} catch (RuntimeException e) {
log.error("Error while creating the application", e);
throw new Exception("Error while creating the application", e);
for (int i = 0; i < 5; i++) {
try {
appId = applicationMgtRestClient.createApplication(applicationCreationModel);
break; // Exit loop if successful
} catch (AuthenticationException e) {
log.error("Attempt " + (i + 1) + " failed: Error while creating the application", e);
Thread.sleep(1000);
}
}
}

Expand Down

0 comments on commit 656315b

Please sign in to comment.