Search in sources :

Example 1 with APIComparator

use of org.wso2.carbon.apimgt.core.util.APIComparator in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testSearchAPIs.

@Test
public void testSearchAPIs() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> userRoles = new HashSet<>(Arrays.asList(CUSTOMER_ROLE, MANAGER_ROLE, EMPLOYEE_ROLE));
    // Sample API names
    final String mixedCaseString = "Mixed Case";
    final String lowerCaseString = "lower case";
    final String upperCaseString = "UPPER CASE";
    final String charSymbolNumString = "mi ##symbol 12num";
    final String symbolSpaceString = "_under & Score_";
    // Search string cases
    final String commonMixedCaseSearchString = "CaSe";
    final String commonLowerCaseSearchString = "case";
    final String commonUpperCaseSearchString = "CASE";
    final String symbolSearchString = "##symbol";
    // In some databases numbers are not used in indexing
    final String numberSearchString = "12n";
    // API Provider, the person who owns the API
    final String provider = "John";
    // Create test data
    Map<String, API> apis = new HashMap<>();
    apis.put(mixedCaseString, SampleTestObjectCreator.createUniqueAPI().name(mixedCaseString).provider(provider).build());
    apis.put(lowerCaseString, SampleTestObjectCreator.createUniqueAPI().name(lowerCaseString).provider(provider).build());
    apis.put(upperCaseString, SampleTestObjectCreator.createUniqueAPI().name(upperCaseString).provider(provider).build());
    apis.put(charSymbolNumString, SampleTestObjectCreator.createUniqueAPI().name(charSymbolNumString).provider(provider).build());
    apis.put(symbolSpaceString, SampleTestObjectCreator.createUniqueAPI().name(symbolSpaceString).provider(provider).build());
    // Add APIs
    testAddGetEndpoint();
    for (Map.Entry<String, API> entry : apis.entrySet()) {
        API api = entry.getValue();
        apiDAO.addAPI(api);
        // Replace with summary object for validation
        apis.put(entry.getKey(), SampleTestObjectCreator.getSummaryFromAPI(api));
    }
    // Sleep for indexing
    Thread.sleep(5000);
    // Expected common string formatApiSearch result
    List<API> commonStringResult = new ArrayList<>();
    commonStringResult.add(apis.get(mixedCaseString));
    commonStringResult.add(apis.get(lowerCaseString));
    commonStringResult.add(apis.get(upperCaseString));
    // Search by common mixed case
    List<API> apiList = apiDAO.searchAPIs(new HashSet<>(), provider, commonMixedCaseSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 3);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, commonStringResult, new APIComparator()), TestUtil.printListDiff(apiList, commonStringResult));
    // Search by common lower case
    apiList = apiDAO.searchAPIs(userRoles, provider, commonLowerCaseSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 3);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, commonStringResult, new APIComparator()), TestUtil.printListDiff(apiList, commonStringResult));
    // Search by common upper case
    apiList = apiDAO.searchAPIs(userRoles, provider, commonUpperCaseSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 3);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, commonStringResult, new APIComparator()), TestUtil.printListDiff(apiList, commonStringResult));
    // Search by symbol
    apiList = apiDAO.searchAPIs(userRoles, provider, symbolSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 1);
    API actualAPI = apiList.get(0);
    API expectedAPI = apis.get(charSymbolNumString);
    Assert.assertEquals(actualAPI, expectedAPI, TestUtil.printDiff(actualAPI, expectedAPI));
    // Search by number
    apiList = apiDAO.searchAPIs(userRoles, provider, numberSearchString, 0, 10);
    Assert.assertEquals(apiList.size(), 1);
    actualAPI = apiList.get(0);
    expectedAPI = apis.get(charSymbolNumString);
    Assert.assertEquals(actualAPI, expectedAPI, TestUtil.printDiff(actualAPI, expectedAPI));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 2 with APIComparator

use of org.wso2.carbon.apimgt.core.util.APIComparator in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsWithUserRoles.

@Test(description = "Tests getting the APIs when the user has roles assigned")
public void testGetAPIsWithUserRoles() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    rolesOfUser.add(SampleTestObjectCreator.ADMIN_ROLE_ID);
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    Assert.assertTrue(apiList.isEmpty());
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    builder = SampleTestObjectCreator.createAlternativeAPI();
    API api2 = builder.build();
    apiDAO.addAPI(api2);
    apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    List<API> expectedAPIs = new ArrayList<>();
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api2));
    Assert.assertTrue(apiList.size() == 2);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
