Browse Source

add ChangeUserNameEvent and add UserServiceTest

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
307cbd8a62
  1. 1
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java
  2. 17
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java
  3. 14
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangeUserNameEvent.java
  4. 10
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java
  5. 38
      fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java

1
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java

@ -6,7 +6,6 @@ import org.bson.types.ObjectId;
@Getter
@Embedded
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode(of = {"identifier"})
public class UserId

17
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java

@ -1,6 +1,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.server.user.web.UserRequest;
import lombok.RequiredArgsConstructor;
@ -14,8 +15,6 @@ public class UserService extends Observable
public User changePlayer(final UserId userId, final ChangeUserRequest message)
{
System.out.println("changePlayer: " + message);
User user = userRepository.findUserById(userId);
if (message.getPassword() != null)
@ -24,9 +23,19 @@ public class UserService extends Observable
notifyObservers(new ChangePasswordEvent(userId, message.getPassword()));
}
userRepository.save(user);
if (message.getName() != null)
{
if (userRepository.findUserByName(message.getName()) == null)
{
user.rename(message.getName());
userRepository.save(user);
setChanged();
notifyObservers(new ChangeUserNameEvent(userId, message.getName()));
}
}
return userRepository.findUserById(userId);
return user;
}
public User getPlayer(final UserId id)

14
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangeUserNameEvent.java

@ -0,0 +1,14 @@
package de.fd.fh.server.user.events;
import de.fd.fh.server.user.UserId;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Getter
public class ChangeUserNameEvent
{
private final UserId userId;
private final String newName;
}

10
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java

@ -1,13 +1,15 @@
package de.fd.fh.server.user.web;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.NoArgsConstructor;
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class ChangeUserRequest
{
private final String name;
private String name;
private final String password;
private String password;
}

38
fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java

@ -9,8 +9,10 @@ import org.mockito.ArgumentCaptor;
import java.util.Observable;
import java.util.Observer;
import static org.junit.jupiter.api.Assertions.*;
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.*;
class UserServiceTest implements Observer
@ -29,19 +31,45 @@ class UserServiceTest implements Observer
final User user =
User.of("testName");
final ChangeUserRequest request =
new ChangeUserRequest("testName", "newPassword");
new ChangeUserRequest(null, "newPassword");
final UserRepository repository = mock(UserRepository.class);
when(repository.findUserById(any(UserId.class)))
.thenReturn(user);
final UserService service = new UserService(repository);
final de.fd.fh.server.user.UserService service = new de.fd.fh.server.user.UserService(repository);
service.addObserver(this);
service.changePlayer(UserId.of("12345"), request);
assertNotNull(event);
}
@Test
void given_storedUser_when_changeUserName_should_renameAndFireEvent()
{
final User user =
User.of("testName");
final ChangeUserRequest request =
new ChangeUserRequest("newTestName", null);
final UserRepository repository = mock(UserRepository.class);
when(repository.findUserById(any(UserId.class)))
.thenReturn(user);
when(repository.findUserByName(any()))
.thenReturn(null);
final de.fd.fh.server.user.UserService service = new de.fd.fh.server.user.UserService(repository);
service.addObserver(this);
final ArgumentCaptor<User> captor = ArgumentCaptor.forClass(User.class);
service.changePlayer(UserId.of("12345"), request);
verify(repository).save(captor.capture());
then(repository).should().findUserById(any(UserId.class));
then(repository).should().findUserByName(any());
verify(repository).save(captor.capture());
then(repository).should().save(any());
then(repository).shouldHaveNoMoreInteractions();
assertNotNull(captor.getValue(), "Should be saved");
assertEquals(captor.getValue().getName(), "newTestName", "User should have new name");
assertNotNull(event);
}
@ -68,7 +96,7 @@ class UserServiceTest implements Observer
when(repository.findUserById(any(UserId.class)))
.thenReturn(user);
final UserRequest result = new UserService(repository).getSmallPlayer(UserId.of("12345"));
final UserRequest result = new de.fd.fh.server.user.UserService(repository).getSmallPlayer(UserId.of("12345"));
assertNotNull(result);
assertEquals("12345", result.getId(), "Wrong UserId");

Loading…
Cancel
Save