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 new file mode 100644 index 0000000..3f6f70e --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolServiceTest.java @@ -0,0 +1,65 @@ +package ch.unisg.executorpool; + +import static org.mockito.Mockito.times; + +import java.net.URI; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import static org.mockito.BDDMockito.*; +import static org.assertj.core.api.Assertions.*; + +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 { + + 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"), + new ExecutorClass.ExecutorUri(URI.create("example.org"))); + + ExecutorPool executorPool = givenAnEmptyExecutorPool(ExecutorPool.getExecutorPool()); + + 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)).publishExecutorAddedEvent(any(ExecutorAddedEvent.class)); + + } + + + private ExecutorPool givenAnEmptyExecutorPool(ExecutorPool executorPool) { + executorPool.getListOfExecutors().getValue().clear(); + return executorPool; + } + + private ExecutorClass givenAnExecutorWithTypeAndUri(ExecutorClass.ExecutorTaskType executorTaskType, + ExecutorUri executorUri) { + + ExecutorClass executor = Mockito.mock(ExecutorClass.class); + 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 new file mode 100644 index 0000000..5c39427 --- /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.assertj.core.api.BDDAssertions.*; + +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 respExecutorUri = responseJson.getString("executorUri"); + String respExecutorTaskType = responseJson.getString("executorTaskType"); + + then(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); + then(respExecutorUri).isEqualTo(executorUri.getValue().toString()); + 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("/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 new file mode 100644 index 0000000..40b7307 --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/AddNewExecutorToExecutorPoolWebControllerTest.java @@ -0,0 +1,75 @@ +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; +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 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; +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 org.json.JSONObject; + + +@WebMvcTest(controllers = AddNewExecutorToExecutorPoolWebController.class) +public class AddNewExecutorToExecutorPoolWebControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private AddNewExecutorToExecutorPoolUseCase addNewExecutorToExecutorPoolUseCase; + + @MockBean + ExecutorRepository executorRepository; + + @MockBean + LoadExecutorPort loadExecutorPort; + + @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 ExecutorUri(java.net.URI.create(executorUri)), new ExecutorTaskType(executorTaskType)); + + Mockito.when(addNewExecutorToExecutorPoolUseCase + .addNewExecutorToExecutorPool(addNewExecutorToExecutorPoolCommand)) + .thenReturn(executorStub); + + mockMvc.perform(post("/executor-pool/AddExecutor") + .contentType(ExecutorJsonRepresentation.EXECUTOR_MEDIA_TYPE) + .content(jsonPayLoad)) + .andExpect(status().isCreated()); + + then(addNewExecutorToExecutorPoolUseCase).should() + .addNewExecutorToExecutorPool(eq(new AddNewExecutorToExecutorPoolCommand( + new ExecutorUri(java.net.URI.create(executorUri)), new ExecutorTaskType(executorTaskType)))); + + + } + + +} 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..a9df873 --- /dev/null +++ b/executor-pool/src/test/java/ch/unisg/executorpool/ExecutorPoolTest.java @@ -0,0 +1,59 @@ +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(); + executorPool.getListOfExecutors().getValue().clear(); + ExecutorClass newExecutor = executorPool.addNewExecutor(new ExecutorUri(java.net.URI.create("example.com")) , new ExecutorTaskType("test-type")); + + var retrievedExecutors = executorPool.getAllExecutorsByType(newExecutor.getExecutorTaskType()); + + 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")); + + var fakeType = new ExecutorTaskType("fake-type"); + + var retrievedExecutor = executorPool.getAllExecutorsByType(fakeType); + + assertThat(retrievedExecutor.size()).isEqualTo(0); + } + + + + +}