Search in sources :

Example 81 with API

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

the class ApiDAOImplIT method testGetAllAverageAndUserRatingsOfAPI.

@Test
public void testGetAllAverageAndUserRatingsOfAPI() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    Rating rating1 = SampleTestObjectCreator.createDefaultRating(api.getId());
    apiDAO.addRating(api.getId(), rating1);
    Rating rating2 = SampleTestObjectCreator.createDefaultRating(api.getId());
    rating2.setRating(2);
    rating2.setUsername("andrew");
    apiDAO.addRating(api.getId(), rating2);
    Rating rating3 = SampleTestObjectCreator.createDefaultRating(api.getId());
    rating3.setRating(3);
    rating3.setUsername("smith");
    apiDAO.addRating(api.getId(), rating3);
    List<Rating> ratingsListFromDB = apiDAO.getRatingsListForApi(api.getId());
    Assert.assertNotNull(ratingsListFromDB);
    Assert.assertEquals(ratingsListFromDB.size(), 3);
    Rating ratingOfJohn = apiDAO.getUserRatingForApiFromUser(api.getId(), "john");
    Rating ratingOfAndrew = apiDAO.getUserRatingForApiFromUser(api.getId(), "andrew");
    Rating ratingOfSmith = apiDAO.getUserRatingForApiFromUser(api.getId(), "smith");
    Assert.assertEquals(ratingOfJohn.getRating(), 4);
    Assert.assertEquals(ratingOfAndrew.getRating(), 2);
    Assert.assertEquals(ratingOfSmith.getRating(), 3);
    double averageRating = apiDAO.getAverageRating(api.getId());
    Assert.assertEquals(averageRating, 3, 0.0001);
}
Also used : Rating(org.wso2.carbon.apimgt.core.models.Rating) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 82 with API

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

the class ApiDAOImplIT method testGetAPIsWhenUserRolesInAPIPermissionsWithoutREAD.

@Test(description = "Tests getting the APIs when the user roles are contained in the API permission list " + "but without READ permissions")
public void testGetAPIsWhenUserRolesInAPIPermissionsWithoutREAD() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    rolesOfUser.add(SampleTestObjectCreator.DEVELOPER_ROLE_ID);
    // This user is not the provider of the API
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
    Assert.assertTrue(apiList.isEmpty());
    Map map = new HashMap();
    map.put(SampleTestObjectCreator.DEVELOPER_ROLE_ID, 0);
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().permissionMap(map);
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
    // Since the API has the role ID of the user but without READ permissions, it is not visible to this user
    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) 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 83 with API

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

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

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

the class ApiDAOImplIT method testGetDocumentInlineContent.

@Test(description = "Getting document inline content for an API")
public void testGetDocumentInlineContent() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    testAddGetEndpoint();
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    apiDAO.addAPI(api);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo();
    apiDAO.addDocumentInfo(api.getId(), documentInfo);
    String inlineDocContent = SampleTestObjectCreator.createDefaultInlineDocumentationContent();
    apiDAO.addDocumentInlineContent(documentInfo.getId(), inlineDocContent, ADMIN);
    String inlineDocContentFromDB = apiDAO.getDocumentInlineContent(documentInfo.getId());
    Assert.assertEquals(inlineDocContent, inlineDocContentFromDB);
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)582 ArrayList (java.util.ArrayList)374 API (org.wso2.carbon.apimgt.core.models.API)359 Test (org.testng.annotations.Test)350 HashMap (java.util.HashMap)318 Test (org.junit.Test)316 API (org.wso2.carbon.apimgt.api.model.API)307 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)255 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)253 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)234 SQLException (java.sql.SQLException)190 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)186 IOException (java.io.IOException)181 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)179 PreparedStatement (java.sql.PreparedStatement)169 Connection (java.sql.Connection)158 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)154 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)149 JSONObject (org.json.simple.JSONObject)142 Resource (org.wso2.carbon.registry.core.Resource)139