From 0288082cd54a40295f33259bc82d95e09a8a40ee Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Fri, 26 Nov 2021 22:16:23 +0100 Subject: [PATCH 1/6] Testing ExecutorPool version 1 --- ...dNewExecutorToExecutorPoolServiceTest.java | 64 +++++++++++++++++ ...ddNewExecutorToExecutorPoolSystemTest.java | 70 +++++++++++++++++++ ...ecutorToExecutorPoolWebControllerTest.java | 69 ++++++++++++++++++ .../ExecutorPoolApplicationTests.java | 13 ---- .../unisg/executorpool/ExecutorPoolTest.java | 56 +++++++++++++++ 5 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java delete mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java new file mode 100644 index 0000000..27f447b --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java @@ -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 executorUri) { + + ExecutorClass executor = Mockito.mock(ExecutorClass.class); + given(ExecutorClass.getExecutorTaskType()).willReturn(executorTaskType); + given(ExecutorClass.getExecutorUri()).willReturn(executorUri); + return executor; + } + + + +} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java new file mode 100644 index 0000000..1eba971 --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java @@ -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 request = new HttpEntity<>(jsonPayLoad,headers); + + return restTemplate.exchange("/executorpool/", HttpMethod.POST, request, Object.class); + + } + +} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java new file mode 100644 index 0000000..cc28edb --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java @@ -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)))))); + + + } + + +} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java deleted file mode 100644 index 77e1032..0000000 --- a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java +++ /dev/null @@ -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() { - } - -} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java new file mode 100644 index 0000000..d94fd6a --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java @@ -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 retrievedExecutor = executorPool.getExecutorByUri(fakeUri); + + assertThat(retrievedExecutor.isPresent()).isFalse(); + } + + + + +} -- 2.45.1 From e1cd5774f14bb1dff3876c798c5e44976d6f5293 Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Sat, 27 Nov 2021 12:53:30 +0100 Subject: [PATCH 2/6] ExecutorPoolTest code should be fine. But half of tests failed --- .../executorpool/domain/ExecutorClass.java | 2 +- ...dNewExecutorToExecutorPoolServiceTest.java | 19 ++++++++++--------- ...ddNewExecutorToExecutorPoolSystemTest.java | 2 +- ...ecutorToExecutorPoolWebControllerTest.java | 8 +++++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java index 5da6fe7..021b58b 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java @@ -18,7 +18,7 @@ public class ExecutorClass { this.executorTaskType = executorTaskType; } - protected static ExecutorClass createExecutorClass(ExecutorUri executorUri, ExecutorTaskType executorTaskType){ + public static ExecutorClass createExecutorClass(ExecutorUri executorUri, ExecutorTaskType executorTaskType){ System.out.println("New Executor: " + executorUri.value.toString() + " " + executorTaskType.getValue()); return new ExecutorClass(executorUri, executorTaskType); } diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java index 27f447b..3f6f70e 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java @@ -3,19 +3,20 @@ 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.mockito.BDDMockito.*; 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.ExecutorAddedEvent; import ch.unisg.executorpool.domain.ExecutorClass; import ch.unisg.executorpool.domain.ExecutorPool; +import ch.unisg.executorpool.domain.ExecutorClass.ExecutorUri; public class AddNewExecutorToExecutorPoolServiceTest { @@ -28,19 +29,19 @@ public class AddNewExecutorToExecutorPoolServiceTest { void addingSucceeds() { ExecutorClass newExecutor = givenAnExecutorWithTypeAndUri(new ExecutorClass.ExecutorTaskType("test-type"), - Optional.of(new ExecutorClass.ExecutorUri(URI.create("example.org")))); + new ExecutorClass.ExecutorUri(URI.create("example.org"))); ExecutorPool executorPool = givenAnEmptyExecutorPool(ExecutorPool.getExecutorPool()); - AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand(newExecutor.getExecutorTaskType(), - Optional.ofNullable(newExecutor.getExecutorUri())); + AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand( + newExecutor.getExecutorUri(), newExecutor.getExecutorTaskType()); ExecutorClass addedExecutor = addNewExecutorToExecutorPoolService.addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand); assertThat(addedExecutor).isNotNull(); assertThat(executorPool.getListOfExecutors().getValue()).hasSize(1); - then(newExecutorAddedEventPort).should(times(1)).publishNewExecutorAddedEvent(any(NewExecutorAddedEvent.class)); + then(newExecutorAddedEventPort).should(times(1)).publishExecutorAddedEvent(any(ExecutorAddedEvent.class)); } @@ -51,11 +52,11 @@ public class AddNewExecutorToExecutorPoolServiceTest { } private ExecutorClass givenAnExecutorWithTypeAndUri(ExecutorClass.ExecutorTaskType executorTaskType, - Optional executorUri) { + ExecutorUri executorUri) { ExecutorClass executor = Mockito.mock(ExecutorClass.class); - given(ExecutorClass.getExecutorTaskType()).willReturn(executorTaskType); - given(ExecutorClass.getExecutorUri()).willReturn(executorUri); + given(executor.getExecutorTaskType()).willReturn(executorTaskType); + given(executor.getExecutorUri()).willReturn(executorUri); return executor; } diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java index 1eba971..05b6099 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java @@ -11,7 +11,7 @@ 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 static org.assertj.core.api.BDDAssertions.*; import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation; import ch.unisg.executorpool.application.port.out.AddExecutorPort; diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java index cc28edb..4d6417d 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java @@ -11,6 +11,8 @@ 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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation; import ch.unisg.executorpool.adapter.in.web.AddNewExecutorToExecutorPoolWebController; @@ -20,7 +22,7 @@ import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolUse 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; +import org.json.JSONObject; @WebMvcTest(controllers = AddNewExecutorToExecutorPoolWebController.class) @@ -47,7 +49,7 @@ public class AddNewExecutorToExecutorPoolWebControllerTest { new ExecutorClass.ExecutorTaskType(executorTaskType)); AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand( - new ExecutorTaskType(executorTaskType), Optional.of(new ExecutorUri(java.net.URI.create(executorUri)))); + new ExecutorUri(java.net.URI.create(executorUri)), new ExecutorTaskType(executorTaskType)); Mockito.when(addNewExecutorToExecutorPoolUseCase .addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand)) @@ -60,7 +62,7 @@ public class AddNewExecutorToExecutorPoolWebControllerTest { then(addNewExecutorToExecutorPoolUseCase).should() .addNewExecutorToExecutorPool(eq(new AddNewExecutorToExecutorPoolCommand( - new ExecutorTaskType(executorTaskType), Optional.of(new ExecutorUri(java.net.URI.create(executorUri)))))); + new ExecutorUri(java.net.URI.create(executorUri)), new ExecutorTaskType(executorTaskType)))); } -- 2.45.1 From d1fe47db6f2b1d2e77b27c257f4df2df0efad886 Mon Sep 17 00:00:00 2001 From: reynisson Date: Sun, 28 Nov 2021 17:13:14 +0100 Subject: [PATCH 3/6] Retrieve task from memory to be able to check task results. Temp fix until we start updating tasks in DB --- .../application/service/RetrieveTaskFromTaskListService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/RetrieveTaskFromTaskListService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/RetrieveTaskFromTaskListService.java index 39026f5..696514b 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/RetrieveTaskFromTaskListService.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/RetrieveTaskFromTaskListService.java @@ -28,6 +28,6 @@ public class RetrieveTaskFromTaskListService implements RetrieveTaskFromTaskList Optional taskFromRepo = Optional.ofNullable(loadTaskFromRepositoryPort.loadTask(query.getTaskId(), taskList.getTaskListName())); - return taskFromRepo; + return task; } } -- 2.45.1 From e03fc296ee2cf4acde931cd5f202f7552becb4c3 Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Fri, 26 Nov 2021 22:16:23 +0100 Subject: [PATCH 4/6] Testing ExecutorPool version 1 --- ...dNewExecutorToExecutorPoolServiceTest.java | 64 +++++++++++++++++ ...ddNewExecutorToExecutorPoolSystemTest.java | 70 +++++++++++++++++++ ...ecutorToExecutorPoolWebControllerTest.java | 69 ++++++++++++++++++ .../ExecutorPoolApplicationTests.java | 13 ---- .../unisg/executorpool/ExecutorPoolTest.java | 56 +++++++++++++++ 5 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java delete mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java create mode 100644 executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java new file mode 100644 index 0000000..27f447b --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java @@ -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 executorUri) { + + ExecutorClass executor = Mockito.mock(ExecutorClass.class); + given(ExecutorClass.getExecutorTaskType()).willReturn(executorTaskType); + given(ExecutorClass.getExecutorUri()).willReturn(executorUri); + return executor; + } + + + +} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java new file mode 100644 index 0000000..1eba971 --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java @@ -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 request = new HttpEntity<>(jsonPayLoad,headers); + + return restTemplate.exchange("/executorpool/", HttpMethod.POST, request, Object.class); + + } + +} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java new file mode 100644 index 0000000..cc28edb --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java @@ -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)))))); + + + } + + +} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java deleted file mode 100644 index 77e1032..0000000 --- a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolApplicationTests.java +++ /dev/null @@ -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() { - } - -} diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java new file mode 100644 index 0000000..d94fd6a --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java @@ -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 retrievedExecutor = executorPool.getExecutorByUri(fakeUri); + + assertThat(retrievedExecutor.isPresent()).isFalse(); + } + + + + +} -- 2.45.1 From 3c8171af8e05057d834512c4791997c567a15007 Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Sat, 27 Nov 2021 12:53:30 +0100 Subject: [PATCH 5/6] ExecutorPoolTest code should be fine. But half of tests failed --- .../executorpool/domain/ExecutorClass.java | 2 +- ...dNewExecutorToExecutorPoolServiceTest.java | 19 ++++++++++--------- ...ddNewExecutorToExecutorPoolSystemTest.java | 2 +- ...ecutorToExecutorPoolWebControllerTest.java | 8 +++++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java index 5da6fe7..021b58b 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorClass.java @@ -18,7 +18,7 @@ public class ExecutorClass { this.executorTaskType = executorTaskType; } - protected static ExecutorClass createExecutorClass(ExecutorUri executorUri, ExecutorTaskType executorTaskType){ + public static ExecutorClass createExecutorClass(ExecutorUri executorUri, ExecutorTaskType executorTaskType){ System.out.println("New Executor: " + executorUri.value.toString() + " " + executorTaskType.getValue()); return new ExecutorClass(executorUri, executorTaskType); } diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java index 27f447b..3f6f70e 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java @@ -3,19 +3,20 @@ 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.mockito.BDDMockito.*; 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.ExecutorAddedEvent; import ch.unisg.executorpool.domain.ExecutorClass; import ch.unisg.executorpool.domain.ExecutorPool; +import ch.unisg.executorpool.domain.ExecutorClass.ExecutorUri; public class AddNewExecutorToExecutorPoolServiceTest { @@ -28,19 +29,19 @@ public class AddNewExecutorToExecutorPoolServiceTest { void addingSucceeds() { ExecutorClass newExecutor = givenAnExecutorWithTypeAndUri(new ExecutorClass.ExecutorTaskType("test-type"), - Optional.of(new ExecutorClass.ExecutorUri(URI.create("example.org")))); + new ExecutorClass.ExecutorUri(URI.create("example.org"))); ExecutorPool executorPool = givenAnEmptyExecutorPool(ExecutorPool.getExecutorPool()); - AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand(newExecutor.getExecutorTaskType(), - Optional.ofNullable(newExecutor.getExecutorUri())); + AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand( + newExecutor.getExecutorUri(), newExecutor.getExecutorTaskType()); ExecutorClass addedExecutor = addNewExecutorToExecutorPoolService.addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand); assertThat(addedExecutor).isNotNull(); assertThat(executorPool.getListOfExecutors().getValue()).hasSize(1); - then(newExecutorAddedEventPort).should(times(1)).publishNewExecutorAddedEvent(any(NewExecutorAddedEvent.class)); + then(newExecutorAddedEventPort).should(times(1)).publishExecutorAddedEvent(any(ExecutorAddedEvent.class)); } @@ -51,11 +52,11 @@ public class AddNewExecutorToExecutorPoolServiceTest { } private ExecutorClass givenAnExecutorWithTypeAndUri(ExecutorClass.ExecutorTaskType executorTaskType, - Optional executorUri) { + ExecutorUri executorUri) { ExecutorClass executor = Mockito.mock(ExecutorClass.class); - given(ExecutorClass.getExecutorTaskType()).willReturn(executorTaskType); - given(ExecutorClass.getExecutorUri()).willReturn(executorUri); + given(executor.getExecutorTaskType()).willReturn(executorTaskType); + given(executor.getExecutorUri()).willReturn(executorUri); return executor; } diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java index 1eba971..05b6099 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java @@ -11,7 +11,7 @@ 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 static org.assertj.core.api.BDDAssertions.*; import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation; import ch.unisg.executorpool.application.port.out.AddExecutorPort; diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java index cc28edb..4d6417d 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java @@ -11,6 +11,8 @@ 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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation; import ch.unisg.executorpool.adapter.in.web.AddNewExecutorToExecutorPoolWebController; @@ -20,7 +22,7 @@ import ch.unisg.executorpool.application.port.in.AddNewExecutorToExecutorPoolUse 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; +import org.json.JSONObject; @WebMvcTest(controllers = AddNewExecutorToExecutorPoolWebController.class) @@ -47,7 +49,7 @@ public class AddNewExecutorToExecutorPoolWebControllerTest { new ExecutorClass.ExecutorTaskType(executorTaskType)); AddNewExecutorToExecutorPoolCommand addNewExecutorToExecutorPoolCommand = new AddNewExecutorToExecutorPoolCommand( - new ExecutorTaskType(executorTaskType), Optional.of(new ExecutorUri(java.net.URI.create(executorUri)))); + new ExecutorUri(java.net.URI.create(executorUri)), new ExecutorTaskType(executorTaskType)); Mockito.when(addNewExecutorToExecutorPoolUseCase .addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand)) @@ -60,7 +62,7 @@ public class AddNewExecutorToExecutorPoolWebControllerTest { then(addNewExecutorToExecutorPoolUseCase).should() .addNewExecutorToExecutorPool(eq(new AddNewExecutorToExecutorPoolCommand( - new ExecutorTaskType(executorTaskType), Optional.of(new ExecutorUri(java.net.URI.create(executorUri)))))); + new ExecutorUri(java.net.URI.create(executorUri)), new ExecutorTaskType(executorTaskType)))); } -- 2.45.1 From c0412f6eba02ac46ea38aed7fee741012c631381 Mon Sep 17 00:00:00 2001 From: reynisson Date: Sun, 28 Nov 2021 17:43:50 +0100 Subject: [PATCH 6/6] Fixed executor-pool tests that were failing --- .../AddNewExecutorToExecutorPoolSystemTest.java | 14 +++++++------- ...ExecutorToExecutorPoolWebControllerTest.java | 8 ++++++-- .../ch/unisg/executorpool/ExecutorPoolTest.java | 17 ++++++++++------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java index 05b6099..5c39427 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolSystemTest.java @@ -23,7 +23,7 @@ import ch.unisg.executorpool.domain.ExecutorClass.ExecutorUri; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class AddNewExecutorToExecutorPoolSystemTest { - + @Autowired private TestRestTemplate restTemplate; @@ -39,17 +39,17 @@ public class AddNewExecutorToExecutorPoolSystemTest { ResponseEntity response = whenAddNewExecutorToEmptyPool(executorTaskType, executorUri); JSONObject responseJson = new JSONObject(response.getBody().toString()); - String respExecutorId = responseJson.getString("executorId"); + String respExecutorUri = responseJson.getString("executorUri"); String respExecutorTaskType = responseJson.getString("executorTaskType"); then(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); - then(respExecutorId).isNotEmpty(); + then(respExecutorUri).isEqualTo(executorUri.getValue().toString()); then(respExecutorTaskType).isEqualTo(executorTaskType.getValue()); then(ExecutorPool.getExecutorPool().getListOfExecutors().getValue()).hasSize(1); - } + } - private ResponseEntity whenAddNewExecutorToEmptyPool(ExecutorTaskType executorTaskType, + private ResponseEntity whenAddNewExecutorToEmptyPool(ExecutorTaskType executorTaskType, ExecutorUri executorUri) throws JSONException { ExecutorPool.getExecutorPool().getListOfExecutors().getValue().clear(); @@ -63,8 +63,8 @@ public class AddNewExecutorToExecutorPoolSystemTest { HttpEntity request = new HttpEntity<>(jsonPayLoad,headers); - return restTemplate.exchange("/executorpool/", HttpMethod.POST, request, Object.class); - + return restTemplate.exchange("/executor-pool/AddExecutor", HttpMethod.POST, request, Object.class); + } } diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java index 4d6417d..40b7307 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java @@ -2,6 +2,7 @@ package ch.unisg.executorpool; import java.util.Optional; +import ch.unisg.executorpool.application.port.out.LoadExecutorPort; import org.bson.json.JsonObject; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -27,7 +28,7 @@ import org.json.JSONObject; @WebMvcTest(controllers = AddNewExecutorToExecutorPoolWebController.class) public class AddNewExecutorToExecutorPoolWebControllerTest { - + @Autowired private MockMvc mockMvc; @@ -37,6 +38,9 @@ public class AddNewExecutorToExecutorPoolWebControllerTest { @MockBean ExecutorRepository executorRepository; + @MockBean + LoadExecutorPort loadExecutorPort; + @Test void testAddNewExecutorToExecutorPool() throws Exception { @@ -55,7 +59,7 @@ public class AddNewExecutorToExecutorPoolWebControllerTest { .addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand)) .thenReturn(executorStub); - mockMvc.perform(post("/executorpool/") + mockMvc.perform(post("/executor-pool/AddExecutor") .contentType(ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE) .content(jsonPayLoad)) .andExpect(status().isCreated()); diff --git a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java index d94fd6a..a9df873 100644 --- a/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java +++ b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java @@ -20,7 +20,7 @@ public class ExecutorPoolTest { void addNewExecutorToExecutorPoolSuccess() { ExecutorPool executorPool = ExecutorPool.getExecutorPool(); executorPool.getListOfExecutors().getValue().clear(); - ExecutorClass newExecutor = executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , + ExecutorClass newExecutor = executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , new ExecutorTaskType("test-type")); assertThat(newExecutor.getExecutorTaskType().getValue()).isEqualTo("test-type"); @@ -31,26 +31,29 @@ public class ExecutorPoolTest { @Test void retrieveExecutorSuccess() { ExecutorPool executorPool = ExecutorPool.getExecutorPool(); + executorPool.getListOfExecutors().getValue().clear(); ExecutorClass newExecutor = executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , new ExecutorTaskType("test-type")); - ExecutorClass retrievedExecutor = executorPool.getExecutorByUri(newExecutor.getExecutorUri()).get(); + var retrievedExecutors = executorPool.getAllExecutorsByType(newExecutor.getExecutorTaskType()); - assertThat(retrievedExecutor).isEqualTo(newExecutor); + assertThat(retrievedExecutors.size()).isEqualTo(1); + assertThat(retrievedExecutors.get(0)).isEqualTo(newExecutor); } @Test void retrieveExecutorFailure() { ExecutorPool executorPool = ExecutorPool.getExecutorPool(); + executorPool.getListOfExecutors().getValue().clear(); executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , new ExecutorTaskType("test-type")); - ExecutorUri fakeUri = new ExecutorUri(java.net.URI.create("fake-Uri")); + var fakeType = new ExecutorTaskType("fake-type"); - Optional retrievedExecutor = executorPool.getExecutorByUri(fakeUri); + var retrievedExecutor = executorPool.getAllExecutorsByType(fakeType); - assertThat(retrievedExecutor.isPresent()).isFalse(); + assertThat(retrievedExecutor.size()).isEqualTo(0); } - + } -- 2.45.1