master- Sync Repo
This commit is contained in:
parent
7c5cd0e2a4
commit
99e512596a
44
jenkins/cd/Jenkinsfile
vendored
Normal file
44
jenkins/cd/Jenkinsfile
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#!groovy
|
||||||
|
pipeline {
|
||||||
|
|
||||||
|
agent any
|
||||||
|
|
||||||
|
environment {
|
||||||
|
BRANCH_NAME=env.GIT_BRANCH.replace("origin/", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Build') {
|
||||||
|
steps {
|
||||||
|
dir('messaging-app') {
|
||||||
|
sh '../mvnw clean verify'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Publish Pacts') {
|
||||||
|
steps {
|
||||||
|
dir('messaging-app') {
|
||||||
|
sh '../mvnw pact:publish -Dpact.consumer.version=${GIT_COMMIT} -Dpact.tag=${BRANCH_NAME}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Check Pact Verifications') {
|
||||||
|
steps {
|
||||||
|
sh 'curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.88.3/pact-1.88.3-linux-x86_64.tar.gz'
|
||||||
|
sh 'tar xzf pact-1.88.3-linux-x86_64.tar.gz'
|
||||||
|
dir('pact/bin') {
|
||||||
|
sh "./pact-broker can-i-deploy --retry-while-unknown=12 --retry-interval=10 -a messaging-app -b http://pact_broker -e ${GIT_COMMIT}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Deploy') {
|
||||||
|
when {
|
||||||
|
branch 'junit5'
|
||||||
|
}
|
||||||
|
steps {
|
||||||
|
echo 'Deploying to prod now...'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
27
jenkins/without-cd/Jenkinsfile-build
Normal file
27
jenkins/without-cd/Jenkinsfile-build
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#!groovy
|
||||||
|
pipeline {
|
||||||
|
|
||||||
|
agent any
|
||||||
|
|
||||||
|
environment {
|
||||||
|
BRANCH_NAME=env.GIT_BRANCH.replace("origin/", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Build') {
|
||||||
|
steps {
|
||||||
|
dir('messaging-app') {
|
||||||
|
sh '../mvnw clean verify'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Publish Pacts') {
|
||||||
|
steps {
|
||||||
|
dir('messaging-app') {
|
||||||
|
sh '../mvnw pact:publish -Dpact.consumer.version=${GIT_COMMIT} -Dpact.tag=${BRANCH_NAME}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
38
jenkins/without-cd/Jenkinsfile-deploy
Normal file
38
jenkins/without-cd/Jenkinsfile-deploy
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#!groovy
|
||||||
|
pipeline {
|
||||||
|
|
||||||
|
agent any
|
||||||
|
|
||||||
|
parameters {
|
||||||
|
string(name: 'GIT_COMMIT', defaultValue: '', description: 'Version (a.k.a. git commit) to deploy')
|
||||||
|
}
|
||||||
|
|
||||||
|
options {
|
||||||
|
skipDefaultCheckout()
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Check Pact Verifications') {
|
||||||
|
steps {
|
||||||
|
sh 'curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.88.3/pact-1.88.3-linux-x86_64.tar.gz'
|
||||||
|
sh 'tar xzf pact-1.88.3-linux-x86_64.tar.gz'
|
||||||
|
dir('pact/bin') {
|
||||||
|
sh "./pact-broker can-i-deploy -a messaging-app -b http://pact_broker -e ${GIT_COMMIT} --to prod"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Deploy') {
|
||||||
|
steps {
|
||||||
|
echo 'Deploying to prod now...'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Tag Pact') {
|
||||||
|
steps {
|
||||||
|
dir('pact/bin') {
|
||||||
|
sh "./pact-broker create-version-tag -a messaging-app -b http://pact_broker -e ${GIT_COMMIT} -t prod"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
2
lombok.config
Normal file
2
lombok.config
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
config.stopBubbling = true
|
||||||
|
lombok.noArgsConstructor.extraPrivate = true
|
||||||
88
pom.xml
Normal file
88
pom.xml
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>de.kreuzwerker.cdc</groupId>
|
||||||
|
<artifactId>messaging-app</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.2.4.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<pact.version>4.0.7</pact.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${junit-jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>au.com.dius</groupId>
|
||||||
|
<artifactId>pact-jvm-consumer-java8</artifactId>
|
||||||
|
<version>${pact.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>au.com.dius</groupId>
|
||||||
|
<artifactId>pact-jvm-consumer-junit5</artifactId>
|
||||||
|
<version>${pact.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>au.com.dius</groupId>
|
||||||
|
<artifactId>pact-jvm-provider-maven</artifactId>
|
||||||
|
<version>${pact.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- pact_broker is the service name in docker-compose -->
|
||||||
|
<pactBrokerUrl>http://pact_broker</pactBrokerUrl>
|
||||||
|
<projectVersion>${pact.consumer.version}</projectVersion>
|
||||||
|
<tags>
|
||||||
|
<tag>${pact.tag}</tag>
|
||||||
|
</tags>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
11
src/main/java/de/kreuzwerker/cdc/messagingapp/Friend.java
Normal file
11
src/main/java/de/kreuzwerker/cdc/messagingapp/Friend.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Friend {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@Slf4j
|
||||||
|
public class MessagingApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MessagingApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
17
src/main/java/de/kreuzwerker/cdc/messagingapp/User.java
Normal file
17
src/main/java/de/kreuzwerker/cdc/messagingapp/User.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String role;
|
||||||
|
private LocalDateTime lastLogin;
|
||||||
|
private List<Friend> friends;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserServiceClient {
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public UserServiceClient(@Value("${user-service.base-url}") String baseUrl) {
|
||||||
|
this.restTemplate = new RestTemplateBuilder().rootUri(baseUrl).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getUser(String id) {
|
||||||
|
final User user = restTemplate.getForObject("/users/" + id, User.class);
|
||||||
|
Assert.hasText(user.getName(), "Name is blank.");
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
2
src/main/resources/application.yml
Normal file
2
src/main/resources/application.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
user-service:
|
||||||
|
base-url: "http://localhost:8090"
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class MessagingApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,103 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import au.com.dius.pact.consumer.dsl.DslPart;
|
||||||
|
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 io.pactfoundation.consumer.dsl.LambdaDsl;
|
||||||
|
import org.assertj.core.groups.Tuple;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
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.http.HttpStatus;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
|
||||||
|
properties = "user-service.base-url:http://localhost:8080",
|
||||||
|
classes = UserServiceClient.class)
|
||||||
|
@Disabled
|
||||||
|
@ExtendWith(PactConsumerTestExt.class)
|
||||||
|
@PactTestFor(providerName = "user-service", port = "8080")
|
||||||
|
public class UserServiceContractTest {
|
||||||
|
|
||||||
|
private static final String NAME = "user name for CDC";
|
||||||
|
private static final LocalDateTime LAST_LOGIN = LocalDateTime.of(2018, 10, 16, 12, 34, 12);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserServiceClient userServiceClient;
|
||||||
|
|
||||||
|
@Pact(consumer = "messaging-app")
|
||||||
|
public RequestResponsePact pactUserExists(PactDslWithProvider builder) {
|
||||||
|
|
||||||
|
// See https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-junit#dsl-matching-methods
|
||||||
|
DslPart body = LambdaDsl.newJsonBody((o) -> o
|
||||||
|
.stringType("name", NAME)
|
||||||
|
.timestamp("lastLogin", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
|
||||||
|
Date.from(LAST_LOGIN.atZone(ZoneId.systemDefault()).toInstant()))
|
||||||
|
.stringMatcher("role", "ADMIN|USER", "ADMIN")
|
||||||
|
.minArrayLike("friends", 0, 2, friend -> friend
|
||||||
|
.stringType("id", "2")
|
||||||
|
.stringType("name", "a friend")
|
||||||
|
)).build();
|
||||||
|
|
||||||
|
return builder.given(
|
||||||
|
"User 1 exists")
|
||||||
|
.uponReceiving("A request to /users/1")
|
||||||
|
.path("/users/1")
|
||||||
|
.method("GET")
|
||||||
|
.willRespondWith()
|
||||||
|
.status(200)
|
||||||
|
.body(body)
|
||||||
|
.toPact();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Pact(consumer = "messaging-app")
|
||||||
|
public RequestResponsePact pactUserDoesNotExist(PactDslWithProvider builder) {
|
||||||
|
|
||||||
|
return builder.given(
|
||||||
|
"User 2 does not exist")
|
||||||
|
.uponReceiving("A request to /users/2")
|
||||||
|
.path("/users/2")
|
||||||
|
.method("GET")
|
||||||
|
.willRespondWith()
|
||||||
|
.status(404)
|
||||||
|
.toPact();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PactTestFor(pactMethod = "pactUserExists")
|
||||||
|
@Test
|
||||||
|
public void userExists() {
|
||||||
|
final User user = userServiceClient.getUser("1");
|
||||||
|
|
||||||
|
assertThat(user.getName()).isEqualTo(NAME);
|
||||||
|
assertThat(user.getLastLogin()).isEqualTo(LAST_LOGIN);
|
||||||
|
assertThat(user.getRole()).isEqualTo("ADMIN");
|
||||||
|
assertThat(user.getFriends()).hasSize(2)
|
||||||
|
// currently not possible to define multiple values, s. https://github.com/DiUS/pact-jvm/issues/379
|
||||||
|
.extracting(Friend::getId, Friend::getName)
|
||||||
|
.containsExactly(Tuple.tuple("2", "a friend"), Tuple.tuple("2", "a friend"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PactTestFor(pactMethod = "pactUserDoesNotExist")
|
||||||
|
@Test
|
||||||
|
public void userDoesNotExist() {
|
||||||
|
HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () ->
|
||||||
|
userServiceClient.getUser("2"));
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, exception.getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
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 io.pactfoundation.consumer.dsl.LambdaDsl;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
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 static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
|
||||||
|
properties = "user-service.base-url:http://localhost:8080",
|
||||||
|
classes = UserServiceClient.class)
|
||||||
|
@Disabled
|
||||||
|
@ExtendWith(PactConsumerTestExt.class)
|
||||||
|
@PactTestFor(providerName = "user-service", port = "8080")
|
||||||
|
public class UserServiceContractTestV1 {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserServiceClient userServiceClient;
|
||||||
|
|
||||||
|
|
||||||
|
@Pact(consumer = "messaging-app")
|
||||||
|
public RequestResponsePact pactUserExists(PactDslWithProvider builder) {
|
||||||
|
return builder.given(
|
||||||
|
"User 1 exists")
|
||||||
|
.uponReceiving("A request to /users/1")
|
||||||
|
.path("/users/1")
|
||||||
|
.method("GET")
|
||||||
|
.willRespondWith()
|
||||||
|
.status(200)
|
||||||
|
.body(LambdaDsl.newJsonBody((o) ->
|
||||||
|
o.stringType("name", "user name for CDC")
|
||||||
|
).build()).toPact();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PactTestFor(pactMethod = "pactUserExists")
|
||||||
|
@Test
|
||||||
|
public void userExists() {
|
||||||
|
User user = userServiceClient.getUser("1");
|
||||||
|
|
||||||
|
assertThat(user.getName()).isEqualTo("user name for CDC");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,100 @@
|
|||||||
|
package de.kreuzwerker.cdc.messagingapp;
|
||||||
|
|
||||||
|
import au.com.dius.pact.consumer.dsl.DslPart;
|
||||||
|
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 io.pactfoundation.consumer.dsl.LambdaDsl;
|
||||||
|
import org.assertj.core.groups.Tuple;
|
||||||
|
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.http.HttpStatus;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
|
||||||
|
properties = "user-service.base-url:http://localhost:9000",
|
||||||
|
classes = UserServiceClient.class)
|
||||||
|
@ExtendWith(PactConsumerTestExt.class)
|
||||||
|
@PactTestFor(providerName = "user-service", port = "9000")
|
||||||
|
public class UserServiceGenericStateWithParameterContractTest {
|
||||||
|
|
||||||
|
private static final String NAME = "user name for CDC";
|
||||||
|
private static final LocalDateTime LAST_LOGIN = LocalDateTime.of(2018, 10, 16, 12, 34, 12);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserServiceClient userServiceClient;
|
||||||
|
|
||||||
|
|
||||||
|
@Pact(consumer = "messaging-app")
|
||||||
|
public RequestResponsePact pactUserExists(PactDslWithProvider builder) {
|
||||||
|
|
||||||
|
// See https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-junit#dsl-matching-methods
|
||||||
|
DslPart body = LambdaDsl.newJsonBody((o) -> o
|
||||||
|
.stringType("id", "1")
|
||||||
|
.stringType("name", NAME)
|
||||||
|
.timestamp("lastLogin", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
|
||||||
|
Date.from(LAST_LOGIN.atZone(ZoneId.systemDefault()).toInstant()))
|
||||||
|
.stringMatcher("role", "ADMIN|USER", "ADMIN")
|
||||||
|
.minArrayLike("friends", 0, 2, friend -> friend
|
||||||
|
.stringType("id", "2")
|
||||||
|
.stringType("name", "a friend")
|
||||||
|
)).build();
|
||||||
|
|
||||||
|
return builder.given("default", Collections.singletonMap("userExists", true))
|
||||||
|
.uponReceiving("A request for an existing user")
|
||||||
|
.path("/users/1")
|
||||||
|
.method("GET")
|
||||||
|
.willRespondWith()
|
||||||
|
.status(200)
|
||||||
|
.body(body)
|
||||||
|
.toPact();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Pact(consumer = "messaging-app")
|
||||||
|
public RequestResponsePact pactUserDoesNotExist(PactDslWithProvider builder) {
|
||||||
|
|
||||||
|
return builder.given("default", Collections.singletonMap("userExists", false))
|
||||||
|
.uponReceiving("A request for a non-existing user")
|
||||||
|
.path("/users/1")
|
||||||
|
.method("GET")
|
||||||
|
.willRespondWith()
|
||||||
|
.status(404)
|
||||||
|
.toPact();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PactTestFor(pactMethod = "pactUserExists")
|
||||||
|
@Test
|
||||||
|
public void userExists() {
|
||||||
|
final User user = userServiceClient.getUser("1");
|
||||||
|
|
||||||
|
assertThat(user.getName()).isEqualTo(NAME);
|
||||||
|
assertThat(user.getLastLogin()).isEqualTo(LAST_LOGIN);
|
||||||
|
assertThat(user.getRole()).isEqualTo("ADMIN");
|
||||||
|
assertThat(user.getFriends()).hasSize(2)
|
||||||
|
// currently not possible to define multiple values, s. https://github.com/DiUS/pact-jvm/issues/379
|
||||||
|
.extracting(Friend::getId, Friend::getName)
|
||||||
|
.containsExactly(Tuple.tuple("2", "a friend"), Tuple.tuple("2", "a friend"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PactTestFor(pactMethod = "pactUserDoesNotExist")
|
||||||
|
@Test
|
||||||
|
public void userDoesNotExist() {
|
||||||
|
HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () ->
|
||||||
|
userServiceClient.getUser("1"));
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, exception.getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user