From 1a25b4d46a479d1cf7dfb76b870b6fe66eba66dc Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Wed, 13 Jan 2021 23:16:09 +0100 Subject: [PATCH] Add UserController --- .../access/AccessContextEventListener.java | 28 ++++++ .../server/user/UserContextEventListener.java | 43 +++++++++ .../user/events/ChangePasswordEvent.java | 18 ++++ .../fh/server/user/web/ChangeUserRequest.java | 13 +++ .../fd/fh/server/user/web/UserController.java | 89 +++++++++++++++++++ .../de/fd/fh/server/user/web/UserRequest.java | 13 +++ .../user/UserContextEventListenerTest.java | 51 +++++++++++ 7 files changed, 255 insertions(+) create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserContextEventListener.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangePasswordEvent.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserRequest.java create mode 100644 fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserContextEventListenerTest.java diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java new file mode 100644 index 0000000..4920e07 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java @@ -0,0 +1,28 @@ +package de.fd.fh.server.access; + +import de.fd.fh.server.user.events.ChangePasswordEvent; +import lombok.RequiredArgsConstructor; + +import java.util.Observable; +import java.util.Observer; + +@RequiredArgsConstructor +public class AccessContextEventListener implements Observer +{ + private final AccessRepository accessRepository; + + @Override + public void update(Observable observable, Object o) + { + if(o instanceof ChangePasswordEvent) + { + final ChangePasswordEvent event = (ChangePasswordEvent) o; + + final Access access = accessRepository.findByUserId(event.getUserId()); + + access.updatePassword(event.getNewPassword()); + + accessRepository.save(access); + } + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserContextEventListener.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserContextEventListener.java new file mode 100644 index 0000000..c9fe1a5 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserContextEventListener.java @@ -0,0 +1,43 @@ +package de.fd.fh.server.user; + +import de.fd.fh.server.access.events.AccountCreatedEvent; +import de.fd.fh.server.access.events.AccountDeletedEvent; +import lombok.RequiredArgsConstructor; + +import java.util.Observable; +import java.util.Observer; + +@RequiredArgsConstructor +public class UserContextEventListener implements Observer +{ + private final UserRepository userRepository; + + @Override + public void update( + final Observable observable, + final Object event) + { + System.out.println("UserContextEventListener " + event); + if (event instanceof AccountCreatedEvent) { + handleAccountCreatedEvent((AccountCreatedEvent) event); + } + if (event instanceof AccountDeletedEvent) { + handleAccountDeletedEvent((AccountDeletedEvent) event); + } + } + + private void handleAccountDeletedEvent(final AccountDeletedEvent event) + { + userRepository.deleteUserById(event.getUserId()); + } + + private void handleAccountCreatedEvent(final AccountCreatedEvent event) + { + System.out.println("handleAccountCreatedEvent " + event); + final User user = new User(event.getUserId(), event.getName()); + System.out.println("User: " + user); + + userRepository.save(user); + System.out.println("UserId: " + user.getId()); + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangePasswordEvent.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangePasswordEvent.java new file mode 100644 index 0000000..bc15e48 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangePasswordEvent.java @@ -0,0 +1,18 @@ +package de.fd.fh.server.user.events; + +import de.fd.fh.server.user.UserId; +import lombok.Getter; + +@Getter +public class ChangePasswordEvent +{ + private final String newPassword; + + private final UserId userId; + + public ChangePasswordEvent(final UserId userId, final String password) + { + this.newPassword = password; + this.userId = userId; + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java new file mode 100644 index 0000000..82fbe76 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java @@ -0,0 +1,13 @@ +package de.fd.fh.server.user.web; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class ChangeUserRequest +{ + private final String name; + + private final String password; +} 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 new file mode 100644 index 0000000..67a932d --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java @@ -0,0 +1,89 @@ +package de.fd.fh.server.user.web; + +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 static spark.Spark.get; +import static spark.Spark.post; + +public class UserController +{ + private ObjectMapper objectMapper = new ObjectMapper(); + + public UserController(final UserService service) + { + 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; + }); + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserRequest.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserRequest.java new file mode 100644 index 0000000..981861c --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserRequest.java @@ -0,0 +1,13 @@ +package de.fd.fh.server.user.web; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class UserRequest +{ + private final String id; + + private final String name; +} diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserContextEventListenerTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserContextEventListenerTest.java new file mode 100644 index 0000000..294c9d7 --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserContextEventListenerTest.java @@ -0,0 +1,51 @@ +package de.fd.fh.server.user; + +import de.fd.fh.server.access.events.AccountCreatedEvent; +import de.fd.fh.server.access.events.AccountDeletedEvent; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +class UserContextEventListenerTest +{ + @Test + void given_accountCreatedEvent_when_accountWasCreated_should_createUser() + { + final AccountCreatedEvent event = new AccountCreatedEvent("testName", UserId.of("12345")); + final UserRepository repository = mock(UserRepository.class); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(User.class); + + new UserContextEventListener(repository).update(null, event); + verify(repository).save(captor.capture()); + + assertNotNull(captor.getValue()); + assertEquals("testName", captor.getValue().getName(), "Should have the correct name"); + assertEquals("12345", captor.getValue().getId().getIdentifier(), "Should have the correct userId"); + then(repository).should().save(any()); + then(repository).shouldHaveNoMoreInteractions(); + } + + @Test + void given_accountDeletedEvent_when_accountWasDeleted_should_deleteUser() + { + final AccountDeletedEvent event = new AccountDeletedEvent(UserId.of("12345")); + final UserRepository repository = mock(UserRepository.class); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(UserId.class); + + new UserContextEventListener(repository).update(null, event); + verify(repository).deleteUserById(captor.capture()); + + assertNotNull(captor.getValue()); + assertEquals("12345", captor.getValue().getIdentifier(), "No correct userId"); + then(repository).should().deleteUserById(any(UserId.class)); + then(repository).shouldHaveNoMoreInteractions(); + } +} \ No newline at end of file