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"));
}
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();
}
}
}
Aggregations