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