Also used : ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with APIComparator

use of org.wso2.carbon.apimgt.core.util.APIComparator in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsByStatusAndLabel.

@Test
public void testGetAPIsByStatusAndLabel() throws Exception {
    // Define statuses used in test
    final String publishedStatus = "PUBLISHED";
    final String createdStatus = "CREATED";
    // Define labels used in test
    final String publicLabel = "public";
    final String privateLabel = "private";
    // Add labels
    LabelDAO labelDAO = DAOFactory.getLabelDAO();
    Label label1 = SampleTestObjectCreator.createLabel(publicLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
    Label label2 = SampleTestObjectCreator.createLabel(privateLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY).build();
    List<Label> labelList = new ArrayList<>();
    labelList.add(label1);
    labelList.add(label2);
    LabelDAOImpl.addLabel(label1);
    LabelDAOImpl.addLabel(label2);
    String publicLabelId = labelDAO.getLabelIdByNameAndType(publicLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
    String privateLabelId = labelDAO.getLabelIdByNameAndType(privateLabel, SampleTestObjectCreator.LABEL_TYPE_GATEWAY);
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    // Define number of APIs to be created for a given status
    final int numberOfPublishedWithLabelPublicPrivate = 1;
    final int numberOfPublishedWithLabelPrivate = 2;
    final int numberOfCreatedWithLabelPublic = 3;
    // Add APIs with Status = PUBLISHED having labels "public" and "private"
    List<API> publishedAPIsPublicPrivateSummary = new ArrayList<>();
    List<String> labelsPublicPrivate = new ArrayList<>(Arrays.asList(publicLabelId, privateLabelId));
    testAddGetEndpoint();
    for (int i = 0; i < numberOfPublishedWithLabelPublicPrivate; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).labels(labelsPublicPrivate).build();
        publishedAPIsPublicPrivateSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // Add APIs with Status = PUBLISHED having label "private"
    List<API> publishedAPIsPrivateSummary = new ArrayList<>();
    List<String> labelsPrivate = new ArrayList<>(Collections.singletonList(privateLabelId));
    for (int i = 0; i < numberOfPublishedWithLabelPrivate; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).labels(labelsPrivate).build();
        publishedAPIsPrivateSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // Add APIs with Status = CREATED having labels "public"
    List<API> createdAPIsPublicSummary = new ArrayList<>();
    List<String> labelsPublic = new ArrayList<>(Collections.singletonList(publicLabelId));
    for (int i = 0; i < numberOfCreatedWithLabelPublic; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(createdStatus).labels(labelsPublic).build();
        createdAPIsPublicSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // verifying APIs with Status = PUBLISHED having labels "public" or "private"
    List<API> publishedPublicPrivateApiListFromDB = apiDAO.getAPIsByStatus(Arrays.asList(publicLabel, privateLabel), publishedStatus);
    List<API> publishedApisWithPublicOrPrivateLabels = new ArrayList<>();
    publishedApisWithPublicOrPrivateLabels.addAll(publishedAPIsPrivateSummary);
    publishedApisWithPublicOrPrivateLabels.addAll(publishedAPIsPublicPrivateSummary);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(publishedPublicPrivateApiListFromDB, publishedApisWithPublicOrPrivateLabels, new APIComparator()));
    List<API> publishedApisWithPrivateLabels = new ArrayList<>();
    publishedApisWithPrivateLabels.addAll(publishedAPIsPrivateSummary);
    publishedApisWithPrivateLabels.addAll(publishedAPIsPublicPrivateSummary);
    // verifying APIs with Status = PUBLISHED having label "private"
    List<API> publishedPrivateApiListFromDB = apiDAO.getAPIsByStatus(Collections.singletonList(privateLabel), publishedStatus);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(publishedPrivateApiListFromDB, publishedApisWithPrivateLabels, new APIComparator()));
    // verifying APIs with Status = CREATED having label "public"
    List<API> createdPublicApiListFromDB = apiDAO.getAPIsByStatus(Collections.singletonList(publicLabel), createdStatus);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(createdPublicApiListFromDB, createdAPIsPublicSummary, new APIComparator()));
    // verifying APIs with Status = CREATED having label "private"
    List<API> createdPrivateApiListFromDB = apiDAO.getAPIsByStatus(Collections.singletonList(privateLabel), createdStatus);
    Assert.assertTrue(createdPrivateApiListFromDB.isEmpty());
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) 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) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 4 with APIComparator

