From d6b9ecd5bdd768c3fea9039a09c019c3d69bd88e Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 02:45:37 +0100 Subject: [PATCH] add missing UserTest and add changeUserName behavior --- .../access/AccessContextEventListener.java | 33 ++++++++++++++----- .../AccessContextEventListenerTest.java | 27 ++++++++++++--- .../java/de/fd/fh/server/user/UserTest.java | 28 ++++++++++++++++ 3 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserTest.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 index 4920e07..216910e 100644 --- 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 @@ -1,6 +1,7 @@ package de.fd.fh.server.access; import de.fd.fh.server.user.events.ChangePasswordEvent; +import de.fd.fh.server.user.events.ChangeUserNameEvent; import lombok.RequiredArgsConstructor; import java.util.Observable; @@ -12,17 +13,33 @@ public class AccessContextEventListener implements Observer private final AccessRepository accessRepository; @Override - public void update(Observable observable, Object o) + public void update(Observable observable, Object event) { - if(o instanceof ChangePasswordEvent) + if (event instanceof ChangePasswordEvent) { - final ChangePasswordEvent event = (ChangePasswordEvent) o; + handlePasswordChange((ChangePasswordEvent) event); + } + else if (event instanceof ChangeUserNameEvent) + { + handleRename((ChangeUserNameEvent) event); + } + } - final Access access = accessRepository.findByUserId(event.getUserId()); + public void handleRename(ChangeUserNameEvent event) + { + final Access access = accessRepository.findByUserId(event.getUserId()); - access.updatePassword(event.getNewPassword()); + access.rename(event.getNewName()); - accessRepository.save(access); - } + accessRepository.save(access); + } + + public void handlePasswordChange(ChangePasswordEvent event) + { + final Access access = accessRepository.findByUserId(event.getUserId()); + + access.updatePassword(event.getNewPassword()); + + accessRepository.save(access); } -} +} \ No newline at end of file diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java index e970141..84e5b4e 100644 --- a/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java @@ -1,13 +1,12 @@ package de.fd.fh.server.access; import de.fd.fh.server.user.UserId; -import de.fd.fh.server.user.UserRepository; -import de.fd.fh.server.user.UserService; import de.fd.fh.server.user.events.ChangePasswordEvent; +import de.fd.fh.server.user.events.ChangeUserNameEvent; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.*; @@ -25,7 +24,7 @@ class AccessContextEventListenerTest final ArgumentCaptor captor = ArgumentCaptor.forClass(Access.class); - new AccessContextEventListener(repository).update(null, event); + new de.fd.fh.server.access.AccessContextEventListener(repository).update(null, event); verify(repository).save(captor.capture()); assertEquals("newPwd", captor.getValue().getPassword(), "Have to be the new password"); @@ -33,4 +32,24 @@ class AccessContextEventListenerTest then(repository).should().save(any(Access.class)); then(repository).shouldHaveNoMoreInteractions(); } + + @Test + void given_changeNameEvent_when_nameChanged_should_renameAccess() + { + final ChangeUserNameEvent event = new ChangeUserNameEvent(UserId.of("12345"), "newName"); + + final AccessRepository repository = mock(AccessRepository.class); + when(repository.findByUserId(any(UserId.class))) + .thenReturn(new Access()); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(Access.class); + + new de.fd.fh.server.access.AccessContextEventListener(repository).update(null, event); + verify(repository).save(captor.capture()); + + assertEquals("newName", captor.getValue().getName(), "Have to be the new name"); + then(repository).should().findByUserId(any(UserId.class)); + then(repository).should().save(any(Access.class)); + then(repository).shouldHaveNoMoreInteractions(); + } } \ No newline at end of file diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserTest.java new file mode 100644 index 0000000..50d2874 --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserTest.java @@ -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."); + } +} \ No newline at end of file