From: Kai Moritz Date: Sun, 14 Mar 2021 09:00:24 +0000 (+0100) Subject: WIP X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=83aa21c8c40f8d102ee5de17e81717f837c6fa94;p=demos%2Fpact WIP --- diff --git a/pwreset/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java b/pwreset/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java deleted file mode 100644 index 8cbc3eb..0000000 --- a/pwreset/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.juplo.demos.consumer.adapter; - -import de.juplo.demos.consumer.domain.User; -import de.juplo.demos.consumer.domain.UsersService; -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; - -import java.util.Optional; - - -@RequiredArgsConstructor -public class RemoteUsersServiceConsumer implements UsersService -{ - private final String uriTemplate; - private final RestTemplate restTemplate; - - - @Override - public Optional getUserByEmail(String email) { - ResponseEntity response = - restTemplate.getForEntity(uriTemplate, UserDTO.class, email); - 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/pwreset/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java b/pwreset/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java deleted file mode 100644 index bcf2692..0000000 --- a/pwreset/src/main/java/de/juplo/demos/consumer/adapter/UserDTO.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.juplo.demos.consumer.adapter; - -import lombok.Setter; - -import java.util.UUID; - - -@Setter -public class UserDTO -{ - UUID id; - String email; -} diff --git a/pwreset/src/main/java/de/juplo/demos/consumer/domain/User.java b/pwreset/src/main/java/de/juplo/demos/consumer/domain/User.java deleted file mode 100644 index 521ad27..0000000 --- a/pwreset/src/main/java/de/juplo/demos/consumer/domain/User.java +++ /dev/null @@ -1,15 +0,0 @@ -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/pwreset/src/main/java/de/juplo/demos/consumer/domain/UsersService.java b/pwreset/src/main/java/de/juplo/demos/consumer/domain/UsersService.java deleted file mode 100644 index f6738e9..0000000 --- a/pwreset/src/main/java/de/juplo/demos/consumer/domain/UsersService.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.juplo.demos.consumer.domain; - -import java.util.Optional; - - -public interface UsersService -{ - Optional getUserByEmail(String email); -} diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/adapter/RemoteUsersServiceConsumer.java b/pwreset/src/main/java/de/juplo/demos/pwreset/adapter/RemoteUsersServiceConsumer.java new file mode 100644 index 0000000..27fdc8f --- /dev/null +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/adapter/RemoteUsersServiceConsumer.java @@ -0,0 +1,40 @@ +package de.juplo.demos.pwreset.adapter; + +import de.juplo.demos.pwreset.domain.User; +import de.juplo.demos.pwreset.domain.UsersService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import java.util.Optional; + + +@RequiredArgsConstructor +public class RemoteUsersServiceConsumer implements UsersService +{ + private final String uriTemplate; + private final RestTemplate restTemplate; + + + @Override + public Optional getUserByEmail(String email) { + ResponseEntity response = + restTemplate.getForEntity(uriTemplate, UserDTO.class, email); + 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/pwreset/src/main/java/de/juplo/demos/pwreset/adapter/UserDTO.java b/pwreset/src/main/java/de/juplo/demos/pwreset/adapter/UserDTO.java new file mode 100644 index 0000000..4f6bd38 --- /dev/null +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/adapter/UserDTO.java @@ -0,0 +1,13 @@ +package de.juplo.demos.pwreset.adapter; + +import lombok.Setter; + +import java.util.UUID; + + +@Setter +public class UserDTO +{ + UUID id; + String email; +} diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/domain/User.java b/pwreset/src/main/java/de/juplo/demos/pwreset/domain/User.java new file mode 100644 index 0000000..3d970bd --- /dev/null +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/domain/User.java @@ -0,0 +1,15 @@ +package de.juplo.demos.pwreset.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/pwreset/src/main/java/de/juplo/demos/pwreset/domain/UsersService.java b/pwreset/src/main/java/de/juplo/demos/pwreset/domain/UsersService.java new file mode 100644 index 0000000..0a07a1f --- /dev/null +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/domain/UsersService.java @@ -0,0 +1,9 @@ +package de.juplo.demos.pwreset.domain; + +import java.util.Optional; + + +public interface UsersService +{ + Optional getUserByEmail(String email); +} diff --git a/pwreset/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java b/pwreset/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java deleted file mode 100644 index f3786af..0000000 --- a/pwreset/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java +++ /dev/null @@ -1,64 +0,0 @@ -package de.juplo.demos.consumer.adapter; - -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 de.juplo.demos.consumer.domain.User; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.web.client.RestTemplate; - -import java.io.IOException; -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\", \"email\": \"pact@juplo.de\"}") - .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 result = usersService.getUserByEmail("pact@juplo.de"); - - assertThat(result.isPresent()).isTrue(); - User user = result.get(); - assertThat(user.getId()).isEqualTo(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")); - assertThat(user.getEmail()).isEqualTo("pact@juplo.de"); - } - catch (Exception e) { - fail("Unexpected exception", e); - } - } -} diff --git a/pwreset/src/test/java/de/juplo/demos/pwreset/adapter/RemoteUsersServiceConsumerIT.java b/pwreset/src/test/java/de/juplo/demos/pwreset/adapter/RemoteUsersServiceConsumerIT.java new file mode 100644 index 0000000..fb784ad --- /dev/null +++ b/pwreset/src/test/java/de/juplo/demos/pwreset/adapter/RemoteUsersServiceConsumerIT.java @@ -0,0 +1,59 @@ +package de.juplo.demos.pwreset.adapter; + +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 de.juplo.demos.pwreset.domain.User; +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\", \"email\": \"pact@juplo.de\"}") + .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 result = usersService.getUserByEmail("pact@juplo.de"); + + assertThat(result.isPresent()).isTrue(); + User user = result.get(); + assertThat(user.getId()).isEqualTo(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")); + assertThat(user.getEmail()).isEqualTo("pact@juplo.de"); + } + catch (Exception e) { + fail("Unexpected exception", e); + } + } +}