Browse Source

add missing UserTest and add changeUserName behavior

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
d6b9ecd5bd
  1. 25
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java
  2. 27
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java
  3. 28
      fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserTest.java

25
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java

@ -1,6 +1,7 @@
package de.fd.fh.server.access; package de.fd.fh.server.access;
import de.fd.fh.server.user.events.ChangePasswordEvent; import de.fd.fh.server.user.events.ChangePasswordEvent;
import de.fd.fh.server.user.events.ChangeUserNameEvent;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.Observable; import java.util.Observable;
@ -12,17 +13,33 @@ public class AccessContextEventListener implements Observer
private final AccessRepository accessRepository; private final AccessRepository accessRepository;
@Override @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);
}
}
public void handleRename(ChangeUserNameEvent event)
{
final Access access = accessRepository.findByUserId(event.getUserId()); 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);
} }
} }

27
fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessContextEventListenerTest.java

@ -1,13 +1,12 @@
package de.fd.fh.server.access; package de.fd.fh.server.access;
import de.fd.fh.server.user.UserId; 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.ChangePasswordEvent;
import de.fd.fh.server.user.events.ChangeUserNameEvent;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; 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.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -25,7 +24,7 @@ class AccessContextEventListenerTest
final ArgumentCaptor<Access> captor = ArgumentCaptor.forClass(Access.class); final ArgumentCaptor<Access> 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()); verify(repository).save(captor.capture());
assertEquals("newPwd", captor.getValue().getPassword(), "Have to be the new password"); 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).should().save(any(Access.class));
then(repository).shouldHaveNoMoreInteractions(); 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<Access> 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();
}
} }

28
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.");
}
}
Loading…
Cancel
Save