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);
}
}
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);
}
}
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();
}
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()));
}
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);
}
}
Aggregations