Search in sources :

Example 66 with LabelDAO

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");
    }
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 67 with LabelDAO

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);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ArrayList(java.util.ArrayList) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) API(org.wso2.carbon.apimgt.core.models.API) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 68 with LabelDAO

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));
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 69 with LabelDAO

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));
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) Test(org.testng.annotations.Test)

Example 70 with LabelDAO

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);
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) Test(org.testng.annotations.Test)

Aggregations

LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)71 Test (org.testng.annotations.Test)70 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)45 API (org.wso2.carbon.apimgt.core.models.API)44 ArrayList (java.util.ArrayList)33 Label (org.wso2.carbon.apimgt.core.models.Label)33 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)28 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)26 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)26 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)26 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)17 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)16 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)15 HashMap (java.util.HashMap)14 DedicatedGateway (org.wso2.carbon.apimgt.core.models.DedicatedGateway)14 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)14 BeforeTest (org.testng.annotations.BeforeTest)13 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 WorkflowExtensionsConfigBuilder (org.wso2.carbon.apimgt.core.workflow.WorkflowExtensionsConfigBuilder)11