Search in sources :

Example 6 with CompositeAPI

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

the class APIStoreImpl method searchCompositeAPIs.

@Override
public List<CompositeAPI> searchCompositeAPIs(String query, int offset, int limit) throws APIManagementException {
    List<CompositeAPI> apiResults;
    // this should be current logged in user
    String user = getUsername();
    // role list of current user
    Set<String> roles = APIUtils.getAllRolesOfUser(user);
    try {
        if (query != null && !query.isEmpty()) {
            apiResults = getApiDAO().searchCompositeAPIs(roles, user, query, offset, limit);
        } else {
            apiResults = getApiDAO().getCompositeAPIs(roles, user, offset, limit);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating searching APIs - " + query;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    return apiResults;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI)

Example 7 with CompositeAPI

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

the class APIStoreImpl method deleteCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public void deleteCompositeApi(String apiId) throws APIManagementException {
    try {
        CompositeAPI api = getApiDAO().getCompositeAPI(apiId);
        // Delete API in gateway
        gateway.deleteCompositeAPI(api);
        getApiDAO().deleteCompositeApi(apiId);
    } catch (GatewayException e) {
        String message = "Error occurred while deleting Composite API with id - " + apiId + " from gateway";
        throw new APIManagementException(message, e, ExceptionCodes.GATEWAY_EXCEPTION);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while deleting the Composite API with id " + apiId;
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI)

Example 8 with CompositeAPI

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

the class APIStoreImpl method createNewCompositeApiVersion.

/**
 * {@inheritDoc}
 */
@Override
public String createNewCompositeApiVersion(String apiId, String newVersion) throws APIManagementException {
    // validate parameters
    if (StringUtils.isEmpty(newVersion)) {
        String errorMsg = "New API version cannot be empty";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
    }
    if (StringUtils.isEmpty(apiId)) {
        String errorMsg = "API ID cannot be empty";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
    }
    String newVersionedId;
    try {
        CompositeAPI api = getApiDAO().getCompositeAPI(apiId);
        if (api.getVersion().equals(newVersion)) {
            String errMsg = "New API version " + newVersion + " cannot be same as the previous version for " + "API " + api.getName();
            log.error(errMsg);
            throw new APIManagementException(errMsg, ExceptionCodes.API_ALREADY_EXISTS);
        }
        CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder(api);
        apiBuilder.id(UUID.randomUUID().toString());
        apiBuilder.version(newVersion);
        apiBuilder.context(api.getContext().replace(api.getVersion(), newVersion));
        apiBuilder.copiedFromApiId(api.getId());
        if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
            apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
        }
        getApiDAO().addApplicationAssociatedAPI(apiBuilder.build());
        newVersionedId = apiBuilder.getId();
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't create new API version from " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    return newVersionedId;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI)

Example 9 with CompositeAPI

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

the class APIStoreImplTestCase method testUpdateCompositeApi.

@Test(description = "Update Composite API")
public void testUpdateCompositeApi() throws APIManagementException {
    // Add a new Composite API
    CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    IdentityProvider idp = Mockito.mock(IdentityProvider.class);
    APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
    String ballerinaImpl = "Ballerina";
    apiStore.addCompositeApi(apiBuilder);
    CompositeAPI createdAPI = apiBuilder.build();
    // Update existing Composite API
    apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
    apiBuilder.id(createdAPI.getId());
    apiBuilder.name(createdAPI.getName());
    apiBuilder.provider(createdAPI.getProvider());
    apiBuilder.version(createdAPI.getVersion());
    apiBuilder.context(createdAPI.getContext());
    Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
    Mockito.when(apiDAO.getCompositeAPIGatewayConfig(apiBuilder.getId())).thenReturn(new ByteArrayInputStream(ballerinaImpl.getBytes(StandardCharsets.UTF_8)));
    Mockito.when(gatewaySourceGenerator.getGatewayConfigFromSwagger(Matchers.anyString(), Matchers.anyString())).thenReturn(ballerinaImpl);
    apiStore.updateCompositeApi(apiBuilder);
    CompositeAPI updatedAPI = apiBuilder.build();
    Assert.assertEquals(updatedAPI.getId(), createdAPI.getId());
    Assert.assertEquals(updatedAPI.getName(), createdAPI.getName());
    Assert.assertEquals(updatedAPI.getProvider(), createdAPI.getProvider());
    Assert.assertEquals(updatedAPI.getVersion(), createdAPI.getVersion());
    Assert.assertEquals(updatedAPI.getContext(), createdAPI.getContext());
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ByteArrayInputStream(java.io.ByteArrayInputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 10 with CompositeAPI

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

the class APIStoreImplTestCase method testCreateNewCompositeApiVersion.

@Test(description = "Create new Composite API version")
public void testCreateNewCompositeApiVersion() throws APIManagementException {
    // Add a new Composite API
    CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    IdentityProvider idp = Mockito.mock(IdentityProvider.class);
    APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
    apiStore.addCompositeApi(apiBuilder);
    CompositeAPI createdAPI = apiBuilder.build();
    // Create new API version
    String newVersion = java.util.UUID.randomUUID().toString();
    Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
    apiStore.createNewCompositeApiVersion(createdAPI.getId(), newVersion);
    final ArgumentCaptor<CompositeAPI> captor = ArgumentCaptor.forClass(CompositeAPI.class);
    Mockito.verify(apiDAO, Mockito.times(2)).addApplicationAssociatedAPI(captor.capture());
    CompositeAPI newAPIVersion = captor.getValue();
    Assert.assertEquals(newAPIVersion.getVersion(), newVersion);
    Assert.assertNotEquals(newAPIVersion.getId(), createdAPI.getId());
    Assert.assertEquals(newAPIVersion.getCopiedFromApiId(), createdAPI.getId());
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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