WIP
authorKai Moritz <kai@juplo.de>
Sat, 13 Mar 2021 14:06:42 +0000 (15:06 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 13 Mar 2021 14:06:42 +0000 (15:06 +0100)
consumer/pom.xml
consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumer.java [new file with mode: 0644]
consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUserServiceConsumerIT.java [new file with mode: 0644]
consumer/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java [new file with mode: 0644]
consumer/src/main/java/de/juplo/demos/consumer/domain/User.java [new file with mode: 0644]
consumer/src/main/java/de/juplo/demos/consumer/domain/UserService.java [new file with mode: 0644]

index d9d8d14..3e70c9f 100644 (file)
@@ -15,6 +15,7 @@
        <description>Demo of a Pact Provider</description>
        <properties>
                <java.version>11</java.version>
+               <pact.version>4.1.0</pact.version>
        </properties>
        <dependencies>
                <dependency>
                        <artifactId>spring-boot-starter-test</artifactId>
                        <scope>test</scope>
                </dependency>
+               <dependency>
+                       <groupId>org.junit.jupiter</groupId>
+                       <artifactId>junit-jupiter-api</artifactId>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>au.com.dius.pact.consumer</groupId>
+                       <artifactId>junit5</artifactId>
+                       <version>${pact.version}</version>
+                       <scope>test</scope>
+               </dependency>
        </dependencies>
 
        <build>
@@ -48,6 +60,9 @@
                                        </excludes>
                                </configuration>
                        </plugin>
+                       <plugin>
+                               <artifactId>maven-failsafe-plugin</artifactId>
+                       </plugin>
                </plugins>
        </build>
 
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 (file)
index 0000000..104339c
--- /dev/null
@@ -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<User> getUser(UUID id) {
+    ResponseEntity<UserDTO> 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 (file)
index 0000000..2a33f89
--- /dev/null
@@ -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 (file)
index 0000000..bcf2692
--- /dev/null
@@ -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 (file)
index 0000000..521ad27
--- /dev/null
@@ -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 (file)
index 0000000..b65f4eb
--- /dev/null
@@ -0,0 +1,10 @@
+package de.juplo.demos.consumer.domain;
+
+import java.util.Optional;
+import java.util.UUID;
+
+
+public interface UserService
+{
+  Optional<User> getUser(UUID id);
+}