use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class SecurityConfigContextTest method testSecurityConfigContextPerEndpointSandbox.
@Test
public void testSecurityConfigContextPerEndpointSandbox() throws Exception {
String json = "{\"endpoint_security\":{\n" + " \"sandbox\":{\n" + " \"enabled\":true,\n" + " \"type\":\"DIGEST\",\n" + " \"username\":\"admin\",\n" + " \"password\":\"admin123#QA\"\n" + " }\n" + " }\n" + "}";
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
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 sandbox = endpointSecurityModelMap.get("sandbox");
Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("digest"));
Assert.assertTrue("Property username does not match.", "admin".equals(sandbox.getUsername()));
Assert.assertTrue("Property base64value does not match. ", new String(Base64.encodeBase64("admin:admin123#QA".getBytes())).equalsIgnoreCase(sandbox.getBase64EncodedPassword()));
Assert.assertTrue("Property securevault_alias does not match.", "TestAPI--v1.0.0--sandbox".equalsIgnoreCase(sandbox.getAlias()));
Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
EndpointSecurityModel production = endpointSecurityModelMap.get("production");
Assert.assertFalse("Property enabled cannot be true.", production.isEnabled());
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class TransportConfigContextTest method testTransportConfigContext.
@Test
public void testTransportConfigContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
api.setTransports(Constants.TRANSPORT_HTTP);
ConfigContext configcontext = new APIConfigContext(api);
TransportConfigContext transportConfigContext = new TransportConfigContext(configcontext, api);
transportConfigContext.validate();
Assert.assertTrue(Constants.TRANSPORT_HTTP.equalsIgnoreCase(transportConfigContext.getContext().get("transport").toString()));
api.setTransports(Constants.TRANSPORT_HTTP + "," + Constants.TRANSPORT_HTTPS);
configcontext = new APIConfigContext(api);
transportConfigContext = new TransportConfigContext(configcontext, api);
Assert.assertTrue(StringUtils.EMPTY.equalsIgnoreCase(transportConfigContext.getContext().get("transport").toString()));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForWebSocketEndpointTemplate.
@Override
public String getConfigStringForWebSocketEndpointTemplate(String endpointType, String resourceKey, String endpointUrl) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new EndpointBckConfigContext(configcontext, api);
configcontext = new EndpointConfigContext(configcontext, 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 + "_endpoints");
context.put("websocketResourceKey", resourceKey);
context.put("endpointUrl", endpointUrl);
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.ConfigContext in project carbon-apimgt by wso2.
the class RESTToSOAPMsgTemplate method getMappingOutSequence.
/**
* gets out sequence for the soap operation
*
* @return out sequence for the converted soap operation
*/
public String getMappingOutSequence() {
ConfigContext configcontext = new SOAPToRESTConfigContext();
StringWriter writer = new StringWriter();
try {
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.init();
org.apache.velocity.Template template = velocityengine.getTemplate(this.getOutSeqTemplatePath());
template.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.
the class TemplateUtilContextTest method testTemplateUtilContext.
@Test
public void testTemplateUtilContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
ConfigContext configcontext = new APIConfigContext(api);
TemplateUtilContext templateUtilContext = new TemplateUtilContext(configcontext);
String xmlSampleText = "<data>TemplateUtilContextTest Class</data>";
String xmlEscapedText = "<data>TemplateUtilContextTest Class</data>";
String result = templateUtilContext.escapeXml(xmlSampleText);
Assert.assertTrue("Failed to escape XML tags in the provided string : " + xmlSampleText, xmlEscapedText.equalsIgnoreCase(result));
Assert.assertNotNull(templateUtilContext.getContext().get("util"));
}
Aggregations