Search in sources :

Example 11 with APINameComparator

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;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIVersionComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionComparator) TreeSet(java.util.TreeSet) JSONObject(org.json.simple.JSONObject) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 12 with APINameComparator

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());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) TreeSet(java.util.TreeSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with APINameComparator

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);
}
Also used : APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) Test(org.junit.Test)

Example 14 with APINameComparator

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);
}
Also used : APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) Test(org.junit.Test)

Example 15 with APINameComparator

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);
}
Also used : APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) Test(org.junit.Test)

Aggregations

APINameComparator (org.wso2.carbon.apimgt.impl.utils.APINameComparator)24 API (org.wso2.carbon.apimgt.api.model.API)18 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)18 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)14 TreeSet (java.util.TreeSet)14 JSONObject (org.json.simple.JSONObject)13 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)12 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)10 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)9 APIVersionComparator (org.wso2.carbon.apimgt.impl.utils.APIVersionComparator)8 Registry (org.wso2.carbon.registry.core.Registry)8 UserStoreException (org.wso2.carbon.user.api.UserStoreException)8 Test (org.junit.Test)7 List (java.util.List)6 CommentList (org.wso2.carbon.apimgt.api.model.CommentList)6