Search in sources :

Example 1 with MicroserviceServer

use of org.wso2.carbon.identity.common.testng.ms.MicroserviceServer in project carbon-identity-framework by wso2.

the class CarbonBasedTestListener method processFields.

private void processFields(Field[] fields, Object realInstance) {
    for (Field field : fields) {
        if (annotationPresent(field, WithRealmService.class)) {
            field.setAccessible(true);
            Annotation annotation = field.getAnnotation(WithRealmService.class);
            WithRealmService withRealmService = (WithRealmService) annotation;
            try {
                RealmService realmService = createRealmService(withRealmService, true);
                field.set(realInstance, realmService);
                IdentityTenantUtil.setRealmService(realmService);
                CarbonCoreDataHolder.getInstance().setRealmService(realmService);
            } catch (IllegalAccessException e) {
                log.error("Error in setting field value: " + field.getName() + ", Class: " + field.getDeclaringClass(), e);
            } catch (UserStoreException e) {
                log.error("Error in setting user store value: " + field.getName() + ", Class: " + field.getDeclaringClass(), e);
            }
        }
        if (annotationPresent(field, InjectMicroservicePort.class)) {
            MicroserviceServer microserviceServer = microserviceServerMap.get(realInstance);
            if (microserviceServer != null) {
                field.setAccessible(true);
                try {
                    field.set(realInstance, microserviceServer.getPort());
                } catch (IllegalAccessException e) {
                    log.error("Error in setting micro-service port: " + field.getName() + ", Class: " + field.getDeclaringClass(), e);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) RealmService(org.wso2.carbon.user.core.service.RealmService) InMemoryRealmService(org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Annotation(java.lang.annotation.Annotation) MicroserviceServer(org.wso2.carbon.identity.common.testng.ms.MicroserviceServer)

Example 2 with MicroserviceServer

use of org.wso2.carbon.identity.common.testng.ms.MicroserviceServer in project carbon-identity-framework by wso2.

the class CarbonBasedTestListener method onAfterClass.

@Override
public void onAfterClass(ITestClass iTestClass, IMethodInstance iMethodInstance) {
    MockInitialContextFactory.destroy();
    MicroserviceServer microserviceServer = microserviceServerMap.get(iMethodInstance.getInstance());
    if (microserviceServer != null) {
        microserviceServer.stop();
        microserviceServer.destroy();
        microserviceServerMap.remove(iMethodInstance.getInstance());
    }
}
Also used : MicroserviceServer(org.wso2.carbon.identity.common.testng.ms.MicroserviceServer)

Example 3 with MicroserviceServer

use of org.wso2.carbon.identity.common.testng.ms.MicroserviceServer in project product-is by wso2.

the class RiskBasedLoginTestCase method testInit.

@BeforeClass(alwaysRun = true)
public void testInit() throws Exception {
    super.init();
    InputStream webappUrl = getClass().getResourceAsStream(ISIntegrationTest.URL_SEPARATOR + "samples" + ISIntegrationTest.URL_SEPARATOR + "authenticators" + ISIntegrationTest.URL_SEPARATOR + "sample-auth.war");
    InputStream jarUrl = getClass().getResourceAsStream(ISIntegrationTest.URL_SEPARATOR + "samples" + ISIntegrationTest.URL_SEPARATOR + "authenticators" + ISIntegrationTest.URL_SEPARATOR + "org.wso2.carbon.identity.sample.extension.authenticators.jar");
    String authenticatorPathString = Utils.getResidentCarbonHome() + File.separator + "repository" + File.separator + "components" + File.separator + "dropins" + File.separator + "org.wso2.carbon.identity.sample.extension.authenticators.jar";
    File jarDestFile = new File(authenticatorPathString);
    FileOutputStream jarDest = new FileOutputStream(jarDestFile);
    copyFileUsingStream(jarUrl, jarDest);
    log.info("Copied the demo authenticator jar file to " + authenticatorPathString);
    Assert.assertTrue(Files.exists(Paths.get(authenticatorPathString)), "Demo Authenticator is not copied " + "successfully. File path: " + authenticatorPathString);
    String authenticatorWarPathString = Utils.getResidentCarbonHome() + File.separator + "repository" + File.separator + "deployment" + File.separator + "server" + File.separator + "webapps" + File.separator + "sample-auth.war";
    File warDestFile = new File(authenticatorWarPathString);
    FileOutputStream warDest = new FileOutputStream(warDestFile);
    copyFileUsingStream(webappUrl, warDest);
    // Waiting for the war file to deploy.
    String authenticatorWebappPathString = Utils.getResidentCarbonHome() + File.separator + "repository" + File.separator + "deployment" + File.separator + "server" + File.separator + "webapps" + File.separator + "sample-auth";
    waitForWebappToDeploy(authenticatorWebappPathString, 120000L);
    log.info("Copied the demo authenticator war file to " + authenticatorWarPathString);
    Assert.assertTrue(Files.exists(Paths.get(authenticatorWarPathString)), "Demo Authenticator war is not copied " + "successfully. File path: " + authenticatorWarPathString);
    log.info("Restarting the server at: " + isServer.getContextUrls().getBackEndUrl());
    serverConfigurationManager = new ServerConfigurationManager(isServer);
    changeISConfiguration();
    log.info("Restarting the server at: " + isServer.getContextUrls().getBackEndUrl() + " is successful");
    super.init();
    logManger = new AuthenticatorClient(backendURL);
    String cookie = this.logManger.login(isServer.getSuperTenant().getTenantAdmin().getUserName(), isServer.getSuperTenant().getTenantAdmin().getPassword(), isServer.getInstance().getHosts().get("default"));
    oauthAdminClient = new OauthAdminClient(backendURL, cookie);
    ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
    applicationManagementServiceClient = new ApplicationManagementServiceClient(sessionCookie, backendURL, configContext);
    webAppAdminClient = new WebAppAdminClient(backendURL, sessionCookie);
    client = HttpClientBuilder.create().disableRedirectHandling().setDefaultCookieStore(cookieStore).build();
    String script = getConditionalAuthScript("RiskBasedLoginScript.js");
    createOauthApp(CALLBACK_URL, PRIMARY_IS_APPLICATION_NAME, oauthAdminClient);
    // Create service provider in primary IS with conditional authentication script enabled.
    createServiceProvider(PRIMARY_IS_APPLICATION_NAME, applicationManagementServiceClient, oauthAdminClient, script);
    microserviceServer = MicroserviceUtil.initMicroserviceServer();
    MicroserviceUtil.deployService(microserviceServer, this);
    superTenantResidentIDP = superTenantIDPMgtClient.getResidentIdP();
    updateResidentIDPProperty(superTenantResidentIDP, "adaptive_authentication.analytics.receiver", "http://localhost:" + microserviceServer.getPort());
    userRiskScores.put(userInfo.getUserName(), 0);
}
Also used : OauthAdminClient(org.wso2.identity.integration.common.clients.oauth.OauthAdminClient) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AuthenticatorClient(org.wso2.carbon.integration.common.admin.client.AuthenticatorClient) InputStream(java.io.InputStream) ServerConfigurationManager(org.wso2.carbon.integration.common.utils.mgt.ServerConfigurationManager) FileOutputStream(java.io.FileOutputStream) ApplicationManagementServiceClient(org.wso2.identity.integration.common.clients.application.mgt.ApplicationManagementServiceClient) WebAppAdminClient(org.wso2.identity.integration.common.clients.webappmgt.WebAppAdminClient) File(java.io.File) BeforeClass(org.testng.annotations.BeforeClass)

Example 4 with MicroserviceServer

use of org.wso2.carbon.identity.common.testng.ms.MicroserviceServer in project carbon-identity-framework by wso2.

the class CarbonBasedTestListener method onBeforeClass.

@Override
public void onBeforeClass(ITestClass iTestClass, IMethodInstance iMethodInstance) {
    Class realClass = iTestClass.getRealClass();
    if (annotationPresent(realClass, WithCarbonHome.class)) {
        System.setProperty(CarbonBaseConstants.CARBON_HOME, realClass.getResource("/").getFile());
        System.setProperty(TestConstants.CARBON_PROTOCOL, TestConstants.CARBON_PROTOCOL_HTTPS);
        System.setProperty(TestConstants.CARBON_HOST, TestConstants.CARBON_HOST_LOCALHOST);
        System.setProperty(TestConstants.CARBON_MANAGEMENT_PORT, TestConstants.CARBON_DEFAULT_HTTPS_PORT);
        copyDefaultConfigsIfNotExists(realClass);
    }
    if (annotationPresent(realClass, WithAxisConfiguration.class)) {
        AxisConfiguration axisConfiguration = new AxisConfiguration();
        ConfigurationContext configurationContext = new ConfigurationContext(axisConfiguration);
        setInternalState(IdentityCoreServiceComponent.class, "configurationContextService", new ConfigurationContextService(configurationContext, configurationContext));
    }
    if (annotationPresent(realClass, WithH2Database.class)) {
        System.setProperty("java.naming.factory.initial", "org.wso2.carbon.identity.common.testng.MockInitialContextFactory");
        Annotation annotation = realClass.getAnnotation(WithH2Database.class);
        WithH2Database withH2Database = (WithH2Database) annotation;
        MockInitialContextFactory.initializeDatasource(withH2Database.jndiName(), this.getClass(), withH2Database.files());
        setInternalState(JDBCPersistenceManager.class, "instance", null);
    }
    if (annotationPresent(realClass, WithRealmService.class)) {
        Annotation annotation = realClass.getAnnotation(WithRealmService.class);
        WithRealmService withRealmService = (WithRealmService) annotation;
        try {
            createRealmService(withRealmService, false);
        } catch (UserStoreException e) {
            log.error("Could not initialize the realm service for test. Test class:  " + realClass.getName(), e);
        }
    }
    if (annotationPresent(realClass, WithRegistry.class)) {
        Annotation annotation = realClass.getAnnotation(WithRegistry.class);
        WithRegistry withRegistry = (WithRegistry) annotation;
        createRegistryService(realClass, withRegistry);
    }
    if (annotationPresent(realClass, WithKeyStore.class)) {
        Annotation annotation = realClass.getAnnotation(WithKeyStore.class);
        WithKeyStore withKeyStore = (WithKeyStore) annotation;
        createKeyStore(realClass, withKeyStore);
    }
    if (annotationPresent(realClass, WithMicroService.class) && !microserviceServerInitialized(iMethodInstance.getInstance())) {
        MicroserviceServer microserviceServer = initMicroserviceServer(iMethodInstance.getInstance());
        scanAndLoadClasses(microserviceServer, realClass, iMethodInstance.getInstance());
    }
    Field[] fields = realClass.getDeclaredFields();
    processFields(fields, iMethodInstance.getInstance());
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) Annotation(java.lang.annotation.Annotation) MicroserviceServer(org.wso2.carbon.identity.common.testng.ms.MicroserviceServer) Field(java.lang.reflect.Field) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ITestClass(org.testng.ITestClass)

Example 5 with MicroserviceServer

use of org.wso2.carbon.identity.common.testng.ms.MicroserviceServer in project carbon-identity-framework by wso2.

the class CarbonBasedTestListener method initMicroserviceServer.

/**
 * Initializes the micro-service server.
 * Detects an available port from the system and use that for the microservice server.
 */
private MicroserviceServer initMicroserviceServer(Object realInstance) throws TestCreationException {
    try {
        ServerSocket s = new ServerSocket(0);
        int port = s.getLocalPort();
        s.close();
        MicroserviceServer microserviceServer = new MicroserviceServer(port);
        microserviceServer.init();
        microserviceServerMap.put(realInstance, microserviceServer);
        return microserviceServer;
    } catch (IOException e) {
        throw new TestCreationException("Could not get an aviailable port for micro-service", e);
    }
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) MicroserviceServer(org.wso2.carbon.identity.common.testng.ms.MicroserviceServer)

Aggregations

MicroserviceServer (org.wso2.carbon.identity.common.testng.ms.MicroserviceServer)4 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ServerSocket (java.net.ServerSocket)1 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)1 ITestClass (org.testng.ITestClass)1 BeforeClass (org.testng.annotations.BeforeClass)1 InMemoryRealmService (org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService)1 AuthenticatorClient (org.wso2.carbon.integration.common.admin.client.AuthenticatorClient)1 ServerConfigurationManager (org.wso2.carbon.integration.common.utils.mgt.ServerConfigurationManager)1 RealmService (org.wso2.carbon.user.core.service.RealmService)1 ConfigurationContextService (org.wso2.carbon.utils.ConfigurationContextService)1 ApplicationManagementServiceClient (org.wso2.identity.integration.common.clients.application.mgt.ApplicationManagementServiceClient)1 OauthAdminClient (org.wso2.identity.integration.common.clients.oauth.OauthAdminClient)1