diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java b/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java index 384d27c..d275c81 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java @@ -1,12 +1,57 @@ package de.fd.fh; +import de.fd.fh.server.access.AccessContextEventListener; +import de.fd.fh.server.access.AccessRepository; +import de.fd.fh.server.access.AccessService; +import de.fd.fh.server.access.web.AccessController; +import de.fd.fh.server.user.UserContextEventListener; +import de.fd.fh.server.user.web.UserController; +import de.fd.fh.server.user.UserRepository; +import de.fd.fh.server.user.UserService; + + +import java.util.HashSet; +import java.util.Observable; +import java.util.Observer; +import java.util.Set; + import static spark.Spark.*; public class ServerApp { + private static AccessRepository accessRepository; + private static UserRepository userRepository; + + private static final Set listeners = new HashSet<>(); public static void main(String[] args) { + initRepositories(); + + initListeners(); + + new AccessController((AccessService) addListeners(new AccessService(accessRepository))); + new UserController((UserService) addListeners(new UserService(userRepository))); + get("/hello", (req, res) -> "Hello World"); + + } + + private static Object addListeners(Observable service) + { + listeners.forEach(service::addObserver); + + return service; + } + + private static void initListeners() + { + listeners.add(new AccessContextEventListener(accessRepository)); + listeners.add(new UserContextEventListener(userRepository)); + } + + private static void initRepositories() { + accessRepository = new AccessRepository(); + userRepository = new UserRepository(); } } diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java new file mode 100644 index 0000000..268e695 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java @@ -0,0 +1,48 @@ +package de.fd.fh.server.user; + +import de.fd.fh.server.user.events.ChangePasswordEvent; +import de.fd.fh.server.user.web.ChangeUserRequest; +import de.fd.fh.server.user.web.UserRequest; +import lombok.RequiredArgsConstructor; + +import java.util.Observable; + +@RequiredArgsConstructor +public class UserService extends Observable +{ + private final UserRepository userRepository; + + public User changePlayer(final UserId userId, final ChangeUserRequest message) + { + System.out.println("changePlayer: " + message); + + User user = userRepository.findUserById(userId); + + if (message.getPassword() != null) + { + setChanged(); + notifyObservers(new ChangePasswordEvent(userId, message.getPassword())); + } + + userRepository.save(user); + + return userRepository.findUserById(userId); + } + + public User getPlayer(final UserId id) + { + return userRepository.findUserById(id); + } + + public UserRequest getSmallPlayer(final UserId userId) + { + final User user = userRepository.findUserById(userId); + + if(user == null) + { + return null; + } + + return new UserRequest(user.getId().getIdentifier(), user.getName()); + } +} diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java new file mode 100644 index 0000000..1c3bcbf --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java @@ -0,0 +1,84 @@ +package de.fd.fh.server.user; + +import de.fd.fh.server.user.web.ChangeUserRequest; +import de.fd.fh.server.user.web.UserRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.util.Observable; +import java.util.Observer; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class UserServiceTest implements Observer +{ + private Object event; + + @BeforeEach + void before() + { + event = null; + } + + @Test + void given_storedUser_when_changePassword_should_changePassword() + { + final User user = + User.of("testName"); + final ChangeUserRequest request = + new ChangeUserRequest("testName", "newPassword"); + final UserRepository repository = mock(UserRepository.class); + when(repository.findUserById(any(UserId.class))) + .thenReturn(user); + + final UserService service = new UserService(repository); + service.addObserver(this); + final ArgumentCaptor captor = ArgumentCaptor.forClass(User.class); + + service.changePlayer(UserId.of("12345"), request); + verify(repository).save(captor.capture()); + + assertNotNull(captor.getValue(), "Should be saved"); + assertNotNull(event); + } + + @Test + void given_storedUser_when_getPlayer_should_returnPlayer() + { + final User user = + User.of("testName"); + final UserRepository repository = mock(UserRepository.class); + when(repository.findUserById(any(UserId.class))) + .thenReturn(user); + + final User result = new UserService(repository).getPlayer(UserId.of("12345")); + + assertNotNull(result); + } + + @Test + void given_storedUser_when_getSmallPlayer_should_returnSmallPlayer() + { + final User user = + new User(UserId.of("12345"), "testName"); + final UserRepository repository = mock(UserRepository.class); + when(repository.findUserById(any(UserId.class))) + .thenReturn(user); + + final UserRequest result = new UserService(repository).getSmallPlayer(UserId.of("12345")); + + assertNotNull(result); + assertEquals("12345", result.getId(), "Wrong UserId"); + assertEquals("testName", result.getName(), "Wrong Name"); + + } + + @Override + public void update(Observable o, Object arg) + { + event = arg; + } +} \ No newline at end of file