From 57522afd192f343996c3a7ad9224556dabf0fddd Mon Sep 17 00:00:00 2001 From: dulith Date: Mon, 3 Jul 2023 13:57:37 +0530 Subject: [PATCH 1/3] Add integrations tests to check malformed API contexts --- .../test/impl/RestAPIPublisherImpl.java | 35 ++++++++++++++ .../utils/base/APIMIntegrationConstants.java | 3 ++ .../APIProductCreationTestCase.java | 37 +++++++++++++++ .../tests/graphql/GraphqlTestCase.java | 39 ++++++++++++++++ .../tests/other/WSDLImportTestCase.java | 46 +++++++++++++++++++ ...APIThroughThePublisherRestAPITestCase.java | 25 ++++++++++ .../tests/websocket/WebSocketAPITestCase.java | 25 ++++++++++ 7 files changed, 210 insertions(+) diff --git a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java index daa6b09b0b..a110a35461 100644 --- a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java +++ b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java @@ -109,6 +109,7 @@ import org.wso2.am.integration.test.ClientAuthenticator; import org.wso2.am.integration.test.Constants; import org.wso2.am.integration.test.utils.APIManagerIntegrationTestException; +import org.wso2.am.integration.test.utils.base.APIMIntegrationConstants; import org.wso2.am.integration.test.utils.bean.*; import org.wso2.carbon.automation.test.utils.http.client.HttpResponse; import java.io.File; @@ -262,6 +263,21 @@ public HttpResponse addAPI(APIRequest apiRequest) throws ApiException { return response; } + public HttpResponse addAPIWithMalformedContext(APIRequest apiRequest) { + + String osVersion = "v3"; + setActivityID(); + HttpResponse response = null; + try { + this.addAPI(apiRequest, osVersion); + } catch (ApiException e) { + response = new HttpResponse(APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR, e.getCode()); + return response; + } + return null; + + } + /** * \ * This method is used to create an API. @@ -366,6 +382,9 @@ public APIDTO addAPI(APIRequest apiRequest, String osVersion) throws ApiExceptio if (e.getResponseBody().contains("already exists")) { return null; } + if (e.getResponseBody().contains(APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR)) { + throw new ApiException(e.getCode(), APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR); + } throw new ApiException(e); } return apidto; @@ -1229,6 +1248,22 @@ public void updateGraphqlSchemaDefinition(String apiId, String schemaDefinition) Assert.assertEquals(HttpStatus.SC_OK, schemaDefinitionDTO.getStatusCode()); } + public HttpResponse importGraphqlSchemaDefinitionWithInvalidContext(File file, String properties) throws ApiException { + ApiResponse apiDtoApiResponse = null; + HttpResponse response = null; + try { + apiDtoApiResponse = apIsApi.importGraphQLSchemaWithHttpInfo(null, "GRAPHQL", + file, properties); + Assert.assertEquals(HttpStatus.SC_CREATED, apiDtoApiResponse.getStatusCode()); + } catch (ApiException e) { + if (e.getResponseBody().contains(APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR)) { + response = new HttpResponse(APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR, e.getCode()); + } + } + return response; + } + + public WSDLValidationResponseDTO validateWsdlDefinition(String url, File wsdlDefinition) throws ApiException { ApiResponse response = validationApi .validateWSDLDefinitionWithHttpInfo(url, wsdlDefinition); diff --git a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java index 957cb110f1..b48fed1dfb 100644 --- a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java +++ b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java @@ -42,6 +42,9 @@ public class APIMIntegrationConstants { public static final String API_RESPONSE_ELEMENT_NAME_APIS = "apis"; public static final String API_RESPONSE_ELEMENT_NAME_ID = "id"; + public static final String API_CONTEXT_MALFORMED_ERROR = "The API context is malformed"; + public static final String API_PRODUCT_CONTEXT_MALFORMED_ERROR = "The API Product context is malformed"; + public static final String OAUTH_DEFAULT_APPLICATION_NAME = "DefaultApplication"; public static final String IS_API_EXISTS = "\"isApiExists\":true"; diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java index 0318d32994..46373bddfc 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java @@ -57,6 +57,7 @@ import org.wso2.carbon.automation.test.utils.http.client.HttpResponse; import org.wso2.carbon.integration.common.admin.client.UserManagementClient; +import javax.ws.rs.core.Response; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; @@ -89,6 +90,8 @@ public class APIProductCreationTestCase extends APIManagerLifecycleBaseTest { private ApiProductTestHelper apiProductTestHelper; private String apiProductId2; private String resourcePath; + private String apiID1; + private String apiID2; @Factory(dataProvider = "userModeDataProvider") public APIProductCreationTestCase(TestUserMode userMode) { @@ -298,6 +301,37 @@ public void testAPIProductNewVersionCreationWithDefaultVersion() throws Exceptio Assert.assertEquals(isDefaultVersion, true, "Copied API Product is not the default version"); } + @Test(groups = {"wso2.am"}, description = "Test creation of API Product with malformed context") + public void testCreateApiProductWithMalformedContext() throws Exception { + // Pre-Conditions : Create APIs + List apisToBeUsed = new ArrayList<>(); + APIDTO apiOne = apiTestHelper.createApiOne(getBackendEndServiceEndPointHttp("wildcard/resources")); + APIDTO apiTwo = apiTestHelper.createApiTwo(getBackendEndServiceEndPointHttp("wildcard/resources")); + apisToBeUsed.add(apiOne); + apisToBeUsed.add(apiTwo); + apiID1 = apiOne.getId(); + apiID2 = apiTwo.getId(); + + // Step 1 : Create APIProduct + final String provider = user.getUserName(); + final String name = UUID.randomUUID().toString(); + final String context = "/" + UUID.randomUUID().toString() + "{version}" ; + final String version = "1.0.0"; + + List policies = Arrays.asList(TIER_UNLIMITED, TIER_GOLD); + + try{ + apiProductTestHelper.createAPIProductInPublisher(provider, name, context, version, + apisToBeUsed, policies); + } catch (ApiException e) { + Assert.assertEquals(e.getCode(), Response.Status.BAD_REQUEST.getStatusCode()); + Assert.assertTrue(e.getResponseBody().contains( + APIMIntegrationConstants.API_PRODUCT_CONTEXT_MALFORMED_ERROR)); + + } + } + + @Test(groups = {"wso2.am"}, description = "Test creation and invocation of API Product which depends " + "on a visibility restricted API") public void testCreateAndInvokeApiProductWithVisibilityRestrictedApi() throws Exception { @@ -781,6 +815,9 @@ private File geTempFileWithContent(String swagger) throws Exception { @AfterClass(alwaysRun = true) public void cleanUpArtifacts() throws Exception { + restAPIPublisher.deleteAPI(apiID1); + restAPIPublisher.deleteAPI(apiID2); + super.cleanUp(); userManagementClient.deleteUser(RESTRICTED_SUBSCRIBER); userManagementClient.deleteUser(STANDARD_SUBSCRIBER); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java index fe0a130d7d..f08d29d4e8 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java @@ -189,6 +189,45 @@ public void createAndPublishGraphQLAPIUsingSchemaWithInterfaces() throws Excepti APIMIntegrationConstants.IS_API_EXISTS); } + @Test(groups = { "wso2.am" }, description = + "Attempt GraphQL API creation using a malformed context") + public void testCreateAndPublishGraphQLAPIUsingSchemaWithMalformedContext() throws Exception { + + String schemaDefinitionWithInterface = IOUtils.toString( + getClass().getClassLoader().getResourceAsStream("graphql" + File.separator + + "schemaWithInterface.graphql"), "UTF-8"); + File file = getTempFileWithContent(schemaDefinitionWithInterface); + GraphQLValidationResponseDTO responseApiDto = restAPIPublisher.validateGraphqlSchemaDefinition(file); + GraphQLValidationResponseGraphQLInfoDTO graphQLInfo = responseApiDto.getGraphQLInfo(); + String arrayToJson = new ObjectMapper().writeValueAsString(graphQLInfo.getOperations()); + JSONArray operations = new JSONArray(arrayToJson); + HttpResponse response = null; + + ArrayList policies = new ArrayList(); + policies.add("Unlimited"); + + JSONObject additionalPropertiesObj = new JSONObject(); + additionalPropertiesObj.put("name", "GraphQLAPIWithInvalidContext"); + additionalPropertiesObj.put("context", "invalidContext{version}"); + additionalPropertiesObj.put("version", API_VERSION_1_0_0); + + JSONObject url = new JSONObject(); + url.put("url", END_POINT_URL); + JSONObject endpointConfig = new JSONObject(); + endpointConfig.put("endpoint_type", "http"); + endpointConfig.put("sandbox_endpoints", url); + endpointConfig.put("production_endpoints", url); + additionalPropertiesObj.put("endpointConfig", endpointConfig); + additionalPropertiesObj.put("policies", policies); + additionalPropertiesObj.put("operations", operations); + + // create Graphql API + response = restAPIPublisher.importGraphqlSchemaDefinitionWithInvalidContext(file, additionalPropertiesObj.toString()); + Assert.assertNotNull(response, "Response cannot be null"); + Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); + + } + @Test(groups = {"wso2.am"}, description = "test retrieve schemaDefinition at publisher") public void testRetrieveSchemaDefinitionAtPublisher() throws Exception { GraphQLSchemaDTO schema = restAPIPublisher.getGraphqlSchemaDefinition(graphqlAPIId); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java index b8769196b2..8097503f57 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java @@ -28,6 +28,7 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import org.wso2.am.integration.clients.publisher.api.ApiException; import org.wso2.am.integration.clients.publisher.api.ApiResponse; import org.wso2.am.integration.clients.publisher.api.v1.dto.APIDTO; import org.wso2.am.integration.clients.publisher.api.v1.dto.APIOperationsDTO; @@ -71,6 +72,7 @@ public class WSDLImportTestCase extends APIManagerLifecycleBaseTest { private final Log log = LogFactory.getLog(WSDLImportTestCase.class); private String WSDL_FILE_API_NAME = "WSDLImportAPIWithWSDLFile"; private String WSDL_FILE_API_CONTEXT = "wsdlimportwithwsdlfile"; + private String WSDL_FILE_MALFORMED_API_CONTEXT = "wsdlimportwithwsdlfile{version}"; private String WSDL_ZIP_API_NAME = "WSDLImportAPIWithZipFile"; private String WSDL_ZIP_API_CONTEXT = "wsdlimportwithzipfile"; private String WSDL_URL_API_NAME = "WSDLImportAPIWithURL"; @@ -247,6 +249,50 @@ public void testWsdlDefinitionImport() throws Exception { accessToken = applicationKeyDTO.getToken().getAccessToken(); } + @Test(groups = {"wso2.am"}, description = "Importing WSDL API definition and create API") + public void testWsdlDefinitionImportWithMalformedContext() throws Exception { + log.info("testWsdlDefinitionImport initiated"); + + // Set environment + ArrayList environment = new ArrayList<>(); + environment.add(Constants.GATEWAY_ENVIRONMENT); + + // Set policies + ArrayList policies = new ArrayList<>(); + policies.add(APIMIntegrationConstants.API_TIER.UNLIMITED); + + // Set endpointConfig + JSONObject url = new JSONObject(); + url.put("url", apiEndPointURL); + JSONObject endpointConfig = new JSONObject(); + endpointConfig.put("endpoint_type", "http"); + endpointConfig.put("sandbox_endpoints", url); + endpointConfig.put("production_endpoints", url); + + // Create additional properties object + JSONObject additionalPropertiesObj = new JSONObject(); + additionalPropertiesObj.put("provider", user.getUserName()); + additionalPropertiesObj.put("name", WSDL_FILE_API_NAME); + additionalPropertiesObj.put("context", WSDL_FILE_MALFORMED_API_CONTEXT); + additionalPropertiesObj.put("version", API_VERSION); + additionalPropertiesObj.put("policies", policies); + additionalPropertiesObj.put("endpointConfig", endpointConfig); + + // Create API by importing the WSDL definition as .wsdl file + String wsdlDefinitionPath = FrameworkPathUtil.getSystemResourceLocation() + "wsdl" + + File.separator + "Sample.wsdl"; + File file = new File(wsdlDefinitionPath); + + try{ + APIDTO wsdlFileApidto = restAPIPublisher.importWSDLSchemaDefinition(file, null, + additionalPropertiesObj.toString(), "SOAP"); + } catch (ApiException e) { + Assert.assertEquals(e.getCode(), Response.Status.BAD_REQUEST.getStatusCode()); + Assert.assertTrue(e.getResponseBody().contains(APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR)); + + } + } + @Test(groups = {"wso2.am"}, description = "Get WSDL API definition of the created API", dependsOnMethods = "testWsdlDefinitionImport") public void testGetWsdlDefinitions() throws Exception { diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java index 8675067918..300d905fb9 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java @@ -149,6 +149,31 @@ public void testCreateAnAPIThroughThePublisherRest() throws Exception { assertTrue(response.getData().contains("lastUpdatedTimestamp"), "Last Updated Timestamp is not available"); } + @Test(groups = { + "wso2.am" }, description = "Create an API Through the Publisher Rest API with malformed context") + public void testCreateAnAPIWithMalformedContextThroughThePublisherRest() + throws Exception { + + // Now APIs with malformed context should not be allowed to create + String apiContextTest = "apim18PublisherTestAPIMalformed`"; + String apiDescription = "This is Test API Created by API Manager Integration Test"; + String apiTag = "tag18-4, tag18-5, tag18-6"; + String apiName = "APIM18PublisherTestMalformed"; + + APIRequest apiCreationRequestBean; + apiCreationRequestBean = new APIRequest(apiName, apiContextTest, new URL(apiProductionEndPointUrl)); + + apiCreationRequestBean.setVersion(apiVersion); + apiCreationRequestBean.setDescription(apiDescription); + apiCreationRequestBean.setTags(apiTag); + apiCreationRequestBean.setTier("Gold"); + + HttpResponse response = restAPIPublisher.addAPIWithMalformedContext(apiCreationRequestBean); + Assert.assertNotNull(response, "Response cannot be null"); + Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); + + } + @Test(groups = {"wso2.am"}, description = "Remove an API Through the Publisher Rest API", dependsOnMethods = "testCreateAnAPIThroughThePublisherRest") public void testRemoveAnAPIThroughThePublisherRest() throws Exception { diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java index cc84def1d0..7f451c516a 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java @@ -101,6 +101,7 @@ enum AUTH_IN { QUERY } private final String apiName = "WebSocketAPI"; + private final String apiNameWithMalformedContext = "WebSocketAPIWithMalformedContext"; private final String applicationName = "WebSocketApplication"; private final String applicationJWTName = "WebSocketJWTTypeApplication"; private final String testMessage = "Web Socket Test Message"; @@ -467,6 +468,30 @@ public void testWebSocketAPIInvalidTokenInvocation() throws Exception { } } + @Test(description = "Create WebSocket API with malformed context", + dependsOnMethods = "testWebSocketAPIRemoveEndpoint") + public void testCreateWebSocketAPIWithMalformedContext() throws Exception { + + provider = user.getUserName(); + String apiContext = "echo{version}"; + String apiVersion = "1.0.0"; + + URI endpointUri = new URI("ws://" + webSocketServerHost + ":" + webSocketServerPort); + + //Create the api creation request object + apiRequest = new APIRequest(apiNameWithMalformedContext, apiContext, endpointUri, endpointUri); + apiRequest.setVersion(apiVersion); + apiRequest.setTiersCollection(APIMIntegrationConstants.API_TIER.ASYNC_UNLIMITED); + apiRequest.setProvider(provider); + apiRequest.setType("WS"); + apiRequest.setApiTier(APIMIntegrationConstants.API_TIER.UNLIMITED); + + HttpResponse response = restAPIPublisher.addAPIWithMalformedContext(apiRequest); + Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); + + + } + /** * Wait for client to receive reply from the server * From e84927a4de3e43f7eae2f261f9a91cff221af5f4 Mon Sep 17 00:00:00 2001 From: dulith Date: Tue, 30 Jan 2024 17:39:57 +0530 Subject: [PATCH 2/3] Remove empty lines --- .../wso2/am/integration/test/impl/RestAPIPublisherImpl.java | 2 -- .../tests/apiproduct/APIProductCreationTestCase.java | 3 --- .../org/wso2/am/integration/tests/graphql/GraphqlTestCase.java | 1 - .../wso2/am/integration/tests/other/WSDLImportTestCase.java | 1 - .../APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java | 1 - .../am/integration/tests/websocket/WebSocketAPITestCase.java | 2 -- 6 files changed, 10 deletions(-) diff --git a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java index a110a35461..13b1621d03 100644 --- a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java +++ b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/impl/RestAPIPublisherImpl.java @@ -275,7 +275,6 @@ public HttpResponse addAPIWithMalformedContext(APIRequest apiRequest) { return response; } return null; - } /** @@ -1263,7 +1262,6 @@ public HttpResponse importGraphqlSchemaDefinitionWithInvalidContext(File file, S return response; } - public WSDLValidationResponseDTO validateWsdlDefinition(String url, File wsdlDefinition) throws ApiException { ApiResponse response = validationApi .validateWSDLDefinitionWithHttpInfo(url, wsdlDefinition); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java index 46373bddfc..09f520f769 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java @@ -327,11 +327,9 @@ public void testCreateApiProductWithMalformedContext() throws Exception { Assert.assertEquals(e.getCode(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertTrue(e.getResponseBody().contains( APIMIntegrationConstants.API_PRODUCT_CONTEXT_MALFORMED_ERROR)); - } } - @Test(groups = {"wso2.am"}, description = "Test creation and invocation of API Product which depends " + "on a visibility restricted API") public void testCreateAndInvokeApiProductWithVisibilityRestrictedApi() throws Exception { @@ -817,7 +815,6 @@ public void cleanUpArtifacts() throws Exception { restAPIPublisher.deleteAPI(apiID1); restAPIPublisher.deleteAPI(apiID2); - super.cleanUp(); userManagementClient.deleteUser(RESTRICTED_SUBSCRIBER); userManagementClient.deleteUser(STANDARD_SUBSCRIBER); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java index f08d29d4e8..b928ca47d2 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java @@ -225,7 +225,6 @@ public void testCreateAndPublishGraphQLAPIUsingSchemaWithMalformedContext() thro response = restAPIPublisher.importGraphqlSchemaDefinitionWithInvalidContext(file, additionalPropertiesObj.toString()); Assert.assertNotNull(response, "Response cannot be null"); Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); - } @Test(groups = {"wso2.am"}, description = "test retrieve schemaDefinition at publisher") diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java index 8097503f57..c6f24bc96d 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/other/WSDLImportTestCase.java @@ -289,7 +289,6 @@ public void testWsdlDefinitionImportWithMalformedContext() throws Exception { } catch (ApiException e) { Assert.assertEquals(e.getCode(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertTrue(e.getResponseBody().contains(APIMIntegrationConstants.API_CONTEXT_MALFORMED_ERROR)); - } } diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java index 300d905fb9..f30fbb8ef5 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java @@ -171,7 +171,6 @@ public void testCreateAnAPIWithMalformedContextThroughThePublisherRest() HttpResponse response = restAPIPublisher.addAPIWithMalformedContext(apiCreationRequestBean); Assert.assertNotNull(response, "Response cannot be null"); Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); - } @Test(groups = {"wso2.am"}, description = "Remove an API Through the Publisher Rest API", diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java index 7f451c516a..efe8f8b52a 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java @@ -488,8 +488,6 @@ public void testCreateWebSocketAPIWithMalformedContext() throws Exception { HttpResponse response = restAPIPublisher.addAPIWithMalformedContext(apiRequest); Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); - - } /** From a31b2d2b6166588d5fbdbd3331fdc42466a9070c Mon Sep 17 00:00:00 2001 From: dulith Date: Wed, 31 Jan 2024 09:20:45 +0530 Subject: [PATCH 3/3] Add constants --- .../utils/base/APIMIntegrationConstants.java | 9 +++++++++ .../tests/graphql/GraphqlTestCase.java | 20 +++++++++---------- ...APIThroughThePublisherRestAPITestCase.java | 4 ++-- .../tests/websocket/WebSocketAPITestCase.java | 3 ++- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java index b48fed1dfb..a1f561ddbf 100644 --- a/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java +++ b/modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/am/integration/test/utils/base/APIMIntegrationConstants.java @@ -42,6 +42,15 @@ public class APIMIntegrationConstants { public static final String API_RESPONSE_ELEMENT_NAME_APIS = "apis"; public static final String API_RESPONSE_ELEMENT_NAME_ID = "id"; + public static final String API_NAME = "name"; + public static final String API_CONTEXT = "context"; + public static final String API_VERSION = "version"; + public static final String ENDPOINT_TYPE = "endpoint_type"; + public static final String SANDBOX_ENDPOINTS = "sandbox_endpoints"; + public static final String PRODUCTION_ENDPOINTS = "production_endpoints"; + public static final String ENDPOINT_CONFIG = "endpointConfig"; + public static final String POLICIES = "policies"; + public static final String OPERATIONS = "operations"; public static final String API_CONTEXT_MALFORMED_ERROR = "The API context is malformed"; public static final String API_PRODUCT_CONTEXT_MALFORMED_ERROR = "The API Product context is malformed"; diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java index b928ca47d2..7fccde5a77 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/graphql/GraphqlTestCase.java @@ -204,22 +204,22 @@ public void testCreateAndPublishGraphQLAPIUsingSchemaWithMalformedContext() thro HttpResponse response = null; ArrayList policies = new ArrayList(); - policies.add("Unlimited"); + policies.add(APIMIntegrationConstants.API_TIER.UNLIMITED); JSONObject additionalPropertiesObj = new JSONObject(); - additionalPropertiesObj.put("name", "GraphQLAPIWithInvalidContext"); - additionalPropertiesObj.put("context", "invalidContext{version}"); - additionalPropertiesObj.put("version", API_VERSION_1_0_0); + additionalPropertiesObj.put(APIMIntegrationConstants.API_NAME, "GraphQLAPIWithInvalidContext"); + additionalPropertiesObj.put(APIMIntegrationConstants.API_CONTEXT, "invalidContext{version}"); + additionalPropertiesObj.put(APIMIntegrationConstants.API_VERSION, API_VERSION_1_0_0); JSONObject url = new JSONObject(); url.put("url", END_POINT_URL); JSONObject endpointConfig = new JSONObject(); - endpointConfig.put("endpoint_type", "http"); - endpointConfig.put("sandbox_endpoints", url); - endpointConfig.put("production_endpoints", url); - additionalPropertiesObj.put("endpointConfig", endpointConfig); - additionalPropertiesObj.put("policies", policies); - additionalPropertiesObj.put("operations", operations); + endpointConfig.put(APIMIntegrationConstants.ENDPOINT_TYPE, "http"); + endpointConfig.put(APIMIntegrationConstants.SANDBOX_ENDPOINTS, url); + endpointConfig.put(APIMIntegrationConstants.PRODUCTION_ENDPOINTS, url); + additionalPropertiesObj.put(APIMIntegrationConstants.ENDPOINT_CONFIG, endpointConfig); + additionalPropertiesObj.put(APIMIntegrationConstants.POLICIES, policies); + additionalPropertiesObj.put(APIMIntegrationConstants.OPERATIONS, operations); // create Graphql API response = restAPIPublisher.importGraphqlSchemaDefinitionWithInvalidContext(file, additionalPropertiesObj.toString()); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java index f30fbb8ef5..09122d34c1 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/publisher/APIM18CreateAnAPIThroughThePublisherRestAPITestCase.java @@ -166,11 +166,11 @@ public void testCreateAnAPIWithMalformedContextThroughThePublisherRest() apiCreationRequestBean.setVersion(apiVersion); apiCreationRequestBean.setDescription(apiDescription); apiCreationRequestBean.setTags(apiTag); - apiCreationRequestBean.setTier("Gold"); + apiCreationRequestBean.setTier(APIMIntegrationConstants.API_TIER.GOLD); HttpResponse response = restAPIPublisher.addAPIWithMalformedContext(apiCreationRequestBean); Assert.assertNotNull(response, "Response cannot be null"); - Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); + Assert.assertEquals(response.getResponseCode(), Response.Status.BAD_REQUEST.getStatusCode(), "Response Code miss matched when creating the API"); } @Test(groups = {"wso2.am"}, description = "Remove an API Through the Publisher Rest API", diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java index efe8f8b52a..aa2405017b 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/websocket/WebSocketAPITestCase.java @@ -72,6 +72,7 @@ import org.wso2.carbon.integration.common.utils.mgt.ServerConfigurationManager; import org.wso2.carbon.utils.xml.StringUtils; +import javax.ws.rs.core.Response; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -487,7 +488,7 @@ public void testCreateWebSocketAPIWithMalformedContext() throws Exception { apiRequest.setApiTier(APIMIntegrationConstants.API_TIER.UNLIMITED); HttpResponse response = restAPIPublisher.addAPIWithMalformedContext(apiRequest); - Assert.assertEquals(response.getResponseCode(), 400, "Response Code miss matched when creating the API"); + Assert.assertEquals(response.getResponseCode(), Response.Status.BAD_REQUEST.getStatusCode(), "Response Code miss matched when creating the API"); } /**