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);
}
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);
}
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));
}
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));
}
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);
}
Aggregations