Skip to content

Commit

Permalink
- junit tests for graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Dec 1, 2023
1 parent ecebfc3 commit 520bb77
Show file tree
Hide file tree
Showing 14 changed files with 7,531 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ public API importBackendAPI(API api, String backendBasePath) throws AppException
}
}

private JsonNode importFromWSDL(API api) throws IOException {
public JsonNode importFromWSDL(API api) throws IOException {
String completeWsdlUrl;
if (api.getApiDefinition().getApiSpecificationFile().endsWith(".url")) {
completeWsdlUrl = Utils.getAPIDefinitionUriFromFile(api.getApiDefinition().getApiSpecificationFile());
Expand Down Expand Up @@ -845,7 +845,7 @@ private JsonNode importFromWSDL(API api) throws IOException {
}
}

private JsonNode importFromSwagger(API api) throws AppException {
public JsonNode importFromSwagger(API api) throws AppException {
HttpEntity entity = MultipartEntityBuilder.create()
.addTextBody("name", api.getName(), ContentType.create(CONTENT_TYPE, StandardCharsets.UTF_8))
.addTextBody("type", "swagger")
Expand Down Expand Up @@ -878,7 +878,7 @@ public JsonNode createBackend(HttpEntity entity, API api) throws AppException {
public JsonNode importGraphql(API api, String backendBasePath) throws AppException {
HttpEntity entity = MultipartEntityBuilder.create()
.addTextBody("name", api.getName(), ContentType.create(CONTENT_TYPE, StandardCharsets.UTF_8))
.addTextBody("type", "swagger")
.addTextBody("type", "graphql")
.addBinaryBody("file", api.getApiDefinition().getApiSpecificationContent(), ContentType.create("application/octet-stream"), FILENAME)
.addTextBody("fileName", "XYZ").addTextBody(ORGANIZATION_ID, api.getOrganization().getId(), ContentType.create(CONTENT_TYPE, StandardCharsets.UTF_8))
.addTextBody("backendUrl", backendBasePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum APISpecType {
"Please note: You need to use the OData-Routing policy for this API. See: https://github.com/Axway-API-Management-Plus/odata-routing-policy"),
ODATA_V3("OData V4", METADATA),
ODATA_V4("OData V4", METADATA),
GRAPHQL("Graphql", "graphql"),
GRAPHQL("Graphql", ".graphqls"),
UNKNOWN("Unknown", ".txt");

final String niceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public static APISpecification getAPISpecification(byte[] apiSpecificationConten


public static APISpecification getAPISpecification(byte[] apiSpecificationContent, String apiDefinitionFile, String apiName, boolean failOnError, boolean logDetectedVersion) throws AppException {
List<APISpecification> specificationTypes = Arrays.asList(new Swagger2xSpecification(), new Swagger1xSpecification(),
new OAS3xSpecification(), new WSDLSpecification(), new GraphqlSpecification(), new WADLSpecification(), new ODataV2Specification(),
List<APISpecification> specificationTypes = Arrays.asList(new OAS3xSpecification(), new Swagger2xSpecification(), new Swagger1xSpecification(),
new GraphqlSpecification(), new WSDLSpecification(), new WADLSpecification(), new ODataV2Specification(),
new ODataV3Specification(), new ODataV4Specification());
if (LOG.isDebugEnabled()) {
LOG.debug("Handle API-Specification: {} , apiDefinitionFile: {} , API Name : {} ", getContentStart(apiSpecificationContent), apiDefinitionFile, apiName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.axway.apim.lib.CoreParameters;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.utils.Utils;
import com.fasterxml.jackson.databind.JsonNode;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -654,4 +655,138 @@ public void isFrontendApiNotExists(){
}


@Test
public void importFromWSDL() throws IOException {
API api = new API();
api.setName("wsdl");
api.setApiDefinition(new APISpecification() {

public String getApiSpecificationFile() {
return "https://localhost/services/test.wsdl";
}

@Override
public byte[] getApiSpecificationContent() {
return "test".getBytes();
}

@Override
public void updateBasePath(String basePath, String host) {

}

@Override
public void configureBasePath(String backendBasePath, API api) throws AppException {

}

@Override
public String getDescription() {
return null;
}

@Override
public APISpecType getAPIDefinitionType() throws AppException {
return null;
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
return false;
}
});
Organization organization = new Organization();
organization.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");
api.setOrganization(organization);
JsonNode jsonNode = apiManagerAPIAdapter.importFromWSDL(api);
System.out.println(jsonNode);
Assert.assertEquals("Test-App-API1-2285", jsonNode.get("name").asText());
}

@Test
public void importFromSwagger() throws AppException {
API api = new API();
api.setName("Test-App-API1-2285");
api.setApiDefinition(new APISpecification() {
@Override
public byte[] getApiSpecificationContent() {
return "test".getBytes();
}

@Override
public void updateBasePath(String basePath, String host) {

}

@Override
public void configureBasePath(String backendBasePath, API api) throws AppException {

}

@Override
public String getDescription() {
return null;
}

@Override
public APISpecType getAPIDefinitionType() throws AppException {
return null;
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
return false;
}
});
Organization organization = new Organization();
organization.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");
api.setOrganization(organization);
JsonNode jsonNode = apiManagerAPIAdapter.importFromSwagger(api);
Assert.assertEquals("Test-App-API1-2285", jsonNode.get("name").asText());
}


@Test
public void importGraphql() throws AppException {
API api = new API();
api.setName("graph");
api.setApiDefinition(new APISpecification() {
@Override
public byte[] getApiSpecificationContent() {
return "test".getBytes();
}

@Override
public void updateBasePath(String basePath, String host) {

}

@Override
public void configureBasePath(String backendBasePath, API api) throws AppException {

}

@Override
public String getDescription() {
return null;
}

@Override
public APISpecType getAPIDefinitionType() throws AppException {
return null;
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
return false;
}
});
Organization organization = new Organization();
organization.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");
api.setOrganization(organization);
JsonNode jsonNode = apiManagerAPIAdapter.importGraphql(api, "https://localhost/graphql");
Assert.assertEquals("graph", jsonNode.get("name").asText());
}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
package com.axway.apim.api.specification;

import com.axway.apim.WiremockWrapper;
import com.axway.apim.adapter.APIManagerAdapter;
import com.axway.apim.adapter.apis.APIManagerAPIAdapter;
import com.axway.apim.adapter.apis.APIManagerOrganizationAdapter;
import com.axway.apim.lib.CoreParameters;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.utils.Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.http.util.Asserts;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class APISpecificationFactoryTest {
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class APISpecificationFactoryTest extends WiremockWrapper {


@BeforeClass
public void init() {
initWiremock();
}

@AfterClass
public void close() {
super.close();
}

ClassLoader classLoader = this.getClass().getClassLoader();

@Test
Expand All @@ -15,6 +42,7 @@ public void getAPISpecificationOpenApi() throws AppException {
Assert.assertNotNull(apiSpecification.getDescription());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationSwagger2() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
Expand All @@ -23,47 +51,54 @@ public void getAPISpecificationSwagger2() throws AppException {
Assert.assertNotNull(apiSpecification.getDescription());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationWADL() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("sample-accounts-api.wadl", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("WADL_API"), apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationWSDL() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("sample.wsdl", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("WSDL_API"), apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationOdataV2() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("ODataV2NorthWindMetadata.xml", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("ODATA_V2"), apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test(expectedExceptions = AppException.class)
public void getAPISpecificationOdataV3() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec/odata").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("ODataV3ODataDemoMetadata.xml", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("ODATA_V3"), apiSpecification.getAPIDefinitionType());
}

@Test
public void getAPISpecificationOdataV4() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec/odata").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("ODataV4TrippinServiceMetadata.xml", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("ODATA_V4"), apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationSwagger12() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("swagger12.json", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.SWAGGER_API_1X, apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationSwagger11() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
Expand All @@ -88,22 +123,35 @@ public void getAPISpecificationUnknown() throws AppException {
}

@Test
public void downloadOpenApi(){

public void downloadOpenApi() throws IOException{
try(InputStream inputStream = APISpecificationFactory.getAPIDefinitionFromURL("https://localhost:8075/openapi.json")) {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> json = objectMapper.readValue(inputStream, Map.class);
Assert.assertEquals((String)json.get("openapi"), "3.0.2");
}
}

@Test
public void downloadWsdl(){

public void downloadWsdl() throws IOException{
try(InputStream inputStream = APISpecificationFactory.getAPIDefinitionFromURL("https://localhost:8075/sample.wsdl")) {
String content = IOUtils.toString(inputStream, "UTF-8");
Assert.assertTrue(content.contains("CustomBinding_MNBArfolyamServiceSoap"));
}
}

@Test
public void downloadGraphql(){

public void downloadGraphql() throws IOException{
try(InputStream inputStream = APISpecificationFactory.getAPIDefinitionFromURL("https://localhost:8075/graphql/starwars.graphqls")) {
String content = IOUtils.toString(inputStream, "UTF-8");
Assert.assertTrue(content.contains("schema {"));
}
}

@Test
public void downloadGraphqlFromIntrospection(){

public void downloadGraphqlFromIntrospection() throws IOException {
try(InputStream inputStream = APISpecificationFactory.getAPIDefinitionFromURL("https://localhost:8075/graphql/introspection")) {
String content = IOUtils.toString(inputStream, "UTF-8");
Assert.assertTrue(content.contains("schema {"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"id": "24e302a8-630b-4dec-b378-e6a0e441872e",
"name": "graph",
"summary": "",
"description": "",
"version": "",
"basePath": "https://swapi-graphql.netlify.app/.netlify/functions/index",
"resourcePath": "/",
"models": {},
"consumes": [],
"produces": [],
"integral": true,
"createdOn": 1701453313780,
"createdBy": "f685545f-4f9d-4cf0-b1ac-7589e4eb7067",
"organizationId": "116c7ff9-f8d1-40de-bf17-ff095d416799",
"serviceType": "graphql",
"hasOriginalDefinition": true,
"importUrl": "https://swapi-graphql.netlify.app/.netlify/functions/index",
"properties": {
"ResourceUri": "https://swapi-graphql.netlify.app/.netlify/functions/index",
"ResourceIdentifier": "VD4BA7RRQO4QJJAOAQW2J23VGKSXCFMH",
"ResourceType": "graphql"
}
}
Loading

0 comments on commit 520bb77

Please sign in to comment.