Skip to content

Commit

Permalink
Added Contract tests using spring-cloud-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
R4il committed Feb 28, 2018
1 parent a32acd2 commit 71648b0
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 16 deletions.
Binary file modified .DS_Store
Binary file not shown.
27 changes: 25 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
buildscript {
ext {
springBootVersion = '1.5.10.RELEASE'
verifierVersion = '1.2.3.RELEASE'
axonVersion = '3.1.2'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'au.com.dius:pact-jvm-provider-gradle_2.12:3.5.13'
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:${verifierVersion}"


}
}

plugins {
id "au.com.dius.pact" version "3.5.13"
id "com.wiredforcode.spawn" version "0.8.2"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'groovy'
apply plugin: 'spring-cloud-contract'


group = 'org.aitesting.microservices'
version = '0.0.1-SNAPSHOT'
Expand All @@ -35,9 +46,21 @@ dependencies {
compile("org.axonframework:axon-spring-boot-autoconfigure:${axonVersion}")
compile("org.axonframework:axon-amqp:${axonVersion}")
compile("org.axonframework:axon-test:${axonVersion}")
compile('au.com.dius:pact-jvm-provider-junit_2.11:3.5.13')


compile('org.springframework.boot:spring-boot-starter-amqp')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.codehaus.groovy:groovy-all:2.4.6')
testCompile("org.springframework.cloud:spring-cloud-starter-contract-verifier:${verifierVersion}")
testCompile("org.springframework.cloud:spring-cloud-contract-spec:${verifierVersion}")
testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
}

contracts {
baseClassMappings {
baseClassMapping(".*", "org.aitesting.microservices.tests.provider.TripContractBase")
}
}
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Fri Feb 23 18:20:49 EST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-all.zip
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include 'groovy'

Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@
import org.aitesting.microservices.tripmanagement.query.service.repositories.TripRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
@RestController
@RequestMapping("api")
public class TripManagementController {

@Autowired
private TripRepository tripRepository;

@GetMapping("trips")
public @ResponseBody
List<Trip> getTrips() {
public List<Trip> getTrips() {
return tripRepository.findAll();
}

@GetMapping("trip/{id}")
public @ResponseBody Trip getTrip(@PathVariable("id") UUID id) {
public Trip getTrip(@PathVariable("id") UUID id) {
return tripRepository.findOne(id);
}
}
Binary file added src/test/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.aitesting.microservices.tests.helpers;

import java.util.UUID;

public class TestConstants {
public static final UUID TRIP_ID1 = UUID.fromString("f849769e-2534-84a6-d475-5c2d701343ab");
public static final UUID TRIP_ID2 = UUID.fromString("5b842415-9447-4b9b-85c6-2e1075214cc4");
public static final UUID TRIP_ID3 = UUID.fromString("7a7d1e99-4823-4aa5-9d3b-2307e88cee0d");
public static final UUID USER_ID = UUID.fromString("123e4567-e89b-12d3-a456-426655440000");
public static final String FROM = "from some place over there";
public static final String TO = "to this other place";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.aitesting.microservices.tests.provider;

import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.aitesting.microservices.tripmanagement.common.TripStatus;
import org.aitesting.microservices.tripmanagement.query.TripManagementQueryApplication;
import org.aitesting.microservices.tripmanagement.query.domain.models.Trip;
import org.aitesting.microservices.tripmanagement.query.service.repositories.TripRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;

import static org.aitesting.microservices.tests.helpers.TestConstants.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TripManagementQueryApplication.class)
public abstract class TripContractBase {

@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
TripRepository tripRepository;

@Before
public void setup() {
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
tripRepository.findAll();
tripRepository.save(new Trip(TRIP_ID1, USER_ID, FROM, TO, TripStatus.STARTED));
tripRepository.save(new Trip(TRIP_ID2, USER_ID, FROM, TO, TripStatus.CREATED));
tripRepository.save(new Trip(TRIP_ID3, USER_ID, FROM, TO, TripStatus.COMPLETED));
}

@After
public void teardown() {
tripRepository.deleteAll();
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aitesting.microservices.tripmanagement.query.unit;
package org.aitesting.microservices.tests.unit;

import org.aitesting.microservices.tripmanagement.common.TripCanceledEvent;
import org.aitesting.microservices.tripmanagement.common.TripCompletedEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aitesting.microservices.tripmanagement.query.unit;
package org.aitesting.microservices.tests.unit;

import org.aitesting.microservices.tripmanagement.query.service.controllers.TripManagementController;
import org.aitesting.microservices.tripmanagement.query.service.repositories.TripRepository;
Expand All @@ -10,6 +10,7 @@

import java.util.UUID;

import static org.aitesting.microservices.tests.helpers.TestConstants.TRIP_ID1;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -36,12 +37,11 @@ public void onGetTripsCall_FindAllIsCalled(){
@Test
public void onGetTripCall_FindOneIsCalled(){
//arrange
UUID tripId = UUID.randomUUID();

//act
tripManagementController.getTrip(tripId);
tripManagementController.getTrip(TRIP_ID1);

//assert
verify(tripRepository, times(1)).findOne(tripId);
verify(tripRepository, times(1)).findOne(TRIP_ID1);
}
}
Binary file added src/test/resources/.DS_Store
Binary file not shown.
25 changes: 25 additions & 0 deletions src/test/resources/contracts/TripQuery/ShouldGetTrip.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package TripQuery

import org.springframework.cloud.contract.spec.*

Contract.make {
description("When a Get request with a TripID is made, the corresponding trip is returned")
request {
method 'GET'
url '/api/trip/f849769e-2534-84a6-d475-5c2d701343ab'
}
response {
status 200
body(
"id": "f849769e-2534-84a6-d475-5c2d701343ab",
"userID": "123e4567-e89b-12d3-a456-426655440000",
"originAddress": "from some place over there",
"destinationAddress": "to this other place",
"status": "STARTED"
)
headers {
contentType(applicationJson())
}
}
}

44 changes: 44 additions & 0 deletions src/test/resources/contracts/TripQuery/shouldGetAllTrips.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package TripQuery

import org.springframework.cloud.contract.spec.*

Contract.make {
description("When a Get request for all the trips should get all the trips")
request {
method 'GET'
url '/api/trips'
}
response {
status 200
body("""
[
\t{
\t\t"id": "f849769e-2534-84a6-d475-5c2d701343ab",
\t\t"userID": "123e4567-e89b-12d3-a456-426655440000",
\t\t"originAddress": "from some place over there",
\t\t"destinationAddress": "to this other place",
\t\t"status": "STARTED"
\t},
\t{
\t\t"id": "5b842415-9447-4b9b-85c6-2e1075214cc4",
\t\t"userID": "123e4567-e89b-12d3-a456-426655440000",
\t\t"originAddress": "from some place over there",
\t\t"destinationAddress": "to this other place",
\t\t"status": "CREATED"
\t},
\t{
\t\t"id": "7a7d1e99-4823-4aa5-9d3b-2307e88cee0d",
\t\t"userID": "123e4567-e89b-12d3-a456-426655440000",
\t\t"originAddress": "from some place over there",
\t\t"destinationAddress": "to this other place",
\t\t"status": "COMPLETED"
\t}
]
"""
)
headers {
contentType(applicationJson())
}
}
}

Binary file added src/test/resources/pacts/.DS_Store
Binary file not shown.

0 comments on commit 71648b0

Please sign in to comment.