Search in sources :

Example 91 with ServiceReferenceHolder

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

the class TestUtils method mockAPIMConfiguration.

public static ServiceReferenceHolder mockAPIMConfiguration(String propertyName, String value, int tenantId) throws RegistryException, UserStoreException, XMLStreamException {
    ServiceReferenceHolder sh = mockRegistryAndUserRealm(tenantId);
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    PowerMockito.when(amConfig.getFirstProperty(propertyName)).thenReturn(value);
    Map<String, Environment> apiGatewayEnvironments = new HashMap<String, Environment>();
    Environment env1 = new Environment();
    apiGatewayEnvironments.put("PROD", env1);
    // Mocking some commonly used configs
    PowerMockito.when(amConfig.getApiGatewayEnvironments()).thenReturn(apiGatewayEnvironments);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_ENABLE_API_DOC_VISIBILITY_LEVELS)).thenReturn("true", "false");
    return sh;
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) HashMap(java.util.HashMap) Environment(org.wso2.carbon.apimgt.api.model.Environment)

Example 92 with ServiceReferenceHolder

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

the class TestUtils method getServiceReferenceHolder.

public static ServiceReferenceHolder getServiceReferenceHolder() throws XMLStreamException {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
    ConfigurationContextService configurationContextService = initConfigurationContextService(false);
    PowerMockito.when(ServiceReferenceHolder.getContextService()).thenReturn(configurationContextService);
    return sh;
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService)

Example 93 with ServiceReferenceHolder

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

the class PolicyUtilTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty("carbon.home", PolicyUtilTest.class.getResource("/").getFile());
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn("carbon.super");
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    eventProcessorService = Mockito.mock(EventProcessorService.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    Mockito.when(serviceReferenceHolder.getEventProcessorService()).thenReturn(eventProcessorService);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
    String[] skipThrottlePolicies = { "skipPolicy1" };
    Mockito.when(throttleProperties.getSkipRedeployingPolicies()).thenReturn(skipThrottlePolicies);
    Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.VELOCITY_LOGGER)).thenReturn(null);
    Mockito.when(serviceReferenceHolder.getAPIMConfiguration()).thenReturn(apiManagerConfiguration);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    policyRetriever = Mockito.mock(PolicyRetriever.class);
    PowerMockito.whenNew(PolicyRetriever.class).withNoArguments().thenReturn(policyRetriever);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.throttle.policy.deployer.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) EventProcessorService(org.wso2.carbon.event.processor.core.EventProcessorService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PolicyRetriever(org.wso2.carbon.apimgt.throttle.policy.deployer.PolicyRetriever) Before(org.junit.Before)

Example 94 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder 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 95 with ServiceReferenceHolder

use of org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder 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)

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