Search in sources :

Example 6 with DedicatedGatewayDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdDedicatedGatewayPut.

/**
 * Add or update Dedicated Gateway of an API
 *
 * @param apiId             UUID of API
 * @param body              DedicatedGatewayDTO
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 OK if the operation was successful
 * @throws NotFoundException when the particular resource does not exist
 */
@Override
public Response compositeApisApiIdDedicatedGatewayPut(String apiId, DedicatedGatewayDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!apiStore.isCompositeAPIExist(apiId)) {
            String errorMessage = "API not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            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();
        }
        String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        DedicatedGateway dedicatedGateway = DedicatedGatewayMappingUtil.fromDTOtoDedicatedGateway(body, apiId, username);
        apiStore.updateDedicatedGateway(dedicatedGateway);
        DedicatedGateway updatedDedicatedGateway = apiStore.getDedicatedGateway(apiId);
        String newFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(DedicatedGatewayMappingUtil.toDedicatedGatewayDTO(updatedDedicatedGateway)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating dedicated gateway of the composite 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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 7 with DedicatedGatewayDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.

the class DedicatedGatewayMappingUtil method fromDTOtoDedicatedGateway.

/**
 * This method maps the the DedicatedGatewayDTO object to DedicatedGateway Object
 *
 * @param dedicatedGatewayDTO contains data of DedicatedGateway
 * @param apiId               UUID of the API
 * @param username            Username
 * @return Dedicated Gateway Object
 */
public static DedicatedGateway fromDTOtoDedicatedGateway(DedicatedGatewayDTO dedicatedGatewayDTO, String apiId, String username) {
    DedicatedGateway dedicatedGateway = new DedicatedGateway();
    dedicatedGateway.setApiId(apiId);
    dedicatedGateway.setUpdatedBy(username);
    if (dedicatedGatewayDTO.getIsEnabled() != null) {
        dedicatedGateway.setEnabled(dedicatedGatewayDTO.getIsEnabled());
    } else {
        dedicatedGateway.setEnabled(false);
    }
    return dedicatedGateway;
}
Also used : DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 8 with DedicatedGatewayDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDedicatedGatewayPut.

/**
 * Add or update Dedicated Gateway of an API
 *
 * @param apiId             UUID of API
 * @param body              DedicatedGatewayDTO
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 OK if the operation was successful
 * @throws NotFoundException when the particular resource does not exist
 */
@Override
public Response apisApiIdDedicatedGatewayPut(String apiId, DedicatedGatewayDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (!apiPublisher.isAPIExists(apiId)) {
            String errorMessage = "API not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            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();
        }
        String existingFingerprint = apisApiIdGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        DedicatedGateway dedicatedGateway = MappingUtil.fromDTOtoDedicatedGateway(body, apiId, username);
        apiPublisher.updateDedicatedGateway(dedicatedGateway);
        DedicatedGateway updatedDedicatedGateway = apiPublisher.getDedicatedGateway(apiId);
        String newFingerprint = apisApiIdGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(MappingUtil.toDedicatedGatewayDTO(updatedDedicatedGateway)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating dedicated gateway of the 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();
    }
}
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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 9 with DedicatedGatewayDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdDedicatedGatewayPut.

@Test
public void testCompositeApisApiIdDedicatedGatewayPut() throws Exception {
    TestUtil.printTestMethodName();
    String apiID = UUID.randomUUID().toString();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    Request request = TestUtil.getRequest();
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Mockito.when(apiStore.isCompositeAPIExist(apiID)).thenReturn(Boolean.TRUE);
    Mockito.doNothing().when(apiStore).updateDedicatedGateway(Mockito.any());
    DedicatedGateway dedicatedGateway = new DedicatedGateway();
    dedicatedGateway.setEnabled(true);
    Mockito.when(apiStore.getDedicatedGateway(apiID)).thenReturn(dedicatedGateway);
    DedicatedGatewayDTO dedicatedGatewayDTO = new DedicatedGatewayDTO();
    dedicatedGatewayDTO.setIsEnabled(true);
    Response response = compositeApisApiService.compositeApisApiIdDedicatedGatewayPut(apiID, dedicatedGatewayDTO, null, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) DedicatedGatewayDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with DedicatedGatewayDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdDedicatedGatewayPutForException.

@Test
public void testCompositeApisApiIdDedicatedGatewayPutForException() throws Exception {
    TestUtil.printTestMethodName();
    String apiID = UUID.randomUUID().toString();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    Request request = TestUtil.getRequest();
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Mockito.when(apiStore.isCompositeAPIExist(apiID)).thenReturn(Boolean.TRUE);
    Mockito.doThrow(new APIManagementException("Error while creating dedicated container based gateway", ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED)).when(apiStore).updateDedicatedGateway(Mockito.any());
    Response response = compositeApisApiService.compositeApisApiIdDedicatedGatewayPut(apiID, new DedicatedGatewayDTO(), null, null, request);
    Assert.assertEquals(ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED.getHttpStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Request(org.wso2.msf4j.Request) DedicatedGatewayDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

DedicatedGateway (org.wso2.carbon.apimgt.core.models.DedicatedGateway)8 Response (javax.ws.rs.core.Response)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 DedicatedGatewayDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DedicatedGatewayDTO)5 DedicatedGatewayDTO (org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO)5 HashMap (java.util.HashMap)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)3 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)3 Request (org.wso2.msf4j.Request)3