package de.juplo.demos.pwreset.adapters.in;
-import de.juplo.demos.pwreset.domain.ports.in.ResetRequestProgress;
-import de.juplo.demos.pwreset.domain.ports.in.PasswordResetService;
+import de.juplo.demos.pwreset.domain.ports.in.PasswordResetRequestProgressDTO;
+import de.juplo.demos.pwreset.domain.ports.in.PasswordResetPort;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RequiredArgsConstructor
public class WebController
{
- private final PasswordResetService passwordResetService;
+ private final PasswordResetPort passwordResetPort;
@GetMapping("/")
throws
IOException
{
- ResetRequestProgress progress = passwordResetService.request(email);
+ PasswordResetRequestProgressDTO progress = passwordResetPort.createRequest(email);
httpServletResponse.sendRedirect(progress.getRequestId().toString());
}
@GetMapping("/{id}")
public ModelAndView getResetRequest(@PathVariable UUID id)
{
- ResetRequestProgress progress = passwordResetService.get(id);
+ PasswordResetRequestProgressDTO progress = passwordResetPort.getRequest(id);
return createModelAndView(progress);
}
@RequestParam String code,
@RequestParam String password)
{
- ResetRequestProgress progress =
- passwordResetService.confirm(
+ PasswordResetRequestProgressDTO progress =
+ passwordResetPort.confirmRequest(
id,
code,
password);
return mav;
}
- ModelAndView createModelAndView(ResetRequestProgress progress)
+ ModelAndView createModelAndView(PasswordResetRequestProgressDTO progress)
{
ModelAndView mav = new ModelAndView();
mav.addObject("progress", progress);
+++ /dev/null
-package de.juplo.demos.pwreset.adapters.out;
-
-import de.juplo.demos.pwreset.domain.ports.out.UsersService;
-import lombok.RequiredArgsConstructor;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.client.RestTemplate;
-
-import java.util.Optional;
-import java.util.UUID;
-
-
-@RequiredArgsConstructor
-public class RemoteUsersServiceConsumer implements UsersService
-{
- private final String uriTemplate;
- private final RestTemplate restTemplate;
-
-
- @Override
- public Optional<UUID> getUserByEmail(String email)
- {
- ResponseEntity<UserDTO> response =
- restTemplate.getForEntity(uriTemplate, UserDTO.class, email);
-
- HttpStatus status = response.getStatusCode();
- UserDTO dto = response.getBody();
-
- switch (response.getStatusCode())
- {
- case OK:
- return
- Optional.of(dto.id);
-
- case NOT_FOUND:
- return Optional.empty();
-
- default:
- throw new RuntimeException(status.toString());
- }
- }
-
- @Override
- public void setPassword(UUID userId, String password)
- {
-
- }
-}
--- /dev/null
+package de.juplo.demos.pwreset.adapters.out;
+
+import de.juplo.demos.pwreset.domain.ports.out.UsersPort;
+import lombok.RequiredArgsConstructor;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.Optional;
+import java.util.UUID;
+
+
+@RequiredArgsConstructor
+public class UsersServiceGateway implements UsersPort
+{
+ private final String uriTemplate;
+ private final RestTemplate restTemplate;
+
+
+ @Override
+ public Optional<UUID> getUserByEmail(String email)
+ {
+ ResponseEntity<UserDTO> response =
+ restTemplate.getForEntity(uriTemplate, UserDTO.class, email);
+
+ HttpStatus status = response.getStatusCode();
+ UserDTO dto = response.getBody();
+
+ switch (response.getStatusCode())
+ {
+ case OK:
+ return
+ Optional.of(dto.id);
+
+ case NOT_FOUND:
+ return Optional.empty();
+
+ default:
+ throw new RuntimeException(status.toString());
+ }
+ }
+
+ @Override
+ public void setPassword(UUID userId, String password)
+ {
+
+ }
+}
--- /dev/null
+package de.juplo.demos.pwreset.domain.internal;
+
+import lombok.Value;
+
+
+@Value
+public class PasswordResetProgress
+{
+ public enum Status
+ {
+ CONFIRMATION_DENIED,
+ CONFIMED,
+ ACCOMPLISHED
+ }
+
+ private final Status status;
+ private final String message;
+}
--- /dev/null
+package de.juplo.demos.pwreset.domain.internal;
+
+import java.util.Optional;
+import java.util.UUID;
+
+
+public class PasswordResetService
+{
+ public PasswordResetRequest createRequest(String email)
+ {
+ return null;
+ }
+
+ public Optional<PasswordResetRequest> findRequest(String email)
+ {
+ return null;
+ }
+
+ public PasswordResetRequest getRequest(UUID requestId)
+ {
+ return null;
+ }
+
+ public PasswordResetProgress confirmRequest(PasswordResetRequest request, String code, String password)
+ {
+ return null;
+ }
+
+ public Optional<PasswordResetProgress> getProgress(UUID requestId)
+ {
+ return null;
+ }
+}
--- /dev/null
+package de.juplo.demos.pwreset.domain.ports.in;
+
+import de.juplo.demos.pwreset.domain.internal.PasswordResetRequest;
+import de.juplo.demos.pwreset.domain.internal.PasswordResetService;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Optional;
+import java.util.UUID;
+
+
+@RequiredArgsConstructor
+public class PasswordResetPort
+{
+ private final PasswordResetService resetService;
+
+
+ public PasswordResetRequestProgressDTO createRequest(String email)
+ {
+ Optional<PasswordResetRequest> result =
+ resetService.findRequest(email);
+ PasswordResetRequest request =
+ result.orElseGet(() -> resetService.createRequest(email));
+ return PasswordResetRequestProgressDTO.from(request);
+ }
+
+ public PasswordResetRequestProgressDTO getRequest(UUID requestId)
+ {
+ return PasswordResetRequestProgressDTO.from(resetService.getRequest(requestId));
+ }
+
+ public PasswordResetRequestProgressDTO confirmRequest(UUID requestId, String code, String password)
+ {
+ return PasswordResetRequestProgressDTO.from(resetService.confirmRequest(requestId, code, password));
+ }
+}
--- /dev/null
+package de.juplo.demos.pwreset.domain.ports.in;
+
+import de.juplo.demos.pwreset.domain.internal.PasswordResetProgress;
+import de.juplo.demos.pwreset.domain.internal.PasswordResetRequest;
+import de.juplo.demos.pwreset.domain.internal.PasswordResetRequestProgress;
+import lombok.RequiredArgsConstructor;
+import lombok.Value;
+
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.UUID;
+
+import static de.juplo.demos.pwreset.domain.internal.PasswordResetProgress.Status.*;
+
+
+@RequiredArgsConstructor
+@Value
+public class PasswordResetRequestProgressDTO
+{
+ private final UUID requestId;
+ private final boolean accepted;
+ private final String message;
+ private final Optional<ZonedDateTime> created;
+ private final Optional<ZonedDateTime> confirmed;
+ private final Optional<ZonedDateTime> accomplished;
+
+ public static PasswordResetRequestProgressDTO from(
+ PasswordResetRequest request,
+ PasswordResetProgress progress)
+ {
+ return
+ new PasswordResetRequestProgressDTO(
+ request.getId(),
+ progress.getStatus() != CONFIRMATION_DENIED,
+ progress.getMessage(),
+ Optional.of(request.getCreated()),
+ progress.getStatus() == CONFIRMATION_DENIED
+ ? Optional.empty()
+ : Optional.of(request.getConfirmed()),
+ progress.getStatus() == ACCOMPLISHED
+ ? Optional.empty()
+ : Optional.of(request.getAccomplihed()));
+ }
+}
+++ /dev/null
-package de.juplo.demos.pwreset.domain.ports.in;
-
-import java.util.UUID;
-
-
-public interface PasswordResetService
-{
- ResetRequestProgress request(String email);
- ResetRequestProgress get(UUID requestId);
- ResetRequestProgress confirm(UUID requestId, String code, String password);
-}
+++ /dev/null
-package de.juplo.demos.pwreset.domain.ports.in;
-
-import lombok.Builder;
-import lombok.Getter;
-import lombok.ToString;
-
-import java.time.ZonedDateTime;
-import java.util.Optional;
-import java.util.UUID;
-
-
-@Builder
-@Getter
-@ToString
-public class ResetRequestProgress
-{
- private final UUID requestId;
- private final boolean accepted;
- private final String message;
- private final Optional<ZonedDateTime> created;
- private final Optional<ZonedDateTime> confirmed;
- private final Optional<ZonedDateTime> accomplished;
-}
--- /dev/null
+package de.juplo.demos.pwreset.domain.ports.out;
+
+import java.util.UUID;
+
+
+public interface MessagingPort
+{
+ void sendRestCode(UUID userId, String code);
+}
+++ /dev/null
-package de.juplo.demos.pwreset.domain.ports.out;
-
-import java.util.UUID;
-
-
-public interface MessagingService
-{
- void sendRestCode(UUID userId, String code);
-}
--- /dev/null
+package de.juplo.demos.pwreset.domain.ports.out;
+
+import java.util.Optional;
+import java.util.UUID;
+
+
+public interface UsersPort
+{
+ Optional<UUID> getUserByEmail(String email);
+ void setPassword(UUID userId, String password);
+}
+++ /dev/null
-package de.juplo.demos.pwreset.domain.ports.out;
-
-import java.util.Optional;
-import java.util.UUID;
-
-
-public interface UsersService
-{
- Optional<UUID> getUserByEmail(String email);
- void setPassword(UUID userId, String password);
-}
+++ /dev/null
-package de.juplo.demos.pwreset.adapters.out;
-
-import au.com.dius.pact.consumer.MockServer;
-import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
-import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
-import au.com.dius.pact.consumer.junit5.PactTestFor;
-import au.com.dius.pact.core.model.RequestResponsePact;
-import au.com.dius.pact.core.model.annotations.Pact;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.springframework.boot.web.client.RestTemplateBuilder;
-import org.springframework.web.client.RestTemplate;
-
-import java.util.Map;
-import java.util.Optional;
-import java.util.UUID;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.fail;
-
-
-@ExtendWith(PactConsumerTestExt.class)
-@PactTestFor(providerName = "UsersProvider")
-public class RemoteUsersServiceConsumerIT
-{
- @Pact(consumer="EmailServicesConsumer")
- public RequestResponsePact getExistingUserByEmail(PactDslWithProvider builder)
- {
- return builder
- .given("User with email pact@juplo.de exists")
- .uponReceiving("Request for user with email pact@juplo.de")
- .path("/get")
- .query("email=pact@juplo.de")
- .method("GET")
- .willRespondWith()
- .status(200)
- .headers(Map.of("Content-Type", "application/json;charset=UTF-8"))
- .body("{\"id\": \"123e4567-e89b-12d3-a456-426614174000\"}")
- .toPact();
- }
-
- @Test
- @PactTestFor(pactMethod = "getExistingUserByEmail")
- public void testGetExistingUserByEmail(MockServer mockServer)
- {
- RestTemplate restTemplate =
- new RestTemplateBuilder()
- .rootUri(mockServer.getUrl())
- .build();
- RemoteUsersServiceConsumer usersService =
- new RemoteUsersServiceConsumer(
- "/get?email={email}",
- restTemplate);
- try
- {
- Optional<UUID> result =
- usersService.getUserByEmail("pact@juplo.de");
-
- assertThat(result.isPresent()).isTrue();
- assertThat(result.get()).isEqualTo(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"));
- }
- catch (Exception e)
- {
- fail("Unexpected exception", e);
- }
- }
-}
--- /dev/null
+package de.juplo.demos.pwreset.adapters.out;
+
+import au.com.dius.pact.consumer.MockServer;
+import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
+import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
+import au.com.dius.pact.consumer.junit5.PactTestFor;
+import au.com.dius.pact.core.model.RequestResponsePact;
+import au.com.dius.pact.core.model.annotations.Pact;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+
+
+@ExtendWith(PactConsumerTestExt.class)
+@PactTestFor(providerName = "UsersProvider")
+public class UsersServiceGatewayContractTest
+{
+ @Pact(consumer="EmailServicesConsumer")
+ public RequestResponsePact getExistingUserByEmail(PactDslWithProvider builder)
+ {
+ return builder
+ .given("User with email pact@juplo.de exists")
+ .uponReceiving("Request for user with email pact@juplo.de")
+ .path("/get")
+ .query("email=pact@juplo.de")
+ .method("GET")
+ .willRespondWith()
+ .status(200)
+ .headers(Map.of("Content-Type", "application/json;charset=UTF-8"))
+ .body("{\"id\": \"123e4567-e89b-12d3-a456-426614174000\"}")
+ .toPact();
+ }
+
+ @Test
+ @PactTestFor(pactMethod = "getExistingUserByEmail")
+ public void testGetExistingUserByEmail(MockServer mockServer)
+ {
+ RestTemplate restTemplate =
+ new RestTemplateBuilder()
+ .rootUri(mockServer.getUrl())
+ .build();
+ UsersServiceGateway usersService =
+ new UsersServiceGateway(
+ "/get?email={email}",
+ restTemplate);
+ try
+ {
+ Optional<UUID> result =
+ usersService.getUserByEmail("pact@juplo.de");
+
+ assertThat(result.isPresent()).isTrue();
+ assertThat(result.get()).isEqualTo(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"));
+ }
+ catch (Exception e)
+ {
+ fail("Unexpected exception", e);
+ }
+ }
+}