use of org.wso2.carbon.apimgt.core.util.APIComparator in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsWhenUserIsProvider.

@Test(description = "Tests getting the APIs when the user is the provider of the API")
public void testGetAPIsWhenUserIsProvider() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    // The ID here is the group ID of the provider of the API. This ID is not assigned permissions for the API
    rolesOfUser.add(ALTERNATIVE_USER_ROLE_ID);
    // But this user is the provider of the API
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    Assert.assertTrue(apiList.isEmpty());
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    apiList = apiDAO.getAPIs(rolesOfUser, ADMIN);
    List<API> expectedAPIs = new ArrayList<>();
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
    // The provider will have all permissions for the API by default
    Assert.assertTrue(apiList.size() == 1);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
Also used : ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with APIComparator

use of org.wso2.carbon.apimgt.core.util.APIComparator in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsByStatus.

@Test
public void testGetAPIsByStatus() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    // Define statuses used in test
    final String publishedStatus = "PUBLISHED";
    final String createdStatus = "CREATED";
    final String maintenanceStatus = "MAINTENANCE";
    // Define number of APIs to be created for a given status
    final int numberOfPublished = 4;
    final int numberOfCreated = 2;
    final int numberOfInMaintenance = 1;
    // Add APIs
    List<API> publishedAPIsSummary = new ArrayList<>();
    testAddGetEndpoint();
    for (int i = 0; i < numberOfPublished; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).build();
        publishedAPIsSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    List<API> createdAPIsSummary = new ArrayList<>();
    for (int i = 0; i < numberOfCreated; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(createdStatus).build();
        createdAPIsSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    List<API> maintenanceAPIsSummary = new ArrayList<>();
    for (int i = 0; i < numberOfInMaintenance; ++i) {
        API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(maintenanceStatus).build();
        maintenanceAPIsSummary.add(SampleTestObjectCreator.getSummaryFromAPI(api));
        apiDAO.addAPI(api);
    }
    // Filter APIs by single status
    List<String> singleStatus = new ArrayList<>();
    singleStatus.add(publishedStatus);
    List<API> apiList = apiDAO.getAPIsByStatus(singleStatus);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, publishedAPIsSummary, new APIComparator()));
    // Filter APIs by two statuses
    List<String> twoStatuses = new ArrayList<>();
    twoStatuses.add(publishedStatus);
    twoStatuses.add(maintenanceStatus);
    apiList = apiDAO.getAPIsByStatus(twoStatuses);
    Assert.assertEquals(apiList.size(), publishedAPIsSummary.size() + maintenanceAPIsSummary.size());
    for (API api : publishedAPIsSummary) {
        Assert.assertTrue(apiList.contains(api));
        apiList.remove(api);
    }
    for (API api : maintenanceAPIsSummary) {
        Assert.assertTrue(apiList.contains(api));
        apiList.remove(api);
    }
    Assert.assertTrue(apiList.isEmpty());
    // Filter APIs by multiple statuses
    List<String> multipleStatuses = new ArrayList<>();
    multipleStatuses.add(publishedStatus);
    multipleStatuses.add(createdStatus);
    multipleStatuses.add(maintenanceStatus);
    apiList = apiDAO.getAPIsByStatus(multipleStatuses);
    Assert.assertEquals(apiList.size(), publishedAPIsSummary.size() + maintenanceAPIsSummary.size() + createdAPIsSummary.size());
    for (API api : publishedAPIsSummary) {
        Assert.assertTrue(apiList.contains(api));
        apiList.remove(api);
    }
    for (API api : maintenanceAPIsSummary) {
        Assert.assertTrue(apiList.contains(api));
        apiList.remove(api);
    }
    for (API api : createdAPIsSummary) {
        Assert.assertTrue(apiList.contains(api));
        apiList.remove(api);
    }
    Assert.assertTrue(apiList.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)10 API (org.wso2.carbon.apimgt.core.models.API)10 ArrayList (java.util.ArrayList)9 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)9 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)9 APIComparator (org.wso2.carbon.apimgt.core.util.APIComparator)9 HashSet (java.util.HashSet)7 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)1 Label (org.wso2.carbon.apimgt.core.models.Label)1