Search in sources :

Example 1 with ConfigurationContextService

use of org.wso2.carbon.utils.ConfigurationContextService in project carbon-apimgt by wso2.

the class TestUtils method initConfigurationContextService.

/**
 * To initialize the (@link {@link ConfigurationContextService}} for testing.
 *
 * @return initialized configuration context service.
 */
public static ConfigurationContextService initConfigurationContextService(boolean initAPIMConfigurationService) throws XMLStreamException {
    ConfigurationContextService configurationContextService = Mockito.mock(ConfigurationContextService.class);
    ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
    AxisConfiguration axisConfiguration = Mockito.mock(AxisConfiguration.class);
    Mockito.doReturn(axisConfiguration).when(configurationContext).getAxisConfiguration();
    TransportInDescription transportInDescription = Mockito.mock(TransportInDescription.class);
    Mockito.doReturn(transportInDescription).when(axisConfiguration).getTransportIn(Mockito.anyString());
    Parameter dynamicSSLProfilesConfigParameter = Mockito.mock(Parameter.class);
    Mockito.when(dynamicSSLProfilesConfigParameter.getParameterElement()).thenReturn(getDynamicSSLElement());
    Mockito.when(transportInDescription.getParameter("dynamicSSLProfilesConfig")).thenReturn(dynamicSSLProfilesConfigParameter);
    TransportOutDescription transportOutDescription = Mockito.mock(TransportOutDescription.class);
    Mockito.doReturn(transportOutDescription).when(axisConfiguration).getTransportOut(Mockito.anyString());
    Mockito.when(transportOutDescription.getParameter("dynamicSSLProfilesConfig")).thenReturn(dynamicSSLProfilesConfigParameter);
    Mockito.doReturn(configurationContext).when(configurationContextService).getServerConfigContext();
    if (initAPIMConfigurationService) {
        APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
        APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
        Mockito.doReturn("true").when(apiManagerConfiguration).getFirstProperty(APIConstants.ENABLE_MTLS_FOR_APIS);
        Mockito.doReturn(apiManagerConfiguration).when(apiManagerConfigurationService).getAPIManagerConfiguration();
        ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
    }
    ServiceReferenceHolder.setContextService(configurationContextService);
    System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    System.setProperty("javax.net.ssl.trustStore", ".");
    return configurationContextService;
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) Parameter(org.apache.axis2.description.Parameter) TransportInDescription(org.apache.axis2.description.TransportInDescription) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

Example 2 with ConfigurationContextService

use of org.wso2.carbon.utils.ConfigurationContextService in project carbon-apimgt by wso2.

the class APIUtil method getRegistryResourceHTTPPermlink.

/**
 * This is to get the registry resource's HTTP permlink path.
 * Once this issue is fixed (https://wso2.org/jira/browse/REGISTRY-2110),
 * we can remove this method, and get permlink from the resource.
 *
 * @param path - Registry resource path
 * @return {@link String} -HTTP permlink
 */
public static String getRegistryResourceHTTPPermlink(String path) {
    String schemeHttp = APIConstants.HTTP_PROTOCOL;
    String schemeHttps = APIConstants.HTTPS_PROTOCOL;
    ConfigurationContextService contetxservice = ServiceReferenceHolder.getContextService();
    // First we will try to generate http permalink and if its disabled then only we will consider https
    int port = CarbonUtils.getTransportProxyPort(contetxservice.getServerConfigContext(), schemeHttp);
    if (port == -1) {
        port = CarbonUtils.getTransportPort(contetxservice.getServerConfigContext(), schemeHttp);
    }
    // getting https parameters if http is disabled. If proxy port is not present we will go for default port
    if (port == -1) {
        port = CarbonUtils.getTransportProxyPort(contetxservice.getServerConfigContext(), schemeHttps);
    }
    if (port == -1) {
        port = CarbonUtils.getTransportPort(contetxservice.getServerConfigContext(), schemeHttps);
    }
    String webContext = ServerConfiguration.getInstance().getFirstProperty("WebContextRoot");
    if (webContext == null || "/".equals(webContext)) {
        webContext = "";
    }
    RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
    String version = "";
    if (registryService == null) {
        log.error("Registry Service has not been set.");
    } else if (path != null) {
        try {
            String[] versions = registryService.getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, CarbonContext.getThreadLocalCarbonContext().getTenantId()).getVersions(path);
            if (versions != null && versions.length > 0) {
                version = versions[0].substring(versions[0].lastIndexOf(";version:"));
            }
        } catch (RegistryException e) {
            log.error("An error occurred while determining the latest version of the " + "resource at the given path: " + path, e);
        }
    }
    if (port != -1 && path != null) {
        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
        return webContext + ((tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) ? "/" + MultitenantConstants.TENANT_AWARE_URL_PREFIX + "/" + tenantDomain : "") + "/registry/resource" + org.wso2.carbon.registry.app.Utils.encodeRegistryPath(path) + version;
    }
    return null;
}
Also used : RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)

Example 3 with ConfigurationContextService

use of org.wso2.carbon.utils.ConfigurationContextService in project carbon-apimgt by wso2.

the class TenantLoadMessageSenderTest method testGetClusteringAgent.

