From 45f6099eb899d8a669f860d8b36949f09278c1c5 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 13 Mar 2021 15:06:42 +0100 Subject: [PATCH] WIP --- consumer/pom.xml | 15 +++++++ .../adapter/RemoteUserServiceConsumer.java | 41 +++++++++++++++++++ .../adapter/RemoteUserServiceConsumerIT.java | 11 +++++ .../juplo/demos/consumer/adapter/UserDTO.java | 13 ++++++ .../de/juplo/demos/consumer/domain/User.java | 15 +++++++ .../demos/consumer/domain/UserService.java | 10 +++++ 6 files changed, 105 insertions(+) create mode 100644 consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumer.java create mode 100644 consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumerIT.java create mode 100644 consumer/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java create mode 100644 consumer/src/main/java/de/juplo/demos/consumer/domain/User.java create mode 100644 consumer/src/main/java/de/juplo/demos/consumer/domain/UserService.java diff --git a/consumer/pom.xml b/consumer/pom.xml index d9d8d14..3e70c9f 100644 --- a/consumer/pom.xml +++ b/consumer/pom.xml @@ -15,6 +15,7 @@ Demo of a Pact Provider 11 + 4.1.0 @@ -32,6 +33,17 @@ spring-boot-starter-test test + + org.junit.jupiter + junit-jupiter-api + test + + + au.com.dius.pact.consumer + junit5 + ${pact.version} + test + @@ -48,6 +60,9 @@ + + maven-failsafe-plugin + diff --git a/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumer.java b/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumer.java new file mode 100644 index 0000000..104339c --- /dev/null +++ b/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumer.java @@ -0,0 +1,41 @@ +package de.juplo.demos.consumer.adapter; + +import de.juplo.demos.consumer.domain.User; +import de.juplo.demos.consumer.domain.UserService; +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 RemoteUserServiceConsumer implements UserService +{ + private String uriTemplate; + private RestTemplate restTemplate; + + + @Override + public Optional getUser(UUID id) { + ResponseEntity response = + restTemplate.getForEntity(uriTemplate, UserDTO.class, id); + HttpStatus status = response.getStatusCode(); + UserDTO dto = response.getBody(); + switch (response.getStatusCode()) { + case OK: + return + Optional.of( + User.builder() + .id(dto.id) + .email(dto.email) + .build()); + case NOT_FOUND: + return Optional.empty(); + default: + throw new RuntimeException(status.toString()); + } + } +} diff --git a/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumerIT.java b/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumerIT.java new file mode 100644 index 0000000..2a33f89 --- /dev/null +++ b/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumerIT.java @@ -0,0 +1,11 @@ +package de.juplo.demos.consumer.adapter; + +import org.junit.jupiter.api.extension.ExtendWith; + + +@ExtendWith(PactConsumerTestExt.class) +@PactTestFor(providerName = "UsersProvider") +public class RemoteUserServiceConsumerIT +{ + +} diff --git a/consumer/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java b/consumer/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java new file mode 100644 index 0000000..bcf2692 --- /dev/null +++ b/consumer/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java @@ -0,0 +1,13 @@ +package de.juplo.demos.consumer.adapter; + +import lombok.Setter; + +import java.util.UUID; + + +@Setter +public class UserDTO +{ + UUID id; + String email; +} diff --git a/consumer/src/main/java/de/juplo/demos/consumer/domain/User.java b/consumer/src/main/java/de/juplo/demos/consumer/domain/User.java new file mode 100644 index 0000000..521ad27 --- /dev/null +++ b/consumer/src/main/java/de/juplo/demos/consumer/domain/User.java @@ -0,0 +1,15 @@ +package de.juplo.demos.consumer.domain; + +import lombok.Builder; +import lombok.Data; + +import java.util.UUID; + + +@Data +@Builder +public class User +{ + private UUID id; + private String email; +} diff --git a/consumer/src/main/java/de/juplo/demos/consumer/domain/UserService.java b/consumer/src/main/java/de/juplo/demos/consumer/domain/UserService.java new file mode 100644 index 0000000..b65f4eb --- /dev/null +++ b/consumer/src/main/java/de/juplo/demos/consumer/domain/UserService.java @@ -0,0 +1,10 @@ +package de.juplo.demos.consumer.domain; + +import java.util.Optional; +import java.util.UUID; + + +public interface UserService +{ + Optional getUser(UUID id); +} -- 2.20.1