From: Kai Moritz Date: Sat, 13 Mar 2021 15:47:27 +0000 (+0100) Subject: WIP X-Git-Tag: wip-consumer~3 X-Git-Url: https://juplo.de/gitweb/?a=commitdiff_plain;ds=inline;h=cb2a8fda60a15b79b72d99a5d1f72a18e4d681a0;p=demos%2Fpact WIP --- diff --git a/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java b/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java index d47283c..b69ef81 100644 --- a/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java +++ b/consumer/src/main/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumer.java @@ -5,12 +5,13 @@ import de.juplo.demos.consumer.domain.UsersService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.Optional; -import java.util.UUID; +@Service @RequiredArgsConstructor public class RemoteUsersServiceConsumer implements UsersService { @@ -19,9 +20,9 @@ public class RemoteUsersServiceConsumer implements UsersService @Override - public Optional getUser(UUID id) { + public Optional getUserByEmail(String email) { ResponseEntity response = - restTemplate.getForEntity(uriTemplate, UserDTO.class, id); + restTemplate.getForEntity(uriTemplate, UserDTO.class, email); HttpStatus status = response.getStatusCode(); UserDTO dto = response.getBody(); switch (response.getStatusCode()) { diff --git a/consumer/src/main/java/de/juplo/demos/consumer/domain/UsersService.java b/consumer/src/main/java/de/juplo/demos/consumer/domain/UsersService.java index dc82975..f6738e9 100644 --- a/consumer/src/main/java/de/juplo/demos/consumer/domain/UsersService.java +++ b/consumer/src/main/java/de/juplo/demos/consumer/domain/UsersService.java @@ -1,10 +1,9 @@ package de.juplo.demos.consumer.domain; import java.util.Optional; -import java.util.UUID; public interface UsersService { - Optional getUser(UUID id); + Optional getUserByEmail(String email); } diff --git a/consumer/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java b/consumer/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java index ee8bd61..6c65008 100644 --- a/consumer/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java +++ b/consumer/src/test/java/de/juplo/demos/consumer/adapter/RemoteUsersServiceConsumerIT.java @@ -1,32 +1,63 @@ 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.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.test.context.junit.jupiter.SpringExtension; +import java.io.IOException; +import java.util.Map; +import java.util.Optional; -@ExtendWith(PactConsumerTestExt.class) +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + + +@ExtendWith({ SpringExtension.class, PactConsumerTestExt.class }) @PactTestFor(providerName = "UsersProvider") public class RemoteUsersServiceConsumerIT { - @Pact(provider="UsersProvider", consumer="EmailServicesConsumer") - public RequestResponsePact createPact(PactDslWithProvider builder) { + @Autowired + RemoteUsersServiceConsumer usersService; + + + @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") - .matchQuery( - "email", - // See: https://stackoverflow.com/a/201378/247276 - "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", - "pact@juplo.de") + .query("email=pact@juplo.de") .method("GET") - .matchHeader("", "", "") + .headers("Accept", "application/json") .willRespondWith() .status(200) - .body("{\"responsetest\": true}") + .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) { + try { + Optional result = usersService.getUserByEmail("pact@juplo.de"); + + assertThat(result.isPresent()).isTrue(); + User user = result.get(); + assertThat(user.getId()).isEqualTo("123e4567-e89b-12d3-a456-426614174000"); + assertThat(user.getEmail()).isEqualTo("pact@juplo.de"); + } + catch (Exception e) { + fail("Unexpected exception", e); + } + } }