use of org.wso2.carbon.apimgt.core.util.APIComparator in project carbon-apimgt by wso2.
the class APIComparatorTestCase method testAPIComparator.
@Test
public void testAPIComparator() {
API api1 = new API.APIBuilder("p1", "name1", "1.0.0").build();
API api2 = new API.APIBuilder("p1", "name1", "1.1.0").build();
API api3 = new API.APIBuilder("p1", "name1", "0.9.1").build();
API api4 = new API.APIBuilder("p1", "name1", "1.0.0").build();
API api5 = new API.APIBuilder("p4", "name4", "1.0.0.wso2v1").build();
API api6 = new API.APIBuilder("p4", "name4", "1.0.0.wso2v1").build();
API api7 = new API.APIBuilder("p4", "name4", "1.0.0").build();
API api8 = new API.APIBuilder("p4", "name4", "1.0.0-SNAPSHOT").build();
API api9 = new API.APIBuilder("p4", "name4", "1.1.1").build();
API api10 = new API.APIBuilder("p2", "name4", "1.0.0-SNAPSHOT").build();
API api11 = new API.APIBuilder("p2", "name9", "1.2.9").build();
API api12 = new API.APIBuilder("p5", "name10", "1.3.0").build();
APIComparator apiComparator = new APIComparator();
int comparisonResult1 = apiComparator.compare(api1, api2);
int comparisonResult2 = apiComparator.compare(api1, api3);
int comparisonResult3 = apiComparator.compare(api1, api4);
int comparisonResult4 = apiComparator.compare(api6, api5);
int comparisonResult5 = apiComparator.compare(api7, api8);
int comparisonResult6 = apiComparator.compare(api9, api10);
int comparisonResult7 = apiComparator.compare(api10, api11);
int comparisonResult8 = apiComparator.compare(api11, api12);
Assert.assertTrue(comparisonResult1 < 0, "Error, comparisonResult is non negative. ");
Assert.assertTrue(comparisonResult2 > 0, "Error, comparisonResult is non positive. ");
Assert.assertEquals(comparisonResult3, 0, "Error, compared versions are not equal. ");
Assert.assertEquals(comparisonResult4, 0, "Error, compared versions are not equal. ");
Assert.assertTrue(comparisonResult5 > 0, "Error, comparisonResult is non positive. ");
Assert.assertTrue(comparisonResult6 > 0, "Error, comparisonResult is non positive. ");
Assert.assertTrue(comparisonResult7 < 0, "Error, comparisonResult is non negative. ");
Assert.assertTrue(comparisonResult8 < 0, "Error, comparisonResult is non negative. ");
}
use of org.wso2.carbon.apimgt.core.util.APIComparator 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.core.util.APIComparator 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.core.util.APIComparator 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.core.util.APIComparator 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));
}
Aggregations