use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class OASParserUtilTest method testGenerateOASConfigForHTTPEndpoints.
@Test
public void testGenerateOASConfigForHTTPEndpoints() throws Exception {
String endpoints = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "endpointsData.json"), "UTF-8");
JSONObject jsonObject = new JSONObject(endpoints);
APIIdentifier identifier = new APIIdentifier("provider", "name", "version");
API api = new API(identifier);
JsonNode prodNode;
JsonNode sandNode;
// start http production only
String httpProduction = jsonObject.getJSONObject("http_production").toString();
api.setEndpointConfig(httpProduction);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(1, prodNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNull(sandNode);
String httpProductionFailover = jsonObject.getJSONObject("http_production_failover").toString();
api.setEndpointConfig(httpProductionFailover);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNull(sandNode);
String httpProductionLoadbalance = jsonObject.getJSONObject("http_production_loadbalance").toString();
api.setEndpointConfig(httpProductionLoadbalance);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNull(sandNode);
// start http sandbox only
String httpSandbox = jsonObject.getJSONObject("http_sandbox").toString();
api.setEndpointConfig(httpSandbox);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNull(prodNode);
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(1, sandNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
String httpSandboxFailover = jsonObject.getJSONObject("http_sandbox_failover").toString();
api.setEndpointConfig(httpSandboxFailover);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNull(prodNode);
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
String httpSandboxLoadbalance = jsonObject.getJSONObject("http_sandbox_loadbalance").toString();
api.setEndpointConfig(httpSandboxLoadbalance);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNull(prodNode);
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
// start http hybrid
String httpHybrid = jsonObject.getJSONObject("http_hybrid").toString();
api.setEndpointConfig(httpHybrid);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(1, prodNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(1, sandNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
String httpHybridFailover = jsonObject.getJSONObject("http_hybrid_failover").toString();
api.setEndpointConfig(httpHybridFailover);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_FAILOVER, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
String httpHybridLoadbalance = jsonObject.getJSONObject("http_hybrid_loadbalance").toString();
api.setEndpointConfig(httpHybridLoadbalance);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(2, prodNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(2, sandNode.get(APIConstants.ENDPOINT_URLS).size());
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_LOADBALANCE, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class OASParserUtilTest method testGenerateOASConfigWithSecuredEndpoints.
@Test
public void testGenerateOASConfigWithSecuredEndpoints() throws Exception {
String endpoints = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "endpointsData.json"), "UTF-8");
JSONObject jsonObject = new JSONObject(endpoints);
APIIdentifier identifier = new APIIdentifier("provider", "name", "version");
API api = new API(identifier);
String endUserName = "end_user";
JsonNode prodNode;
JsonNode sandNode;
JsonNode securityConfig;
String defaultEndpoints = jsonObject.getJSONObject("default_endpoints").toString();
api.setEndpointSecured(true);
api.setEndpointConfig(defaultEndpoints);
api.setEndpointAuthDigest(false);
api.setEndpointUTUsername(endUserName);
// check default production endpoint security
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
// check default sandbox endpoint security
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, false);
Assert.assertNotNull(sandNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
// check default production endpoint digest auth security
api.setEndpointAuthDigest(true);
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_DEFAULT, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_DIGEST.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
// --------- check http endpoints security
String httpHybrid = jsonObject.getJSONObject("http_hybrid").toString();
api.setEndpointConfig(httpHybrid);
api.setEndpointAuthDigest(false);
api.setEndpointUTUsername(endUserName);
// check http production
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
// check http sandbox
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(sandNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_HTTP, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
// ----------- check address endpoints security
String soapHybrid = jsonObject.getJSONObject("soap_hybrid").toString();
api.setEndpointConfig(soapHybrid);
api.setEndpointAuthDigest(false);
api.setEndpointUTUsername(endUserName);
// check address production
prodNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(prodNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_ADDRESS, prodNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = prodNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
// check address sandbox
sandNode = OASParserUtil.generateOASConfigForEndpoints(api, true);
Assert.assertNotNull(sandNode);
Assert.assertEquals(APIConstants.ENDPOINT_TYPE_ADDRESS, sandNode.get(APIConstants.X_WSO2_ENDPOINT_TYPE).asText());
Assert.assertNotNull(sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG));
securityConfig = sandNode.get(APIConstants.ENDPOINT_SECURITY_CONFIG);
Assert.assertEquals(APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase(), securityConfig.get(APIConstants.ENDPOINT_SECURITY_TYPE).asText());
Assert.assertEquals(endUserName, securityConfig.get(APIConstants.ENDPOINT_SECURITY_USERNAME).asText());
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class OASTestBase method testGenerateAPIDefinition2.
public void testGenerateAPIDefinition2(APIDefinition parser, String content, OASParserEvaluator evaluator) throws Exception {
JSONObject jsonObject = new JSONObject(content);
String equalNoOfResources = jsonObject.getJSONObject("equalNoOfResources").toString();
APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
API api = new API(identifier);
api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope)));
api.setUriTemplates(new HashSet<>(Arrays.asList(petGet, petPost, itemGet, itemPost)));
String definition = parser.generateAPIDefinition(new SwaggerData(api), equalNoOfResources);
APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
Assert.assertTrue(response.isValid());
Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
Assert.assertEquals(4, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
Assert.assertTrue(uriTemplates.contains(petPost));
Assert.assertTrue(uriTemplates.contains(itemGet));
Assert.assertTrue(uriTemplates.contains(itemPost));
Set<Scope> scopes = parser.getScopes(definition);
Assert.assertEquals(2, scopes.size());
Assert.assertTrue(scopes.contains(sampleScope));
Assert.assertTrue(scopes.contains(extensionScope));
// Remove operation and path from API object
String extraResourcesInDefinition = jsonObject.getJSONObject("extraResourcesInDefinition").toString();
api.setUriTemplates(new HashSet<>(Arrays.asList(itemGet, itemPost)));
definition = parser.generateAPIDefinition(new SwaggerData(api), extraResourcesInDefinition);
response = parser.validateAPIDefinition(definition, false);
Assert.assertTrue(response.isValid());
Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
uriTemplates = parser.getURITemplates(definition);
Assert.assertEquals(2, uriTemplates.size());
// assert generated paths
if (evaluator != null) {
evaluator.eval(definition);
}
Iterator iterator = uriTemplates.iterator();
while (iterator.hasNext()) {
URITemplate element = (URITemplate) iterator.next();
if ("/pets".equalsIgnoreCase(element.getUriTemplate())) {
Assert.fail("Removed paths from API operation should not present.");
}
if ("/items".equalsIgnoreCase(element.getUriTemplate()) && "PUT".equalsIgnoreCase(element.getHTTPVerb())) {
Assert.fail("Removed item from API operation should not present.");
}
}
Assert.assertTrue(uriTemplates.contains(itemGet));
Assert.assertTrue(uriTemplates.contains(itemPost));
// Add operation and path to API object
String lessResourcesInDefinition = jsonObject.getJSONObject("lessResourcesInDefinition").toString();
api.setUriTemplates(new HashSet<>(Arrays.asList(petGet, petPost, itemGet, itemPost)));
definition = parser.generateAPIDefinition(new SwaggerData(api), lessResourcesInDefinition);
response = parser.validateAPIDefinition(definition, false);
Assert.assertTrue(response.isValid());
Assert.assertTrue(response.getParser().getClass().equals(parser.getClass()));
uriTemplates = parser.getURITemplates(definition);
Assert.assertEquals(4, uriTemplates.size());
Assert.assertTrue(uriTemplates.contains(petGet));
Assert.assertTrue(uriTemplates.contains(petPost));
Assert.assertTrue(uriTemplates.contains(itemGet));
Assert.assertTrue(uriTemplates.contains(itemPost));
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class AsyncApiParserTest method testGetAsyncAPIDefinitionForStoreFail.
@Test
public void testGetAsyncAPIDefinitionForStoreFail() throws Exception {
Map<String, String> hostsWithSchemes = new HashMap<>();
hostsWithSchemes.put("ws", "ws://localhost:9099");
hostsWithSchemes.put("wss", "wss://localhost:8099");
APIIdentifier identifier = new APIIdentifier("admin", "HelloServer2", "1.0");
API api = new API(identifier);
AsyncApiParser asyncApiParser = new AsyncApiParser();
String asyncAPIDefinition = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("definitions" + File.separator + "asyncAPI" + File.separator + "incorrectWebSocket.yml"), "UTF-8");
api.setAsyncApiDefinition(asyncAPIDefinition);
try {
asyncApiParser.getAsyncApiDefinitionForStore(api, asyncAPIDefinition, hostsWithSchemes);
} catch (RuntimeException e) {
Assert.assertNotNull(e);
}
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class AsyncApiParserTest method testGenerateAsyncAPIDefinition.
@Test
public void testGenerateAsyncAPIDefinition() throws Exception {
APIIdentifier identifier = new APIIdentifier("admin", "HelloServer", "1.0");
API api = new API(identifier);
api.setType("WS");
api.setContext("/hello/1.0");
api.setTransports("ws,wss");
api.setEndpointConfig("{'endpoint_type':'http','sandbox_endpoints':{'url':'wss://echo.websocket.org:443'}," + "'production_endpoints':{'url':'wss://echo.websocket.org:443'}}");
AsyncApiParser asyncApiParser = new AsyncApiParser();
String asyncAPIDefinition = asyncApiParser.generateAsyncAPIDefinition(api);
Assert.assertNotNull(asyncAPIDefinition);
}
Aggregations