From f6afde28f9fd6bb58f40afaa6a873e718124dc8e Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 20:03:59 +0100 Subject: [PATCH] add UserClient with updateUser-Method and test --- .../java/de/fd/fh/network/FeignClients.java | 2 + .../java/de/fd/fh/network/UserClient.java | 18 ++++++ .../de/fd/fh/network/FeignClientsTest.java | 17 ++++++ .../java/de/fd/fh/network/UserClientTest.java | 57 +++++++++++++++++++ .../de/fd/fh/server/user/UserService.java | 2 +- .../fd/fh/server/user/web/UserController.java | 1 + .../de/fd/fh/server/user/UserServiceTest.java | 2 +- .../network/messages}/ChangeUserRequest.java | 2 +- .../shared/network/messages/Identifier.java | 12 ++++ .../network/messages/PlayerResponse.java | 13 +++++ 10 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 fh.fd.ci.client/src/main/java/de/fd/fh/network/UserClient.java create mode 100644 fh.fd.ci.client/src/test/java/de/fd/fh/network/FeignClientsTest.java create mode 100644 fh.fd.ci.client/src/test/java/de/fd/fh/network/UserClientTest.java rename {fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web => fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages}/ChangeUserRequest.java (84%) create mode 100644 fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/Identifier.java create mode 100644 fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/PlayerResponse.java diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java index 16ce86d..9dc77f0 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java @@ -12,10 +12,12 @@ public class FeignClients private static final String BASE_URL = "http://localhost:4567"; private AccessClient accessClient; + private UserClient userClient; public FeignClients() { accessClient = buildClient(AccessClient.class); + userClient = buildClient(UserClient.class); } T buildClient(final Class client) diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/UserClient.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/UserClient.java new file mode 100644 index 0000000..e928f09 --- /dev/null +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/UserClient.java @@ -0,0 +1,18 @@ +package de.fd.fh.network; + +import de.fd.fh.shared.Utils; +import de.fd.fh.shared.network.messages.ChangeUserRequest; +import de.fd.fh.shared.network.messages.PlayerResponse; +import feign.Headers; +import feign.Param; +import feign.RequestLine; + +public interface UserClient +{ + @RequestLine("POST /users") + @Headers(Utils.AUTHENTICATION_HEADER + ":Bearer {authToken}") + PlayerResponse updatePlayer( + @Param("authToken") final String authenticationHeader, + final ChangeUserRequest request + ); +} diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/FeignClientsTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/FeignClientsTest.java new file mode 100644 index 0000000..629286b --- /dev/null +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/FeignClientsTest.java @@ -0,0 +1,17 @@ +package de.fd.fh.network; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FeignClientsTest +{ + @Test + void testInitFeignClients() + { + final FeignClients feignClients = new FeignClients(); + + assertNotNull(feignClients.getAccessClient()); + assertNotNull(feignClients.getUserClient()); + } +} \ No newline at end of file diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/UserClientTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/UserClientTest.java new file mode 100644 index 0000000..b7a2375 --- /dev/null +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/UserClientTest.java @@ -0,0 +1,57 @@ +package de.fd.fh.network; + +import de.fd.fh.shared.network.messages.ChangeUserRequest; +import de.fd.fh.shared.network.messages.PlayerResponse; +import feign.Feign; +import feign.gson.GsonDecoder; +import feign.gson.GsonEncoder; +import feign.mock.HttpMethod; +import feign.mock.MockClient; +import feign.mock.MockTarget; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class UserClientTest +{ + private UserClient client; + private MockClient mockClient; + + @BeforeEach + void before() + { + mockClient = new MockClient(); + + client = Feign.builder() + .decoder(new GsonDecoder()) + .encoder(new GsonEncoder()) + .client(mockClient) + .target(new MockTarget<>(UserClient.class)); + } + + @Test + void updatePlayer() + { + mockClient.ok(HttpMethod.POST, "/users", + "{\n" + + " \"id\": {\n" + + " \"identifier\":\"12345\"\n" + + " },\n" + + " \"name\":\"TestName\"\n" + + "}"); + + final ChangeUserRequest request = new ChangeUserRequest( + "TestName", + "TestPassword"); + + final PlayerResponse response = client.updatePlayer("TestAuthHeader", request); + + assertNotNull(response); + assertEquals("TestName", response.getName()); + assertEquals("12345", response.getId().getIdentifier()); + + mockClient.verifyOne(HttpMethod.POST, "/users"); + } +} \ No newline at end of file diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java index 930a807..5aa31a2 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java @@ -2,7 +2,7 @@ package de.fd.fh.server.user; import de.fd.fh.server.user.events.ChangePasswordEvent; import de.fd.fh.server.user.events.ChangeUserNameEvent; -import de.fd.fh.server.user.web.ChangeUserRequest; +import de.fd.fh.shared.network.messages.ChangeUserRequest; import de.fd.fh.server.user.web.UserRequest; import lombok.RequiredArgsConstructor; diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java index a264e6b..f276326 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java @@ -6,6 +6,7 @@ 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 de.fd.fh.shared.network.messages.ChangeUserRequest; import lombok.RequiredArgsConstructor; import spark.Request; import spark.Response; diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java index 357ef0e..86b3c7d 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java @@ -1,6 +1,6 @@ package de.fd.fh.server.user; -import de.fd.fh.server.user.web.ChangeUserRequest; +import de.fd.fh.shared.network.messages.ChangeUserRequest; import de.fd.fh.server.user.web.UserRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/ChangeUserRequest.java similarity index 84% rename from fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java rename to fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/ChangeUserRequest.java index a41e949..a45fc3e 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java +++ b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/ChangeUserRequest.java @@ -1,4 +1,4 @@ -package de.fd.fh.server.user.web; +package de.fd.fh.shared.network.messages; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/Identifier.java b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/Identifier.java new file mode 100644 index 0000000..deafda1 --- /dev/null +++ b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/Identifier.java @@ -0,0 +1,12 @@ +package de.fd.fh.shared.network.messages; + + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class Identifier +{ + private String identifier; +} diff --git a/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/PlayerResponse.java b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/PlayerResponse.java new file mode 100644 index 0000000..1ec5505 --- /dev/null +++ b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/PlayerResponse.java @@ -0,0 +1,13 @@ +package de.fd.fh.shared.network.messages; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class PlayerResponse +{ + private final Identifier id; + + private String name; +}