Search in sources :

Example 6 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.

@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) Template(org.apache.velocity.Template)

Example 7 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException 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("-", "_"));
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 8 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class ThrottleTemplateBuilderTestCase method testSiddiQueryForApp.

@Test
public void testSiddiQueryForApp() throws APITemplateException {
    ApplicationPolicy policy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    ApplicationThrottlePolicyTemplateBuilder templateBuilder = new ApplicationThrottlePolicyTemplateBuilder(policy);
    String siddhiQuery = templateBuilder.getThrottlePolicyForAppLevel();
    String sampleQuery = SampleTestObjectCreator.createDefaultSiddhiAppforAppPolicy();
    Assert.assertEquals(siddhiQuery, sampleQuery);
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Test(org.testng.annotations.Test)

Example 9 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class ThrottleTemplateBuilderTestCase method testSiddhiQueryForAPIPolicy.

@Test
public void testSiddhiQueryForAPIPolicy() throws APITemplateException {
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    APIThrottlePolicyTemplateBuilder templateBuilder = new APIThrottlePolicyTemplateBuilder(apiPolicy);
    Map<String, String> siddhiQueryMap = templateBuilder.getThrottlePolicyTemplateForPipelines();
    String actualQuery = siddhiQueryMap.get("resource_SampleAPIPolicy_condition_0");
    String expectedQuery = SampleTestObjectCreator.createDefaultSiddhiAppForAPIThrottlePolicy();
    Assert.assertEquals(actualQuery, expectedQuery);
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 10 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class ThrottleTemplateBuilderTestCase method testSiddhiQueryForSubscriptionPolicy.

@Test
public void testSiddhiQueryForSubscriptionPolicy() throws APITemplateException {
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    SubscriptionThrottlePolicyTemplateBuilder templateBuilder = new SubscriptionThrottlePolicyTemplateBuilder(policy);
    String siddhiQuery = templateBuilder.getThrottlePolicyForSubscriptionLevel();
    String sampleQuery = SampleTestObjectCreator.createDefaultSiddhiAppforSubscriptionPolicy();
    Assert.assertEquals(siddhiQuery, sampleQuery);
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Test(org.testng.annotations.Test)

Aggregations

StringWriter (java.io.StringWriter)14 Template (org.apache.velocity.Template)14 VelocityContext (org.apache.velocity.VelocityContext)14 VelocityEngine (org.apache.velocity.app.VelocityEngine)14 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)11 HashMap (java.util.HashMap)10 Test (org.testng.annotations.Test)9 API (org.wso2.carbon.apimgt.core.models.API)6 VelocityException (org.apache.velocity.exception.VelocityException)5 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)5 RegistryException (org.wso2.carbon.registry.api.RegistryException)5 UserStoreException (org.wso2.carbon.user.api.UserStoreException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)4 GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)4 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)4 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)4 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3