@Test
public void testGetClusteringAgent() {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ConfigurationContextService configurationContextService = PowerMockito.mock(ConfigurationContextService.class);
    PowerMockito.when(ServiceReferenceHolder.getContextService()).thenReturn(configurationContextService);
    ConfigurationContext serverConfigContext = PowerMockito.mock(ConfigurationContext.class);
    PowerMockito.when(ServiceReferenceHolder.getContextService().getServerConfigContext()).thenReturn(serverConfigContext);
    AxisConfiguration axisConfiguration = PowerMockito.mock(AxisConfiguration.class);
    PowerMockito.when(ServiceReferenceHolder.getContextService().getServerConfigContext().getAxisConfiguration()).thenReturn(axisConfiguration);
    ClusteringAgent clusteringAgent = PowerMockito.mock(ClusteringAgent.class);
    PowerMockito.when(ServiceReferenceHolder.getContextService().getServerConfigContext().getAxisConfiguration().getClusteringAgent()).thenReturn(clusteringAgent);
    tenantLoadMessageSender.getClusteringAgent();
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ClusteringAgent(org.apache.axis2.clustering.ClusteringAgent) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with ConfigurationContextService

use of org.wso2.carbon.utils.ConfigurationContextService in project carbon-business-process by wso2.

the class AttachmentMgtDAOBasicOperationsTest method setUp.

@Override
protected void setUp() throws Exception {
    // Setup the MockAttachment-Server
    try {
        ConfigurationContext context = ConfigurationContextFactory.createConfigurationContextFromFileSystem(AXIS2_XML_FILE_PATH, "axis2.xml");
        ConfigurationContextService service = new ConfigurationContextService(context, null);
        AttachmentServerHolder.getInstance().setConfigurationContextService(service);
    } catch (Exception e) {
    }
    server = new MockAttachmentServer();
    attachmentServerHolder = AttachmentServerHolder.getInstance();
    attachmentServerHolder.setAttachmentServer(server);
    server.init();
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) AttachmentMgtException(org.wso2.carbon.attachment.mgt.skeleton.AttachmentMgtException)

Example 5 with ConfigurationContextService

use of org.wso2.carbon.utils.ConfigurationContextService in project carbon-apimgt by wso2.

the class UserAwareAPIProviderTest method init.

@Before
public void init() throws Exception {
    System.setProperty("carbon.home", "");
    apiIdentifier = new APIIdentifier("admin_identifier1_v1.0");
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.mockStatic(RegistryUtils.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    RealmService realmService = Mockito.mock(RealmService.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    userRegistry = Mockito.mock(UserRegistry.class);
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    resource = Mockito.mock(Resource.class, Mockito.CALLS_REAL_METHODS);
    Mockito.doReturn(apiManagerConfiguration).when(apiManagerConfigurationService).getAPIManagerConfiguration();
    Mockito.doReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE).when(apiManagerConfiguration).getFirstProperty(APIConstants.API_GATEWAY_TYPE);
    Mockito.doReturn("true").when(apiManagerConfiguration).getFirstProperty(APIConstants.API_PUBLISHER_ENABLE_ACCESS_CONTROL_LEVELS);
    Mockito.doReturn(userRegistry).when(registryService).getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt());
    Mockito.doReturn(userRegistry).when(registryService).getGovernanceSystemRegistry();
    Mockito.doReturn(userRegistry).when(registryService).getConfigSystemRegistry(Mockito.anyInt());
    Mockito.doReturn(userRegistry).when(registryService).getConfigSystemRegistry();
    Mockito.doReturn(resource).when(userRegistry).newResource();
    Mockito.doReturn(null).when(userRegistry).getUserRealm();
    PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.when(APIUtil.getAPIPath(Mockito.any(APIIdentifier.class))).thenReturn("test");
    PowerMockito.when(APIUtil.getArtifactManager(Mockito.any(Registry.class), Mockito.anyString())).thenReturn(artifactManager);
    PowerMockito.when(APIUtil.getInternalOrganizationDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    PowerMockito.doNothing().when(ServiceReferenceHolder.class, "setUserRealm", Mockito.any());
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    PowerMockito.when(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName())).thenReturn(apiIdentifier.getProviderName());
    Mockito.doReturn(realmService).when(serviceReferenceHolder).getRealmService();
    Mockito.doReturn(tenantManager).when(realmService).getTenantManager();
    Mockito.doReturn(registryService).when(serviceReferenceHolder).getRegistryService();
    Mockito.doReturn(apiManagerConfigurationService).when(serviceReferenceHolder).getAPIManagerConfigurationService();
    PowerMockito.when(APIUtil.compareRoleList(Mockito.any(String[].class), Mockito.anyString())).thenCallRealMethod();
    ConfigurationContextService configurationContextService = TestUtils.initConfigurationContextService(true);
    PowerMockito.when(ServiceReferenceHolder.getContextService()).thenReturn(configurationContextService);
    userAwareAPIProvider = new UserAwareAPIProvider(ADMIN_ROLE_NAME);
    PrivilegedCarbonContext prcontext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prcontext);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) RealmService(org.wso2.carbon.user.core.service.RealmService) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Before(org.junit.Before)

Aggregations

ConfigurationContextService (org.wso2.carbon.utils.ConfigurationContextService)9 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)5 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)4 Before (org.junit.Before)3 ServiceClient (org.apache.axis2.client.ServiceClient)2 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)2 Test (org.junit.Test)2 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)2 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)2 RealmService (org.wso2.carbon.user.core.service.RealmService)2 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)2 ClusteringAgent (org.apache.axis2.clustering.ClusteringAgent)1 Parameter (org.apache.axis2.description.Parameter)1 TransportInDescription (org.apache.axis2.description.TransportInDescription)1 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)1