Search in sources :

Example 6 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class SubscriptionMappingUtilTestCase method testFromSubscriptionListToDTO.

@Test
public void testFromSubscriptionListToDTO() {
    String subUuid1 = UUID.randomUUID().toString();
    String subUuid2 = UUID.randomUUID().toString();
    Subscription subscription1 = SampleTestObjectCreator.createSubscription(subUuid1);
    Subscription subscription2 = SampleTestObjectCreator.createSubscription(subUuid2);
    List<Subscription> subscriptionList = new ArrayList<>();
    subscriptionList.add(subscription1);
    subscriptionList.add(subscription2);
    SubscriptionListDTO subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscriptionList, 10, 0);
    assertEquals(subscriptionListDTO.getCount(), (Integer) subscriptionList.size());
    assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
    assertEquals(subscription1.getApi().getName(), subscriptionListDTO.getList().get(0).getApiName());
    assertEquals(subscription1.getApi().getId(), subscriptionListDTO.getList().get(0).getApiIdentifier());
    assertEquals(subscription1.getApi().getVersion(), subscriptionListDTO.getList().get(0).getApiVersion());
    assertEquals(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription1.getStatus().toString()).name(), subscriptionListDTO.getList().get(0).getLifeCycleStatus().name());
    assertEquals(subscriptionListDTO.getCount(), (Integer) subscriptionList.size());
    assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
    assertEquals(subscription2.getApi().getName(), subscriptionListDTO.getList().get(1).getApiName());
    assertEquals(subscription2.getApi().getId(), subscriptionListDTO.getList().get(1).getApiIdentifier());
    assertEquals(subscription2.getApi().getVersion(), subscriptionListDTO.getList().get(1).getApiVersion());
    assertEquals(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription2.getStatus().toString()).name(), subscriptionListDTO.getList().get(1).getLifeCycleStatus().name());
}
Also used : ArrayList(java.util.ArrayList) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO) Test(org.testng.annotations.Test)

Example 7 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Retrieve subscriptions
 *
 * @param apiContext Context of the API
 * @param apiVersion API version
 * @param limit      Limit value
 * @return Subscriptions of the API
 * @throws NotFoundException If failed to retrieve subscriptions
 */
@Override
public Response subscriptionsGet(String apiContext, String apiVersion, Integer limit, String accept, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<SubscriptionValidationData> subscriptionsOfApi;
        if (StringUtils.isEmpty(apiContext) || StringUtils.isEmpty(apiVersion)) {
            APIUtils.logDebug("API Context or version is null or empty. Retrieving subscriptions of all APIs", log);
            subscriptionsOfApi = apiMgtAdminService.getAPISubscriptions(limit);
        } else {
            subscriptionsOfApi = apiMgtAdminService.getAPISubscriptionsOfApi(apiContext, apiVersion);
        }
        SubscriptionListDTO subscriptionsList = new SubscriptionListDTO();
        subscriptionsList.setList(MappingUtil.convertToSubscriptionListDto(subscriptionsOfApi));
        return Response.ok(subscriptionsList).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions.";
        HashMap<String, String> paramList = new HashMap<String, String>();
        if (!StringUtils.isEmpty(apiContext)) {
            paramList.put(APIMgtConstants.ExceptionsConstants.API_CONTEXT, apiContext);
        }
        if (!StringUtils.isEmpty(apiVersion)) {
            paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, apiVersion);
        }
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO)

Example 8 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Get all subscriptions.
 * {@code <p/>}
 * If apiId is specified this will return the subscribed applications of that api
 * If application id is specified this will return the api subscriptions of that application
 *
 * @param apiId         ID of the API
 * @param applicationId ID of the Application
 * @param offset        offset value
 * @param limit         limit value
 * @param ifNoneMatch   If-None-Match header value
 * @param request       msf4j request object
 * @return Subscription List
 * @throws NotFoundException If failed to get the subscription
 */
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
    List<Subscription> subscribedApiList = null;
    SubscriptionListDTO subscriptionListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!StringUtils.isEmpty(apiId)) {
            subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
        } else if (!StringUtils.isEmpty(applicationId)) {
            Application application = apiStore.getApplicationByUuid(applicationId);
            if (application != null) {
                if (!StringUtils.isEmpty(apiType)) {
                    ApiType apiTypeEnum = ApiType.fromString(apiType);
                    if (apiTypeEnum == null) {
                        throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
                    }
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
                } else {
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
                }
                subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
            } else {
                String errorMessage = "Application not found: " + applicationId;
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
                HashMap<String, String> paramList = new HashMap<String, String>();
                paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            }
        } else {
            // mandatory parameters not provided
            String errorMessage = "Either applicationId or apiId should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(subscriptionListDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApiType(org.wso2.carbon.apimgt.core.dao.ApiType) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 9 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class MappingUtilTestCase method convertToSubscriptionListDTOTest.

@Test
public void convertToSubscriptionListDTOTest() {
    List<SubscriptionValidationData> validationDataList = new ArrayList<>();
    validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
    validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
    validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
    validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
    List<SubscriptionDTO> subscriptionListDTO = MappingUtil.convertToSubscriptionListDto(validationDataList);
    Assert.assertNotNull(subscriptionListDTO);
    Assert.assertEquals(validationDataList.size(), subscriptionListDTO.size());
    for (int i = 0; i < subscriptionListDTO.size(); i++) {
        Assert.assertEquals(subscriptionListDTO.get(i).getApiName(), validationDataList.get(i).getApiName());
        Assert.assertEquals(subscriptionListDTO.get(i).getApiContext(), validationDataList.get(i).getApiContext());
        Assert.assertEquals(subscriptionListDTO.get(i).getApiProvider(), validationDataList.get(i).getApiProvider());
        Assert.assertEquals(subscriptionListDTO.get(i).getApiVersion(), validationDataList.get(i).getApiVersion());
    }
}
Also used : ArrayList(java.util.ArrayList) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 10 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImplTestCase method subscriptionsGetTest.

@Test
public void subscriptionsGetTest() 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);
    SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
    Mockito.when(apiMgtAdminService.getAPISubscriptionsOfApi(API_CONTEXT, API_VERSION)).thenReturn(createSubscriptionValidationDataList());
    Response response = subscriptionsApiService.subscriptionsGet(API_CONTEXT, API_VERSION, LIMIT, null, getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Subscription (org.wso2.carbon.apimgt.core.models.Subscription)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)3 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO)3 Application (org.wso2.carbon.apimgt.core.models.Application)2 SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)2 ZoneId (java.time.ZoneId)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)1 ApiType (org.wso2.carbon.apimgt.core.dao.ApiType)1