Search in sources :

Example 1 with WithRealmService

use of org.wso2.carbon.identity.common.testng.WithRealmService 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 WithRealmService

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

the class CarbonBasedTestListener method createRealmService.

private RealmService createRealmService(WithRealmService withRealmService, boolean reuseIfAvailable) throws UserStoreException {
    if (testSessionRealmService != null && reuseIfAvailable) {
        return testSessionRealmService;
    }
    try {
        int tenantId = withRealmService.tenantId();
        String tenantDomain = withRealmService.tenantDomain();
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        testSessionRealmService = new InMemoryRealmService(tenantId);
        UserStoreManager userStoreManager = testSessionRealmService.getTenantUserRealm(tenantId).getUserStoreManager();
        ((MockUserStoreManager) userStoreManager).addSecondaryUserStoreManager("PRIMARY", (MockUserStoreManager) userStoreManager);
        IdentityTenantUtil.setRealmService(testSessionRealmService);
        TenantManager tenantManager = new InMemoryTenantManager();
        testSessionRealmService.setTenantManager(tenantManager);
        RegistryDataHolder.getInstance().setRealmService(testSessionRealmService);
        OSGiDataHolder.getInstance().setUserRealmService(testSessionRealmService);
        Class[] singletonClasses = withRealmService.injectToSingletons();
        for (Class singletonClass : singletonClasses) {
            Object instance = getSingletonInstance(singletonClass);
            if (instance != null) {
                setInstanceValue(testSessionRealmService, RealmService.class, singletonClass, instance);
            } else {
                setInstanceValue(testSessionRealmService, RealmService.class, singletonClass, null);
            }
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
    return testSessionRealmService;
}
Also used : MockUserStoreManager(org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager) ITestClass(org.testng.ITestClass) MockUserStoreManager(org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) InMemoryTenantManager(org.wso2.carbon.identity.common.testng.realm.InMemoryTenantManager) TenantManager(org.wso2.carbon.user.api.TenantManager) InMemoryTenantManager(org.wso2.carbon.identity.common.testng.realm.InMemoryTenantManager) InMemoryRealmService(org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService)

Example 3 with WithRealmService

use of org.wso2.carbon.identity.common.testng.WithRealmService 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)

Aggregations

Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 ITestClass (org.testng.ITestClass)2 MicroserviceServer (org.wso2.carbon.identity.common.testng.ms.MicroserviceServer)2 InMemoryRealmService (org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)1 InMemoryTenantManager (org.wso2.carbon.identity.common.testng.realm.InMemoryTenantManager)1 MockUserStoreManager (org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager)1 TenantManager (org.wso2.carbon.user.api.TenantManager)1 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)1 RealmService (org.wso2.carbon.user.core.service.RealmService)1 ConfigurationContextService (org.wso2.carbon.utils.ConfigurationContextService)1