Testing executor pool #92

Merged
rahimiankeanu merged 7 commits from testing-executorPool into dev 2021-11-28 16:50:43 +00:00
5 changed files with 259 additions and 13 deletions
Showing only changes of commit 0288082cd5 - Show all commits

View File

@ -0,0 +1,64 @@
package ch.unisg.executorpool;
import static org.mockito.Mockito.times;
import java.net.URI;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.then;
import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolCommand;
import ch.unisg.executorpool.application.port.out.AddExecutorPort;
import ch.unisg.executorpool.application.port.out.ExecutorAddedEventPort;
import ch.unisg.executorpool.application.service.AddNewExecutorToExecutorPoolService;
import ch.unisg.executorpool.domain.ExecutorClass;
import ch.unisg.executorpool.domain.ExecutorPool;
public class AddNewExecutorToExecutorPoolServiceTest {
private final AddExecutorPort addExecutorPort = Mockito.mock(AddExecutorPort.class);
private final ExecutorAddedEventPort newExecutorAddedEventPort = Mockito.mock(ExecutorAddedEventPort.class);
private final AddNewExecutorToExecutorPoolService addNewExecutorToExecutorPoolService = new AddNewExecutorToExecutorPoolService(
newExecutorAddedEventPort, addExecutorPort);
@Test
void addingSucceeds() {
ExecutorClass newExecutor = givenAnExecutorWithTypeAndUri(new ExecutorClass.ExecutorTaskType("test-type"),
Optional.of(new ExecutorClass.ExecutorUri(URI.create("example.org"))));
ExecutorPool executorPool = givenAnEmptyExecutorPool(ExecutorPool.getExecutorPool());
AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand(newExecutor.getExecutorTaskType(),
Optional.ofNullable(newExecutor.getExecutorUri()));
ExecutorClass addedExecutor = addNewExecutorToExecutorPoolService.addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand);
assertThat(addedExecutor).isNotNull();
assertThat(executorPool.getListOfExecutors().getValue()).hasSize(1);
then(newExecutorAddedEventPort).should(times(1)).publishNewExecutorAddedEvent(any(NewExecutorAddedEvent.class));
}
private ExecutorPool givenAnEmptyExecutorPool(ExecutorPool executorPool) {
executorPool.getListOfExecutors().getValue().clear();
return executorPool;
}
private ExecutorClass givenAnExecutorWithTypeAndUri(ExecutorClass.ExecutorTaskType executorTaskType,
Optional<ExecutorClass.ExecutorUri> executorUri) {
ExecutorClass executor = Mockito.mock(ExecutorClass.class);
given(ExecutorClass.getExecutorTaskType()).willReturn(executorTaskType);
given(ExecutorClass.getExecutorUri()).willReturn(executorUri);
return executor;
}
}

View File

@ -0,0 +1,70 @@
package ch.unisg.executorpool;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
//import static org.mockito.BDDMockito.then;
import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation;
import ch.unisg.executorpool.application.port.out.AddExecutorPort;
import ch.unisg.executorpool.domain.ExecutorPool;
import ch.unisg.executorpool.domain.ExecutorClass;
import ch.unisg.executorpool.domain.ExecutorClass.ExecutorTaskType;
import ch.unisg.executorpool.domain.ExecutorClass.ExecutorUri;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AddNewExecutorToExecutorPoolSystemTest {
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private AddExecutorPort addExecutorPort;
@Test
void AddNewExecutorToExecutorPool() throws JSONException {
ExecutorTaskType executorTaskType = new ExecutorTaskType("system-integration-test-type");
ExecutorUri executorUri = new ExecutorUri(java.net.URI.create("example.org"));
ResponseEntity response = whenAddNewExecutorToEmptyPool(executorTaskType, executorUri);
JSONObject responseJson = new JSONObject(response.getBody().toString());
String respExecutorId = responseJson.getString("executorId");
String respExecutorTaskType = responseJson.getString("executorTaskType");
then(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
then(respExecutorId).isNotEmpty();
then(respExecutorTaskType).isEqualTo(executorTaskType.getValue());
then(ExecutorPool.getExecutorPool().getListOfExecutors().getValue()).hasSize(1);
}
private ResponseEntity whenAddNewExecutorToEmptyPool(ExecutorTaskType executorTaskType,
ExecutorUri executorUri) throws JSONException {
ExecutorPool.getExecutorPool().getListOfExecutors().getValue().clear();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE);
String jsonPayLoad = new JSONObject().put("executorTaskType", executorTaskType.getValue())
.put("executorUri", executorUri.getValue()).toString();
HttpEntity<String> request = new HttpEntity<>(jsonPayLoad,headers);
return restTemplate.exchange("/executorpool/", HttpMethod.POST, request, Object.class);
}
}

