use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIUtilTest method testGetCustomOutSequence.
@Test
public void testGetCustomOutSequence() throws Exception {
APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Collection collection = Mockito.mock(Collection.class);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "out" + RegistryConstants.PATH_SEPARATOR;
Mockito.when(registry.get(eq(path))).thenReturn(collection);
String[] childPaths = { "test" };
Mockito.when(collection.getChildren()).thenReturn(childPaths);
InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
Resource resource = Mockito.mock(Resource.class);
Mockito.when(registry.get(eq("test"))).thenReturn(resource);
Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
OMElement customSequence = APIUtil.getCustomSequence("sample", 1, "out", apiIdentifier);
Assert.assertNotNull(customSequence);
sampleSequence.close();
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIUtilTest method testGetCustomInSequence.
@Test
public void testGetCustomInSequence() throws Exception {
APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Collection collection = Mockito.mock(Collection.class);
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "in" + RegistryConstants.PATH_SEPARATOR;
Mockito.when(registry.get(eq(path))).thenReturn(collection);
String[] childPaths = { "test" };
Mockito.when(collection.getChildren()).thenReturn(childPaths);
InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
Resource resource = Mockito.mock(Resource.class);
Mockito.when(registry.get(eq("test"))).thenReturn(resource);
Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
OMElement customSequence = APIUtil.getCustomSequence("sample", 1, "in", apiIdentifier);
Assert.assertNotNull(customSequence);
sampleSequence.close();
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class SequenceUtilsTestCase method testGetRestToSoapConvertedSequence.
public void testGetRestToSoapConvertedSequence() throws Exception {
String provider = "admin";
String apiName = "test-api";
String version = "1.0.0";
String seqType = "in";
API api = new API(new APIIdentifier(provider, apiName, version));
List<SOAPToRestSequence> soapToRestSequences = new ArrayList<SOAPToRestSequence>();
SOAPToRestSequence seq = new SOAPToRestSequence("post", "test", "<>", Direction.IN);
soapToRestSequences.add(seq);
SOAPToRestSequence seq2 = new SOAPToRestSequence("post", "test", "<>", Direction.OUT);
soapToRestSequences.add(seq2);
api.setSoapToRestSequences(soapToRestSequences);
String resourceName = "test_get.xml";
Resource resource = Mockito.mock(Resource.class);
ResourceImpl resourceImpl = Mockito.mock(ResourceImpl.class);
Collection collection = Mockito.mock(Collection.class);
String[] paths = new String[0];
byte[] content = new byte[1];
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
PowerMockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(((Collection) userRegistry.get(Mockito.anyString()))).thenReturn(collection);
Mockito.when(collection.getChildren()).thenReturn(paths);
Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
Mockito.when(resource.getContent()).thenReturn(content);
Mockito.when(resourceImpl.getName()).thenReturn(resourceName);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenReturn(-1);
try {
SequenceUtils.getRestToSoapConvertedSequence(api, seqType);
} catch (APIManagementException e) {
Assert.fail("Failed to get the sequences from the registry");
}
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class SearchApiServiceImpl method search.
public Response search(Integer limit, Integer offset, String query, String ifNoneMatch, MessageContext messageContext) throws APIManagementException {
SearchResultListDTO resultListDTO = new SearchResultListDTO();
List<SearchResultDTO> allmatchedResults = new ArrayList<>();
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
query = query == null ? "*" : query;
if (!query.contains(":")) {
query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getOrganization(messageContext);
Map<String, Object> result = null;
if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
result = apiProvider.searchPaginatedContent(query, organization, offset, limit);
} else {
result = apiProvider.searchPaginatedAPIs(query, organization, offset, limit, null, null);
}
ArrayList<Object> apis;
/* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
TreeSet and a HashMap.
Hence the below logic.
*/
Object apiSearchResults = result.get("apis");
if (apiSearchResults instanceof List<?>) {
apis = (ArrayList<Object>) apiSearchResults;
} else if (apiSearchResults instanceof HashMap) {
Collection<String> values = ((HashMap) apiSearchResults).values();
apis = new ArrayList<Object>(values);
} else {
apis = new ArrayList<Object>();
apis.addAll((Collection<?>) apiSearchResults);
}
for (Object searchResult : apis) {
if (searchResult instanceof API) {
API api = (API) searchResult;
SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
allmatchedResults.add(apiResult);
} else if (searchResult instanceof APIProduct) {
APIProduct apiproduct = (APIProduct) searchResult;
SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIProductToAPIResultDTO(apiproduct);
allmatchedResults.add(apiResult);
} else if (searchResult instanceof Map.Entry) {
Map.Entry pair = (Map.Entry) searchResult;
SearchResultDTO docResult;
if (pair.getValue() instanceof API) {
docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
} else {
docResult = SearchResultMappingUtil.fromDocumentationToProductDocumentResultDTO((Documentation) pair.getKey(), (APIProduct) pair.getValue());
}
allmatchedResults.add(docResult);
}
}
Object totalLength = result.get("length");
Integer length = 0;
if (totalLength != null) {
length = (Integer) totalLength;
}
List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
resultListDTO.setList(allmatchedObjectResults);
resultListDTO.setCount(allmatchedResults.size());
SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
return Response.ok().entity(resultListDTO).build();
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class SequenceUtils method updateResourcePolicyFromRegistryResourceId.
/**
* Updates resource policy resource for the given resource id from the registry.
*
* @param identifier API identifier
* @param resourceId Resource identifier
* @param content resource policy content
* @throws APIManagementException
*/
public static void updateResourcePolicyFromRegistryResourceId(APIIdentifier identifier, String resourceId, String content) throws APIManagementException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
APIUtil.loadTenantRegistry(tenantId);
UserRegistry registry = registryService.getGovernanceSystemRegistry(tenantId);
String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE;
Collection collection = (Collection) registry.get(resourcePath);
String[] resources = collection.getChildren();
if (resources == null) {
handleException("Cannot find any resource policies at the path: " + resourcePath);
}
for (String path : resources) {
Collection resourcePolicyCollection = (Collection) registry.get(path);
String[] resourcePolicies = resourcePolicyCollection.getChildren();
if (resourcePolicies == null) {
if (log.isDebugEnabled()) {
log.debug("Cannot find resource policies under path: " + path);
}
continue;
}
for (String resourcePolicyPath : resourcePolicies) {
Resource resource = registry.get(resourcePolicyPath);
if (StringUtils.isNotEmpty(resourceId) && resourceId.equals(((ResourceImpl) resource).getUUID())) {
resource.setContent(content);
resource.setMediaType(SOAPToRESTConstants.TEXT_XML);
registry.put(resourcePolicyPath, resource);
break;
}
}
}
if (log.isDebugEnabled()) {
log.debug("Number of REST resources for " + resourcePath + " is: " + resources.length);
}
} catch (UserStoreException e) {
handleException("Error while reading tenant information", e);
} catch (RegistryException e) {
handleException("Error when create registry instance", e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
handleException("Error while setting the resource policy content for the registry resource", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
Aggregations