Search in sources :

Example 6 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList 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 7 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList 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 8 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList 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 9 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testAttributeSearchAPIs.

@Test(description = "Retrieve summary of paginated data of all available APIs that match the given search criteria")
public void testAttributeSearchAPIs() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    testAddGetEndpoint();
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    apiDAO.addAPI(api);
    Set<String> roles = new HashSet<>();
    Map<String, String> attributeMap = new HashMap<>();
    attributeMap.put("name", api.getName());
    List<API> apiList = apiDAO.attributeSearchAPIs(roles, api.getProvider(), attributeMap, 0, 2);
    Assert.assertTrue(apiList.size() > 0);
}
Also used : HashMap(java.util.HashMap) 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 10 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList 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

ArrayList (java.util.ArrayList)33 API (org.wso2.carbon.apimgt.core.models.API)21 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)15 API (org.wso2.carbon.apimgt.api.model.API)14 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)12 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 JSONObject (org.json.simple.JSONObject)9 APIComparator (org.wso2.carbon.apimgt.core.util.APIComparator)8 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)7 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)6 TreeSet (java.util.TreeSet)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)5 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4 List (java.util.List)4