Search in sources :

Example 31 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class ResourcesApiServiceImplTestCase method resourcesGetTest.

@Test
public void resourcesGetTest() throws Exception {
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    ResourcesApiServiceImpl resourcesApiService = new ResourcesApiServiceImpl();
    UriTemplate uriTemplateOne = SampleTestObjectCreator.createUniqueUriTemplate();
    UriTemplate uriTemplateTwo = SampleTestObjectCreator.createUniqueUriTemplate();
    UriTemplate uriTemplateThree = SampleTestObjectCreator.createUniqueUriTemplate();
    List<UriTemplate> uriTemplateList = new ArrayList<>();
    uriTemplateList.add(uriTemplateOne);
    uriTemplateList.add(uriTemplateTwo);
    uriTemplateList.add(uriTemplateThree);
    Mockito.when(apiMgtAdminService.getAllResourcesForApi(API_CONTEXT, API_VERSION)).thenReturn(uriTemplateList);
    Response response = resourcesApiService.resourcesGet(API_CONTEXT, API_VERSION, null, getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertEquals(((ResourcesListDTO) response.getEntity()).getList().size(), 3);
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) ResourcesListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ResourcesListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 32 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate 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 33 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class APIPublisherImpl method createUriTemplateList.

private void createUriTemplateList(API.APIBuilder apiBuilder, boolean update) throws APIManagementException {
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    if (apiBuilder.getUriTemplates().isEmpty()) {
        apiBuilder.uriTemplates(APIUtils.getDefaultUriTemplates());
        apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
    }
    for (UriTemplate uriTemplate : apiBuilder.getUriTemplates().values()) {
        UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
        if (StringUtils.isEmpty(uriTemplateBuilder.getTemplateId())) {
            uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(uriTemplate.getUriTemplate(), uriTemplate.getHttpVerb()));
        }
        Map<String, Endpoint> endpointMap = uriTemplateBuilder.getEndpoint();
        validateEndpoints(endpointMap, update);
        validateApiPolicy(uriTemplateBuilder.getPolicy());
        uriTemplateMap.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
    }
    apiBuilder.uriTemplates(uriTemplateMap);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) HashMap(java.util.HashMap) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 34 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class APIPublisherImpl method getAPIbyUUID.

@Override
public API getAPIbyUUID(String uuid) throws APIManagementException {
    API api = null;
    try {
        api = super.getAPIbyUUID(uuid);
        if (api != null) {
            api.setUserSpecificApiPermissions(getAPIPermissionsOfLoggedInUser(getUsername(), api));
            String permissionString = api.getApiPermission();
            if (!StringUtils.isEmpty(permissionString)) {
                api.setApiPermission(replaceGroupIdWithName(permissionString));
            }
            if (!getScopesForApi(uuid).isEmpty()) {
                String swagger = getApiSwaggerDefinition(uuid);
                List<String> globalScopes = new APIDefinitionFromSwagger20().getGlobalAssignedScopes(swagger);
                List<APIResource> apiResourceList = new APIDefinitionFromSwagger20().parseSwaggerAPIResources(new StringBuilder(swagger));
                api.setScopes(globalScopes);
                for (APIResource apiResource : apiResourceList) {
                    if (apiResource.getUriTemplate().getScopes().isEmpty()) {
                        UriTemplate retrievedUriTemplateFromApi = api.getUriTemplates().get(apiResource.getUriTemplate().getTemplateId());
                        if (retrievedUriTemplateFromApi != null) {
                            UriTemplate.UriTemplateBuilder uriTemplate = new UriTemplate.UriTemplateBuilder(retrievedUriTemplateFromApi);
                            uriTemplate.scopes(apiResource.getScope());
                            api.getUriTemplates().replace(apiResource.getUriTemplate().getTemplateId(), uriTemplate.build());
                        }
                    }
                }
            }
        }
    } catch (ParseException e) {
        String errorMsg = "Error occurred while parsing the permission json string for API " + api.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.JSON_PARSE_ERROR);
    }
    return api;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) API(org.wso2.carbon.apimgt.core.models.API) ParseException(org.json.simple.parser.ParseException) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 35 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class APIStoreImpl method setUriTemplates.

private void setUriTemplates(CompositeAPI.Builder apiBuilder) {
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    if (apiBuilder.getUriTemplates() == null || apiBuilder.getUriTemplates().isEmpty()) {
        apiBuilder.uriTemplates(APIUtils.getDefaultUriTemplates());
        apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
    } else {
        for (UriTemplate uriTemplate : apiBuilder.getUriTemplates().values()) {
            UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
            if (StringUtils.isEmpty(uriTemplateBuilder.getTemplateId())) {
                uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(uriTemplate.getUriTemplate(), uriTemplate.getHttpVerb()));
            }
            uriTemplateMap.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
        }
        apiBuilder.uriTemplates(uriTemplateMap);
    }
}
Also used : HashMap(java.util.HashMap) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Aggregations

UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)38 HashMap (java.util.HashMap)30 API (org.wso2.carbon.apimgt.core.models.API)22 Test (org.testng.annotations.Test)15 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)13 ArrayList (java.util.ArrayList)11 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)10 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)8 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)8 Map (java.util.Map)7 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)7 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)7 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)7 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)7 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)6 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 Swagger (io.swagger.models.Swagger)5