use of org.wso2.carbon.apimgt.impl.utils.APINameComparator in project carbon-apimgt by wso2.
the class APIConsumerImpl method filterMultipleVersionedAPIs.
private Map<String, Object> filterMultipleVersionedAPIs(Map<String, Object> searchResults) {
Object apiObj = searchResults.get("apis");
ArrayList<Object> apiSet;
ArrayList<APIProduct> apiProductSet = new ArrayList<>();
if (apiObj instanceof Set) {
apiSet = new ArrayList<>(((Set) apiObj));
} else {
apiSet = (ArrayList<Object>) apiObj;
}
// Store the length of the APIs list with the versioned APIs
int apiSetLengthWithVersionedApis = apiSet.size();
int totalLength = Integer.parseInt(searchResults.get("length").toString());
// filter store results if displayMultipleVersions is set to false
Boolean displayMultipleVersions = APIUtil.isAllowDisplayMultipleVersions();
if (!displayMultipleVersions) {
SortedSet<API> resultApis = new TreeSet<API>(new APINameComparator());
for (Object result : apiSet) {
if (result instanceof API) {
resultApis.add((API) result);
} else if (result instanceof Map.Entry) {
Map.Entry<Documentation, API> entry = (Map.Entry<Documentation, API>) result;
resultApis.add(entry.getValue());
} else if (result instanceof APIProduct) {
apiProductSet.add((APIProduct) result);
}
}
Map<String, API> latestPublishedAPIs = new HashMap<String, API>();
Comparator<API> versionComparator = new APIVersionComparator();
String key;
// Run the result api list through API version comparator and filter out multiple versions
for (API api : resultApis) {
key = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName();
API existingAPI = latestPublishedAPIs.get(key);
if (existingAPI != null) {
// this one has a higher version number
if (versionComparator.compare(api, existingAPI) > 0) {
latestPublishedAPIs.put(key, api);
}
} else {
// We haven't seen this API before
latestPublishedAPIs.put(key, api);
}
}
// filter apiSet
ArrayList<Object> tempApiSet = new ArrayList<Object>();
for (Object result : apiSet) {
API api = null;
String mapKey;
API latestAPI;
if (result instanceof API) {
api = (API) result;
mapKey = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName();
if (latestPublishedAPIs.containsKey(mapKey)) {
latestAPI = latestPublishedAPIs.get(mapKey);
if (latestAPI.getId().equals(api.getId())) {
tempApiSet.add(api);
}
}
} else if (result instanceof Map.Entry) {
Map.Entry<Documentation, API> docEntry = (Map.Entry<Documentation, API>) result;
api = docEntry.getValue();
mapKey = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName();
if (latestPublishedAPIs.containsKey(mapKey)) {
latestAPI = latestPublishedAPIs.get(mapKey);
if (latestAPI.getId().equals(api.getId())) {
tempApiSet.add(docEntry);
}
}
}
}
// Store the length of the APIs list without the versioned APIs
int apiSetLengthWithoutVersionedApis = tempApiSet.size();
apiSet = tempApiSet;
ArrayList<Object> resultAPIandProductSet = new ArrayList<>();
resultAPIandProductSet.addAll(apiSet);
resultAPIandProductSet.addAll(apiProductSet);
resultAPIandProductSet.sort(new ContentSearchResultNameComparator());
if (apiObj instanceof Set) {
searchResults.put("apis", new LinkedHashSet<>(resultAPIandProductSet));
} else {
searchResults.put("apis", resultAPIandProductSet);
}
searchResults.put("length", totalLength - (apiSetLengthWithVersionedApis - (apiSetLengthWithoutVersionedApis + apiProductSet.size())));
}
return searchResults;
}
use of org.wso2.carbon.apimgt.impl.utils.APINameComparator in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetAllPublishedAPIs.
@Test
public void testGetAllPublishedAPIs() throws APIManagementException, GovernanceException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
APINameComparator apiNameComparator = Mockito.mock(APINameComparator.class);
SortedSet<API> apiSortedSet = new TreeSet<API>(apiNameComparator);
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(APIUtil.getArtifactManager(apiConsumer.registry, APIConstants.API_KEY)).thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
APIIdentifier apiId1 = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
API api = new API(apiId1);
Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PUBLISHED");
Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
Map<String, API> latestPublishedAPIs = new HashMap<String, API>();
latestPublishedAPIs.put("user:key", api);
apiSortedSet.addAll(latestPublishedAPIs.values());
}
use of org.wso2.carbon.apimgt.impl.utils.APINameComparator in project carbon-apimgt by wso2.
the class APINameComparatorTest method testVersionCompare.
@Test
public void testVersionCompare() throws Exception {
Mockito.when(apiIdentifier1.getApiName()).thenReturn(API_NAME_1);
Mockito.when(apiIdentifier2.getApiName()).thenReturn(API_NAME_1);
Mockito.when(apiIdentifier1.getVersion()).thenReturn(VERSION_1);
Mockito.when(apiIdentifier2.getVersion()).thenReturn(VERSION_2);
APINameComparator apiNameComparator = new APINameComparator();
Assert.assertTrue(apiNameComparator.compare(api1, api2) < 0);
}
use of org.wso2.carbon.apimgt.impl.utils.APINameComparator in project carbon-apimgt by wso2.
the class APINameComparatorTest method testApiNameCompare.
@Test
public void testApiNameCompare() throws Exception {
Mockito.when(apiIdentifier1.getApiName()).thenReturn(API_NAME_1);
Mockito.when(apiIdentifier2.getApiName()).thenReturn(API_NAME_2);
APINameComparator apiNameComparator = new APINameComparator();
Assert.assertTrue(apiNameComparator.compare(api1, api2) < 0);
}
use of org.wso2.carbon.apimgt.impl.utils.APINameComparator in project carbon-apimgt by wso2.
the class APINameComparatorTest method testApiNameCompare.
@Test
public void testApiNameCompare() throws Exception {
Mockito.when(apiIdentifier1.getApiName()).thenReturn(API_NAME_1);
Mockito.when(apiIdentifier2.getApiName()).thenReturn(API_NAME_2);
APINameComparator apiNameComparator = new APINameComparator();
Assert.assertTrue(apiNameComparator.compare(api1, api2) < 0);
}
Aggregations