Commit 17d49403 authored by Mohamed Khalil Labidi's avatar Mohamed Khalil Labidi
Browse files

Merge branch 'func-tests' into 'master'

Add functional tests to the RMConnectionHelper and the SchedulerConnectionHelper classes

See merge request melodic/morphemic-preprocessor!14
parents a0f00769 0dca4f5e
......@@ -47,5 +47,13 @@ dependencies {
compile group: 'org.eclipse.emf', name: 'org.eclipse.emf.common', version: '2.20.0'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
annotationProcessor "org.projectlombok:lombok:1.18.12"
compile 'org.junit.jupiter:junit-jupiter:5.6.2'
testImplementation 'org.mockito:mockito-inline:3.7.7'
testImplementation 'org.mockito:mockito-junit-jupiter:3.7.7'
}
test {
useJUnitPlatform()
}
......@@ -98,8 +98,6 @@ public class RMConnectionHelper {
try {
LOGGER.debug("Disconnecting from RM...");
rmRestInterface.rmDisconnect(sessionId);
// Remove the stored session
userPreferences.remove(sessionPreferencesId);
LOGGER.info("Disconnected from RM.");
} catch (NotConnectedException nce) {
......@@ -108,13 +106,31 @@ public class RMConnectionHelper {
}else{
LOGGER.info("Already disconnected from RM");
}
// Clear local session
sessionId = "";
// Remove the stored session
userPreferences.remove(sessionPreferencesId);
}catch (Exception e){
// Exception will trigger if the sessionId is empty
LOGGER.info("Already disconnected from RM");
// Clear local session
sessionId = "";
// Remove the stored session
userPreferences.remove(sessionPreferencesId);
}
}
public static String getSessionId() {
return sessionId;
}
// For Testing Dev
public static void setSessionPreferencesId(String sessionPreferencesId) {
RMConnectionHelper.sessionPreferencesId = sessionPreferencesId;
}
public static void setRmRestInterface(RMRestInterface rmRestInterface) {
RMConnectionHelper.rmRestInterface = rmRestInterface;
}
}
......@@ -16,6 +16,9 @@ public class SchedulerConnectionHelper {
private static String paURL;
// For testing purpose only
private static boolean isActive;
/**
*
......@@ -48,8 +51,10 @@ public class SchedulerConnectionHelper {
if(!restSmartProxy.isConnected()) {
restSmartProxy.init(connectionInfo);
LOGGER.info("Connected to Scheduler");
isActive = true;
}else{
LOGGER.info("Already connected to Scheduler");
isActive = true;
}
return restSmartProxy;
}
......@@ -63,6 +68,7 @@ public class SchedulerConnectionHelper {
try {
if(restSmartProxy.isConnected()) {
restSmartProxy.disconnect();
isActive = false;
LOGGER.info("Disconnected from Scheduler");
}else{
LOGGER.info("Already disconnected from Scheduler");
......@@ -71,4 +77,17 @@ public class SchedulerConnectionHelper {
LOGGER.warn("WARNING: Not able to disconnect due to: " + e.toString());
}
}
// All of the following methods are used for Test Driven Dev
public static boolean getIsActive() {
return isActive;
}
public static String getPaURL() {
return paURL;
}
public static void setRestSmartProxy(RestSmartProxyImpl restSmartProxy) {
SchedulerConnectionHelper.restSmartProxy = restSmartProxy;
}
}
package org.activeeon.morphemic.service;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import org.ow2.proactive.resourcemanager.exception.RMException;
import org.ow2.proactive.scheduler.common.exception.NotConnectedException;
import org.ow2.proactive_grid_cloud_portal.common.RMRestInterface;
import javax.security.auth.login.LoginException;
import java.security.KeyException;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class RMConnectionHelperTest {
private final String DUMMY_USERNAME = "USERNAME";
private final String DUMMY_PASSWORD = "PASSWORD";
private final String DUMMY_SESSION_ID = "SESSION_ID";
@Mock
RMRestInterface rmRestInterface;
/**
*
* Set up and mock all needed parameters and inject them, if needed, in the RMConnectionHelper
* class using the class setters
*
* @throws LoginException In case the login is not valid
* @throws KeyException In case the password is not valid
* @throws RMException In case an error happens in the RM
* @throws NotConnectedException In case if the RM is not connected
*/
@BeforeEach
void setUp() throws LoginException, KeyException, RMException, NotConnectedException {
// Enable all mocks
MockitoAnnotations.openMocks(this);
// Mock the RM Interface connect method to return the dummy sessionId, since we are testing on invalid URL,
// username and password
when(rmRestInterface.rmConnect(DUMMY_USERNAME,DUMMY_PASSWORD)).thenReturn(DUMMY_SESSION_ID);
// Mock the RM Interface disconnect method to set the RM Interface inactive when the disconnect is called
// for the dummy sessionId
doAnswer(invocation -> when(rmRestInterface.isActive(DUMMY_SESSION_ID)).thenReturn(false))
.when(rmRestInterface).rmDisconnect(DUMMY_SESSION_ID);
// Inject the current mocked RM Interface to the RMConnectionHelper class
RMConnectionHelper.setRmRestInterface(rmRestInterface);
// Initialize a testing user preference variable
// It is used to store the session
String DUMMY_PREFERENCE_ID = "TESTING_PREF";
RMConnectionHelper.setSessionPreferencesId(DUMMY_PREFERENCE_ID);
// Temporary disable all Logging from the RMConnectionHelper class
// It is enabled after the tests are completed
Logger.getLogger(RMConnectionHelper.class).setLevel(Level.OFF);
}
/**
* Test the init method of the RMConnectionHelper class
* The test consists in calling the actual init function and verify if it has created the correct RMInterface
* instance and check if the actual sessionId of the class is empty
*/
@Test
void init() {
String DUMMY_CONNECTION_URL = "http://notExist.activeeon.com:8080";
RMRestInterface initRMRestInterface = RMConnectionHelper.init(DUMMY_CONNECTION_URL);
String DUMMY_RM_INTERFACE_MESSAGE = "org.ow2.proactive_grid_cloud_portal.common.RMRestInterface";
assertTrue(initRMRestInterface.toString().contains(DUMMY_RM_INTERFACE_MESSAGE));
assertEquals(RMConnectionHelper.getSessionId(), "");
}
/**
*
* Test the connect method of the RMConnectionHelper class
* The test consists in calling the actual connect function and verify if the created sessionId for the dummy username
* and password are equal to their values as defined in the mocked RMInterface
*
* @throws LoginException In case the login is not valid
* @throws KeyException In case the password is not valid
* @throws RMException In case an error happens in the RM
*/
@Test
void connect() throws LoginException, KeyException, RMException {
RMConnectionHelper.connect(DUMMY_USERNAME,DUMMY_PASSWORD);
assertEquals(DUMMY_SESSION_ID,RMConnectionHelper.getSessionId());
}
/**
* Test the disconnect method of the RMConnectionHelper class
* The test consists in calling the actual disconnect method and then verify if the sessionId was deleted
*/
@Test
void disconnect() {
RMConnectionHelper.disconnect();
assertEquals("", RMConnectionHelper.getSessionId());
}
/**
* Test the getSessionId method of the RMConnectionHelper class
* The test consists of checking if the actual getSessionId method return the
* correct initial value of the sessionId
*/
@Test
void getSessionId() {
assertEquals(RMConnectionHelper.getSessionId(), "");
}
/**
* Enable all Logging in RMConnectionHelper class
*/
@AfterEach
void enableLogging() {
Logger.getLogger(RMConnectionHelper.class).setLevel(Level.ALL);
}
}
\ No newline at end of file
package org.activeeon.morphemic.service;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.ow2.proactive.authentication.ConnectionInfo;
import org.ow2.proactive_grid_cloud_portal.smartproxy.RestSmartProxyImpl;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class SchedulerConnectionHelperTest {
private final String DUMMY_CONNECTION_URL = "http://notExist.activeeon.com:8080";
private final String DUMMY_USERNAME = "username";
private final String DUMMY_PASSWORD = "password";
private final ConnectionInfo DUMMY_CONNECTION_INFO = new ConnectionInfo(DUMMY_CONNECTION_URL,
DUMMY_USERNAME,
DUMMY_PASSWORD,
null,
true);
@Mock
RestSmartProxyImpl restSmartProxy;
/**
* Set up and mock all needed parameters and inject them, if needed, into the SchedulerConnectionHelper class
* using the class setters
*/
@BeforeEach
void setUp() {
// Enable all mocks
MockitoAnnotations.openMocks(this);
// Mock the Rest Smart Proxy icConnected method to return true
when(restSmartProxy.isConnected()).thenReturn(false);
// Mock the Rest Smart Proxy init method to do nothing when it is called instead of creating an actual
// proxy on an incorrect URL ==> This will avoid exceptions
doNothing().when(restSmartProxy).init(DUMMY_CONNECTION_INFO);
// Inject the current Rest Smart Proxy into the SchedulerConnectionHelper class
SchedulerConnectionHelper.setRestSmartProxy(restSmartProxy);
// Temporary disable all Logging from the RMConnectionHelper class
// It is enabled after the tests are completed
Logger.getLogger(SchedulerConnectionHelper.class).setLevel(Level.OFF);
}
/**
* Test the init method of the SchedulerConnectionHelper class
* The test consists in calling the actual init method of the SchedulerConnectionHelper class and then verify
* if initialized Gateway URL is equal to the Dummy URL passed in the parameters
*/
@Test
void init() {
SchedulerConnectionHelper.init(DUMMY_CONNECTION_URL);
assertEquals(DUMMY_CONNECTION_URL,SchedulerConnectionHelper.getPaURL());
}
/**
* Test the connect method of the SchedulerConnectionHelper class
* The test consists in calling the actual connect method of the SchedulerConnectionHelper class using the
* dummy username and password and then check if the the SchedulerConnectionHelper isActive variable is true
*/
@Test
void connect() {
SchedulerConnectionHelper.connect(DUMMY_USERNAME, DUMMY_PASSWORD);
assertTrue(SchedulerConnectionHelper.getIsActive());
}
/**
* Test the disconnect method in SchedulerConnectionHelper class
* The test consists in calling the actual disconnect method of the SchedulerConnectionHelper class and then
* check if the SchedulerConnectionHelper getIsActive method return false
*/
@Test
void disconnect() {
SchedulerConnectionHelper.disconnect();
assertFalse(SchedulerConnectionHelper.getIsActive());
}
/**
* Enable all Logging in RMConnectionHelper class
*/
@AfterEach
void enableLogging() {
Logger.getLogger(SchedulerConnectionHelper.class).setLevel(Level.ALL);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment