use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsException.
@Test(description = "Error occurred while deleting API with zero subscriptions")
public void testDeleteApiWithZeroSubscriptionsException() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, labelDao, workflowDAO, null, null, null, gateway);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
// LifeCycleException
Mockito.doThrow(LifecycleException.class).when(apiLifecycleManager).removeLifecycle(api.getLifecycleInstanceId());
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while Disassociating the API with Lifecycle id " + uuid);
}
// ApiDAOException
Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).deleteAPI(uuid);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while deleting the API with id " + uuid);
}
// GatewayException
Mockito.doThrow(GatewayException.class).when(gateway).deleteAPI(api);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while deleting API with id - " + uuid + " from gateway");
}
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testGetAPILifeCycleData.
@Test(description = "Get api lifecycle data")
public void testGetAPILifeCycleData() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
String lifecycleId = api.getLifecycleInstanceId();
String lcState = api.getLifeCycleStatus();
LifecycleState bean = new LifecycleState();
bean.setState(APIStatus.CREATED.getStatus());
Mockito.when(apiDAO.getAPISummary(uuid)).thenReturn(api);
Mockito.doReturn(bean).when(apiLifecycleManager).getLifecycleDataForState(lifecycleId, lcState);
apiPublisher.getAPILifeCycleData(uuid);
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class ApiDAOImpl method changeLifeCycleStatus.
/**
* Change the lifecycle status of a given API
*
* @param apiID The UUID of the respective API
* @param status The lifecycle status that the API must be set to
* @throws APIMgtDAOException if error occurs while accessing data layer
*/
@Override
public void changeLifeCycleStatus(String apiID, String status) throws APIMgtDAOException {
final String query = "UPDATE AM_API SET CURRENT_LC_STATUS = ?, LAST_UPDATED_TIME = ? WHERE UUID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
statement.setString(1, status);
statement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
statement.setString(3, apiID);
statement.execute();
connection.commit();
} catch (SQLException e) {
connection.rollback();
String msg = "changing Life Cycle Status for API: " + apiID + " to Status: " + status;
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
String msg = "changing Life Cycle Status for API: " + apiID + " to Status: " + status;
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIPublisherImpl method updateCheckListItem.
/**
* This method used to Update the lifecycle checklist of API
*
* @param apiId UUID of the API
* @param status Current API lifecycle status.
* @param checkListItemMap Check list item values.
* @throws APIManagementException If failed to update checklist item values.
*/
@Override
public void updateCheckListItem(String apiId, String status, Map<String, Boolean> checkListItemMap) throws APIManagementException {
API api = getApiDAO().getAPI(apiId);
try {
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lastUpdatedTime(LocalDateTime.now());
apiBuilder.updatedBy(getUsername());
apiBuilder.lifecycleState(getApiLifecycleManager().getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus()));
for (Map.Entry<String, Boolean> checkListItem : checkListItemMap.entrySet()) {
getApiLifecycleManager().checkListItemEvent(api.getLifecycleInstanceId(), api.getLifeCycleStatus(), checkListItem.getKey(), checkListItem.getValue());
}
} catch (LifecycleException e) {
String errorMsg = "Couldn't get the lifecycle status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
}
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class ApiDAOImplIT method addAPIWithGivenData.
/**
* This method adds an API with given information
*
* @param apiName API name
* @param apiVersion API version
* @param apiContext API context
* @param apiProvider API provider
* @param apiVisibility API visibility
* @param visibleRoles roles that are eligible to consume the API
* @param initialLifecycleStatus initial lifecycle status
* @param description API description
* @param tags tag list for the API
* @param uriTemplates URI templates, i.e - resources
* @param finalLifecycleStatus final lifecycle status
* @throws APIMgtDAOException if it fails to add the API
*/
private void addAPIWithGivenData(String apiName, String apiVersion, String apiContext, String apiProvider, API.Visibility apiVisibility, Set<String> visibleRoles, String initialLifecycleStatus, String description, Set<String> tags, Map<String, UriTemplate> uriTemplates, String finalLifecycleStatus) throws APIMgtDAOException {
API.APIBuilder builder;
ApiDAO apiDAO = DAOFactory.getApiDAO();
builder = SampleTestObjectCreator.createCustomAPI(apiName, apiVersion, apiContext);
builder.provider(apiProvider);
builder.createdBy(apiProvider);
builder.visibility(apiVisibility);
// visible roles should be added for restricted APIs
if (apiVisibility != null && API.Visibility.RESTRICTED.toString().equalsIgnoreCase(apiVisibility.toString())) {
builder.visibleRoles(visibleRoles);
}
builder.lifeCycleStatus(initialLifecycleStatus);
builder.description(description);
builder.tags(tags);
builder.uriTemplates(uriTemplates);
builder.endpoint(Collections.emptyMap());
API api = builder.build();
apiDAO.addAPI(api);
apiDAO.changeLifeCycleStatus(api.getId(), finalLifecycleStatus);
}
Aggregations