Search in sources :

Example 61 with APIManagerConfigurationService

use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.

the class APIMgtGatewayJWTGeneratorImplTest method testJWTGeneratorClaimConfig.

@Test
public void testJWTGeneratorClaimConfig() {
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    ExtendedJWTConfigurationDto jwtConfigurationDto = Mockito.mock(ExtendedJWTConfigurationDto.class);
    Mockito.when(apiManagerConfiguration.getJwtConfigurationDto()).thenReturn(jwtConfigurationDto);
    String defaultDialectUri = "http://wso2.org/claims";
    // default dialect if not changed
    AbstractAPIMgtGatewayJWTGenerator generator = new APIMgtGatewayJWTGeneratorImpl();
    // Set jwtConfigurationDto
    generator.setJWTConfigurationDto(jwtConfigurationDto);
    Assert.assertEquals(generator.getDialectURI(), ClaimsRetriever.DEFAULT_DIALECT_URI);
    // Test dialect after changing it through config
    String claimDialect = "http://test.com";
    Mockito.when(jwtConfigurationDto.getConsumerDialectUri()).thenReturn(claimDialect);
    generator = new APIMgtGatewayJWTGeneratorImpl();
    generator.setJWTConfigurationDto(jwtConfigurationDto);
    Assert.assertEquals(generator.getDialectURI(), claimDialect);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ExtendedJWTConfigurationDto(org.wso2.carbon.apimgt.impl.dto.ExtendedJWTConfigurationDto) APIMgtGatewayJWTGeneratorImpl(org.wso2.carbon.apimgt.common.gateway.jwtgenerator.APIMgtGatewayJWTGeneratorImpl) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) AbstractAPIMgtGatewayJWTGenerator(org.wso2.carbon.apimgt.common.gateway.jwtgenerator.AbstractAPIMgtGatewayJWTGenerator) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 62 with APIManagerConfigurationService

use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.

the class TenantServiceCreatorTestCase method testCreatedConfigurationContext.

