Browse Source

Add UserController

pr-readme
Steffen Nitschke 4 years ago
parent
commit
1a25b4d46a
  1. 28
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessContextEventListener.java
  2. 43
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserContextEventListener.java
  3. 18
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangePasswordEvent.java
  4. 13
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/ChangeUserRequest.java
  5. 89
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java
  6. 13
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserRequest.java
  7. 51
      fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserContextEventListenerTest.java

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

@ -0,0 +1,28 @@
package de.fd.fh.server.access;
import de.fd.fh.server.user.events.ChangePasswordEvent;
import lombok.RequiredArgsConstructor;
import java.util.Observable;
import java.util.Observer;
@RequiredArgsConstructor
public class AccessContextEventListener implements Observer
{
private final AccessRepository accessRepository;
@Override
public void update(Observable observable, Object o)
{
if(o instanceof ChangePasswordEvent)
{
final ChangePasswordEvent event = (ChangePasswordEvent) o;
final Access access = accessRepository.findByUserId(event.getUserId());
access.updatePassword(event.getNewPassword());
accessRepository.save(access);
}
}
}

43
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserContextEventListener.java

@ -0,0 +1,43 @@
package de.fd.fh.server.user;
import de.fd.fh.server.access.events.AccountCreatedEvent;
import de.fd.fh.server.access.events.AccountDeletedEvent;
import lombok.RequiredArgsConstructor;
import java.util.Observable;
import java.util.Observer;
@RequiredArgsConstructor
public class UserContextEventListener implements Observer
{
private final UserRepository userRepository;
@Override
public void update(
final Observable observable,
final Object event)
{
System.out.println("UserContextEventListener " + event);
if (event instanceof AccountCreatedEvent) {
handleAccountCreatedEvent((AccountCreatedEvent) event);
}
if (event instanceof AccountDeletedEvent) {
handleAccountDeletedEvent((AccountDeletedEvent) event);
}
}
private void handleAccountDeletedEvent(final AccountDeletedEvent event)
{
userRepository.deleteUserById(event.getUserId());
}
private void handleAccountCreatedEvent(final AccountCreatedEvent event)
{
System.out.println("handleAccountCreatedEvent " + event);
final User user = new User(event.getUserId(), event.getName());
System.out.println("User: " + user);
userRepository.save(user);
System.out.println("UserId: " + user.getId());
}
}

18
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/events/ChangePasswordEvent.java

@ -0,0 +1,18 @@
package de.fd.fh.server.user.events;
import de.fd.fh.server.user.UserId;
import lombok.Getter;
@Getter
public class ChangePasswordEvent
{
private final String newPassword;
private final UserId userId;
public ChangePasswordEvent(final UserId userId, final String password)
{
this.newPassword = password;
this.userId = userId;
}
}

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

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

89
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java

@ -0,0 +1,89 @@
package de.fd.fh.server.user.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.fd.fh.server.access.AccessToken;
import de.fd.fh.server.user.User;
import de.fd.fh.server.user.UserId;
import de.fd.fh.server.user.UserService;
import static spark.Spark.get;
import static spark.Spark.post;
public class UserController
{
private ObjectMapper objectMapper = new ObjectMapper();
public UserController(final UserService service)
{
post("/users",
((request, response) ->
{
final UserId userId =
((AccessToken) request.session()
.attribute("userId"))
.getUserId();
final ChangeUserRequest message = objectMapper.readValue(request.body(),
ChangeUserRequest.class);
final User user = service.changePlayer(
userId,
message);
if (user == null)
{
response.status(400);
}
else
{
response.status(200);
response.type("application/json");
return objectMapper.writeValueAsString(user);
}
return response;
}
));
get("/users",
(request, response) ->
{
final UserId userId =
((AccessToken) request.session()
.attribute("userId"))
.getUserId();
final User user = service.getPlayer(userId);
if (user == null)
{
response.status(400);
}
else
{
response.status(200);
response.type("application/json");
return objectMapper.writeValueAsString(user);
}
return response;
}
);
get("/users/:user_id",
(request, response) ->
{
final UserId userId = UserId.of(request.params(":user_id"));
final UserRequest user = service.getSmallPlayer(userId);
if (user == null)
{
response.status(404);
}
response.body(objectMapper.writeValueAsString(user));
return response;
});
}
}

13
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserRequest.java

@ -0,0 +1,13 @@
package de.fd.fh.server.user.web;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Getter
public class UserRequest
{
private final String id;
private final String name;
}

51
fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserContextEventListenerTest.java

@ -0,0 +1,51 @@
package de.fd.fh.server.user;
import de.fd.fh.server.access.events.AccountCreatedEvent;
import de.fd.fh.server.access.events.AccountDeletedEvent;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
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.mock;
import static org.mockito.Mockito.verify;
class UserContextEventListenerTest
{
@Test
void given_accountCreatedEvent_when_accountWasCreated_should_createUser()
{
final AccountCreatedEvent event = new AccountCreatedEvent("testName", UserId.of("12345"));
final UserRepository repository = mock(UserRepository.class);
final ArgumentCaptor<User> captor = ArgumentCaptor.forClass(User.class);
new UserContextEventListener(repository).update(null, event);
verify(repository).save(captor.capture());
assertNotNull(captor.getValue());
assertEquals("testName", captor.getValue().getName(), "Should have the correct name");
assertEquals("12345", captor.getValue().getId().getIdentifier(), "Should have the correct userId");
then(repository).should().save(any());
then(repository).shouldHaveNoMoreInteractions();
}
@Test
void given_accountDeletedEvent_when_accountWasDeleted_should_deleteUser()
{
final AccountDeletedEvent event = new AccountDeletedEvent(UserId.of("12345"));
final UserRepository repository = mock(UserRepository.class);
final ArgumentCaptor<UserId> captor = ArgumentCaptor.forClass(UserId.class);
new UserContextEventListener(repository).update(null, event);
verify(repository).deleteUserById(captor.capture());
assertNotNull(captor.getValue());
assertEquals("12345", captor.getValue().getIdentifier(), "No correct userId");
then(repository).should().deleteUserById(any(UserId.class));
then(repository).shouldHaveNoMoreInteractions();
}
}
Loading…
Cancel
Save