Search in sources :

Example 6 with APIConfigContext

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

Example 7 with APIConfigContext

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) VelocityContext(org.apache.velocity.VelocityContext) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.api.RegistryException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException)

Example 8 with APIConfigContext

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.api.RegistryException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 9 with APIConfigContext

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));
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) List(java.util.List) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Example 10 with APIConfigContext

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));
}
Also used : SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) EndpointSecurity(org.wso2.carbon.apimgt.api.model.EndpointSecurity) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) EndpointSecurityModel(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 APIConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)16 ConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext)16 API (org.wso2.carbon.apimgt.api.model.API)14 VelocityContext (org.apache.velocity.VelocityContext)12 HashMap (java.util.HashMap)9 API (org.wso2.carbon.apimgt.core.models.API)8 Map (java.util.Map)7 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)7 ArrayList (java.util.ArrayList)6 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 EndpointSecurityModel (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel)6 SecurityConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext)6 StringWriter (java.io.StringWriter)5 Template (org.apache.velocity.Template)5 VelocityEngine (org.apache.velocity.app.VelocityEngine)5 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)4