Search in sources :

Example 6 with APIConfigContext

use of org.wso2.carbon.apimgt.core.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.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class APIConfigContextTestCase method testWithNullParameters.

@Test(expectedExceptions = APITemplateException.class)
public void testWithNullParameters() throws APITemplateException {
    API emptyNamedAPI = new API.APIBuilder(null, null, "1.0.0").context("testcontext").id(UUID.randomUUID().toString()).build();
    APIConfigContext apiConfigContext = new APIConfigContext(emptyNamedAPI, "org.test");
    apiConfigContext.validate();
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 8 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class APIConfigContextTestCase method testWithEmptyParameters.

@Test(expectedExceptions = APITemplateException.class)
public void testWithEmptyParameters() throws APITemplateException {
    API emptyNamedAPI = new API.APIBuilder("", "", "").context("testcontext").id(UUID.randomUUID().toString()).build();
    APIConfigContext apiConfigContext = new APIConfigContext(emptyNamedAPI, "org.test");
    apiConfigContext.validate();
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 9 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class APIConfigContextTestCase method testResourceConfigContext.

@Test
public void testResourceConfigContext() {
    APIConfigContext apiConfigContext = new APIConfigContext(SampleTestObjectCreator.createDefaultAPI().build(), "org.test");
    TemplateBuilderDTO templateBuilderDTO = new TemplateBuilderDTO();
    templateBuilderDTO.setTemplateId("t1");
    List<TemplateBuilderDTO> templateList = new ArrayList<TemplateBuilderDTO>();
    templateList.add(templateBuilderDTO);
    ResourceConfigContext resourceConfigContext = new ResourceConfigContext(apiConfigContext, templateList);
    List<TemplateBuilderDTO> templatesFromContext = (List<TemplateBuilderDTO>) resourceConfigContext.getContext().get("apiResources");
    String templateIdFromContext = templatesFromContext.get(0).getTemplateId();
    Assert.assertEquals(templateIdFromContext, "t1");
}
Also used : ArrayList(java.util.ArrayList) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 10 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class APIPublisherImpl method saveSwagger20Definition.

/**
 * {@inheritDoc}
 */
@Override
public void saveSwagger20Definition(String apiId, String jsonText) throws APIManagementException {
    try {
        LocalDateTime localDateTime = LocalDateTime.now();
        Map<String, String> oldScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(apiId));
        Map<String, String> newScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(jsonText);
        Map<String, String> updatedScopes = new HashMap<>(newScopes);
        updatedScopes.keySet().retainAll(oldScopes.keySet());
        oldScopes.keySet().removeAll(updatedScopes.keySet());
        newScopes.keySet().removeAll(updatedScopes.keySet());
        for (Map.Entry<String, String> scopeEntry : newScopes.entrySet()) {
            getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
        }
        for (Map.Entry<String, String> scopeEntry : oldScopes.entrySet()) {
            getKeyManager().deleteScope(scopeEntry.getKey());
        }
        for (Map.Entry<String, String> scopeEntry : updatedScopes.entrySet()) {
            Scope scope = getKeyManager().retrieveScope(scopeEntry.getKey());
            scope.setDescription(scopeEntry.getValue());
            getKeyManager().updateScope(scope);
        }
        API api = getAPIbyUUID(apiId);
        Map<String, UriTemplate> oldUriTemplateMap = api.getUriTemplates();
        List<APIResource> apiResourceList = apiDefinitionFromSwagger20.parseSwaggerAPIResources(new StringBuilder(jsonText));
        Map<String, UriTemplate> updatedUriTemplateMap = new HashMap<>();
        for (APIResource apiResource : apiResourceList) {
            updatedUriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
        }
        Map<String, UriTemplate> uriTemplateMapNeedTobeUpdate = APIUtils.getMergedUriTemplates(oldUriTemplateMap, updatedUriTemplateMap);
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.uriTemplates(uriTemplateMapNeedTobeUpdate);
        createUriTemplateList(apiBuilder, true);
        apiBuilder.updatedBy(getUsername());
        apiBuilder.lastUpdatedTime(localDateTime);
        api = apiBuilder.build();
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String existingGatewayConfig = getApiGatewayConfig(apiId);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(existingGatewayConfig, jsonText);
        getApiDAO().updateAPI(apiId, api);
        getApiDAO().updateApiDefinition(apiId, jsonText, getUsername());
        getApiDAO().updateGatewayConfig(apiId, updatedGatewayConfig, getUsername());
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't update the Swagger Definition";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashMap(java.util.HashMap) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Aggregations

API (org.wso2.carbon.apimgt.core.models.API)8 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)7 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 Test (org.testng.annotations.Test)4 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)3 TemplateBuilderDTO (org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO)3 StringWriter (java.io.StringWriter)2 LocalDateTime (java.time.LocalDateTime)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Template (org.apache.velocity.Template)2 VelocityContext (org.apache.velocity.VelocityContext)2 VelocityEngine (org.apache.velocity.app.VelocityEngine)2 ParseErrorException (org.apache.velocity.exception.ParseErrorException)2 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)2