use of org.wso2.carbon.apimgt.core.dao.LabelDAO 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.LabelDAO 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.LabelDAO in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testAddGetAPIWithLabels.
@Test
public void testAddGetAPIWithLabels() throws Exception {
LabelDAO labelDAO = DAOFactory.getLabelDAO();
Label labelPublic = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
Label labelPrivate = SampleTestObjectCreator.createLabel("private", SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
List<Label> labelList = new ArrayList<>();
labelList.add(labelPublic);
labelList.add(labelPrivate);
LabelDAOImpl.addLabel(labelPublic);
LabelDAOImpl.addLabel(labelPrivate);
String publicLabelFromDB = labelDAO.getLabelIdByNameAndType("public", SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
String privateLabelFromDB = labelDAO.getLabelIdByNameAndType("private", SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
ApiDAO apiDAO = DAOFactory.getApiDAO();
List<String> labelIds = new ArrayList<>();
labelIds.add(publicLabelFromDB);
labelIds.add(privateLabelFromDB);
Collections.sort(labelIds);
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
API apiWithBothLabels = builder.labels(labelIds).build();
testAddGetEndpoint();
apiDAO.addAPI(apiWithBothLabels);
List<String> publicLabelOnly = new ArrayList<>();
publicLabelOnly.add(publicLabelFromDB);
API.APIBuilder builder2 = SampleTestObjectCreator.createAlternativeAPI();
API apiWithPublicLabel = builder2.labels(publicLabelOnly).build();
apiDAO.addAPI(apiWithPublicLabel);
API apiFromDB = apiDAO.getAPI(apiWithBothLabels.getId());
Assert.assertNotNull(apiFromDB);
Assert.assertEquals(apiFromDB.getLabels().size(), 2);
Assert.assertTrue(apiWithBothLabels.getLabels().equals(apiFromDB.getLabels()), TestUtil.printDiff(apiWithBothLabels.getLabels(), apiFromDB.getLabels()));
List<API> apiListPublicPrivate = apiDAO.getAPIsByGatewayLabel(Arrays.asList(labelPublic.getName(), labelPrivate.getName()));
Assert.assertEquals(apiListPublicPrivate.size(), 2);
Assert.assertTrue(TestUtil.testAPIEqualsLazy(apiListPublicPrivate.get(0), apiWithBothLabels) || TestUtil.testAPIEqualsLazy(apiListPublicPrivate.get(1), apiWithBothLabels));
Assert.assertTrue(TestUtil.testAPIEqualsLazy(apiListPublicPrivate.get(0), apiWithPublicLabel) || TestUtil.testAPIEqualsLazy(apiListPublicPrivate.get(1), apiWithPublicLabel));
List<API> apiListPrivate = apiDAO.getAPIsByGatewayLabel(Collections.singletonList(labelPrivate.getName()));
Assert.assertEquals(apiListPrivate.size(), 1);
Assert.assertTrue(TestUtil.testAPIEqualsLazy(apiListPrivate.get(0), apiWithBothLabels));
Assert.assertFalse(TestUtil.testAPIEqualsLazy(apiListPrivate.get(0), apiWithPublicLabel));
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class LabelDAOImplIT method testUpdateLabel.
@Test
public void testUpdateLabel() throws Exception {
LabelDAO labelDAO = DAOFactory.getLabelDAO();
Label label = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
Label labelAddedtoDB = LabelDAOImpl.addLabel(label);
List<String> accessUrls = new ArrayList<>();
accessUrls.add("https://updated.public");
Label updatedLabel = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_GATEWAY).id(labelAddedtoDB.getId()).accessUrls(accessUrls).build();
labelDAO.updateLabel(updatedLabel);
List<Label> labelsFromDb = labelDAO.getLabelsByType(SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
Assert.assertNotNull(labelsFromDb);
Assert.assertEquals(labelsFromDb.size(), 2);
Assert.assertTrue(labelsFromDb.contains(updatedLabel));
}
use of org.wso2.carbon.apimgt.core.dao.LabelDAO in project carbon-apimgt by wso2.
the class LabelDAOImplIT method testGetLabelsByNameForIncorrectLabelNames.
@Test
public void testGetLabelsByNameForIncorrectLabelNames() throws Exception {
LabelDAO labelDAO = DAOFactory.getLabelDAO();
Label label1 = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
Label label2 = SampleTestObjectCreator.createLabel("private", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
LabelDAOImpl.addLabel(label1);
LabelDAOImpl.addLabel(label2);
List<String> labelNames = new ArrayList<>();
labelNames.add("test");
labelNames.add(label1.getName());
List<Label> labelFromDb = labelDAO.getLabelsByName(labelNames);
Assert.assertNotNull(labelFromDb);
Assert.assertEquals(labelFromDb.size(), 1);
}
Aggregations