use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIConfigContextTestCase method testAPINameWithNumber.
@Test
public void testAPINameWithNumber() throws APITemplateException {
String apiId = UUID.randomUUID().toString();
API nameWithNumberAPI = new API.APIBuilder("provider", "1111testapi", "1.0.0").id(apiId).context("testcontext").build();
APIConfigContext apiConfigContext = new APIConfigContext(nameWithNumberAPI, "org.test");
apiConfigContext.validate();
String actualServiceName = (String) apiConfigContext.getContext().get("serviceName");
Assert.assertEquals(actualServiceName, "prefix_1111testapi_" + apiId.replaceAll("-", "_"));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForPrototypeScriptAPI.
@Override
public String getConfigStringForPrototypeScriptAPI(Environment environment) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new TransportConfigContext(configcontext, api);
configcontext = new ResourceConfigContext(configcontext, api);
configcontext = new EndpointBckConfigContext(configcontext, api);
configcontext = new EndpointConfigContext(configcontext, api);
configcontext = new SecurityConfigContext(configcontext, api);
configcontext = new JwtConfigContext(configcontext);
configcontext = new ResponseCacheConfigContext(configcontext, api);
configcontext = new HandlerConfigContex(configcontext, handlers);
configcontext = new EnvironmentConfigContext(configcontext, environment);
configcontext = new TemplateUtilContext(configcontext);
// @todo: this validation might be better to do when the builder is initialized.
configcontext.validate();
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
/* first, initialize velocity engine */
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
initVelocityEngine(velocityengine);
Template t = velocityengine.getTemplate(this.getPrototypeTemplatePath());
t.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForEndpointTemplate.
/**
* Sets the necessary variables to velocity context.
*
* @param endpointType Type of the endpoint : production or sandbox
* @return The string of endpoint file content
* @throws APITemplateException Thrown if an error occurred
*/
@Override
public String getConfigStringForEndpointTemplate(String endpointType) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new EndpointConfigContext(configcontext, this.apiProduct, api);
configcontext = new TemplateUtilContext(configcontext);
configcontext.validate();
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
initVelocityEngine(velocityengine);
context.put("type", endpointType);
Template template = velocityengine.getTemplate(this.getEndpointTemplatePath());
template.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error");
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPIProduct.
@Test
public void testResourceConfigContextForAPIProduct() throws Exception {
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier("admin", "TestAPIProduct", "1.0.0", UUID.randomUUID().toString());
APIProduct apiProduct = new APIProduct(apiProductIdentifier);
apiProduct.setProductResources(getAPIProductResources(apiProductIdentifier));
apiProduct.setType(APIConstants.API_PRODUCT);
ConfigContext configcontext = new APIConfigContext(apiProduct);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, apiProduct);
resourceConfigContext.validate();
VelocityContext context = resourceConfigContext.getContext();
Assert.assertNotNull(context.get("apiType"));
Assert.assertEquals(context.get("apiType"), APIConstants.API_PRODUCT);
Object aggregates = context.get("aggregates");
Assert.assertNotNull(aggregates);
Assert.assertTrue(aggregates instanceof List);
List<APIProductResource> apiProductResources = (List<APIProductResource>) aggregates;
Assert.assertTrue(assertAPIProductResourceList(apiProduct.getProductResources(), apiProductResources));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext in project carbon-apimgt by wso2.
the class SecurityConfigContextTest method testSecurityConfigContextForAPIProduct.
@Test
public void testSecurityConfigContextForAPIProduct() throws Exception {
APIProduct apiProduct = new APIProduct(new APIProductIdentifier("admin", "TestProduct", "1.0.0"));
apiProduct.setUuid(UUID.randomUUID().toString());
String apiid = UUID.randomUUID().toString();
List<APIProductResource> apiProductResourceList = new ArrayList<>();
APIProductResource apiProductResource = new APIProductResource();
apiProductResource.setApiIdentifier(new APIIdentifier("admin_api1_v1"));
apiProductResource.setApiId(apiid);
Map<String, EndpointSecurity> endpointSecurityMap = new HashMap<>();
EndpointSecurity endpointSecurity = new EndpointSecurity();
endpointSecurity.setType("BASIC");
endpointSecurity.setUsername("admin");
endpointSecurity.setPassword("admin123");
endpointSecurity.setEnabled(true);
endpointSecurityMap.put("production", endpointSecurity);
apiProductResource.setApiId(apiid);
apiProductResource.setEndpointSecurityMap(endpointSecurityMap);
apiProductResourceList.add(apiProductResource);
apiProduct.setProductResources(apiProductResourceList);
ConfigContext configcontext = new APIConfigContext(apiProduct);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
Map<String, APIDTO> apidtoMap = new HashMap<>();
apidtoMap.put(apiid, new APIDTO().name("api1").version("v1").provider("admin"));
SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, apiProduct, apiManagerConfiguration, apidtoMap);
securityConfigContext.validate();
VelocityContext velocityContext = securityConfigContext.getContext();
Assert.assertNotNull(velocityContext.get("endpoint_security"));
Map<String, Map<String, EndpointSecurityModel>> endpointSecurityModelMap = (Map<String, Map<String, EndpointSecurityModel>>) velocityContext.get("endpoint_security");
Map<String, EndpointSecurityModel> endpointSecurityModelMap1 = endpointSecurityModelMap.get(apiProductResource.getApiId());
EndpointSecurityModel production = endpointSecurityModelMap1.get("production");
Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("basic"));
Assert.assertTrue("Property username does not match.", "admin".equals(production.getUsername()));
Assert.assertTrue("Property base64value does not match. ", new String(Base64.encodeBase64("admin:admin123".getBytes())).equalsIgnoreCase(production.getBase64EncodedPassword()));
Assert.assertTrue("Property securevault_alias does not match.", "TestProduct--v1.0.0--api1--vv1--production".equalsIgnoreCase(production.getAlias()));
Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
}
Aggregations