From f394ba7b62856d8f8008643fe65195d31b6b158d Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 13:58:24 +0100 Subject: [PATCH 1/5] rename LoginRequest to LoginResponse --- .../java/de/fd/fh/server/access/AccessService.java | 14 +++++++------- .../fd/fh/server/access/web/AccessController.java | 4 ++-- .../de/fd/fh/server/access/AccessServiceTest.java | 6 +++--- .../{LoginRequest.java => LoginResponse.java} | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) rename fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/{LoginRequest.java => LoginResponse.java} (84%) diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessService.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessService.java index 752d321..4549bbc 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessService.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessService.java @@ -3,7 +3,7 @@ package de.fd.fh.server.access; import de.fd.fh.server.access.events.AccountCreatedEvent; import de.fd.fh.server.access.events.AccountDeletedEvent; import de.fd.fh.server.user.UserId; -import de.fd.fh.shared.network.messages.LoginRequest; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; import lombok.RequiredArgsConstructor; import org.bson.types.ObjectId; @@ -97,7 +97,7 @@ public class AccessService extends Observable } } - public LoginRequest authorization(final String header) + public LoginResponse authorization(final String header) { System.out.println("authorization"); final String auth = header.substring("Basic ".length()); @@ -117,12 +117,12 @@ public class AccessService extends Observable access.setToken(AccessToken.of(access)); accessRepository.save(access); - final LoginRequest loginRequest = new LoginRequest(); - loginRequest.setUserId(access.getUserId().getIdentifier()); - loginRequest.setToken(access.getToken().getToken()); - loginRequest.setName(access.getName()); + final LoginResponse loginResponse = new LoginResponse(); + loginResponse.setUserId(access.getUserId().getIdentifier()); + loginResponse.setToken(access.getToken().getToken()); + loginResponse.setName(access.getName()); - return loginRequest; + return loginResponse; } return null; 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 3e32964..fd5af6c 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 @@ -5,7 +5,7 @@ import de.fd.fh.server.access.AccessService; import de.fd.fh.server.access.AccessToken; import de.fd.fh.server.user.UserId; import de.fd.fh.shared.Utils; -import de.fd.fh.shared.network.messages.LoginRequest; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; import static spark.Spark.*; @@ -51,7 +51,7 @@ public class AccessController { final String header = request.headers(Utils.AUTHENTICATION_HEADER); - final LoginRequest login = service.authorization(header); + final LoginResponse login = service.authorization(header); if (login == null) { diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java index 073a84a..4ec739a 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessServiceTest.java @@ -2,7 +2,7 @@ package de.fd.fh.server.access; import com.mongodb.WriteResult; import de.fd.fh.server.user.UserId; -import de.fd.fh.shared.network.messages.LoginRequest; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -176,7 +176,7 @@ class AccessServiceTest implements Observer when(repository.findByUserName(any())) .thenReturn(access); - final LoginRequest result = new AccessService(repository).authorization(header); + final LoginResponse result = new AccessService(repository).authorization(header); assertNotNull(result); assertEquals(result.getName(), "testName", "Wrong UserName"); @@ -191,7 +191,7 @@ class AccessServiceTest implements Observer final String header = "Basic " + new String(message); final AccessRepository repository = mock(AccessRepository.class); - final LoginRequest result = new AccessService(repository).authorization(header); + final LoginResponse result = new AccessService(repository).authorization(header); assertNull(result, "Return LoginRequest but wrong permissions"); } diff --git a/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginRequest.java b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginResponse.java similarity index 84% rename from fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginRequest.java rename to fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginResponse.java index 4e1c38c..734cb52 100644 --- a/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginRequest.java +++ b/fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/LoginResponse.java @@ -3,7 +3,7 @@ package de.fd.fh.shared.network.messages; import lombok.Data; @Data -public class LoginRequest +public class LoginResponse { private String name; private String userId; From 2cd85077f3460ccb69a2e7a66a198e38a257d3e9 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 14:04:40 +0100 Subject: [PATCH 2/5] add first Feign Client with registrate method and test --- fh.fd.ci.client/build.gradle | 6 +++ .../java/de/fd/fh/network/AccessClient.java | 11 +++++ .../java/de/fd/fh/network/FeignClients.java | 29 ++++++++++++ .../de/fd/fh/network/AccessClientTest.java | 46 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java create mode 100644 fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java create mode 100644 fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java diff --git a/fh.fd.ci.client/build.gradle b/fh.fd.ci.client/build.gradle index 2e0dd87..2593e5e 100644 --- a/fh.fd.ci.client/build.gradle +++ b/fh.fd.ci.client/build.gradle @@ -7,6 +7,12 @@ mainClassName = 'de.fd.fh.ClientApp' dependencies { implementation project(':fh.fd.ci.shared') + + implementation 'io.github.openfeign:feign-okhttp:11.0' + implementation 'io.github.openfeign:feign-gson:11.0' + implementation 'io.github.openfeign:feign-slf4j:11.0' + + testImplementation 'io.github.openfeign:feign-mock:11.0' } javafx { diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java new file mode 100644 index 0000000..9bfa233 --- /dev/null +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java @@ -0,0 +1,11 @@ +package de.fd.fh.network; + +import de.fd.fh.shared.network.messages.RegistrateRequest; +import feign.RequestLine; +import feign.Response; + +public interface AccessClient +{ + @RequestLine("POST /accounts/registrate") + Response registrate(RegistrateRequest request); +} 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 new file mode 100644 index 0000000..16ce86d --- /dev/null +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java @@ -0,0 +1,29 @@ +package de.fd.fh.network; + +import feign.Feign; +import feign.gson.GsonDecoder; +import feign.gson.GsonEncoder; +import feign.okhttp.OkHttpClient; +import lombok.Getter; + +@Getter +public class FeignClients +{ + private static final String BASE_URL = "http://localhost:4567"; + + private AccessClient accessClient; + + public FeignClients() + { + accessClient = buildClient(AccessClient.class); + } + + T buildClient(final Class client) + { + return Feign.builder() + .client(new OkHttpClient()) + .encoder(new GsonEncoder()) + .decoder(new GsonDecoder()) + .target(client, BASE_URL); + } +} diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java new file mode 100644 index 0000000..adb3016 --- /dev/null +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java @@ -0,0 +1,46 @@ +package de.fd.fh.network; + +import de.fd.fh.shared.network.messages.RegistrateRequest; +import feign.Feign; +import feign.Response; +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; + +class AccessClientTest +{ + private AccessClient client; + private MockClient mockClient; + + @BeforeEach + void before() + { + mockClient = new MockClient(); + + client = Feign.builder() + .decoder(new GsonDecoder()) + .encoder(new GsonEncoder()) + .client(mockClient) + .target(new MockTarget<>(AccessClient.class)); + } + + @Test + void testRegistrate() + { + mockClient.ok(HttpMethod.POST, "/accounts/registrate"); + + final RegistrateRequest request = RegistrateRequest.of("TestName", "TestPassword"); + + final Response result = client.registrate(request); + + assertEquals(200, result.status(), "Should return status code 200."); + + mockClient.verifyStatus(); + } +} \ No newline at end of file From 01fcf86ce2f70dde1009d81065c87e24f2bd0703 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 14:06:12 +0100 Subject: [PATCH 3/5] add login method to AccessClient and test --- .../java/de/fd/fh/network/AccessClient.java | 8 +++++ .../de/fd/fh/network/AccessClientTest.java | 32 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java index 9bfa233..6ce4d47 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java @@ -1,6 +1,10 @@ package de.fd.fh.network; +import de.fd.fh.shared.Utils; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; +import feign.Headers; +import feign.Param; import feign.RequestLine; import feign.Response; @@ -8,4 +12,8 @@ public interface AccessClient { @RequestLine("POST /accounts/registrate") Response registrate(RegistrateRequest request); + + @RequestLine("POST /accounts/login") + @Headers(Utils.AUTHENTICATION_HEADER + ":Basic {authToken}") + LoginResponse login(@Param("authToken") final String authenticationHeader); } diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java index adb3016..7080233 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java @@ -1,7 +1,10 @@ package de.fd.fh.network; +import de.fd.fh.shared.Utils; +import de.fd.fh.shared.network.messages.LoginResponse; import de.fd.fh.shared.network.messages.RegistrateRequest; import feign.Feign; +import feign.Request; import feign.Response; import feign.gson.GsonDecoder; import feign.gson.GsonEncoder; @@ -11,7 +14,7 @@ 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.*; class AccessClientTest { @@ -43,4 +46,31 @@ class AccessClientTest mockClient.verifyStatus(); } + + @Test + void testLogin() + { + mockClient + .ok(HttpMethod.POST, "/accounts/login", + "{\n" + + " \"name\":\"testName\",\n" + + " \"userId\": \"12345\",\n" + + " \"token\": \"testToken\"\n" + + "}" + ); + final LoginResponse result = client.login("testToken"); + + final Request request = mockClient.verifyOne(HttpMethod.POST, "/accounts/login"); + + assertNotNull(result); + assertEquals("testName", result.getName()); + assertEquals("12345", result.getUserId()); + assertEquals("testToken", result.getToken()); + + assertTrue(request.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().isPresent(), + "Should have auth header"); + final String authHeader = request.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().get(); + + assertEquals("Basic testToken", authHeader, "Should use given token."); + } } \ No newline at end of file From 91b655be9ecc5c34f47f1acfaffc48899f623c23 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 14:07:50 +0100 Subject: [PATCH 4/5] add logout method to AccessClient and test --- .../src/main/java/de/fd/fh/network/AccessClient.java | 4 ++++ .../test/java/de/fd/fh/network/AccessClientTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java index 6ce4d47..77db09a 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java @@ -16,4 +16,8 @@ public interface AccessClient @RequestLine("POST /accounts/login") @Headers(Utils.AUTHENTICATION_HEADER + ":Basic {authToken}") LoginResponse login(@Param("authToken") final String authenticationHeader); + + @RequestLine("POST /accounts/logout") + @Headers(Utils.AUTHENTICATION_HEADER + ":Bearer {authToken}") + Response logout(@Param("authToken") final String authenticationHeader); } diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java index 7080233..74cc629 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java @@ -73,4 +73,14 @@ class AccessClientTest assertEquals("Basic testToken", authHeader, "Should use given token."); } + + @Test + void testLogout() + { + mockClient.ok(HttpMethod.POST, "/accounts/logout"); + + final Response result = client.logout("testToken"); + + assertEquals(200, result.status(), "Should return status code 200."); + } } \ No newline at end of file From e777c9ffb1153adc77c02313d9506b8e2d790c97 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 14:09:19 +0100 Subject: [PATCH 5/5] add delete player method to AccessClient and test --- .../src/main/java/de/fd/fh/network/AccessClient.java | 3 +++ .../test/java/de/fd/fh/network/AccessClientTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java index 77db09a..fd69866 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java @@ -20,4 +20,7 @@ public interface AccessClient @RequestLine("POST /accounts/logout") @Headers(Utils.AUTHENTICATION_HEADER + ":Bearer {authToken}") Response logout(@Param("authToken") final String authenticationHeader); + + @RequestLine("DELETE /accounts/{playerId}") + Response deletePlayer(@Param("playerId") final String playerId); } diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java index 74cc629..c22bb79 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java @@ -83,4 +83,14 @@ class AccessClientTest assertEquals(200, result.status(), "Should return status code 200."); } + + @Test + void testDeletePlayer() + { + mockClient.ok(HttpMethod.DELETE, "/accounts/12345"); + + final Response result = client.deletePlayer("12345"); + + assertEquals(200, result.status(), "Should return status code 200."); + } } \ No newline at end of file