use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testGetAPIByStatus.
@Test(expectedExceptions = APIMgtDAOException.class)
public void testGetAPIByStatus() 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 by role
final int numberOfPublishedAdmin = 4;
final int numberOfCreatedAdmin = 2;
final int numberOfMaintenanceAdmin = 1;
final int numberOfPublishedCreator = 5;
final int numberOfCreatedCreator = 3;
final int numberOfMaintenanceCreator = 1;
Set<String> singleRole = new HashSet<>();
singleRole.add(ADMIN);
// Add APIs
List<API> publishedAPIsSummaryAdmin = new ArrayList<>();
testAddGetEndpoint();
for (int i = 0; i < numberOfPublishedAdmin; ++i) {
API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).visibleRoles(singleRole).build();
publishedAPIsSummaryAdmin.add(SampleTestObjectCreator.getSummaryFromAPI(api));
apiDAO.addAPI(api);
}
List<API> createdAPIsSummaryAdmin = new ArrayList<>();
for (int i = 0; i < numberOfCreatedAdmin; ++i) {
API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(createdStatus).visibleRoles(singleRole).build();
createdAPIsSummaryAdmin.add(SampleTestObjectCreator.getSummaryFromAPI(api));
apiDAO.addAPI(api);
}
List<API> maintenanceAPIsSummaryAdmin = new ArrayList<>();
for (int i = 0; i < numberOfMaintenanceAdmin; ++i) {
API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(maintenanceStatus).visibleRoles(singleRole).build();
maintenanceAPIsSummaryAdmin.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(singleRole, singleStatus, new ArrayList<>());
Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, publishedAPIsSummaryAdmin, new APIComparator()));
List<String> twoStatus = new ArrayList<>();
twoStatus.add(createdStatus);
twoStatus.add(maintenanceStatus);
Set<String> twoRoles = new HashSet<>();
twoRoles.add(ADMIN);
twoRoles.add(CREATOR);
// Add APIs
List<API> publishedAPIsSummaryTwoRoles = new ArrayList<>();
testAddGetEndpoint();
for (int i = 0; i < numberOfPublishedCreator; ++i) {
API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(publishedStatus).visibleRoles(twoRoles).build();
publishedAPIsSummaryTwoRoles.add(SampleTestObjectCreator.getSummaryFromAPI(api));
apiDAO.addAPI(api);
}
List<API> createdAPIsSummaryTwoRoles = new ArrayList<>();
for (int i = 0; i < numberOfCreatedCreator; ++i) {
API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(createdStatus).visibleRoles(twoRoles).build();
createdAPIsSummaryTwoRoles.add(SampleTestObjectCreator.getSummaryFromAPI(api));
apiDAO.addAPI(api);
}
List<API> maintenanceAPIsSummaryTwoRoles = new ArrayList<>();
for (int i = 0; i < numberOfMaintenanceCreator; ++i) {
API api = SampleTestObjectCreator.createUniqueAPI().lifeCycleStatus(maintenanceStatus).visibleRoles(twoRoles).build();
maintenanceAPIsSummaryTwoRoles.add(SampleTestObjectCreator.getSummaryFromAPI(api));
apiDAO.addAPI(api);
}
apiList = apiDAO.getAPIsByStatus(twoRoles, twoStatus, new ArrayList<>());
Assert.assertEquals(apiList.size(), publishedAPIsSummaryTwoRoles.size() + maintenanceAPIsSummaryTwoRoles.size());
for (API api : publishedAPIsSummaryTwoRoles) {
Assert.assertTrue(apiList.contains(api));
apiList.remove(api);
}
for (API api : maintenanceAPIsSummaryTwoRoles) {
Assert.assertTrue(apiList.contains(api));
apiList.remove(api);
}
Assert.assertTrue(apiList.isEmpty());
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testGetAPIs.
@Test(description = "Tests getting the APIs when the user has no roles assigned")
public void testGetAPIs() throws Exception {
ApiDAO apiDAO = DAOFactory.getApiDAO();
List<API> apiList = apiDAO.getAPIs(new HashSet<>(), 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(new HashSet<>(), 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.APIList in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testGetAPIsWhenUserRolesInAPIPermissions.
@Test(description = "Tests getting the APIs when the user roles are contained in the API permission list")
public void testGetAPIsWhenUserRolesInAPIPermissions() 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());
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
API api1 = builder.build();
testAddGetEndpoint();
apiDAO.addAPI(api1);
apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
List<API> expectedAPIs = new ArrayList<>();
expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
// Since the API has the role ID of the user with READ permissions, it is visible to this user
Assert.assertTrue(apiList.size() == 1);
Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testGetAPIsWhenAPIHasNoPermissionsAssigned.
@Test(description = "Tests getting the APIs when the API has no permissions assigned")
public void testGetAPIsWhenAPIHasNoPermissionsAssigned() 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 not the provider of the API
List<API> apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
Assert.assertTrue(apiList.isEmpty());
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().permissionMap(null);
API api1 = builder.build();
testAddGetEndpoint();
apiDAO.addAPI(api1);
apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
List<API> expectedAPIs = new ArrayList<>();
expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
// Since the API has no permissions assigned specifically, it is visible to every one
Assert.assertTrue(apiList.size() == 1);
Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.
the class ApiDAOImplIT method compareResults.
/**
* Compare the results of attribute search in store
*
* @param userRoles List of the roles of the user.
* @param attributeMap Map containing the attributes to be searched
* @param expectedAPINames List of expected APIs.
* @return true if returned API list has all expected APIs, false otherwise
* @throws APIMgtDAOException if error occurs while accessing data layer
*/
private boolean compareResults(List<String> userRoles, List<String> labels, Map<String, String> attributeMap, String[] expectedAPINames) throws APIMgtDAOException {
ApiDAO apiDAO = DAOFactory.getApiDAO();
List<API> apiList = apiDAO.searchAPIsByAttributeInStore(userRoles, labels, attributeMap, 10, 0);
List<String> resultAPINameList = new ArrayList<>();
for (API api : apiList) {
resultAPINameList.add(api.getName());
}
List<String> expectedAPINameList = Arrays.asList(expectedAPINames);
// check if returned API list has all expected APIs
return resultAPINameList.containsAll(expectedAPINameList) && expectedAPINameList.containsAll(resultAPINameList);
}
Aggregations