diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java index 6bd9676..6594de0 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java @@ -70,6 +70,22 @@ public class AccessController return response; } + public Object registrate(Request request, Response response) throws java.io.IOException + { + final RegistrateRequest message = + objectMapper.readValue(request.body(), RegistrateRequest.class); + + if (service.createPlayer(message)) + { + response.status(201); + } + else + { + response.status(400); + } + return response; + } + public AccessController(final AccessService service) { this.service = service; @@ -85,23 +101,7 @@ public class AccessController accessToken); }); - post("/accounts/registrate", - (request, response) -> - { - final RegistrateRequest message = - objectMapper.readValue(request.body(), RegistrateRequest.class); - - if (service.createPlayer(message)) - { - response.status(201); - } - else - { - response.status(400); - } - return response; - } - ); + post("/accounts/registrate", this::registrate); post("/accounts/login", this::login); diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java index ecd494d..22d64e2 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java @@ -13,6 +13,8 @@ import spark.Request; import spark.Response; import spark.Session; +import java.io.IOException; + import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.then; @@ -159,4 +161,40 @@ class AccessControllerTest assertEquals(Integer.valueOf(401), statusCaptor.getValue(), "Should return Status code 401."); } + + @Test + void registrateTest() throws IOException + { + when(request.body()).thenReturn("{\"userName\":\"TestName\",\"password\":\"TestPassword\"}"); + + when(service.createPlayer(any())).thenReturn(true); + + final ArgumentCaptor statusCaptor = ArgumentCaptor.forClass(Integer.class); + + new AccessController(service).registrate(request, response); + + verify(response).status(statusCaptor.capture()); + + then(service).should().createPlayer(any()); + then(service).shouldHaveNoMoreInteractions(); + assertEquals(Integer.valueOf(201), statusCaptor.getValue(), "Should return Status code 201."); + } + + @Test + void failedRegistrateTest() throws IOException + { + when(request.body()).thenReturn("{\"userName\":\"TestName\",\"password\":\"TestPassword\"}"); + + when(service.createPlayer(any())).thenReturn(false); + + final ArgumentCaptor statusCaptor = ArgumentCaptor.forClass(Integer.class); + + new AccessController(service).registrate(request, response); + + verify(response).status(statusCaptor.capture()); + + then(service).should().createPlayer(any()); + then(service).shouldHaveNoMoreInteractions(); + assertEquals(Integer.valueOf(400), statusCaptor.getValue(), "Should return Status code 400."); + } } \ No newline at end of file diff --git a/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java index b3c08d4..1b37a02 100644 --- a/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java +++ b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java @@ -2,9 +2,11 @@ package de.fd.fh.shared.network.messages; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; @Data @AllArgsConstructor(staticName = "of") +@NoArgsConstructor public class RegistrateRequest { private String userName;