master- Sync Repo

This commit is contained in:
Mali Bayhan 2021-12-10 09:49:13 -08:00
parent 77e3169e6b
commit 6c2157dac5
18 changed files with 505 additions and 0 deletions

View File

@ -0,0 +1,42 @@
#!groovy
pipeline {
agent any
parameters {
string(name: 'pactConsumerTags', defaultValue: 'junit5', description: 'Tags to verify')
}
stages {
stage ('Get Latest Prod Version From Pact Broker') {
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') {
script {
env.PROD_VERSION = sh(script: "./pact-broker describe-version -a user-service -b http://pact_broker -l prod | tail -1 | cut -f 1 -d \\|", returnStdout: true).trim()
}
}
echo "Current prod version: " + PROD_VERSION
}
}
stage("Checkout Latest Prod Version") {
steps {
sh "git checkout ${PROD_VERSION}"
}
}
stage ('Run Contract Tests') {
steps {
dir('user-service') {
sh "../mvnw clean test " +
"-Pcontract-tests " +
"-Dpact.provider.version=${PROD_VERSION} " +
"-Dpact.verifier.publishResults=true " +
"-Dpactbroker.tags=prod,${params.pactConsumerTags}"
}
}
}
}
}

35
jenkins/cd/Jenkinsfile vendored Normal file
View File

@ -0,0 +1,35 @@
#!groovy
pipeline {
agent any
stages {
stage ('Build') {
steps {
dir('user-service') {
sh "../mvnw clean verify " +
"-Dpact.provider.version=${GIT_COMMIT} " +
"-Dpact.verifier.publishResults=true"
}
}
}
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 user-service -b http://pact_broker -e ${GIT_COMMIT}"
}
}
}
stage('Deploy') {
when {
branch 'junit5'
}
steps {
echo 'Deploying to prod now...'
}
}
}
}

View File

@ -0,0 +1,18 @@
#!groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
dir('user-service') {
sh "../mvnw clean verify " +
"-Dpact.provider.version=${GIT_COMMIT} " +
"-Dpact.verifier.publishResults=true"
}
}
}
}
}

View 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 user-service -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 user-service -b http://pact_broker -e ${GIT_COMMIT} -t prod"
}
}
}
}
}

72
pom.xml Normal file
View File

@ -0,0 +1,72 @@
<?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>user-service</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>
</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>au.com.dius</groupId>
<artifactId>pact-jvm-provider-junit5</artifactId>
<version>4.0.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>contract-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*ContractTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,13 @@
package de.kreuzwerker.cdc.userservice;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Friend {
private String id;
private String name;
}

View File

@ -0,0 +1,15 @@
package de.kreuzwerker.cdc.userservice;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class GlobalControllerExceptionHandler {
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleNotFound() {
}
}

View File

@ -0,0 +1,5 @@
package de.kreuzwerker.cdc.userservice;
public class NotFoundException extends RuntimeException {
}

View File

@ -0,0 +1,22 @@
package de.kreuzwerker.cdc.userservice;
import java.util.Date;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.Singular;
@Data
@Builder
public class User {
private String id;
private String legacyId;
private String name;
private UserRole role;
private Date lastLogin;
@Singular
private List<Friend> friends;
}

View File

@ -0,0 +1,21 @@
package de.kreuzwerker.cdc.userservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/users/{userId}")
public User getUser(@PathVariable String userId) {
return userService.findUser(userId);
}
}

View File

@ -0,0 +1,8 @@
package de.kreuzwerker.cdc.userservice;
public enum UserRole {
ADMIN,
USER;
}

View File

@ -0,0 +1,21 @@
package de.kreuzwerker.cdc.userservice;
import java.util.Date;
import java.util.UUID;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public User findUser(String userId) {
return User.builder()
.id(userId)
.legacyId(UUID.randomUUID().toString())
.name("Beth")
.role(UserRole.ADMIN)
.lastLogin(new Date())
.friend(Friend.builder().id("2").name("Ronald Smith").build())
.friend(Friend.builder().id("3").name("Matt Spencer").build())
.build();
}
}

View File

@ -0,0 +1,12 @@
package de.kreuzwerker.cdc.userservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}

View File

@ -0,0 +1,8 @@
server:
port: 8090
spring:
jackson:
serialization:
write-dates-as-timestamps: false
date-format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

View File

@ -0,0 +1,41 @@
package de.kreuzwerker.cdc.userservice;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.State;
import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit5.HttpTestTarget;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
@Provider("user-service")
@PactFolder("pacts")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Disabled
public class ContractTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@State("User 1 exists")
public void user1Exists() {
// nothing to do, real service is used
}
}

View File

@ -0,0 +1,66 @@
package de.kreuzwerker.cdc.userservice;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.State;
import au.com.dius.pact.provider.junit.loader.PactBroker;
import au.com.dius.pact.provider.junit5.HttpTestTarget;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.web.server.LocalServerPort;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@Provider("user-service")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//pact_broker is the service name in docker-compose
@PactBroker(host = "pact_broker", tags = "${pactbroker.tags:prod}")
public class GenericStateWithParameterContractTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@MockBean
private UserService userService;
@State("default")
public void toDefaultState(Map<String, Object> params) {
final boolean userExists = (boolean) params.get("userExists");
if (userExists) {
when(userService.findUser(any())).thenReturn(User.builder()
.id("1")
.legacyId(UUID.randomUUID().toString())
.name("Beth")
.role(UserRole.ADMIN)
.lastLogin(new Date())
.friend(Friend.builder().id("2").name("Ronald Smith").build())
.friend(Friend.builder().id("3").name("Matt Spencer").build())
.build());
} else {
when(userService.findUser(any())).thenThrow(NotFoundException.class);
}
}
}

View File

@ -0,0 +1,56 @@
package de.kreuzwerker.cdc.userservice;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.State;
import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit5.HttpTestTarget;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.web.server.LocalServerPort;
import java.util.Date;
import java.util.UUID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@Provider("user-service")
@PactFolder("pacts")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Disabled
public class MockedUserServiceContractTest {
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port));
}
@MockBean
private UserService userService;
@State("User 1 exists")
public void user1Exists() {
when(userService.findUser(any())).thenReturn(User.builder()
.id("1")
.legacyId(UUID.randomUUID().toString())
.name("Beth")
.role(UserRole.ADMIN)
.lastLogin(new Date())
.friend(Friend.builder().id("2").name("Ronald Smith").build())
.friend(Friend.builder().id("3").name("Matt Spencer").build())
.build());
}
@State("User 2 does not exist")
public void user2DoesNotExist() {
when(userService.findUser(any())).thenThrow(NotFoundException.class);
}
}

View File

@ -0,0 +1,12 @@
package de.kreuzwerker.cdc.userservice;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class UserServiceApplicationTests {
@Test
public void contextLoads() {
}
}