View File

@ -0,0 +1,69 @@
package ch.unisg.executorpool;
import java.util.Optional;
import org.bson.json.JsonObject;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.then;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation;
import ch.unisg.executorpool.adapter.in.web.AddNewExecutorToExecutorPoolWebController;
import ch.unisg.executorpool.adapter.out.persistence.mongodb.ExecutorRepository;
import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolCommand;
import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolUseCase;
import ch.unisg.executorpool.domain.ExecutorClass;
import ch.unisg.executorpool.domain.ExecutorClass.ExecutorTaskType;
import ch.unisg.executorpool.domain.ExecutorClass.ExecutorUri;
import net.minidev.json.JSONObject;
@WebMvcTest(controllers = AddNewExecutorToExecutorPoolWebController.class)
public class AddNewExecutorToExecutorPoolWebControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AddNewExecutorToExecutorPoolUseCase addNewExecutorToExecutorPoolUseCase;
@MockBean
ExecutorRepository executorRepository;
@Test
void testAddNewExecutorToExecutorPool() throws Exception {
String executorTaskType = "test-request-type";
String executorUri = "example.org";
String jsonPayLoad = new JSONObject().put("executorTaskType", executorTaskType).put("executorUri", executorUri).toString();
ExecutorClass executorStub = ExecutorClass.createExecutorClass(new ExecutorUri(java.net.URI.create(executorUri)),
new ExecutorClass.ExecutorTaskType(executorTaskType));
AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand(
new ExecutorTaskType(executorTaskType), Optional.of(new ExecutorUri(java.net.URI.create(executorUri))));
Mockito.when(addNewExecutorToExecutorPoolUseCase
.addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand))
.thenReturn(executorStub);
mockMvc.perform(post("/executorpool/")
.contentType(ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE)
.content(jsonPayLoad))
.andExpect(status().isCreated());
then(addNewExecutorToExecutorPoolUseCase).should()
.addNewExecutorToExecutorPool(eq(new AddNewExecutorToExecutorPoolCommand(
new ExecutorTaskType(executorTaskType), Optional.of(new ExecutorUri(java.net.URI.create(executorUri))))));
}
}

View File

@ -1,13 +0,0 @@
package ch.unisg.executorpool;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ExecutorPoolApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -0,0 +1,56 @@
package ch.unisg.executorpool;
import java.net.URI;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import ch.unisg.executorpool.domain.ExecutorClass;
import ch.unisg.executorpool.domain.ExecutorPool;
import ch.unisg.executorpool.domain.ExecutorClass.ExecutorUri;
import ch.unisg.executorpool.domain.ExecutorClass.ExecutorTaskType;
public class ExecutorPoolTest {
private static final URI URI = null;
@Test
void addNewExecutorToExecutorPoolSuccess() {
ExecutorPool executorPool = ExecutorPool.getExecutorPool();
executorPool.getListOfExecutors().getValue().clear();
ExecutorClass newExecutor = executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) ,
new ExecutorTaskType("test-type"));
assertThat(newExecutor.getExecutorTaskType().getValue()).isEqualTo("test-type");
assertThat(executorPool.getListOfExecutors().getValue()).hasSize(1);
assertThat(executorPool.getListOfExecutors().getValue().get(0)).isEqualTo(newExecutor);
}
@Test
void retrieveExecutorSuccess() {
ExecutorPool executorPool = ExecutorPool.getExecutorPool();
ExecutorClass newExecutor = executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , new ExecutorTaskType("test-type"));
ExecutorClass retrievedExecutor = executorPool.getExecutorByUri(newExecutor.getExecutorUri()).get();
assertThat(retrievedExecutor).isEqualTo(newExecutor);
}
@Test
void retrieveExecutorFailure() {
ExecutorPool executorPool = ExecutorPool.getExecutorPool();
executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , new ExecutorTaskType("test-type"));
ExecutorUri fakeUri = new ExecutorUri(java.net.URI.create("fake-Uri"));
Optional<ExecutorClass> retrievedExecutor = executorPool.getExecutorByUri(fakeUri);
assertThat(retrievedExecutor.isPresent()).isFalse();
}
}