Search in sources :

Example 1 with SubscriptionListDTO

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

the class TestMappingUtilTestCase method testSubscriptionListToSubscriptionListDTOMapping.

@Test(description = "Subscription list to Subscription DTO list mapping")
void testSubscriptionListToSubscriptionListDTOMapping() {
    Policy subscriptionPolicy1 = SampleTestObjectCreator.goldSubscriptionPolicy;
    API api1 = SampleTestObjectCreator.createDefaultAPI().name("newName1").build();
    Application application1 = SampleTestObjectCreator.createDefaultApplication();
    application1.setName("newNameApp1");
    String uuid1 = UUID.randomUUID().toString();
    Subscription subscription1 = new Subscription(uuid1, application1, api1, subscriptionPolicy1);
    subscription1.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
    Policy subscriptionPolicy2 = SampleTestObjectCreator.silverSubscriptionPolicy;
    API api2 = SampleTestObjectCreator.createDefaultAPI().name("newName2").build();
    Application application2 = SampleTestObjectCreator.createDefaultApplication();
    application1.setName("newNameApp2");
    String uuid2 = UUID.randomUUID().toString();
    Subscription subscription2 = new Subscription(uuid2, application2, api2, subscriptionPolicy2);
    subscription2.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
    List<Subscription> subscriptions = new ArrayList<>();
    subscriptions.add(subscription1);
    subscriptions.add(subscription2);
    SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptions, 10, 0);
    assertEquals((Integer) subscriptions.size(), subscriptionListDTO.getCount());
    assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
    assertEquals(subscription1.getStatus().name(), subscriptionListDTO.getList().get(0).getSubscriptionStatus().name());
    assertEquals(subscription1.getApplication().getId(), subscriptionListDTO.getList().get(0).getApplicationInfo().getApplicationId());
    assertEquals(subscription1.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(0).getPolicy());
    assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
    assertEquals(subscription2.getStatus().name(), subscriptionListDTO.getList().get(1).getSubscriptionStatus().name());
    assertEquals(subscription2.getApplication().getId(), subscriptionListDTO.getList().get(1).getApplicationInfo().getApplicationId());
    assertEquals(subscription2.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(1).getPolicy());
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO) Test(org.testng.annotations.Test)

Example 2 with SubscriptionListDTO

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

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Retrieve all subscriptions for a particular API
 *
 * @param apiId       ID of the API
 * @param limit       Maximum subscriptions to return
 * @param offset      Starting position of the pagination
 * @param ifNoneMatch If-Match header value
 * @param request     ms4j request object
 * @return List of qualifying subscriptions DTOs as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    List<Subscription> subscriptionList;
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (StringUtils.isNotEmpty(apiId)) {
            subscriptionList = apiPublisher.getSubscriptionsByAPI(apiId);
            SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptionList, limit, offset);
            return Response.ok().entity(subscriptionListDTO).build();
        } else {
            RestApiUtil.handleBadRequest("API ID can not be null", log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions of API " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)

Example 3 with SubscriptionListDTO

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

the class SubscriptionsApiServiceImplTestCase method subscriptionsApiVersionEmptyGetTest.

@Test
public void subscriptionsApiVersionEmptyGetTest() throws Exception {
    Response response = getResponse(API_CONTEXT, null);
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
Also used : Response(javax.ws.rs.core.Response) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with SubscriptionListDTO

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

the class SubscriptionsApiServiceImplTestCase method subscriptionsApiContextEmptyGetTest.

@Test
public void subscriptionsApiContextEmptyGetTest() throws Exception {
    Response response = getResponse(null, API_VERSION);
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
Also used : Response(javax.ws.rs.core.Response) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with SubscriptionListDTO

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

the class SubscriptionMappingUtil method fromSubscriptionListToDTO.

/**
 * Converts a List object of SubscribedAPIs into a DTO
 *
 * @param subscriptions a list of SubscribedAPI objects
 * @param limit max number of objects returned
 * @param offset starting index
 * @return SubscriptionListDTO object containing SubscriptionDTOs
 */
public static SubscriptionListDTO fromSubscriptionListToDTO(List<Subscription> subscriptions, Integer limit, Integer offset) {
    SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
    List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
    if (subscriptionDTOs == null) {
        subscriptionDTOs = new ArrayList<>();
        subscriptionListDTO.setList(subscriptionDTOs);
    }
    // identifying the proper start and end indexes
    int size = subscriptions.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Subscription subscription = subscriptions.get(i);
        subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
    }
    subscriptionListDTO.setCount(subscriptionDTOs.size());
    return subscriptionListDTO;
}
Also used : Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO)

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