use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateAPIStatusDeprecatePreviousVersionsAndNotRequireReSubscription.
@Test(description = "Update api status with deprecating previous versions and not require re-subscriptions")
public void testUpdateAPIStatusDeprecatePreviousVersionsAndNotRequireReSubscription() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, applicationDAO, apiSubscriptionDAO, apiLifecycleManager, gatewaySourceGenerator, workflowDAO, gateway);
API previousApi = SampleTestObjectCreator.createDefaultAPI().build();
String previousApiUUID = previousApi.getId();
String lifecycleIdPrevious = previousApi.getLifecycleInstanceId();
String lcStatePrevious = previousApi.getLifeCycleStatus();
LifecycleState previousLifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleIdPrevious);
List<AvailableTransitionBean> list = new ArrayList<>();
AvailableTransitionBean bean = new AvailableTransitionBean("Deprecate", APIStatus.DEPRECATED.getStatus());
list.add(bean);
previousLifecycleState.setAvailableTransitionBeanList(list);
Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleIdPrevious, lcStatePrevious)).thenReturn(previousLifecycleState);
Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.PUBLISHED.getStatus(), APIStatus.DEPRECATED.getStatus(), lifecycleIdPrevious, USER, previousApi)).thenReturn(previousLifecycleState);
previousLifecycleState.setState(APIStatus.DEPRECATED.getStatus());
API api = SampleTestObjectCreator.createDefaultAPI().copiedFromApiId(previousApiUUID).build();
String uuid = api.getId();
String lcState = api.getLifeCycleStatus();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
LifecycleState lifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleId);
Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleId, lcState)).thenReturn(lifecycleState);
Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api)).thenReturn(lifecycleState);
lifecycleState.setState(APIStatus.PUBLISHED.getStatus());
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lifecycleState(lifecycleState);
apiBuilder.updatedBy(USER);
api = apiBuilder.build();
Mockito.when(apiDAO.getAPI(previousApiUUID)).thenReturn(previousApi);
Map<String, Boolean> checklist = new HashMap<>();
checklist.put(APIMgtConstants.DEPRECATE_PREVIOUS_VERSIONS, true);
Application application = SampleTestObjectCreator.createDefaultApplication();
List<Subscription> subscriptions = new ArrayList<>();
Subscription subscription = new Subscription(previousApiUUID, application, previousApi, new SubscriptionPolicy(TIER));
subscriptions.add(subscription);
Mockito.when(apiSubscriptionDAO.getAPISubscriptionsByAPI(previousApiUUID)).thenReturn(subscriptions);
apiPublisher.updateAPIStatus(uuid, APIStatus.PUBLISHED.getStatus(), checklist);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api);
}
use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateSubscriptionStatusException.
@Test(description = "Error when updating subscription status", expectedExceptions = APIManagementException.class)
public void testUpdateSubscriptionStatusException() throws APIManagementException {
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiSubscriptionDAO);
Mockito.doThrow(new APIMgtDAOException("", new Throwable())).when(apiSubscriptionDAO).updateSubscriptionStatus(SUB_ID, APIMgtConstants.SubscriptionStatus.ACTIVE);
apiPublisher.updateSubscriptionStatus(SUB_ID, APIMgtConstants.SubscriptionStatus.ACTIVE);
}
use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO 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.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptions.
@Test(description = "Delete API with zero Subscriptions")
public void testDeleteApiWithZeroSubscriptions() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
List<String> roleIdsOfUser = new ArrayList<>();
roleIdsOfUser.add(ADMIN_ROLE_ID);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.build();
String uuid = api.getId();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(ALTERNATIVE_USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, labelDao, null, null, null, null, gateway);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
Mockito.when(identityProvider.getIdOfUser(ALTERNATIVE_USER)).thenReturn(USER_ID);
Mockito.when(identityProvider.getRoleIdsOfUser(USER_ID)).thenReturn(roleIdsOfUser);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.deleteAPI(uuid);
Mockito.verify(apiDAO, Mockito.times(1)).getAPI(uuid);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).removeLifecycle(lifecycleId);
Mockito.verify(apiDAO, Mockito.times(1)).deleteAPI(uuid);
}
use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.
the class ApiDAOImpl method deleteCompositeApi.
@Override
public void deleteCompositeApi(String apiId) throws APIMgtDAOException {
APISubscriptionDAO apiSubscriptionDAO = DAOFactory.getAPISubscriptionDAO();
List<Subscription> subscriptions = apiSubscriptionDAO.getAPISubscriptionsByAPI(apiId);
for (Subscription subscription : subscriptions) {
apiSubscriptionDAO.deleteAPISubscription(subscription.getId());
}
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(API_DELETE)) {
persistAPIDelete(connection, statement, apiId);
} catch (SQLException | IOException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting Composite API: " + apiId, e);
}
}
Aggregations