Search in sources :

Example 36 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testGetMediationSequenceUuidCustomSequenceNotFound.

@Test
public void testGetMediationSequenceUuidCustomSequenceNotFound() 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 + "custom" + RegistryConstants.PATH_SEPARATOR;
    Mockito.when(registry.get(eq(path))).thenReturn(null, collection);
    String[] childPaths = { "test" };
    Mockito.when(collection.getChildren()).thenReturn(childPaths);
    String expectedUUID = UUID.randomUUID().toString();
    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);
    Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
    String actualUUID = APIUtil.getMediationSequenceUuid("sample", 1, "custom", apiIdentifier);
    Assert.assertEquals(expectedUUID, actualUUID);
    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) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testGetAPIWithGovernanceArtifactAdvancedThrottlingDisabled.

@Test
public void testGetAPIWithGovernanceArtifactAdvancedThrottlingDisabled() throws Exception {
    System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        API expectedAPI = getUniqueAPI();
        final String provider = expectedAPI.getId().getProviderName();
        final String tenantDomain = org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        final int tenantId = -1234;
        System.setProperty("carbon.home", "");
        File siteConfFile = new File(Thread.currentThread().getContextClassLoader().getResource("tenant-conf.json").getFile());
        String tenantConfValue = FileUtils.readFileToString(siteConfFile);
        GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
        Registry registry = Mockito.mock(Registry.class);
        ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
        Resource resource = Mockito.mock(Resource.class);
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        RealmService realmService = Mockito.mock(RealmService.class);
        TenantManager tenantManager = Mockito.mock(TenantManager.class);
        APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
        APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
        ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
        SubscriptionPolicy policy = Mockito.mock(SubscriptionPolicy.class);
        SubscriptionPolicy[] policies = new SubscriptionPolicy[] { policy };
        QuotaPolicy quotaPolicy = Mockito.mock(QuotaPolicy.class);
        RequestCountLimit limit = Mockito.mock(RequestCountLimit.class);
        RegistryService registryService = Mockito.mock(RegistryService.class);
        UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
        APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
        Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
        Mockito.when(apimConfigService.getTenantConfig(tenantDomain)).thenReturn(tenantConfValue);
        PowerMockito.mockStatic(ApiMgtDAO.class);
        PowerMockito.mockStatic(GovernanceUtils.class);
        PowerMockito.mockStatic(MultitenantUtils.class);
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
        Mockito.when(apiMgtDAO.getAPIID(Mockito.any(String.class))).thenReturn(123);
        Mockito.when(apiMgtDAO.getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, provider)).thenReturn(new String[] { "Unlimited" });
        Mockito.when(artifact.getId()).thenReturn("");
        Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn(provider);
        Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT)).thenReturn("15");
        Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_TIER)).thenReturn("Unlimited");
        Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(tenantDomain);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
        Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
        Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
        Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
        Mockito.when(registryService.getConfigSystemRegistry(tenantId)).thenReturn(userRegistry);
        String artifactPath = "";
        Mockito.when(GovernanceUtils.getArtifactPath(registry, "")).thenReturn(artifactPath);
        Mockito.when(registry.get(artifactPath)).thenReturn(resource);
        Mockito.when(resource.getLastModified()).thenReturn(expectedAPI.getLastUpdated());
        Mockito.when(resource.getCreatedTime()).thenReturn(expectedAPI.getLastUpdated());
        Mockito.when(resource.getContent()).thenReturn(tenantConfValue.getBytes());
        Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
        Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
        Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
        Mockito.when(apiMgtDAO.getSubscriptionPolicies(tenantId)).thenReturn(policies);
        Mockito.when(policy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
        Mockito.when(quotaPolicy.getLimit()).thenReturn(limit);
        Mockito.when(registry.getTags(artifactPath)).thenReturn(getTagsFromSet(expectedAPI.getTags()));
        ArrayList<URITemplate> urlList = getURLTemplateList(expectedAPI.getUriTemplates());
        Mockito.when(apiMgtDAO.getAllURITemplates(Mockito.anyString(), Mockito.anyString())).thenReturn(urlList);
        CORSConfiguration corsConfiguration = expectedAPI.getCorsConfiguration();
        Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_HEADERS)).thenReturn(corsConfiguration.getAccessControlAllowHeaders().toString());
        Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_METHODS)).thenReturn(corsConfiguration.getAccessControlAllowMethods().toString());
        Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_ORIGIN)).thenReturn(corsConfiguration.getAccessControlAllowOrigins().toString());
        API api = APIUtil.getAPI(artifact);
        Assert.assertNotNull(api);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) Resource(org.wso2.carbon.registry.core.Resource) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIUtil.getOAuthConfigurationFromTenantRegistry(org.wso2.carbon.apimgt.impl.utils.APIUtil.getOAuthConfigurationFromTenantRegistry) Registry(org.wso2.carbon.registry.core.Registry) CORSConfiguration(org.wso2.carbon.apimgt.api.model.CORSConfiguration) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) RealmService(org.wso2.carbon.user.core.service.RealmService) API(org.wso2.carbon.apimgt.api.model.API) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) File(java.io.File) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testGetMediationSequenceUuidOutSequence.

@Test
public void testGetMediationSequenceUuidOutSequence() 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 path = APIConstants.API_CUSTOM_SEQUENCE_LOCATION + File.separator + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT;
    Mockito.when(registry.get(eq(path))).thenReturn(collection);
    String[] childPaths = { "test" };
    Mockito.when(collection.getChildren()).thenReturn(childPaths);
    String expectedUUID = UUID.randomUUID().toString();
    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);
    Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
    String actualUUID = APIUtil.getMediationSequenceUuid("sample", 1, "out", apiIdentifier);
    Assert.assertEquals(expectedUUID, actualUUID);
    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) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testIsPerAPISequence.

@Test
public void testIsPerAPISequence() 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);
    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;
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
    Mockito.when(registry.resourceExists(eq(path))).thenReturn(true);
    Collection collection = Mockito.mock(Collection.class);
    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);
    boolean isPerAPiSequence = APIUtil.isPerAPISequence("sample", 1, apiIdentifier, "in");
    Assert.assertTrue(isPerAPiSequence);
}
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) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testGetCustomSequenceNotFound.

@Test
public void testGetCustomSequenceNotFound() 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 + "custom" + RegistryConstants.PATH_SEPARATOR;
    Mockito.when(registry.get(eq(path))).thenReturn(null, collection);
    String[] childPaths = { "test" };
    Mockito.when(collection.getChildren()).thenReturn(childPaths);
    String expectedUUID = UUID.randomUUID().toString();
    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);
    Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
    OMElement customSequence = APIUtil.getCustomSequence("sample", 1, "custom", 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)

Aggregations

UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)147 Resource (org.wso2.carbon.registry.core.Resource)96 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)86 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)84 Test (org.junit.Test)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)66 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)65 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)53 API (org.wso2.carbon.apimgt.api.model.API)47 UserStoreException (org.wso2.carbon.user.api.UserStoreException)44 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)43 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)41 Registry (org.wso2.carbon.registry.core.Registry)39 IOException (java.io.IOException)38 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)37 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)34 ArrayList (java.util.ArrayList)33 QName (javax.xml.namespace.QName)32 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)32