Search in sources :

Example 96 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class ServiceReferenceHolderTestCase method testServiceReferenceHolder.

@Test
public void testServiceReferenceHolder() {
    ServiceReferenceHolder serviceReferenceHolder = ServiceReferenceHolder.getInstance();
    Assert.assertTrue(serviceReferenceHolder instanceof ServiceReferenceHolder);
    serviceReferenceHolder.getThrottleDataHolder();
    ThrottleDataHolder throttleDataHolder = Mockito.mock(ThrottleDataHolder.class);
    serviceReferenceHolder.setThrottleDataHolder(throttleDataHolder);
    ConfigurationContextService cfgCtxService = Mockito.mock(ConfigurationContextService.class);
    serviceReferenceHolder.setConfigurationContextService(cfgCtxService);
    cfgCtxService = serviceReferenceHolder.getConfigurationContextService();
    Assert.assertNotNull(cfgCtxService);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    serviceReferenceHolder.setAPIManagerConfigurationService(apiManagerConfigurationService);
    apiManagerConfigurationService = serviceReferenceHolder.getApiManagerConfigurationService();
    Assert.assertNotNull(apiManagerConfigurationService);
    Assert.assertNull(serviceReferenceHolder.getAPIManagerConfiguration());
    Assert.assertNull(serviceReferenceHolder.getServerConfigurationContext());
    ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
    serviceReferenceHolder.setThrottleProperties(throttleProperties);
    Assert.assertNotNull(serviceReferenceHolder.getThrottleProperties());
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Test(org.junit.Test)

Example 97 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIRealmUtilTest method testLoggedInUserClaims.

@Test
public void testLoggedInUserClaims() throws APIManagementException, UserStoreException, XMLStreamException {
    String configuredClaims = "http://wso2.org/claim1,http://wso2.org/claim2,http://wso2.org/claim3";
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(MultitenantUtils.getTenantDomain("admin")).thenReturn("carbon.super");
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    claimValues.put("claim3", "http://wso2.org/claim3");
    PowerMockito.when(APIUtil.getTenantId("carbon.super")).thenReturn(-1234);
    PowerMockito.when(APIUtil.getClaims("admin", -1234, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
    Map loggedInUserClaims = APIRealmUtils.getUserClaims("admin");
    assertNotNull(loggedInUserClaims);
    assertEquals(configuredClaims.split(",").length, loggedInUserClaims.size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RealmService(org.wso2.carbon.user.core.service.RealmService) TreeMap(java.util.TreeMap) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 98 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIUtilTest method testIsRoleNameNotExist.

@Test
public void testIsRoleNameNotExist() throws Exception {
    String userName = "John";
    String roleName = "developer";
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(realmService.getTenantUserRealm(Mockito.anyInt())).thenReturn(userRealm);
    Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    Mockito.when(userStoreManager.isExistingRole(roleName)).thenReturn(false);
    Assert.assertFalse(APIUtil.isRoleNameExist(userName, roleName));
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 99 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIUtilTest method testGetAppAttributeKeysFromRegistry.

@Test
public void testGetAppAttributeKeysFromRegistry() 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);
        final int tenantId = -1234;
        final String property = APIConstants.ApplicationAttributes.APPLICATION_CONFIGURATIONS;
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        RegistryService registryService = Mockito.mock(RegistryService.class);
        Resource resource = Mockito.mock(Resource.class);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        File siteConfFile = new File(Thread.currentThread().getContextClassLoader().getResource("tenant-conf.json").getFile());
        String tenantConfValue = FileUtils.readFileToString(siteConfFile);
        APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
        Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
        Mockito.when(apimConfigService.getTenantConfig(tenantDomain)).thenReturn(tenantConfValue);
        JSONParser parser = new JSONParser();
        JSONObject json = (JSONObject) parser.parse(tenantConfValue);
        JSONObject applicationAttributes = (JSONObject) json.get(property);
        JSONObject appAttributes = APIUtil.getAppAttributeKeysFromRegistry(tenantDomain);
        Assert.assertEquals(applicationAttributes, appAttributes);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) JSONObject(org.json.simple.JSONObject) Resource(org.wso2.carbon.registry.core.Resource) JSONParser(org.json.simple.parser.JSONParser) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) File(java.io.File) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 100 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder in project carbon-apimgt by wso2.

the class APIUtilTest method testIsPerAPISequenceResourceMissing.

@Test
public void testIsPerAPISequenceResourceMissing() 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(false);
    boolean isPerAPiSequence = APIUtil.isPerAPISequence("sample", 1, apiIdentifier, "in");
    Assert.assertFalse(isPerAPiSequence);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)109 Test (org.junit.Test)105 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)102 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)64 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)63 RealmService (org.wso2.carbon.user.core.service.RealmService)53 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)49 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)47 Resource (org.wso2.carbon.registry.core.Resource)40 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)38 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)30 InputStream (java.io.InputStream)29 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)28 Before (org.junit.Before)27 API (org.wso2.carbon.apimgt.api.model.API)24 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)23 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 HashMap (java.util.HashMap)21 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)21 QName (javax.xml.namespace.QName)19