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);
}
}
}
}
}
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;
}
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());
}
Aggregations