use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.
@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new EndpointContext(endpoint, packageName);
VelocityContext context = configcontext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class EndpointBckConfigContextTest method testEndpointBckConfigContext.
@Test
public void testEndpointBckConfigContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
api.setUrl(url);
api.setSandboxUrl(url);
ConfigContext configcontext = new APIConfigContext(api);
EndpointBckConfigContext endpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
Assert.assertTrue(api.getEndpointConfig().contains(url));
// setting an empty string as the endpoint config and checking the value which is returned
api.setEndpointConfig("");
String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + api.getUrl() + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + api.getSandboxUrl() + "\"," + "\"config\":null},\"endpoint_type\":\"http\"}";
EndpointBckConfigContext secondEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
Assert.assertTrue(api.getEndpointConfig().contains(endpointConfig));
// setting null as the endpoint config and checking the value which is returned
api.setEndpointConfig(null);
EndpointBckConfigContext thirdEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
Assert.assertTrue(api.getEndpointConfig().contains(endpointConfig));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class EnvironmentConfigContextTest method testEnvironmentConfigContext.
@Test
public void testEnvironmentConfigContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + url + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"}";
api.setEndpointConfig(endpointConfig);
api.setUrl(url);
api.setSandboxUrl(url);
ConfigContext configcontext = new APIConfigContext(api);
Environment environment = new Environment();
environment.setType("production");
EnvironmentConfigContext environmentConfigContext = new EnvironmentConfigContext(configcontext, environment);
Assert.assertNotNull(environmentConfigContext.getContext().get("environment"));
Assert.assertNotNull(environmentConfigContext.getContext().get("environmentType"));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPI.
@Test
public void testResourceConfigContextForAPI() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
api.setUriTemplates(setAPIUriTemplates());
ConfigContext configcontext = new APIConfigContext(api);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, api);
resourceConfigContext.validate();
Assert.assertNotNull(resourceConfigContext.getContext().get("resources"));
// assign an empty URITemplate set and check the result
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
api.setUriTemplates(uriTemplates);
configcontext = new APIConfigContext(api);
resourceConfigContext = new ResourceConfigContext(configcontext, api);
String errorClass = "org.wso2.carbon.apimgt.api.APIManagementException";
String expectedErrorMessage = "At least one resource is required";
try {
resourceConfigContext.validate();
} catch (APIManagementException e) {
Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
}
// set a null value for URITemplate and check the result
api.setUriTemplates(null);
configcontext = new APIConfigContext(api);
resourceConfigContext = new ResourceConfigContext(configcontext, api);
try {
resourceConfigContext.validate();
} catch (APIManagementException e) {
Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class SecurityConfigContextTest method testSecurityConfigContextOauth.
@Test
public void testSecurityConfigContextOauth() throws Exception {
String json = "{\"endpoint_security\":{\n" + " \"production\":{\n" + " \"enabled\":true,\n" + " \"type\":\"oauth\",\n" + " \"clientId\":\"123-456\",\n" + " \"clientSecret\":\"admin\",\n" + " \"grantType\":\"client_credentials\"\n" + " },\n" + " \"sandbox\":{\n" + " \"enabled\":true,\n" + " \"type\":\"oauth\",\n" + " \"clientId\":\"123-4567\",\n" + " \"clientSecret\":\"admin\",\n" + " \"grantType\":\"client_credentials\"\n" + " }\n" + " }\n" + "}";
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setUuid(UUID.randomUUID().toString());
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
api.setTransports(Constants.TRANSPORT_HTTP);
api.setEndpointConfig(json);
ConfigContext configcontext = new APIConfigContext(api);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
securityConfigContext.validate();
VelocityContext velocityContext = securityConfigContext.getContext();
Assert.assertNotNull(velocityContext.get("endpoint_security"));
Map<String, EndpointSecurityModel> endpointSecurityModelMap = (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
EndpointSecurityModel production = endpointSecurityModelMap.get("production");
Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("oauth"));
Assert.assertTrue("Property clientid does not match.", "123-456".equals(production.getClientId()));
Assert.assertEquals(production.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--production");
EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("oauth"));
Assert.assertTrue("Property username does not match.", "123-4567".equals(sandbox.getClientId()));
Assert.assertEquals(sandbox.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--sandbox");
Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
}
Aggregations