Search in sources :

Example 26 with DedicatedGateway

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

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

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

the class ApiDAOImpl method updateDedicatedGateway.

/**
 * @see ApiDAO#updateDedicatedGateway(DedicatedGateway, List)
 */
@Override
public void updateDedicatedGateway(DedicatedGateway dedicatedGateway, List<String> labels) throws APIMgtDAOException {
    // labels will come in 2 ways.
    // 1. auto-generated label - Update from dedicateGateway false to true
    // 2. default label - Update from dedicatedGateway true to false
    String apiId = dedicatedGateway.getApiId();
    final String query = "UPDATE AM_API SET HAS_OWN_GATEWAY = ?, LAST_UPDATED_TIME = ?, UPDATED_BY = ? " + "WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setBoolean(1, dedicatedGateway.isEnabled());
            statement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(3, dedicatedGateway.getUpdatedBy());
            statement.setString(4, apiId);
            // if the labels are not null or not empty
            if (labels != null && !labels.isEmpty()) {
                deleteLabelsMapping(connection, apiId);
                addLabelMapping(connection, apiId, labels);
            }
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            String msg = "Couldn't update dedicated Gateway details of API : " + apiId;
            connection.rollback();
            throw new APIMgtDAOException(msg, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error Executing query for updating Container Based Gateway", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 29 with DedicatedGateway

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

the class ApiDAOImplIT method testUpdateGetDedicatedGatewayWhenLabelsAreEmpty.

@Test
public void testUpdateGetDedicatedGatewayWhenLabelsAreEmpty() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    DedicatedGateway dedicatedGateway = new DedicatedGateway();
    dedicatedGateway.setEnabled(true);
    dedicatedGateway.setUpdatedBy(api.getCreatedBy());
    dedicatedGateway.setApiId(api.getId());
    List<String> labels = new ArrayList<>();
    apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
    DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
    Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
Also used : ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) Test(org.testng.annotations.Test)

Example 30 with DedicatedGateway

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

the class ApiDAOImplIT method testUpdateGetDedicatedGatewayWhenLabelsAreNull.

@Test
public void testUpdateGetDedicatedGatewayWhenLabelsAreNull() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    DedicatedGateway dedicatedGateway = new DedicatedGateway();
    dedicatedGateway.setEnabled(true);
    dedicatedGateway.setUpdatedBy(api.getCreatedBy());
    dedicatedGateway.setApiId(api.getId());
    apiDAO.updateDedicatedGateway(dedicatedGateway, null);
    DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
    Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) Test(org.testng.annotations.Test)

Aggregations

DedicatedGateway (org.wso2.carbon.apimgt.core.models.DedicatedGateway)33 Test (org.testng.annotations.Test)20 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)20 API (org.wso2.carbon.apimgt.core.models.API)19 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)14 ArrayList (java.util.ArrayList)12 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)9 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)8 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)8 BeforeTest (org.testng.annotations.BeforeTest)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 Label (org.wso2.carbon.apimgt.core.models.Label)5 HashMap (java.util.HashMap)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)4 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)4