Search in sources :

Example 11 with CompositeAPI

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

the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdGet.

@Test
public void testCompositeApisApiIdGet() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String apiID = UUID.randomUUID().toString();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    CompositeAPI compositeAPI = SampleTestObjectCreator.createCompositeAPIModelBuilder().build();
    Mockito.when(apiStore.isCompositeAPIExist(apiID)).thenReturn(Boolean.TRUE);
    Mockito.when(apiStore.getCompositeAPIbyId(apiID)).thenReturn(compositeAPI);
    Response response = compositeApisApiService.compositeApisApiIdGet(apiID, null, null, request);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getEntity().toString().contains(compositeAPI.getName()));
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with CompositeAPI

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

the class CompositeApisApiServiceImplTestCase method testCompositeApisGet.

@Test
public void testCompositeApisGet() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String query = "*";
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    CompositeAPI compositeAPI1 = SampleTestObjectCreator.createCompositeAPIModelBuilder().build();
    CompositeAPI compositeAPI2 = SampleTestObjectCreator.createCompositeAPIModelBuilder().build();
    List<CompositeAPI> compositeAPIList = new ArrayList<>();
    compositeAPIList.add(compositeAPI1);
    compositeAPIList.add(compositeAPI2);
    Mockito.when(apiStore.searchCompositeAPIs(query, 0, 10)).thenReturn(compositeAPIList);
    Response response = compositeApisApiService.compositeApisGet(10, 0, query, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with CompositeAPI

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

the class CompositeApisApiServiceImpl method compositeApisPost.

@Override
public Response compositeApisPost(CompositeAPIDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        CompositeAPI.Builder apiBuilder = CompositeAPIMappingUtil.toAPI(body);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Application app = apiStore.getApplicationByUuid(apiBuilder.getApplicationId());
        // One application can only have one Composite API in default implementation
        if (apiStore.getAPISubscriptionsByApplication(app, ApiType.COMPOSITE).size() > 0) {
            String errorMessage = "A Composite API already exists for application : " + app.getId();
            APIMgtResourceAlreadyExistsException e = new APIMgtResourceAlreadyExistsException(errorMessage, ExceptionCodes.COMPOSITE_API_ALREADY_EXISTS);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, app.getId());
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        apiStore.addCompositeApi(apiBuilder);
        CompositeAPI returnAPI = apiStore.getCompositeAPIbyId(apiBuilder.build().getId());
        return Response.status(Response.Status.CREATED).entity(CompositeAPIMappingUtil.toCompositeAPIDTO(returnAPI)).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, body.getName());
        paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, body.getVersion());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        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) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 14 with CompositeAPI

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

the class CompositeAPIMappingUtil method toCompositeAPIDTO.

/**
 * Converts {@link CompositeAPI} to a {@link CompositeAPIDTO}.
 *
 * @param api CompositeAPI
 * @return API DTO
 */
public static CompositeAPIDTO toCompositeAPIDTO(CompositeAPI api) {
    CompositeAPIDTO compositeAPIDTO = new CompositeAPIDTO();
    compositeAPIDTO.setId(api.getId());
    compositeAPIDTO.setName(api.getName());
    compositeAPIDTO.setProvider(api.getProvider());
    compositeAPIDTO.setVersion(api.getVersion());
    compositeAPIDTO.setContext(api.getContext());
    compositeAPIDTO.setDescription(api.getDescription());
    compositeAPIDTO.setLabels(new ArrayList<>(api.getLabels()));
    compositeAPIDTO.setApplicationId(api.getApplicationId());
    compositeAPIDTO.hasOwnGateway(api.hasOwnGateway());
    return compositeAPIDTO;
}
Also used : CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO)

Example 15 with CompositeAPI

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

the class CompositeApisApiServiceImplTestCase method testCompositeApisPost.

@Test
public void testCompositeApisPost() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    CompositeAPI compositeAPI = SampleTestObjectCreator.createCompositeAPIModelBuilder().build();
    CompositeAPIDTO compositeAPIDTO = CompositeAPIMappingUtil.toCompositeAPIDTO(compositeAPI);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    Mockito.when(apiStore.getApplicationByUuid(UUID.randomUUID().toString())).thenReturn(application);
    Mockito.when(apiStore.getCompositeAPIbyId(compositeAPI.getId())).thenReturn(compositeAPI);
    Response response = compositeApisApiService.compositeApisPost(compositeAPIDTO, request);
    Assert.assertEquals(201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)19 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)9 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 BeforeTest (org.testng.annotations.BeforeTest)3 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)3 CompositeAPIDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO)3 Request (org.wso2.msf4j.Request)3 IOException (java.io.IOException)2 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)2