Search in sources :

Example 76 with Response

use of org.wso2.msf4j.Response in project carbon-apimgt by wso2.

the class PoliciesApiServiceImplTestCase method testPoliciesTierLevelTierNameGetException.

@Test
public void testPoliciesTierLevelTierNameGetException() throws Exception {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    Mockito.doThrow(new LabelException("Error occurred", ExceptionCodes.POLICY_LEVEL_NOT_SUPPORTED)).when(apiPublisher).getPolicyByName(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum("subscription"), "Gold");
    Response response = policiesApiService.policiesTierLevelTierNameGet("Gold", "subscription", null, null, getRequest());
    assertEquals(response.getStatus(), 400);
    assertTrue(response.getEntity().toString().contains("Throttle Policy level invalid"));
}
Also used : Response(javax.ws.rs.core.Response) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with Response

use of org.wso2.msf4j.Response in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImplTestCase method testSubscriptionsSubscriptionIdGetExist.

@Test
public void testSubscriptionsSubscriptionIdGetExist() throws Exception {
    printTestMethodName();
    SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    String sub1 = UUID.randomUUID().toString();
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    Mockito.doReturn(null).doThrow(new IllegalArgumentException()).when(apiPublisher).getSubscriptionByUUID(sub1);
    Response response = subscriptionsApiService.subscriptionsSubscriptionIdGet(sub1, null, null, getRequest());
    assertEquals(response.getStatus(), 404);
    assertTrue(response.getEntity().toString().contains("Subscription not found"));
}
Also used : Response(javax.ws.rs.core.Response) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 78 with Response

use of org.wso2.msf4j.Response in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImplTestCase method testSubscriptionsGetException.

@Test
public void testSubscriptionsGetException() throws Exception {
    printTestMethodName();
    SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String subscriptionId = UUID.randomUUID().toString();
    Subscription subscription = SampleTestObjectCreator.createSubscription(subscriptionId);
    subscription.setStatus(APIMgtConstants.SubscriptionStatus.REJECTED);
    Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.SUBSCRIPTION_STATE_INVALID)).when(apiPublisher).getSubscriptionByUUID(subscriptionId);
    Response response = subscriptionsApiService.subscriptionsUnblockSubscriptionPost(subscriptionId, null, null, getRequest());
    assertEquals(response.getStatus(), 400);
    assertTrue(response.getEntity().toString().contains("Invalid state change for subscription"));
}
Also used : Response(javax.ws.rs.core.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 79 with Response

use of org.wso2.msf4j.Response in project carbon-apimgt by wso2.

the class MappingUtil method toWorkflowResponseDTO.

/**
 * Map WorkflowResponse to WorkflowResponseDTO
 * @param response WorkflowResponse object
 * @return WorkflowResponseDTO mapped WorkflowResponseDTO
 */
public static WorkflowResponseDTO toWorkflowResponseDTO(WorkflowResponse response) {
    WorkflowResponseDTO responseDTO = new WorkflowResponseDTO();
    responseDTO.setWorkflowStatus(WorkflowStatusEnum.valueOf(response.getWorkflowStatus().toString()));
    responseDTO.setJsonPayload(response.getJSONPayload());
    return responseDTO;
}
Also used : WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO)

Example 80 with Response

use of org.wso2.msf4j.Response 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)

Aggregations

Response (javax.ws.rs.core.Response)344 Test (org.testng.annotations.Test)329 Test (org.junit.Test)317 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)317 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)248 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)217 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)201 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)189 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)153 HashMap (java.util.HashMap)151 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)149 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)146 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)141 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)139 ArrayList (java.util.ArrayList)120 BJSON (org.ballerinalang.model.values.BJSON)94 Request (org.wso2.msf4j.Request)92 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)72 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)67 Path (javax.ws.rs.Path)66