Commit b7d200ad authored by Mohamed Khalil Labidi's avatar Mohamed Khalil Labidi
Browse files

Merge branch 'gateways-func-test' into 'master'

Add functional tests to the PAResourceManager and the PAScheduler gateways

See merge request melodic/morphemic-preprocessor!15
parents 0c62933e 30506639
......@@ -234,8 +234,15 @@ public class PASchedulerGateway {
* Disconnect from the ProActive server
*/
public void disconnect() {
SchedulerConnectionHelper.disconnect();
restSmartProxy = SchedulerConnectionHelper.disconnect();
}
// For testing purpose
public RestSmartProxyImpl getRestSmartProxy() {
return restSmartProxy;
}
public void setRestSmartProxy(RestSmartProxyImpl restSmartProxy) {
this.restSmartProxy = restSmartProxy;
}
}
......@@ -251,4 +251,18 @@ public class PAResourceManagerGateway {
LOGGER.info("Node removed!");
return result;
}
// For testing purpose only
public RMRestInterface getRmRestInterface() {
return rmRestInterface;
}
public void setRmRestInterface(RMRestInterface rmRestInterface) {
this.rmRestInterface = rmRestInterface;
}
public String getSessionId(){
return RMConnectionHelper.getSessionId();
}
}
......@@ -64,7 +64,7 @@ public class SchedulerConnectionHelper {
* Disconnect from the Scheduler
*
*/
public static synchronized void disconnect() {
public static synchronized RestSmartProxyImpl disconnect() {
try {
if(restSmartProxy.isConnected()) {
restSmartProxy.disconnect();
......@@ -76,6 +76,7 @@ public class SchedulerConnectionHelper {
} catch (PermissionException e) {
LOGGER.warn("WARNING: Not able to disconnect due to: " + e.toString());
}
return restSmartProxy;
}
// All of the following methods are used for Test Driven Dev
......
package org.activeeon.morphemic.application.deployment;
import lombok.SneakyThrows;
import org.activeeon.morphemic.service.SchedulerConnectionHelper;
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.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.ow2.proactive.scheduler.common.job.JobResult;
import org.ow2.proactive.scheduler.common.job.JobState;
import org.ow2.proactive.scheduler.common.job.TaskFlowJob;
import org.ow2.proactive.scheduler.common.task.TaskId;
import org.ow2.proactive.scheduler.common.task.TaskResult;
import org.ow2.proactive.scheduler.job.JobIdImpl;
import org.ow2.proactive_grid_cloud_portal.smartproxy.RestSmartProxyImpl;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.AdditionalMatchers.not;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
class PASchedulerGatewayTest {
private static final Logger LOGGER = Logger.getLogger(PASchedulerGatewayTest.class);
private final String DUMMY_USERNAME = "username";
private final String DUMMY_PASSWORD = "password";
private final TaskFlowJob DUMMY_JOB = new TaskFlowJob();
private final long DUMMY_JOB_ID = 1234;
private final String DUMMY_TASK_NAME = "dummyTask";
private final String DUMMY_TASK_ID = "dummyTaskId";
private final long DUMMY_RESULT = 0;
private final File DUMMY_XML_FILE = new File("");
private final Map<String, String> DUMMY_VARIABLES = new HashMap<>();
private final long DUMMY_TIMEOUT = 10000;
private final List<String> DUMMY_JOB_IDS = IntStream.range(1,4).mapToObj(i -> "JobId"+i).collect(Collectors.toList());
private PASchedulerGateway schedulerGateway;
// Enable the mocking of static methods
MockedStatic<SchedulerConnectionHelper> mb = Mockito.mockStatic(SchedulerConnectionHelper.class);
@Mock
private RestSmartProxyImpl restSmartProxy;
@Mock
private JobResult jobResult;
@Mock
private Map<Long, Map<String, Serializable>> dummyResults;
@Mock
private JobState jobState;
@Mock
private TaskResult taskResult;
@Mock
private TaskId taskId;
/**
* Enable all mocks
* Set up the required resources to build the tests
*/
@SneakyThrows
@BeforeEach
void setUp() {
// Enable all mocks
MockitoAnnotations.openMocks(this);
String DUMMY_CONNECTION_URL = "http://notExist.activeeon.com:8080";
schedulerGateway = new PASchedulerGateway(DUMMY_CONNECTION_URL);
// For the connect function of PASchedulerGateway class
// Mock the restSmartProxy in the SchedulerConnectionHelper connect method to return a successful connection
// when valid credentials are used and return false otherwise
mb.when(()->SchedulerConnectionHelper.connect(DUMMY_USERNAME,DUMMY_PASSWORD)).then(implementation->{
when(restSmartProxy.isConnected()).thenReturn(true);
return restSmartProxy;
});
mb.when(()->SchedulerConnectionHelper.connect((not(eq(DUMMY_USERNAME))),not(eq(DUMMY_PASSWORD)))).then(implementation->{
when(restSmartProxy.isConnected()).thenReturn(false);
return restSmartProxy;
});
// For the disconnect function of PASchedulerGateway class
// Mock the restSmartProxy in the SchedulerConnectionHelper disconnect method to return a successful disconnection
mb.when(SchedulerConnectionHelper::disconnect).then(implementation->{
mb.when(()->restSmartProxy.isConnected()).thenReturn(false);
return restSmartProxy;
});
// For submit with Job method of PASchedulerGateway class
// Mock the restSmartProxy to return a successful job submission with a dummy id when a dummy TaskFlowJob is submitted
try {
when(restSmartProxy.submit(DUMMY_JOB)).thenReturn(new JobIdImpl(DUMMY_JOB_ID, ""));
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For submit with XML file method of PASchedulerGateway class
// Mock the restSmartProxy to return a successful job submission with a dummy id when a dummy XML file is submitted
try {
when(restSmartProxy.submit(DUMMY_XML_FILE)).thenReturn(new JobIdImpl(DUMMY_JOB_ID, ""));
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For submit with XML file and variables method of PASchedulerGateway class
// Mock the restSmartProxy to return a successful job submission with a dummy id when a dummy XML file and dummy variables are submitted
try {
when(restSmartProxy.submit(DUMMY_XML_FILE, DUMMY_VARIABLES)).thenReturn(new JobIdImpl(DUMMY_JOB_ID, ""));
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For the getJobState method of PASchedulerGateway class
// Mock the restSmartProxy to return a dummy JobState when the method is called
try {
when(jobState.getId()).thenReturn(new JobIdImpl(DUMMY_RESULT, ""));
when(restSmartProxy.getJobState(String.valueOf(DUMMY_JOB_ID))).thenReturn(jobState);
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For the waitForJob method of PASchedulerGateway class
// Mock the restSmartProxy to return a dummy JobResult when the method is called
try {
when(jobResult.getJobId()).thenReturn(new JobIdImpl(DUMMY_JOB_ID, ""));
when(restSmartProxy.waitForJob(String.valueOf(DUMMY_JOB_ID), DUMMY_TIMEOUT)).thenReturn(jobResult);
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For the getJobResultMaps method of PASchedulerGateway class
// Mock the restSmartProxy to return a dummy JobResults when the method is called
try {
// To avoid creating a map, the MAP class is mocked in sort that the size() function return a static value
when(dummyResults.size()).thenReturn(3);
when(restSmartProxy.getJobResultMaps(DUMMY_JOB_IDS)).thenReturn(dummyResults);
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For the killJob method of PASchedulerGateway class
// Mock the restSmartProxy to return true when the method is called
try {
when(restSmartProxy.killJob(String.valueOf(DUMMY_JOB_ID))).thenReturn(true);
when(restSmartProxy.killJob(not(eq(String.valueOf(DUMMY_JOB_ID))))).thenReturn(false);
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For the waitForTask method of PASchedulerGateway class
// Mock the restSmartProxy to return a dummy TaskResult when the method is called
try {
when(taskId.value()).thenReturn(DUMMY_TASK_ID);
when(taskResult.getTaskId()).thenReturn(taskId);
when(restSmartProxy.waitForTask(String.valueOf(DUMMY_JOB_ID), DUMMY_TASK_NAME, DUMMY_TIMEOUT)).thenReturn(taskResult);
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For the getTaskResult method of PASchedulerGateway class
// Mock the restSmartProxy to return a dummy TaskResult when the method is called
try {
when(taskId.value()).thenReturn(DUMMY_TASK_ID);
when(taskResult.getTaskId()).thenReturn(taskId);
when(restSmartProxy.getTaskResult(String.valueOf(DUMMY_JOB_ID), DUMMY_TASK_NAME)).thenReturn(taskResult);
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
// For all the remaining methods
schedulerGateway.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);
Logger.getLogger(PASchedulerGateway.class).setLevel(Level.OFF);
}
/**
* Enable all loggers and close the static mocking after each test
*/
@AfterEach
void tearDown() {
// Enable all loggers
Logger.getLogger(SchedulerConnectionHelper.class).setLevel(Level.ALL);
Logger.getLogger(PASchedulerGateway.class).setLevel(Level.ALL);
// Close the static mocking
mb.close();
}
/**
* Test if the submitWithJob method of the PASchedulerGateway class return the expected value
*/
@Test
void submitWithJob() {
Long jobId = schedulerGateway.submit(DUMMY_JOB).longValue();
assertEquals(DUMMY_JOB_ID,jobId);
}
/**
* Test if the submitWithJob method (with an XML file as input) of the PASchedulerGateway class return the expected value
*/
@Test
void submitWithXML() {
Long jobId = schedulerGateway.submit(DUMMY_XML_FILE).longValue();
assertEquals(DUMMY_JOB_ID,jobId);
}
/**
* Test if the submitWithJob method (with an XML file and a list of variables as inputs) of the PASchedulerGateway class return the expected value
*/
@Test
void submitWithXMLFileAndAMapOfVariables() {
Long jobId = schedulerGateway.submit(DUMMY_XML_FILE, DUMMY_VARIABLES).longValue();
assertEquals(DUMMY_JOB_ID,jobId);
}
/**
* Test if the getJobState method of the PASchedulerGateway class return the expected result
*/
@Test
void getJobState() {
JobState actualJobState = schedulerGateway.getJobState(String.valueOf(DUMMY_JOB_ID));
assertEquals(DUMMY_RESULT, actualJobState.getId().longValue());
}
/**
* Test if the waitForJob method of the PASchedulerGateway class return the expected result
*/
@Test
void waitForJob() {
JobResult actualJobResult = schedulerGateway.waitForJob(String.valueOf(DUMMY_JOB_ID),DUMMY_TIMEOUT);
assertEquals(DUMMY_JOB_ID, actualJobResult.getJobId().longValue());
}
/**
* Test if the getJobResults method of the PASchedulerGateway class return the expected result
*/
@SneakyThrows
@Test
void getJobResultMaps() {
Map<Long, Map<String, Serializable>> actualResults = schedulerGateway.getJobResultMaps(DUMMY_JOB_IDS);
assertEquals(3, actualResults.size());
}
/**
* Test if the killJob method of the PASchedulerGateway class return the expected result
*/
@Test
void killJob() {
assertTrue(schedulerGateway.killJob(String.valueOf(DUMMY_JOB_ID)));
long DUMMY_INVALID_JOB_ID = 5678;
assertFalse(schedulerGateway.killJob(String.valueOf(DUMMY_INVALID_JOB_ID)));
}
/**
* Test if the waitForTask method of the PASchedulerGateway class return the expected result
*/
@Test
void waitForTask() {
TaskId actualTaskId = schedulerGateway.waitForTask(String.valueOf(DUMMY_JOB_ID), DUMMY_TASK_NAME, DUMMY_TIMEOUT).getTaskId();
assertEquals(DUMMY_TASK_ID, actualTaskId.value());
}
/**
* Test if the getTaskResult method of the PASchedulerGateway class return the expected result
*/
@Test
void getTaskResult() {
TaskId actualTaskId = schedulerGateway.getTaskResult(String.valueOf(DUMMY_JOB_ID), DUMMY_TASK_NAME).getTaskId();
assertEquals(DUMMY_TASK_ID, actualTaskId.value());
}
/**
* Test the connection method of the PASchedulerGateway class
* If a valid username and password are used the isConnected() method is expected to return true
* Otherwise it should return false
*/
@Test
void connect() {
schedulerGateway.connect(DUMMY_USERNAME,DUMMY_PASSWORD);
assertTrue(schedulerGateway.getRestSmartProxy().isConnected());
schedulerGateway.connect("wrongUsername", "wrongPassword");
assertFalse(schedulerGateway.getRestSmartProxy().isConnected());
}
/**
* Test the disconnect method of the PASchedulerGateway class
* When the function is called, the isConnected() method is expected to return false
*/
@Test
void disconnect() {
schedulerGateway.disconnect();
assertFalse(schedulerGateway.getRestSmartProxy().isConnected());
}
}
\ No newline at end of file
package org.activeeon.morphemic.infrastructure.deployment;
import lombok.SneakyThrows;
import org.activeeon.morphemic.service.RMConnectionHelper;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Ignore;
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.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.ow2.proactive.resourcemanager.common.NSState;
import org.ow2.proactive_grid_cloud_portal.common.RMRestInterface;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.AdditionalMatchers.not;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class PAResourceManagerGatewayTest {
private static final Logger LOGGER = Logger.getLogger(PAResourceManagerGatewayTest.class);
PAResourceManagerGateway paResourceManagerGateway;
private final String DUMMY_USERNAME = "username";
private final String DUMMY_PASSWORD = "password";
private final String DUMMY_NODE_SOURCE = "nodeSource";
private final String DUMMY_NODE_SOURCE_URL = "http://notExist.activeeon.com:8080/nodes/1435";
private final String INVALID_DUMMY_NODE_SOURCE_URL = "http://notExist.activeeon.com:8080/nodes/3412";
private final String INVALID_DUMMY_NODE_SOURCE = "invalidNodeSource";
private final String DUMMY_SESSION_ID = "sessionId";
private final boolean preempt = true;
private final List<String> DUMMY_TAGS = IntStream.range(1,4).mapToObj(i-> "Tag"+ i).collect(Collectors.toList());
private final Set<String> DUMMY_NODES_URLS = new HashSet<>();
// Enable the mocking of static methods
MockedStatic<RMConnectionHelper> mb = Mockito.mockStatic(RMConnectionHelper.class);
@Mock
private RMRestInterface rmRestInterface;
/**
* Enable all mocks
* Set up the required resources to build the tests
*/
@SneakyThrows
@BeforeEach
void setUp() {
// Enable all mocks
MockitoAnnotations.openMocks(this);
for(int i=0; i<4; i++){
DUMMY_NODES_URLS.add("http://notExist.activeeon.com:808"+i);
}
String DUMMY_CONNECTION_URL = "http://notExist.activeeon.com:8080";
paResourceManagerGateway = new PAResourceManagerGateway(DUMMY_CONNECTION_URL);
mb.when(RMConnectionHelper::getSessionId).thenReturn(DUMMY_SESSION_ID);
// For the connect method of the PAResourceManagerGateway class
mb.when(()->RMConnectionHelper.connect(DUMMY_USERNAME, DUMMY_PASSWORD)).thenAnswer(invocation -> {
when(rmRestInterface.isActive(anyString())).thenReturn(false);
mb.when(RMConnectionHelper::getSessionId).thenReturn(DUMMY_SESSION_ID);
return null;
});
// For the disconnect method of the PAResourceManagerGateway class
mb.when(RMConnectionHelper::disconnect).thenAnswer(invocation -> {
mb.when(RMConnectionHelper::getSessionId).thenReturn("");
return null;
});
// For the searchNodes method of the PAResourceManagerGateway class
try {
when(rmRestInterface.searchNodes(RMConnectionHelper.getSessionId(), DUMMY_TAGS, true)).thenReturn(DUMMY_NODES_URLS);
}catch(Exception e){
LOGGER.debug(e.getMessage());
}
// For the undeployNodeSource method of the PAResourceManagerGateway class
try {
NSState correctNSState = new NSState();
correctNSState.setResult(true);
NSState incorrectNSState = new NSState();
incorrectNSState.setResult(false);
when(rmRestInterface.undeployNodeSource(RMConnectionHelper.getSessionId(), DUMMY_NODE_SOURCE, preempt)).thenReturn(correctNSState);
when(rmRestInterface.undeployNodeSource(RMConnectionHelper.getSessionId(), not(eq(DUMMY_NODE_SOURCE)), preempt)).thenReturn(incorrectNSState);
}catch(Exception e){
LOGGER.debug(e.getMessage());
}
// For the removeNodeSource method of the PAResourceManagerGateway class
try {
when(rmRestInterface.removeNodeSource(RMConnectionHelper.getSessionId(), DUMMY_NODE_SOURCE, preempt)).thenReturn(true);
when(rmRestInterface.removeNodeSource(RMConnectionHelper.getSessionId(), not(eq(DUMMY_NODE_SOURCE)), preempt)).thenReturn(false);
}catch(Exception e){
LOGGER.debug(e.getMessage());
}
// For the releaseNodeSource method of the PAResourceManagerGateway class
try {
when(rmRestInterface.releaseNode(RMConnectionHelper.getSessionId(), DUMMY_NODE_SOURCE_URL)).thenReturn(true);
when(rmRestInterface.releaseNode(RMConnectionHelper.getSessionId(), not(eq(DUMMY_NODE_SOURCE_URL)))).thenReturn(false);
}catch(Exception e){
LOGGER.debug(e.getMessage());
}
// For the removeNode method of the PAResourceManagerGateway class
try {
when(rmRestInterface.removeNode(RMConnectionHelper.getSessionId(), DUMMY_NODE_SOURCE_URL, preempt)).thenReturn(true);
when(rmRestInterface.removeNode(RMConnectionHelper.getSessionId(), not(eq(DUMMY_NODE_SOURCE_URL)), preempt)).thenReturn(false);
}catch(Exception e){
LOGGER.debug(e.getMessage());
}
// For all test methods
paResourceManagerGateway.setRmRestInterface(rmRestInterface);
// Temporary disable all Logging from the RMConnectionHelper class
// It is enabled after the tests are completed
Logger.getLogger(RMConnectionHelper.class).setLevel(Level.OFF);
Logger.getLogger(PAResourceManagerGateway.class).setLevel(Level.OFF);
}
/**
* Enable all loggers and close the static mocking after each test
*/
@AfterEach
void tearDown() {
// Enable all loggers
Logger.getLogger(RMConnectionHelper.class).setLevel(Level.ALL);
Logger.getLogger(PAResourceManagerGateway.class).setLevel(Level.ALL);
// Close the static mocking
mb.close();
}
/**
* Test the connect method of the PAResourceManagerGateway class
*/
@SneakyThrows
@Test
void connect() {
try {
paResourceManagerGateway.connect(DUMMY_USERNAME, DUMMY_PASSWORD);
assertEquals(DUMMY_SESSION_ID, paResourceManagerGateway.getSessionId());
}catch (Exception e){
LOGGER.debug(e.getMessage());
}
}
/**
* Test the disconnect method of the PAResourceManagerGateway class
*/
@Test
void disconnect() {
paResourceManagerGateway.disconnect();
assertEquals("", paResourceManagerGateway.getSessionId());
}
/**
*
*/
@Ignore
@Test
void getAsyncDeployedNodesInformation() {