Search in sources :

Example 1 with TermData

use of org.wso2.carbon.registry.common.TermData in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetTagsWithAttributes.

@Test
public void testGetTagsWithAttributes() throws Exception {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, apiPersistenceInstance);
    System.setProperty(CARBON_HOME, "");
    PowerMockito.mockStatic(GovernanceUtils.class);
    UserRegistry userRegistry1 = Mockito.mock(UserRegistry.class);
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(userRegistry1);
    Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry1);
    List<TermData> list = new ArrayList<TermData>();
    TermData termData = new TermData("testTerm", 10);
    list.add(termData);
    Mockito.when(GovernanceUtils.getTermDataList(Mockito.anyMap(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean())).thenReturn(list);
    ResourceDO resourceDO = Mockito.mock(ResourceDO.class);
    Resource resource = new ResourceImpl("dw", resourceDO);
    resource.setContent("testContent");
    Mockito.when(userRegistry1.resourceExists(Mockito.anyString())).thenReturn(true);
    Mockito.when(userRegistry1.get(Mockito.anyString())).thenReturn(resource);
    assertNotNull(apiConsumer.getTagsWithAttributes("testDomain"));
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) TermData(org.wso2.carbon.registry.common.TermData) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with TermData

use of org.wso2.carbon.registry.common.TermData in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getAllTags.

@Override
public Set<Tag> getAllTags(Organization org, UserContext ctx) throws APIPersistenceException {
    TreeSet<Tag> tempTagSet = new TreeSet<Tag>(new Comparator<Tag>() {

        @Override
        public int compare(Tag o1, Tag o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    Registry userRegistry = null;
    boolean tenantFlowStarted = false;
    String tagsQueryPath = null;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        tenantFlowStarted = holder.isTenantFlowStarted();
        userRegistry = holder.getRegistry();
        tagsQueryPath = RegistryConstants.QUERIES_COLLECTION_PATH + "/tag-summary";
        Map<String, String> params = new HashMap<String, String>();
        params.put(RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.TAG_SUMMARY_RESULT_TYPE);
        String userNameLocal;
        if (holder.isAnonymousMode()) {
            userNameLocal = APIConstants.WSO2_ANONYMOUS_USER;
        } else {
            userNameLocal = getTenantAwareUsername(ctx.getUserame());
        }
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userNameLocal);
        Map<String, Tag> tagsData = new HashMap<String, Tag>();
        Map<String, List<String>> criteriaPublished = new HashMap<String, List<String>>();
        criteriaPublished.put(APIConstants.LCSTATE_SEARCH_KEY, new ArrayList<String>() {

            {
                add(APIConstants.PUBLISHED);
            }
        });
        // rxt api media type
        List<TermData> termsPublished = GovernanceUtils.getTermDataList(criteriaPublished, APIConstants.API_OVERVIEW_TAG, APIConstants.API_RXT_MEDIA_TYPE, true);
        if (termsPublished != null) {
            for (TermData data : termsPublished) {
                tempTagSet.add(new Tag(data.getTerm(), (int) data.getFrequency()));
            }
        }
        Map<String, List<String>> criteriaPrototyped = new HashMap<String, List<String>>();
        criteriaPrototyped.put(APIConstants.LCSTATE_SEARCH_KEY, new ArrayList<String>() {

            {
                add(APIConstants.PROTOTYPED);
            }
        });
        // rxt api media type
        List<TermData> termsPrototyped = GovernanceUtils.getTermDataList(criteriaPrototyped, APIConstants.API_OVERVIEW_TAG, APIConstants.API_RXT_MEDIA_TYPE, true);
        if (termsPrototyped != null) {
            for (TermData data : termsPrototyped) {
                tempTagSet.add(new Tag(data.getTerm(), (int) data.getFrequency()));
            }
        }
        return tempTagSet;
    } catch (RegistryException e) {
        try {
            // give a warn.
            if (userRegistry != null && !userRegistry.resourceExists(tagsQueryPath)) {
                log.warn("Failed to retrieve tags query resource at " + tagsQueryPath);
                return Collections.EMPTY_SET;
            }
        } catch (RegistryException e1) {
            // Even if we should ignore this exception, we are logging this as a warn log.
            // The reason is that, this error happens when we try to add some additional logs in an error
            // scenario and it does not affect the execution path.
            log.warn("Unable to execute the resource exist method for tags query resource path : " + tagsQueryPath, e1);
        }
        throw new APIPersistenceException("Failed to get all the tags", e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) HashMap(java.util.HashMap) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) TreeSet(java.util.TreeSet) TermData(org.wso2.carbon.registry.common.TermData) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) List(java.util.List) Tag(org.wso2.carbon.apimgt.api.model.Tag)

Aggregations

ArrayList (java.util.ArrayList)2 TermData (org.wso2.carbon.registry.common.TermData)2 Registry (org.wso2.carbon.registry.core.Registry)2 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)2 HashMap (java.util.HashMap)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Tag (org.wso2.carbon.apimgt.api.model.Tag)1 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)1 Resource (org.wso2.carbon.registry.core.Resource)1 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1 ResourceDO (org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO)1