Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Naduni Pamudika committed Jan 16, 2024
1 parent a5652ec commit 75cbdd7
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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.am.integration.test.utils;

import java.io.IOException;
import java.net.Socket;

public class ServerPortsUtils {

public static String LOCALHOST = "localhost";
public static final int httpPortLowerRange = 8080;
public static final int httpPortUpperRange = 8099;
public static final int httpsPortLowerRange = 9950;
public static final int httpsPortUpperRange = 9999;

/**
* Check whether give port is available
*
* @param port Port Number
* @return status
*/
private static boolean isPortFree(int port, String host) {

Socket s = null;
try {
s = new Socket(host, port);
// something is using the port and has responded.
return false;
} catch (IOException e) {
//port available
return true;
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
throw new RuntimeException("Unable to close connection ", e);
}
}
}
}

public static int getAvailableHttpPort(String host) {
return getAvailablePort(httpPortLowerRange, httpPortUpperRange, host);
}

public static int getAvailableHttpsPort(String host) {
return getAvailablePort(httpsPortLowerRange, httpsPortUpperRange, host);
}

/**
* Find a free port to start backend WebSocket server in given port range
*
* @param lowerPortLimit from port number
* @param upperPortLimit to port number
* @return Available Port Number
*/
private static int getAvailablePort(int lowerPortLimit, int upperPortLimit, String host) {
while (lowerPortLimit < upperPortLimit) {
if (ServerPortsUtils.isPortFree(lowerPortLimit, host)) {
return lowerPortLimit;
}
lowerPortLimit += 1;
}
return -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@
import org.wso2.am.integration.test.utils.base.APIMIntegrationConstants;
import org.wso2.am.integration.test.utils.bean.APIRequest;
import org.wso2.am.integration.test.utils.http.HttpRequestUtil;
import org.wso2.carbon.automation.engine.context.AutomationContext;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse;
import org.wso2.carbon.integration.common.admin.client.LogViewerClient;
import org.wso2.carbon.logging.view.data.xsd.LogEvent;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,6 +82,7 @@ public class APIEndpointCertificateTestCase extends APIManagerLifecycleBaseTest
String apiId;
WireMockServer wireMockServer;
private String accessToken;
private LogViewerClient logViewerClient;

@Factory(dataProvider = "userModeDataProvider")
public APIEndpointCertificateTestCase(TestUserMode userMode) {
Expand Down Expand Up @@ -137,6 +142,11 @@ public void initialize() throws Exception {
accessToken = applicationKeyDTO.getToken().getAccessToken();
waitForAPIDeploymentSync(user.getUserName(), apiRequest.getName(), apiRequest.getVersion(),
APIMIntegrationConstants.IS_API_EXISTS);
AutomationContext autoContext = new AutomationContext();
logViewerClient = new LogViewerClient(autoContext.getContextUrls().getBackEndUrl(),
autoContext.getSuperTenant().getTenantAdmin().getUserName(),
autoContext.getSuperTenant().getTenantAdmin().getPassword());
logViewerClient.clearLogs();
}

@Test(groups = {"wso2.am"}, description = "Invoke API without inserting Endpoint Certificate")
Expand Down Expand Up @@ -263,7 +273,9 @@ public void testSearchEndpointCertificates() throws ApiException, ParseException
"testSearchEndpointCertificates"})
public void testInvokeAPI() throws ApiException, InterruptedException, XPathExpressionException, IOException {

Thread.sleep(60000); // Sleep to reload the transport
// Thread.sleep(60000); // Sleep to reload the transport
// Wait for SSLProfile with the uploaded certificate to be reloaded in Gateway
waitForSSLProfileReload();
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("accept", "application/json");
requestHeaders.put("Authorization", "Bearer " + accessToken);
Expand All @@ -282,7 +294,8 @@ public void testInvokeAPIAfterRemovingCertificate() throws InterruptedException,
Assert.assertEquals(response.getStatusCode(), 200);
response = restAPIPublisher.deleteEndpointCertificate("endpoint-2");
Assert.assertEquals(response.getStatusCode(), 200);
Thread.sleep(60500); // Sleep to reload the transport
// Thread.sleep(60500); // Sleep to reload the transport
waitForSSLProfileReload();
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("accept", "application/json");
requestHeaders.put("Authorization", "Bearer " + accessToken);
Expand Down Expand Up @@ -320,6 +333,28 @@ private void startSecureEndpoint(int securedEndpointPort) {
wireMockServer.start();
}

private void waitForSSLProfileReload() throws RemoteException, InterruptedException {
LogEvent[] logEvents = new LogEvent[0];
Thread.sleep(60000);
logEvents = logViewerClient.getAllRemoteSystemLogs();
int retryAttempt = 0;
boolean isSSProfileReloaded = false;
while (retryAttempt < 5 && !isSSProfileReloaded) {
for (LogEvent logEvent : logEvents) {
if (logEvent.getMessage()
.contains("PassThroughHttpSender reloading SSL Config")) {
isSSProfileReloaded = true;
log.info("SSLProfile has been reloaded successfully");
logViewerClient.clearLogs();
break;
}
}
retryAttempt++;
log.info("SSLProfile has not been reloaded. Retry attempt - " + retryAttempt);
Thread.sleep(12000);
}
}

@AfterClass(alwaysRun = true)
public void destroy() throws ApiException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,9 @@ private void testThrottling(String accessToken) throws Exception {
"Received response in not a Connection Ack response");
socket.setResponseMessage(null);
for (int count = 1; count <= limit; count++) {
if (count == limit) {
Thread.sleep(3000);
}
if (count == 1) {
//Send initial graphQL subscription request message
textMessage = "{\"id\":\"2\",\"type\":\"start\",\"payload\":{\"variables\":{},\"extensions\":{},"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.http.HttpStatus;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static Object[][] userModeDataProvider() {
return new Object[][] { new Object[] { TestUserMode.SUPER_TENANT_ADMIN }};
}

@BeforeClass(alwaysRun = true)
@BeforeTest(alwaysRun = true)
public void setEnvironment() throws Exception {
super.init(userMode);
superTenantKeyManagerContext = new AutomationContext(APIMIntegrationConstants.AM_PRODUCT_GROUP_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,24 @@ public void setEnvironment() throws Exception {
super.init(userMode);
storeURLHttp = "https://localhost:9943/";

}

@Test(groups = {"wso2.am"}, description = "Testing Notification Feature")
public void notificationTestCase() throws Exception {

//Setting greenMail server
ServerSetup setup = new ServerSetup(SMTP_TEST_PORT, "localhost", "smtp");
greenMail = new GreenMail(setup);
//Creating user in greenMail server
greenMail.setUser(USER_EMAIL_ADDRESS, EMAIL_USERNAME, EMAIL_PASSWORD);
greenMail.start();
log.info("green mail server started ");
try {
greenMail.start();
} catch (IllegalStateException e) {
log.warn("There was a problem starting GreenMail server. Retrying in 10 seconds");
Thread.sleep(10000);
greenMail.start();
}
log.info("green mail server started");

}

@Test(groups = {"wso2.am"}, description = "Testing Notification Feature")
public void notificationTestCase() throws Exception {

// Adding API
String url = getGatewayURLNhttp() + "response";
Expand Down Expand Up @@ -212,6 +218,7 @@ public void destroy() throws Exception {
undeployAndDeleteAPIRevisionsUsingRest(newApiId, restAPIPublisher);
restAPIPublisher.deleteAPI(apiId);
restAPIPublisher.deleteAPI(newApiId);
greenMail.stop();
}

@DataProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.google.gson.Gson;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -56,10 +57,16 @@
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse;

import javax.ws.rs.core.Response;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -202,22 +209,37 @@ public void testValidateInOutSequence()
String inSequence = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
"artifacts" + File.separator + "AM" + File.separator + "soap" + File.separator
+ "in-sequence-check-phone-numbers.xml"), "UTF-8");
String inSequenceRandomOrderedElement = removeSpacesInSequence(inSequence);
ResourcePolicyListDTO resourcePolicyInListDTO = restAPIPublisher
.getApiResourcePolicies(soapToRestAPIId, "in", "checkPhoneNumbers", "post");
resourcePoliciesIn = resourcePolicyInListDTO.getList();
resourcePoliciesIn.forEach((item) -> {
assertEquals(item.getContent(), inSequence, "Invalid In-Sequence");
try {
String itemElement = removeSpacesInSequence(item.getContent());
log.info("Generated In sequence: " + item.getContent());
boolean equals = inSequenceRandomOrderedElement.equals(itemElement);
assertEquals(equals, true, "Invalid In-Sequence");
} catch (Exception e) {
throw new RuntimeException(e);
}
});

// Validate out-sequence
String outSequence = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
"artifacts" + File.separator + "AM" + File.separator + "soap" + File.separator
+ "out-sequence-check-phone-numbers.xml"), "UTF-8");
String outSequenceElement = removeSpacesInSequence(outSequence);
ResourcePolicyListDTO resourcePolicyOutListDTO = restAPIPublisher
.getApiResourcePolicies(soapToRestAPIId, "out", "checkPhoneNumbers", "post");
resourcePoliciesOut = resourcePolicyOutListDTO.getList();
resourcePoliciesOut.forEach((item) -> {
assertEquals(item.getContent(), outSequence, "Invalid Out-Sequence");
try {
String itemElement = removeSpacesInSequence(item.getContent());
log.info("Generated out sequence: " + item.getContent());
assertEquals(itemElement, outSequenceElement, "Invalid Out-Sequence");
} catch (Exception e) {
log.error(e.getMessage());
}
});
}

Expand Down Expand Up @@ -485,6 +507,7 @@ public void testUpdateInOutSequence() throws Exception {
String updatedInSequence = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
"artifacts" + File.separator + "AM" + File.separator + "soap" + File.separator
+ "updated-in-sequence-check-phone-numbers.xml"), "UTF-8");
String inSequenceRandomOrderedElement = removeSpacesInSequence(updatedInSequence);
resourcePoliciesIn.forEach((item) -> {
ResourcePolicyInfoDTO updatedResourcePoliciesIn = null;
item.setContent(updatedInSequence);
Expand All @@ -494,23 +517,33 @@ public void testUpdateInOutSequence() throws Exception {
} catch (org.wso2.am.integration.clients.publisher.api.ApiException e) {
log.error(e.getMessage());
}
assertEquals(updatedResourcePoliciesIn.getContent(), updatedInSequence, "In-Sequence not updated");
try {
String itemElement = removeSpacesInSequence(updatedResourcePoliciesIn.getContent());
log.info("Generated updated in sequence: " + updatedResourcePoliciesIn.getContent());
boolean equals = inSequenceRandomOrderedElement.equals(itemElement);
assertEquals(equals, true, "In-Sequence not updated");
} catch (Exception e) {
log.error(e.getMessage());
}
});

