Steffen Nitschke
4 years ago
3 changed files with 177 additions and 0 deletions
-
45fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java
-
48fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserService.java
-
84fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserServiceTest.java
@ -1,12 +1,57 @@ |
|||||
package de.fd.fh; |
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.*; |
import static spark.Spark.*; |
||||
|
|
||||
public class ServerApp |
public class ServerApp |
||||
{ |
{ |
||||
|
private static AccessRepository accessRepository; |
||||
|
private static UserRepository userRepository; |
||||
|
|
||||
|
private static final Set<Observer> listeners = new HashSet<>(); |
||||
|
|
||||
public static void main(String[] args) |
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"); |
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(); |
||||
} |
} |
||||
} |
} |
@ -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()); |
||||
|
} |
||||
|
} |
@ -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<User> 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; |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue