Search in sources :

Example 96 with Collection

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();
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) OMElement(org.apache.axiom.om.OMElement) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 97 with Collection

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();
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) OMElement(org.apache.axiom.om.OMElement) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 98 with Collection

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");
    }
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 99 with Collection

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();
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) SearchResultDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SearchResultDTO) SearchResultListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SearchResultListDTO) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) API(org.wso2.carbon.apimgt.api.model.API) HashMap(java.util.HashMap) Map(java.util.Map)

Example 100 with Collection

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();
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Collection(org.wso2.carbon.registry.core.Collection) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Aggregations

Collection (org.wso2.carbon.registry.core.Collection)45 Resource (org.wso2.carbon.registry.core.Resource)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 Test (org.junit.Test)24 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)23 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)20 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 ArrayList (java.util.ArrayList)17 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)17 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)16 FileInputStream (java.io.FileInputStream)14 OMElement (org.apache.axiom.om.OMElement)13 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)13 Collection (java.util.Collection)11 CollectionImpl (org.wso2.carbon.registry.core.CollectionImpl)11 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)11 File (java.io.File)10