Jenkins
4 years ago
22 changed files with 1103 additions and 212 deletions
-
14Jenkinsfile
-
37fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java
-
5fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Access.java
-
31fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java
-
7fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessService.java
-
169fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
-
1fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java
-
17fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java
-
14fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangeUserNameEvent.java
-
10fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java
-
149fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java
-
166fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java
-
86fh.fd.ci.server/src/test/java/de/fd/fh/server/ApiTestUtils.java
-
27fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java
-
45fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java
-
48fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTest.java
-
18fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java
-
225fh.fd.ci.server/src/test/java/de/fd/fh/server/access/web/AccessControllerTest.java
-
38fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java
-
28fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserTest.java
-
174fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java
-
2fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/RegistrateRequest.java
@ -0,0 +1,14 @@ |
|||
package de.fd.fh.server.user.events; |
|||
|
|||
import de.fd.fh.server.user.UserId; |
|||
import lombok.Getter; |
|||
import lombok.RequiredArgsConstructor; |
|||
|
|||
@RequiredArgsConstructor |
|||
@Getter |
|||
public class ChangeUserNameEvent |
|||
{ |
|||
private final UserId userId; |
|||
|
|||
private final String newName; |
|||
} |
@ -1,13 +1,15 @@ |
|||
package de.fd.fh.server.user.web; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Getter; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
@RequiredArgsConstructor |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Getter |
|||
public class ChangeUserRequest |
|||
{ |
|||
private final String name; |
|||
private String name; |
|||
|
|||
private final String password; |
|||
private String password; |
|||
} |
@ -1,89 +1,90 @@ |
|||
package de.fd.fh.server.user.web; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import de.fd.fh.server.access.AccessToken; |
|||
import de.fd.fh.server.user.User; |
|||
import de.fd.fh.server.user.UserId; |
|||
import de.fd.fh.server.user.UserService; |
|||
import lombok.RequiredArgsConstructor; |
|||
import spark.Request; |
|||
import spark.Response; |
|||
|
|||
import static spark.Spark.get; |
|||
import static spark.Spark.post; |
|||
|
|||
@RequiredArgsConstructor |
|||
public class UserController |
|||
{ |
|||
private ObjectMapper objectMapper = new ObjectMapper(); |
|||
|
|||
public UserController(final UserService service) |
|||
private final UserService service; |
|||
|
|||
public Response getUser(Request request, Response response) throws JsonProcessingException |
|||
{ |
|||
final UserId userId = UserId.of(request.params(":user_id")); |
|||
final UserRequest user = service.getSmallPlayer(userId); |
|||
|
|||
if (user == null) |
|||
{ |
|||
response.status(404); |
|||
return response; |
|||
} |
|||
response.body(objectMapper.writeValueAsString(user)); |
|||
return response; |
|||
} |
|||
|
|||
public Response getCompleteUser(Request request, Response response) throws JsonProcessingException |
|||
{ |
|||
post("/users", |
|||
((request, response) -> |
|||
{ |
|||
final UserId userId = |
|||
((AccessToken) request.session() |
|||
.attribute("userId")) |
|||
.getUserId(); |
|||
|
|||
final ChangeUserRequest message = objectMapper.readValue(request.body(), |
|||
ChangeUserRequest.class); |
|||
|
|||
final User user = service.changePlayer( |
|||
userId, |
|||
message); |
|||
|
|||
if (user == null) |
|||
{ |
|||
response.status(400); |
|||
} |
|||
else |
|||
{ |
|||
response.status(200); |
|||
response.type("application/json"); |
|||
|
|||
return objectMapper.writeValueAsString(user); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
)); |
|||
|
|||
get("/users", |
|||
(request, response) -> |
|||
{ |
|||
final UserId userId = |
|||
((AccessToken) request.session() |
|||
.attribute("userId")) |
|||
.getUserId(); |
|||
|
|||
final User user = service.getPlayer(userId); |
|||
|
|||
if (user == null) |
|||
{ |
|||
response.status(400); |
|||
} |
|||
else |
|||
{ |
|||
response.status(200); |
|||
response.type("application/json"); |
|||
|
|||
return objectMapper.writeValueAsString(user); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
); |
|||
|
|||
get("/users/:user_id", |
|||
(request, response) -> |
|||
{ |
|||
final UserId userId = UserId.of(request.params(":user_id")); |
|||
final UserRequest user = service.getSmallPlayer(userId); |
|||
|
|||
if (user == null) |
|||
{ |
|||
response.status(404); |
|||
} |
|||
response.body(objectMapper.writeValueAsString(user)); |
|||
return response; |
|||
}); |
|||
final UserId userId = |
|||
((AccessToken) request.session() |
|||
.attribute("userId")) |
|||
.getUserId(); |
|||
|
|||
final User user = service.getPlayer(userId); |
|||
|
|||
if (user == null) |
|||
{ |
|||
response.status(404); |
|||
} |
|||
else |
|||
{ |
|||
response.status(200); |
|||
response.type("application/json"); |
|||
|
|||
response.body(objectMapper.writeValueAsString(user)); |
|||
|
|||
return response; |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
|
|||
public Response updateUser(Request request, Response response) throws java.io.IOException |
|||
{ |
|||
final UserId userId = |
|||
((AccessToken) request.session() |
|||
.attribute("userId")) |
|||
.getUserId(); |
|||
|
|||
final ChangeUserRequest message = objectMapper.readValue(request.body(), |
|||
ChangeUserRequest.class); |
|||
|
|||
final User user = service.changePlayer( |
|||
userId, |
|||
message); |
|||
|
|||
if (user == null) |
|||
{ |
|||
response.status(400); |
|||
} |
|||
else |
|||
{ |
|||
response.status(200); |
|||
response.type("application/json"); |
|||
|
|||
response.body(objectMapper.writeValueAsString(user)); |
|||
|
|||
return response; |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
} |
@ -0,0 +1,166 @@ |
|||
package de.fd.fh; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import de.fd.fh.server.ApiTestUtils; |
|||
import de.fd.fh.server.access.web.AccessController; |
|||
import de.fd.fh.server.user.web.UserController; |
|||
import de.fd.fh.shared.Utils; |
|||
import org.junit.jupiter.api.AfterAll; |
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.Test; |
|||
import spark.Response; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.BDDMockito.then; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.when; |
|||
import static spark.Spark.awaitInitialization; |
|||
import static spark.Spark.stop; |
|||
|
|||
class ServerAppTest |
|||
{ |
|||
private static UserController userController = mock(UserController.class); |
|||
private static AccessController accessController = mock(AccessController.class); |
|||
|
|||
@BeforeAll |
|||
static void before() |
|||
{ |
|||
ServerApp.initController(accessController, userController); |
|||
ServerApp.main(null); |
|||
|
|||
awaitInitialization(); |
|||
} |
|||
|
|||
@AfterAll |
|||
static void after() |
|||
{ |
|||
stop(); |
|||
} |
|||
|
|||
@Test |
|||
void testHalloWorld() |
|||
{ |
|||
String url = "/hello"; |
|||
Map<String, String> headers = new HashMap<>(); |
|||
headers.put(Utils.AUTHENTICATION_HEADER, "Bearer testToken"); |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("GET", url, null, headers, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
assertEquals("Hello World", res.getBody()); |
|||
} |
|||
|
|||
@Test |
|||
void testRegistrate() throws IOException |
|||
{ |
|||
when(accessController.registrate(any(), any())).thenReturn("Test"); |
|||
|
|||
String url = "/accounts/registrate"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("POST", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(accessController).should().registrate(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
void testLogin() throws JsonProcessingException |
|||
{ |
|||
when(accessController.login(any(), any())).thenReturn("Test"); |
|||
|
|||
String url = "/accounts/login"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("POST", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(accessController).should().login(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
void testLogout() |
|||
{ |
|||
when(accessController.logout(any(), any())).thenReturn("Test"); |
|||
|
|||
String url = "/accounts/logout"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("POST", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(accessController).should().logout(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
void testDeletePlayer() |
|||
{ |
|||
final Response response = mock(Response.class); |
|||
|
|||
when(accessController.deletePlayer(any(), any())).thenReturn(response); |
|||
|
|||
String url = "/accounts/12345"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("DELETE", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(accessController).should().deletePlayer(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
void updateUser() throws IOException |
|||
{ |
|||
when(userController.updateUser(any(), any())).thenReturn(mock(Response.class)); |
|||
|
|||
String url = "/users"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("POST", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(userController).should().updateUser(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
void getPlayers() throws JsonProcessingException |
|||
{ |
|||
when(userController.getCompleteUser(any(), any())).thenReturn(mock(Response.class)); |
|||
|
|||
String url = "/users"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("GET", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(userController).should().getCompleteUser(any(), any()); |
|||
} |
|||
|
|||
@Test |
|||
void getPlayer() throws JsonProcessingException |
|||
{ |
|||
when(userController.getUser(any(), any())).thenReturn(mock(Response.class)); |
|||
|
|||
String url = "/users/12345"; |
|||
|
|||
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>() |
|||
.request("GET", url, null, null, String.class); |
|||
|
|||
assertNotNull(res); |
|||
assertEquals(200, res.getStatus()); |
|||
then(userController).should().getUser(any(), any()); |
|||
} |
|||
} |
@ -0,0 +1,86 @@ |
|||
package de.fd.fh.server; |
|||
|
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import lombok.Getter; |
|||
import lombok.Setter; |
|||
import spark.utils.IOUtils; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.OutputStream; |
|||
import java.io.OutputStreamWriter; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.util.Map; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.fail; |
|||
|
|||
public class ApiTestUtils<T> |
|||
{ |
|||
public TestResponse<T> request( |
|||
final String method, |
|||
final String path, |
|||
final String requestBody, |
|||
final Map<String, String> header, |
|||
final Class<T> responseBodyType) |
|||
{ |
|||
try |
|||
{ |
|||
URL url = new URL("http://localhost:4567" + path); |
|||
final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
|||
connection.setRequestMethod(method); |
|||
connection.setDoOutput(true); |
|||
|
|||
if (requestBody != null) |
|||
{ |
|||
final OutputStream outputStream = connection.getOutputStream(); |
|||
final OutputStreamWriter outputStreamWriter = |
|||
new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); |
|||
outputStreamWriter.write(requestBody); |
|||
outputStreamWriter.flush(); |
|||
outputStreamWriter.close(); |
|||
} |
|||
|
|||
if (header != null) |
|||
{ |
|||
header.forEach(connection::addRequestProperty); |
|||
} |
|||
|
|||
connection.connect(); |
|||
|
|||
final String body = IOUtils.toString(connection.getInputStream()); |
|||
return new TestResponse<T>(connection.getResponseCode(), body, responseBodyType); |
|||
} |
|||
catch (final IOException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
fail("Sending request failed: " + e.getMessage()); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
@Getter |
|||
@Setter |
|||
public static class TestResponse<T> |
|||
{ |
|||
|
|||
private final String body; |
|||
private final int status; |
|||
private final Class<T> type; |
|||
|
|||
public TestResponse( |
|||
final int status, |
|||
final String body, |
|||
final Class<T> type) |
|||
{ |
|||
this.status = status; |
|||
this.body = body; |
|||
this.type = type; |
|||
} |
|||
|
|||
public T json() throws IOException |
|||
{ |
|||
return new ObjectMapper().readValue(body, type); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
package de.fd.fh.server.access; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class AccessTest |
|||
{ |
|||
@Test |
|||
void removeTokenTest() |
|||
{ |
|||
final Access access = new Access(null, null, null, null, new AccessToken(), null); |
|||
|
|||
access.removeToken(); |
|||
|
|||
assertNull(access.getToken()); |
|||
} |
|||
|
|||
@Test |
|||
void setTokenTest() |
|||
{ |
|||
final Access access = new Access(); |
|||
|
|||
access.setToken(new AccessToken()); |
|||
|
|||
assertNotNull(access.getToken()); |
|||
} |
|||
|
|||
@Test |
|||
void updatePasswordTest() |
|||
{ |
|||
final Access access = new Access(null,null, "TestPassword", null, null, null); |
|||
|
|||
access.updatePassword("NewTestPassword"); |
|||
|
|||
assertEquals("NewTestPassword", access.getPassword(), "Should update password."); |
|||
} |
|||
|
|||
@Test |
|||
void renameTest() |
|||
{ |
|||
final Access access = new Access(null, "TestName", null, null, null, null); |
|||
|
|||
access.rename("NewTestName"); |
|||
|
|||
assertEquals("NewTestName", access.getName(), "Should update name."); |
|||
} |
|||
} |
@ -0,0 +1,225 @@ |
|||
package de.fd.fh.server.access.web; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import de.fd.fh.server.access.AccessService; |
|||
import de.fd.fh.server.access.AccessToken; |
|||
import de.fd.fh.server.access.Role; |
|||
import de.fd.fh.server.user.UserId; |
|||
import de.fd.fh.shared.network.messages.LoginRequest; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.ArgumentCaptor; |
|||
import org.mockito.Mock; |
|||
import org.mockito.MockitoAnnotations; |
|||
import spark.Request; |
|||
import spark.Response; |
|||
import spark.Session; |
|||
|
|||
import java.io.IOException; |
|||
import java.time.LocalDateTime; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.BDDMockito.then; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
class AccessControllerTest |
|||
{ |
|||
@Mock |
|||
Request request; |
|||
@Mock |
|||
Response response; |
|||
@Mock |
|||
Session session; |
|||
@Mock |
|||
AccessService service; |
|||
|
|||
@BeforeEach |
|||
void before() |
|||
{ |
|||
MockitoAnnotations.openMocks(this); |
|||
} |
|||
|
|||
@Test |
|||
void deletePlayerTest() |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(request.session()).thenReturn(session); |
|||
when(session.attribute(any())).thenReturn(new AccessToken()); |
|||
when(service.deleteAccount(any(), any())).thenReturn(true); |
|||
|
|||
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new AccessController(service).deletePlayer(request, response); |
|||
|
|||
verify(response).status(captor.capture()); |
|||
|
|||
then(service).should().deleteAccount(any(), any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
|
|||
assertEquals(captor.getValue(), Integer.valueOf(200), "Should return Status code 200."); |
|||
} |
|||
|
|||
@Test |
|||
void failedDeletePlayerTest() |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(request.session()).thenReturn(session); |
|||
when(session.attribute(any())).thenReturn(new AccessToken()); |
|||
when(service.deleteAccount(any(), any())).thenReturn(false); |
|||
|
|||
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new AccessController(service).deletePlayer(request, response); |
|||
|
|||
verify(response).status(captor.capture()); |
|||
|
|||
then(service).should().deleteAccount(any(), any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
|
|||
assertEquals(captor.getValue(), Integer.valueOf(400), "Should return Status code 400."); |
|||
} |
|||
|
|||
@Test |
|||
void testLogout() |
|||
{ |
|||
when(request.headers(any())).thenReturn("testHeader"); |
|||
when(service.logout(any())).thenReturn(true); |
|||
|
|||
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new AccessController(service).logout(request, response); |
|||
|
|||
verify(response).status(captor.capture()); |
|||
|
|||
then(service).should().logout(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
|
|||
assertEquals(captor.getValue(), Integer.valueOf(200), "Should return Status code 200."); |
|||
} |
|||
|
|||
@Test |
|||
void failedLogoutTest() |
|||
{ |
|||
when(request.headers(any())).thenReturn("testHeader"); |
|||
when(service.logout(any())).thenReturn(false); |
|||
|
|||
final ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new AccessController(service).logout(request, response); |
|||
|
|||
verify(response).status(captor.capture()); |
|||
|
|||
then(service).should().logout(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
|
|||
assertEquals(captor.getValue(), Integer.valueOf(400), "Should return Status code 400."); |
|||
} |
|||
|
|||
@Test |
|||
void loginTest() throws JsonProcessingException |
|||
{ |
|||
final LoginRequest dummy = new LoginRequest(); |
|||
dummy.setName("TestName"); |
|||
dummy.setToken("TestToken"); |
|||
dummy.setUserId("12345"); |
|||
|
|||
when(request.headers(any())).thenReturn("testHeader"); |
|||
when(service.authorization(any())).thenReturn(dummy); |
|||
|
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
final ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class); |
|||
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class); |
|||
|
|||
new AccessController(service).login(request, response); |
|||
|
|||
verify(response).status(statusCaptor.capture()); |
|||
verify(response).type(typeCaptor.capture()); |
|||
verify(response).body(bodyCaptor.capture()); |
|||
|
|||
then(service).should().authorization(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
|
|||
assertEquals(Integer.valueOf(200), statusCaptor.getValue(), "Should return Status code 200."); |
|||
assertEquals("application/json", typeCaptor.getValue(), "Should have return type json."); |
|||
assertEquals("{\"name\":\"TestName\",\"userId\":\"12345\",\"token\":\"TestToken\"}", |
|||
bodyCaptor.getValue(), "Should return correct Body."); |
|||
} |
|||
|
|||
@Test |
|||
void failedLoginTest() throws JsonProcessingException |
|||
{ |
|||
when(request.headers(any())).thenReturn("testHeader"); |
|||
when(service.authorization(any())).thenReturn(null); |
|||
|
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
new AccessController(service).login(request, response); |
|||
|
|||
verify(response).status(statusCaptor.capture()); |
|||
|
|||
then(service).should().authorization(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
|
|||
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."); |
|||
} |
|||
|
|||
@Test |
|||
void testBefore() |
|||
{ |
|||
final AccessToken dummy = new AccessToken("TestToken", LocalDateTime.now(), Role.USER, UserId.random()); |
|||
|
|||
when(request.pathInfo()).thenReturn("/test/path"); |
|||
when(request.headers(any())).thenReturn("TestAuthHeader"); |
|||
when(request.session()).thenReturn(session); |
|||
when(service.before(any(), any())) |
|||
.thenReturn(dummy); |
|||
|
|||
final ArgumentCaptor<AccessToken> captor = ArgumentCaptor.forClass(AccessToken.class); |
|||
|
|||
new AccessController(service).before(request); |
|||
|
|||
verify(session).attribute(any(), captor.capture()); |
|||
|
|||
assertEquals(dummy, captor.getValue(), "Should store Token in Session"); |
|||
then(service).should().before(any(), any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
package de.fd.fh.server.user; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class UserTest |
|||
{ |
|||
@Test |
|||
void testRename() |
|||
{ |
|||
final User user = new User(UserId.random(), "TestName"); |
|||
|
|||
user.rename("NewTestName"); |
|||
|
|||
assertEquals("NewTestName", user.getName(), "Should rename user."); |
|||
} |
|||
|
|||
@Test |
|||
void failedRenameTest() |
|||
{ |
|||
final User user = new User(UserId.random(), "TestName"); |
|||
|
|||
user.rename(null); |
|||
|
|||
assertEquals("TestName", user.getName(), "Should rename user."); |
|||
} |
|||
} |
@ -0,0 +1,174 @@ |
|||
package de.fd.fh.server.user.web; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import de.fd.fh.server.access.AccessToken; |
|||
import de.fd.fh.server.user.User; |
|||
import de.fd.fh.server.user.UserId; |
|||
import de.fd.fh.server.user.UserService; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.ArgumentCaptor; |
|||
import org.mockito.Mock; |
|||
import org.mockito.MockitoAnnotations; |
|||
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; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
class UserControllerTest |
|||
{ |
|||
@Mock |
|||
Request request; |
|||
@Mock |
|||
Response response; |
|||
@Mock |
|||
Session session; |
|||
@Mock |
|||
UserService service; |
|||
|
|||
@BeforeEach |
|||
void before() |
|||
{ |
|||
MockitoAnnotations.openMocks(this); |
|||
} |
|||
|
|||
@Test |
|||
void getUserTest() throws JsonProcessingException |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(service.getSmallPlayer(any())).thenReturn(new UserRequest("12345", "TestName")); |
|||
|
|||
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
|||
|
|||
final Response result = new UserController(service).getUser(request, response); |
|||
|
|||
verify(response).body(captor.capture()); |
|||
|
|||
assertEquals(response, result, "Should return the incomming response."); |
|||
assertEquals("{\"id\":\"12345\",\"name\":\"TestName\"}", captor.getValue(), |
|||
"Should return UserRequest in Body."); |
|||
then(service).should().getSmallPlayer(any()); |
|||
} |
|||
|
|||
@Test |
|||
void failedGetUserTest() throws JsonProcessingException |
|||
{ |
|||
when(request.params(any())).thenReturn("12345"); |
|||
when(service.getSmallPlayer(any())).thenReturn(null); |
|||
|
|||
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
final Response result = new UserController(service).getUser(request, response); |
|||
|
|||
verify(response).status(statusCaptor.capture()); |
|||
|
|||
assertEquals(response, result, "Should return the incomming response."); |
|||
assertEquals(Integer.valueOf(404), statusCaptor.getValue(), "Should return not found status code."); |
|||
then(service).should().getSmallPlayer(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
void getCompleteUserTest() throws JsonProcessingException |
|||
{ |
|||
when(request.session()).thenReturn(session); |
|||
when(session.attribute(any())).thenReturn( |
|||
new AccessToken(null, null, null, UserId.of("12345"))); |
|||
when(service.getPlayer(any(UserId.class))).thenReturn(User.of("TestName")); |
|||
|
|||
final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class); |
|||
final ArgumentCaptor<String> bodyCapture = ArgumentCaptor.forClass(String.class); |
|||
final ArgumentCaptor<String> typeCapture = ArgumentCaptor.forClass(String.class); |
|||
|
|||
final Response result = new UserController(service).getCompleteUser(request, response); |
|||
|
|||
verify(response).status(statusCapture.capture()); |
|||
verify(response).type(typeCapture.capture()); |
|||
verify(response).body(bodyCapture.capture()); |
|||
|
|||
assertEquals(response, result, "Should return the incoming Response"); |
|||
assertEquals(Integer.valueOf(200), statusCapture.getValue(), "Should return status code 200."); |
|||
assertEquals("application/json", typeCapture.getValue(), "Should have content type json."); |
|||
assertEquals("{\"id\":null,\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json."); |
|||
|
|||
then(service).should().getPlayer(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
void failedGetCompleteUserTest() throws JsonProcessingException |
|||
{ |
|||
when(request.session()).thenReturn(session); |
|||
when(session.attribute(any())).thenReturn( |
|||
new AccessToken(null, null, null, UserId.of("12345"))); |
|||
when(service.getPlayer(any(UserId.class))).thenReturn(null); |
|||
|
|||
final ArgumentCaptor<Integer> capture = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
final Response result = new UserController(service).getCompleteUser(request, response); |
|||
|
|||
verify(response).status(capture.capture()); |
|||
|
|||
assertEquals(response, result, "Should return the incoming Response"); |
|||
assertEquals(Integer.valueOf(404), capture.getValue(), "Should return status code 200."); |
|||
then(service).should().getPlayer(any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
void updateUserTest() throws IOException |
|||
{ |
|||
when(request.session()).thenReturn(session); |
|||
when(session.attribute(any())).thenReturn( |
|||
new AccessToken(null, null, null, UserId.of("12345"))); |
|||
when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}"); |
|||
when(service.changePlayer(any(), any())).thenReturn(new User(UserId.of("12345"), "TestName")); |
|||
|
|||
final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class); |
|||
final ArgumentCaptor<String> bodyCapture = ArgumentCaptor.forClass(String.class); |
|||
final ArgumentCaptor<String> typeCapture = ArgumentCaptor.forClass(String.class); |
|||
|
|||
final Response result = new UserController(service).updateUser(request, response); |
|||
|
|||
verify(response).status(statusCapture.capture()); |
|||
verify(response).type(typeCapture.capture()); |
|||
verify(response).body(bodyCapture.capture()); |
|||
|
|||
assertEquals(response, result, "Should return the incoming Response"); |
|||
assertEquals(Integer.valueOf(200), statusCapture.getValue(), "Should return status code 200."); |
|||
assertEquals("application/json", typeCapture.getValue(), "Should have content type json."); |
|||
assertEquals("{\"id\":{\"identifier\":\"12345\"},\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json."); |
|||
|
|||
then(service).should().changePlayer(any(), any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
void failedUpdateUserTest() throws IOException |
|||
{ |
|||
when(request.session()).thenReturn(session); |
|||
when(session.attribute(any())).thenReturn( |
|||
new AccessToken(null, null, null, UserId.of("12345"))); |
|||
when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}"); |
|||
when(service.changePlayer(any(), any())).thenReturn(null); |
|||
|
|||
final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class); |
|||
|
|||
final Response result = new UserController(service).updateUser(request, response); |
|||
|
|||
verify(response).status(statusCapture.capture()); |
|||
|
|||
assertEquals(response, result, "Should return the incoming Response"); |
|||
assertEquals(Integer.valueOf(400), statusCapture.getValue(), "Should return status code 400."); |
|||
|
|||
then(service).should().changePlayer(any(), any()); |
|||
then(service).shouldHaveNoMoreInteractions(); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue