Browse Source

extract registrate method

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
ea211b5a7b
  1. 34
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
  2. 38
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java
  3. 2
      fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java

34
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java

@ -70,6 +70,22 @@ public class AccessController
return response; 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) public AccessController(final AccessService service)
{ {
this.service = service; this.service = service;
@ -85,23 +101,7 @@ public class AccessController
accessToken); 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); post("/accounts/login", this::login);

38
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.Response;
import spark.Session; import spark.Session;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
@ -159,4 +161,40 @@ class AccessControllerTest
assertEquals(Integer.valueOf(401), statusCaptor.getValue(), "Should return Status code 401."); 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<Integer> 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<Integer> 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.");
}
} }

2
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.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@AllArgsConstructor(staticName = "of") @AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class RegistrateRequest public class RegistrateRequest
{ {
private String userName; private String userName;

Loading…
Cancel
Save