// Update out-sequence
String updatedOutSequence = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(
"artifacts" + File.separator + "AM" + File.separator + "soap" + File.separator
+ "updated-out-sequence-check-phone-numbers.xml"), "UTF-8");
String updatedOutSequenceElement = removeSpacesInSequence(updatedOutSequence);
resourcePoliciesOut.forEach((item) -> {
item.setContent(updatedOutSequence);
ResourcePolicyInfoDTO updatedResourcePoliciesOut = null;
try {
updatedResourcePoliciesOut = restAPIPublisher
.updateApiResourcePolicies(soapToRestAPIId, item.getId(), item.getResourcePath(), item, null);
} catch (org.wso2.am.integration.clients.publisher.api.ApiException e) {
log.error(e.getMessage());
}
assertEquals(updatedResourcePoliciesOut.getContent(), updatedOutSequence, "Out-Sequence not updated");
item.setContent(updatedOutSequence);
ResourcePolicyInfoDTO updatedResourcePoliciesOut = null;
try {
updatedResourcePoliciesOut = restAPIPublisher
.updateApiResourcePolicies(soapToRestAPIId, item.getId(), item.getResourcePath(), item, null);
log.info("Generated updated out sequence: " + updatedResourcePoliciesOut.getContent());
String updatedResourcePoliciesOutElement = removeSpacesInSequence(updatedResourcePoliciesOut.getContent());
assertEquals(updatedResourcePoliciesOutElement, updatedOutSequenceElement, "Out-Sequence not updated");
} catch (Exception e) {
log.error(e.getMessage());
}
});
}

Expand Down Expand Up @@ -611,6 +644,30 @@ private File getTempFileWithContent(String schema) throws Exception {
return temp;
}

public static String removeSpacesInSequence(String xml) throws Exception {
String closedXml = "<root>" + xml + "</root>";
InputStream stream = new ByteArrayInputStream(closedXml.getBytes());
XMLStreamReader parser;
StAXOMBuilder builder;
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
parser = factory.createXMLStreamReader(stream);
XMLStreamReader filteredReader = factory.createFilteredReader(parser, new StreamFilter() {
public boolean accept(XMLStreamReader r) {
return !r.isWhiteSpace();
}
});
builder = new StAXOMBuilder(filteredReader);
} catch (XMLStreamException e) {
String msg = "Error in initializing the parser.";
log.error(msg, e);
throw new Exception(msg, e);
}

return builder.getDocumentElement().toString().replace(" ","");
}

@DataProvider
public static Object[][] userModeDataProvider() {
return new Object[][]{
Expand Down
Loading

0 comments on commit 75cbdd7

Please sign in to comment.