Search in sources :

Example 21 with CompositeAPI

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

the class ApiDAOImpl method addApplicationAssociatedAPI.

@Override
public void addApplicationAssociatedAPI(CompositeAPI api) throws APIMgtDAOException {
    final String query = "INSERT INTO AM_API (PROVIDER, NAME, CONTEXT, VERSION, " + "DESCRIPTION, UUID, API_TYPE_ID, CREATED_BY, CREATED_TIME, LAST_UPDATED_TIME, COPIED_FROM_API, " + "UPDATED_BY, LC_WORKFLOW_STATUS) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            addCompositeAPIRelatedInformation(connection, statement, api);
            String policyUuid = DAOFactory.getPolicyDAO().getSubscriptionPolicy(ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED).getUuid();
            APISubscriptionDAOImpl apiSubscriptionDAO = (APISubscriptionDAOImpl) DAOFactory.getAPISubscriptionDAO();
            apiSubscriptionDAO.createSubscription(api.getId(), api.getApplicationId(), UUID.randomUUID().toString(), policyUuid, APIMgtConstants.SubscriptionStatus.ACTIVE, connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding Composite API: " + api.getProvider() + " - " + api.getName() + " - " + api.getVersion(), e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding Composite API: " + api.getProvider() + " - " + api.getName() + " - " + api.getVersion(), e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 22 with CompositeAPI

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

the class APIStoreImpl method updateCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public void updateCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    apiBuilder.provider(getUsername());
    apiBuilder.updatedBy(getUsername());
    CompositeAPI originalAPI = getApiDAO().getCompositeAPI(apiBuilder.getId());
    apiBuilder.createdTime(originalAPI.getCreatedTime());
    // workflow status is an internal property and shouldn't be allowed to update externally
    apiBuilder.workflowStatus(originalAPI.getWorkflowStatus());
    APIUtils.verifyValidityOfApiUpdate(apiBuilder, originalAPI);
    try {
        String updatedSwagger = apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder);
        InputStream gatewayConfig = getApiDAO().getCompositeAPIGatewayConfig(apiBuilder.getId());
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(IOUtils.toString(gatewayConfig, StandardCharsets.UTF_8), updatedSwagger);
        CompositeAPI api = apiBuilder.build();
        if (originalAPI.getContext() != null && !originalAPI.getContext().equals(apiBuilder.getContext())) {
            if (isContextExist(api.getContext())) {
                throw new APIManagementException("Context already Exist", ExceptionCodes.API_ALREADY_EXISTS);
            }
        }
        // publishing config to gateway
        gateway.addCompositeAPI(api);
        getApiDAO().updateApiDefinition(api.getId(), updatedSwagger, api.getUpdatedBy());
        getApiDAO().updateCompositeAPIGatewayConfig(api.getId(), new ByteArrayInputStream(updatedGatewayConfig.getBytes(StandardCharsets.UTF_8)), api.getUpdatedBy());
        if (log.isDebugEnabled()) {
            log.debug("API " + api.getName() + "-" + api.getVersion() + " was updated successfully.");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (IOException e) {
        String errorMsg = "Error occurred while reading gateway configuration the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IOException(java.io.IOException) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 23 with CompositeAPI

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

the class APIStoreImpl method addCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public String addCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    apiBuilder.provider(getUsername());
    if (StringUtils.isEmpty(apiBuilder.getId())) {
        apiBuilder.id(UUID.randomUUID().toString());
    }
    LocalDateTime localDateTime = LocalDateTime.now();
    apiBuilder.createdTime(localDateTime);
    apiBuilder.lastUpdatedTime(localDateTime);
    apiBuilder.createdBy(getUsername());
    apiBuilder.updatedBy(getUsername());
    if (!isApiNameExist(apiBuilder.getName()) && !isContextExist(apiBuilder.getContext())) {
        setUriTemplates(apiBuilder);
        setGatewayDefinitionSource(apiBuilder);
        if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
            apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
        }
        try {
            CompositeAPI createdAPI = apiBuilder.build();
            APIUtils.validate(createdAPI);
            // publishing config to gateway
            gateway.addCompositeAPI(createdAPI);
            getApiDAO().addApplicationAssociatedAPI(createdAPI);
            if (log.isDebugEnabled()) {
                log.debug("API " + createdAPI.getName() + "-" + createdAPI.getVersion() + " was created " + "successfully.", log);
            }
        } catch (GatewayException e) {
            String message = "Error publishing service configuration to Gateway " + apiBuilder.getName();
            log.error(message, e);
            throw new APIManagementException(message, e, ExceptionCodes.GATEWAY_EXCEPTION);
        } catch (APIMgtDAOException e) {
            String message = "Error when adding composite API " + apiBuilder.getName();
            throw new APIManagementException(message, e, e.getErrorHandler());
        }
    } else {
        String message = "Duplicate API already Exist with name/Context " + apiBuilder.getName();
        log.error(message);
        throw new APIManagementException(message, ExceptionCodes.API_ALREADY_EXISTS);
    }
    return apiBuilder.getId();
}
Also used : LocalDateTime(java.time.LocalDateTime) 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 24 with CompositeAPI

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

the class ApiDAOImplIT method testAddGetDeleteCompositeAPI.

@Test
public void testAddGetDeleteCompositeAPI() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    String gateWayConfig = SampleTestObjectCreator.createSampleGatewayConfig();
    CompositeAPI compositeAPI = SampleTestObjectCreator.createUniqueCompositeAPI().gatewayConfig(gateWayConfig).build();
    // Add application associated with Composite API
    apiDAO.addApplicationAssociatedAPI(compositeAPI);
    CompositeAPI addedAPI = apiDAO.getCompositeAPI(compositeAPI.getId());
    Assert.assertNotNull(addedAPI);
    Assert.assertEquals(compositeAPI.getContext(), addedAPI.getContext());
    // Composite API gateway config
    Assert.assertNotNull(apiDAO.getCompositeAPIGatewayConfig(addedAPI.getId()));
    // Check for swagger definition
    Assert.assertNotNull(apiDAO.getCompositeApiSwaggerDefinition(addedAPI.getId()));
    // Update gateway config
    String fingerprintBeforeUpdate = ETagUtils.generateETag(apiDAO.getLastUpdatedTimeOfGatewayConfig(addedAPI.getId()));
    Assert.assertNotNull(fingerprintBeforeUpdate);
    Thread.sleep(1);
    String gwConfig = SampleTestObjectCreator.createAlternativeGatewayConfig();
    apiDAO.updateCompositeAPIGatewayConfig(addedAPI.getId(), new ByteArrayInputStream(gwConfig.getBytes(StandardCharsets.UTF_8)), ADMIN);
    String fingerprintAfterUpdate = ETagUtils.generateETag(apiDAO.getLastUpdatedTimeOfGatewayConfig(addedAPI.getId()));
    Assert.assertNotNull(fingerprintAfterUpdate);
    Assert.assertNotEquals(fingerprintBeforeUpdate, fingerprintAfterUpdate);
    // Composite API Summary
    CompositeAPI summaryAPI = apiDAO.getCompositeAPISummary(compositeAPI.getId());
    Assert.assertNotNull(summaryAPI);
    Assert.assertEquals(compositeAPI.getContext(), summaryAPI.getContext());
    // Composite APIs retrieving
    List<CompositeAPI> compositeAPIS = apiDAO.getCompositeAPIs(new HashSet<>(), compositeAPI.getProvider(), 4, 4);
    Assert.assertEquals(compositeAPI.getId(), compositeAPIS.get(0).getId());
    // Delete Composite API
    apiDAO.deleteCompositeApi(compositeAPI.getId());
    Assert.assertFalse(apiDAO.isAPIExists(compositeAPI.getId()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 25 with CompositeAPI

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

the class APIStoreImplTestCase method testGetCompositeAPIbyId.

@Test(description = "Test getting composite API by UUID")
public void testGetCompositeAPIbyId() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    CompositeAPI api = SampleTestObjectCreator.createUniqueCompositeAPI().build();
    String apiId = api.getId();
    APIStore apiStore = getApiStoreImpl(apiDAO);
    Mockito.when(apiDAO.getCompositeAPI(apiId)).thenReturn(api);
    CompositeAPI apiReturned = apiStore.getCompositeAPIbyId(apiId);
    Mockito.verify(apiDAO, Mockito.times(1)).getCompositeAPI(apiId);
    Assert.assertEquals(apiReturned, api);
    // Error path
    Mockito.when(apiDAO.getCompositeAPI(apiId)).thenThrow(APIMgtDAOException.class);
    try {
        apiStore.getCompositeAPIbyId(apiId);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while retrieving API with id " + apiId);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) 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