Search in sources :

Example 16 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 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 17 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 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 18 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 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 19 with APIConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.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)

Example 20 with APIConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext in project carbon-apimgt by wso2.

the class APIStoreImpl method updateCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public void updateCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    apiBuilder.provider(getUsername());
    apiBuilder.updatedBy(getUsername());
    CompositeAPI originalAPI = getApiDAO().getCompositeAPI(apiBuilder.getId());
    apiBuilder.createdTime(originalAPI.getCreatedTime());
    // workflow status is an internal property and shouldn't be allowed to update externally
    apiBuilder.workflowStatus(originalAPI.getWorkflowStatus());
    APIUtils.verifyValidityOfApiUpdate(apiBuilder, originalAPI);
    try {
        String updatedSwagger = apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder);
        InputStream gatewayConfig = getApiDAO().getCompositeAPIGatewayConfig(apiBuilder.getId());
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(IOUtils.toString(gatewayConfig, StandardCharsets.UTF_8), updatedSwagger);
        CompositeAPI api = apiBuilder.build();
        if (originalAPI.getContext() != null && !originalAPI.getContext().equals(apiBuilder.getContext())) {
            if (isContextExist(api.getContext())) {
                throw new APIManagementException("Context already Exist", ExceptionCodes.API_ALREADY_EXISTS);
            }
        }
        // publishing config to gateway
        gateway.addCompositeAPI(api);
        getApiDAO().updateApiDefinition(api.getId(), updatedSwagger, api.getUpdatedBy());
        getApiDAO().updateCompositeAPIGatewayConfig(api.getId(), new ByteArrayInputStream(updatedGatewayConfig.getBytes(StandardCharsets.UTF_8)), api.getUpdatedBy());
        if (log.isDebugEnabled()) {
            log.debug("API " + api.getName() + "-" + api.getVersion() + " was updated successfully.");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (IOException e) {
        String errorMsg = "Error occurred while reading gateway configuration the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IOException(java.io.IOException) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

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