@Test
public void testCreatedConfigurationContext() throws Exception {
    TenantServiceCreator tenantServiceCreator = new TenantServiceCreator();
    ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
    // Failed to create Tenant's synapse sequences Error
    PowerMockito.mockStatic(Cache.class);
    Cache cache = Mockito.mock(Cache.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(APIManagerConfigurationService.class);
    PowerMockito.mockStatic(CacheProvider.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    final APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    PowerMockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
    PowerMockito.when(cacheProvider.getDefaultCacheTimeout()).thenReturn((long) 900);
    Mockito.when(CacheProvider.getGatewayKeyCache()).thenReturn(cache);
    Mockito.when(CacheProvider.getResourceCache()).thenReturn(cache);
    Mockito.when(CacheProvider.getGatewayTokenCache()).thenReturn(cache);
    Mockito.when(CacheProvider.getInvalidTokenCache()).thenReturn(cache);
    tenantServiceCreator.createdConfigurationContext(configurationContext);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(FileUtils.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn("abc.com");
    AxisConfiguration axisConfiguration = Mockito.mock(AxisConfiguration.class);
    Mockito.when(configurationContext.getAxisConfiguration()).thenReturn(axisConfiguration);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    URL url = new URL("http", "localhost", 5000, "/fle/");
    Mockito.when(axisConfiguration.getRepository()).thenReturn(url);
    File tenantAxis2Repo = Mockito.mock(File.class);
    File synapseConfigsDir = Mockito.mock(File.class);
    // Couldn't create the synapse-config root on the file system error is logged.
    tenantServiceCreator.createdConfigurationContext(configurationContext);
    PowerMockito.whenNew(File.class).withArguments("/file/").thenReturn(tenantAxis2Repo);
    PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(synapseConfigsDir);
    Mockito.when(synapseConfigsDir.mkdir()).thenReturn(true);
    String synapseConfigsDirLocation = "/file/synapse-confgs";
    Mockito.when(synapseConfigsDir.getAbsolutePath()).thenReturn(synapseConfigsDirLocation);
    Mockito.doNothing().when(axisConfiguration).addParameter(SynapseConstants.Axis2Param.SYNAPSE_CONFIG_LOCATION, synapseConfigsDirLocation);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
    Mockito.when(privilegedCarbonContext.getRegistry(RegistryType.SYSTEM_CONFIGURATION)).thenReturn(userRegistry);
    PowerMockito.whenNew(ConfigurationManager.class).withArguments(userRegistry, configurationContext).thenReturn(configurationManager);
    ConfigurationTracker tracker = Mockito.mock(ConfigurationTracker.class);
    Mockito.when(configurationManager.getTracker()).thenReturn(tracker);
    Mockito.when(tracker.getCurrentConfigurationName()).thenReturn("config-name");
    Mockito.when(synapseConfigsDir.exists()).thenReturn(false, false, false, true);
    copyFile("/repository/resources/apim-synapse-config/main.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "main.xml");
    copyFile("/repository/resources/apim-synapse-config/fault.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "falut.xml");
    copyFile("/repository/resources/apim-synapse-config/_auth_failure_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_auth_failure_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_resource_mismatch_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_resource_mismatch_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_throttle_out_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_throttle_out_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_sandbox_key_error_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_sandbox_key_error_.xml");
    copyFile("/repository/resources/apim-synapse-config/_production_key_error_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_production_key_error_.xml");
    copyFile("/repository/resources/apim-synapse-config/_cors_request_handler_.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_cors_request_handler_.xml");
    copyFile("/repository/resources/apim-synapse-config/_threat_fault.xml", "/file/synapse-confgs" + File.separator + "sequences" + File.separator + "_threat_fault.xml");
    // test IOException Error while copying API manager specific synapse sequences
    tenantServiceCreator.createdConfigurationContext(configurationContext);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ConfigurationTracker(org.wso2.carbon.mediation.initializer.configurations.ConfigurationTracker) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) CacheProvider(org.wso2.carbon.apimgt.impl.caching.CacheProvider) URL(java.net.URL) File(java.io.File) ConfigurationManager(org.wso2.carbon.mediation.initializer.configurations.ConfigurationManager) Cache(javax.cache.Cache) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 63 with APIManagerConfigurationService

use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService 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 64 with APIManagerConfigurationService

use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.

the class APIUtilTest method testisAllowDisplayAPIsWithMultipleStatus.

@Test
public void testisAllowDisplayAPIsWithMultipleStatus() {
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_DISPLAY_ALL_APIS)).thenReturn("true");
    Assert.assertEquals(true, APIUtil.isAllowDisplayAPIsWithMultipleStatus());
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_DISPLAY_ALL_APIS)).thenReturn("false");
    Assert.assertEquals(false, APIUtil.isAllowDisplayAPIsWithMultipleStatus());
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_DISPLAY_ALL_APIS)).thenReturn("");
    Assert.assertEquals(false, APIUtil.isAllowDisplayAPIsWithMultipleStatus());
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_DISPLAY_ALL_APIS)).thenReturn(null);
    Assert.assertEquals(false, APIUtil.isAllowDisplayAPIsWithMultipleStatus());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 65 with APIManagerConfigurationService

use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.

the class APIUtilTest method testGetAPI.

@Test
public void testGetAPI() throws Exception {
    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;
    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);
    PowerMockito.mockStatic(ApiMgtDAO.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(artifact.getId()).thenReturn("");
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn(provider);
    Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(tenantDomain);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
    String artifactPath = "";
    PowerMockito.mockStatic(GovernanceUtils.class);
    Mockito.when(GovernanceUtils.getArtifactPath(registry, "")).thenReturn(artifactPath);
    Mockito.when(registry.get(artifactPath)).thenReturn(resource);
    Mockito.when(resource.getLastModified()).thenReturn(expectedAPI.getLastUpdated());
    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.getPolicyName()).thenReturn("policy");
    Mockito.when(policy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
    Mockito.when(quotaPolicy.getLimit()).thenReturn(limit);
    Mockito.when(registry.getTags(artifactPath)).thenReturn(getTagsFromSet(expectedAPI.getTags()));
    HashMap<String, String> urlPatterns = getURLTemplatePattern(expectedAPI.getUriTemplates());
    Mockito.when(apiMgtDAO.getURITemplatesPerAPIAsString(Mockito.any(String.class))).thenReturn(urlPatterns);
    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, registry);
    Assert.assertNotNull(api);
}
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) 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) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)58 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)55 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)49 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)39 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)33 RealmService (org.wso2.carbon.user.core.service.RealmService)24 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)21 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)20 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 API (org.wso2.carbon.apimgt.api.model.API)17 HashMap (java.util.HashMap)16 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)15 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)15 ArrayList (java.util.ArrayList)14 Registry (org.wso2.carbon.registry.core.Registry)14 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)13 Resource (org.wso2.carbon.registry.core.Resource)13 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)12 Before (org.junit.Before)11