Skip to content

Commit 27c0fee

Browse files
committed
update cdc example, much cleaner
1 parent a220c03 commit 27c0fee

File tree

8 files changed

+135
-105
lines changed

8 files changed

+135
-105
lines changed

examples/consumer-driven-contracts/payment-consumer/src/main/java/payment/consumer/Consumer.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package payment.consumer;
22

3-
import com.intuit.karate.FileUtils;
4-
import com.intuit.karate.JsonUtils;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
54
import java.net.HttpURLConnection;
65
import java.net.URL;
6+
import java.nio.charset.StandardCharsets;
77
import org.apache.commons.io.IOUtils;
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
@@ -18,6 +18,7 @@ public class Consumer {
1818
private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
1919

2020
private final String paymentServiceUrl;
21+
private final ObjectMapper mapper = new ObjectMapper();
2122

2223
public Consumer(String paymentServiceUrl) {
2324
this.paymentServiceUrl = paymentServiceUrl;
@@ -34,14 +35,14 @@ public Payment create(Payment payment) {
3435
con.setRequestMethod("POST");
3536
con.setDoOutput(true);
3637
con.setRequestProperty("Content-Type", "application/json");
37-
String json = JsonUtils.toJson(payment);
38+
String json = mapper.writeValueAsString(payment);
3839
IOUtils.write(json, con.getOutputStream(), "utf-8");
3940
int status = con.getResponseCode();
4041
if (status != 200) {
4142
throw new RuntimeException("status code was " + status);
4243
}
43-
String content = FileUtils.toString(con.getInputStream());
44-
return JsonUtils.fromJson(content, Payment.class);
44+
String content = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8);
45+
return mapper.readValue(content, Payment.class);
4546
} catch (Exception e) {
4647
throw new RuntimeException(e);
4748
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
package payment.consumer;
22

3-
import com.intuit.karate.netty.FeatureServer;
3+
import com.intuit.karate.core.MockServer;
44
import java.io.File;
5-
import org.junit.AfterClass;
6-
import org.junit.Test;
7-
import static org.junit.Assert.*;
8-
import org.junit.BeforeClass;
5+
import org.junit.jupiter.api.AfterAll;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
99
import payment.producer.Payment;
1010

1111
/**
1212
*
1313
* @author pthomas3
1414
*/
15-
public class ConsumerIntegrationAgainstMockTest {
16-
17-
private static FeatureServer server;
18-
private static Consumer consumer;
19-
20-
@BeforeClass
21-
public static void beforeClass() {
15+
class ConsumerIntegrationAgainstMockTest {
16+
17+
static MockServer server;
18+
static Consumer consumer;
19+
20+
@BeforeAll
21+
static void beforeAll() {
2222
File file = new File("../payment-producer/src/test/java/payment/producer/mock/payment-mock.feature");
23-
server = FeatureServer.start(file, 0, false, null);
23+
server = MockServer.feature(file).http(0).build();
2424
String paymentServiceUrl = "http://localhost:" + server.getPort();
25-
consumer = new Consumer(paymentServiceUrl);
26-
}
27-
25+
consumer = new Consumer(paymentServiceUrl);
26+
}
27+
2828
@Test
29-
public void testPaymentCreate() throws Exception {
29+
void testPaymentCreate() throws Exception {
3030
Payment payment = new Payment();
3131
payment.setAmount(5.67);
3232
payment.setDescription("test one");
3333
payment = consumer.create(payment);
3434
assertTrue(payment.getId() > 0);
3535
assertEquals(payment.getAmount(), 5.67, 0);
36-
assertEquals(payment.getDescription(), "test one");
36+
assertEquals(payment.getDescription(), "test one");
3737
}
38-
39-
@AfterClass
40-
public static void afterClass() {
38+
39+
@AfterAll
40+
static void afterAll() {
4141
server.stop();
42-
}
43-
42+
}
43+
4444
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package payment.consumer;
22

3-
import org.junit.AfterClass;
4-
import org.junit.Test;
5-
import static org.junit.Assert.*;
6-
import org.junit.BeforeClass;
3+
import org.junit.jupiter.api.AfterAll;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
77
import org.springframework.context.ConfigurableApplicationContext;
88
import payment.producer.Payment;
99
import payment.producer.PaymentService;
@@ -12,20 +12,20 @@
1212
*
1313
* @author pthomas3
1414
*/
15-
public class ConsumerIntegrationTest {
16-
17-
private static ConfigurableApplicationContext context;
18-
private static Consumer consumer;
19-
20-
@BeforeClass
21-
public static void beforeClass() {
15+
class ConsumerIntegrationTest {
16+
17+
static ConfigurableApplicationContext context;
18+
static Consumer consumer;
19+
20+
@BeforeAll
21+
static void beforeAll() {
2222
context = PaymentService.start();
2323
String paymentServiceUrl = "http://localhost:" + PaymentService.getPort(context);
24-
consumer = new Consumer(paymentServiceUrl);
24+
consumer = new Consumer(paymentServiceUrl);
2525
}
26-
26+
2727
@Test
28-
public void testPaymentCreate() throws Exception {
28+
void testPaymentCreate() throws Exception {
2929
Payment payment = new Payment();
3030
payment.setAmount(5.67);
3131
payment.setDescription("test one");
@@ -34,10 +34,10 @@ public void testPaymentCreate() throws Exception {
3434
assertEquals(payment.getAmount(), 5.67, 0);
3535
assertEquals(payment.getDescription(), "test one");
3636
}
37-
38-
@AfterClass
39-
public static void afterClass() {
37+
38+
@AfterAll
39+
static void afterAll() {
4040
PaymentService.stop(context);
4141
}
42-
42+
4343
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
package payment.producer.contract;
22

3-
import com.intuit.karate.KarateOptions;
4-
import com.intuit.karate.junit4.Karate;
5-
import payment.producer.PaymentService;
6-
import org.junit.AfterClass;
7-
import org.junit.BeforeClass;
8-
import org.junit.runner.RunWith;
3+
import com.intuit.karate.Results;
4+
import com.intuit.karate.Runner;
5+
import org.junit.jupiter.api.AfterAll;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
99
import org.springframework.context.ConfigurableApplicationContext;
10+
import payment.producer.PaymentService;
1011

1112
/**
1213
*
1314
* @author pthomas3
1415
*/
15-
@RunWith(Karate.class)
16-
@KarateOptions(features = "classpath:payment/producer/contract/payment-contract.feature")
17-
public class PaymentContractTest {
18-
19-
private static ConfigurableApplicationContext context;
20-
21-
@BeforeClass
22-
public static void beforeClass() {
16+
class PaymentContractTest {
17+
18+
static ConfigurableApplicationContext context;
19+
20+
@BeforeAll
21+
static void beforeAll() {
2322
context = PaymentService.start();
23+
}
24+
25+
@Test
26+
void testReal() {
2427
String paymentServiceUrl = "http://localhost:" + PaymentService.getPort(context);
25-
System.setProperty("payment.service.url", paymentServiceUrl);
28+
Results results = Runner.path("classpath:payment/producer/contract/payment-contract.feature")
29+
.systemProperty("payment.service.url", paymentServiceUrl)
30+
.parallel(1);
31+
assertTrue(results.getFailCount() == 0, results.getErrorMessages());
2632
}
27-
28-
@AfterClass
29-
public static void afterClass() {
33+
34+
@AfterAll
35+
static void afterAll() {
3036
PaymentService.stop(context);
31-
}
32-
37+
}
38+
3339
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
package payment.producer.mock;
22

3-
import com.intuit.karate.FileUtils;
4-
import com.intuit.karate.KarateOptions;
5-
import com.intuit.karate.junit4.Karate;
6-
import com.intuit.karate.netty.FeatureServer;
7-
import java.io.File;
8-
import org.junit.AfterClass;
9-
import org.junit.BeforeClass;
10-
import org.junit.runner.RunWith;
3+
import com.intuit.karate.Results;
4+
import com.intuit.karate.Runner;
5+
import com.intuit.karate.core.MockServer;
6+
import org.junit.jupiter.api.AfterAll;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
1110

1211
/**
1312
*
1413
* @author pthomas3
1514
*/
16-
@RunWith(Karate.class)
17-
@KarateOptions(features = "classpath:payment/producer/contract/payment-contract.feature")
18-
public class PaymentContractAgainstMockTest {
19-
20-
private static FeatureServer server;
21-
22-
@BeforeClass
23-
public static void beforeClass() {
24-
File file = FileUtils.getFileRelativeTo(PaymentContractAgainstMockTest.class, "payment-mock.feature");
25-
server = FeatureServer.start(file, 0, false, null);
15+
class PaymentContractAgainstMockTest {
16+
17+
static MockServer server;
18+
19+
@BeforeAll
20+
static void beforeAll() {
21+
server = MockServer.feature("classpath:payment/producer/mock/payment-mock.feature").http(0).build();
22+
}
23+
24+
@Test
25+
void testMock() {
2626
String paymentServiceUrl = "http://localhost:" + server.getPort();
27-
System.setProperty("payment.service.url", paymentServiceUrl);
27+
Results results = Runner.path("classpath:payment/producer/contract/payment-contract.feature")
28+
.systemProperty("payment.service.url", paymentServiceUrl)
29+
.parallel(1);
30+
assertTrue(results.getFailCount() == 0, results.getErrorMessages());
2831
}
29-
30-
@AfterClass
31-
public static void afterClass() {
32-
server.stop();
33-
}
34-
32+
33+
@AfterAll
34+
static void afterAll() {
35+
server.stop();
36+
}
37+
3538
}

examples/consumer-driven-contracts/pom.xml

+25-9
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,32 @@
1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<java.version>1.8</java.version>
18-
<maven.compiler.version>3.6.0</maven.compiler.version>
18+
<maven.compiler.version>3.8.1</maven.compiler.version>
19+
<maven.surefire.version>2.22.2</maven.surefire.version>
1920
<spring.boot.version>2.3.4.RELEASE</spring.boot.version>
21+
<junit5.version>5.7.0</junit5.version>
2022
<karate.version>2.0.0</karate.version>
2123
</properties>
2224

2325
<dependencies>
2426
<dependency>
2527
<groupId>com.intuit.karate</groupId>
26-
<artifactId>karate-apache</artifactId>
28+
<artifactId>karate-core</artifactId>
2729
<version>${karate.version}</version>
28-
<!-- <scope>test</scope> not ideal but our consumer depends on some karate utils -->
29-
</dependency>
30+
<scope>test</scope>
31+
</dependency>
3032
<dependency>
31-
<groupId>com.intuit.karate</groupId>
32-
<artifactId>karate-junit4</artifactId>
33-
<version>${karate.version}</version>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-api</artifactId>
35+
<version>${junit5.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-engine</artifactId>
41+
<version>${junit5.version}</version>
3442
<scope>test</scope>
35-
</dependency>
43+
</dependency>
3644
</dependencies>
3745

3846
<build>
@@ -55,7 +63,15 @@
5563
<target>${java.version}</target>
5664
<compilerArgument>-Werror</compilerArgument>
5765
</configuration>
58-
</plugin>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-surefire-plugin</artifactId>
70+
<version>${maven.surefire.version}</version>
71+
<configuration>
72+
<argLine>-Dfile.encoding=UTF-8</argLine>
73+
</configuration>
74+
</plugin>
5975
</plugins>
6076
</build>
6177

karate-archetype/src/main/resources/archetype-resources/pom.xml

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<java.version>1.8</java.version>
13-
<maven.compiler.version>3.6.0</maven.compiler.version>
14-
<karate.version>0.9.6</karate.version>
13+
<maven.compiler.version>3.8.1</maven.compiler.version>
14+
<maven.surefire.version>2.22.2</maven.surefire.version>
15+
<karate.version>1.0.0</karate.version>
1516
</properties>
1617

1718
<dependencies>
@@ -47,8 +48,11 @@
4748
<plugin>
4849
<groupId>org.apache.maven.plugins</groupId>
4950
<artifactId>maven-surefire-plugin</artifactId>
50-
<version>2.22.2</version>
51-
</plugin>
51+
<version>${maven.surefire.version}</version>
52+
<configuration>
53+
<argLine>-Dfile.encoding=UTF-8</argLine>
54+
</configuration>
55+
</plugin>
5256
</plugins>
5357
</build>
5458

karate-mock-servlet/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ If you need to create a completely new `HttpClient` implementation from scratch,
2929
Karate defaults to the [`ApacheHttpClient`](../karate-core/src/main/java/com/intuit/karate/http/ApacheHttpClient.java), and to change this for a test-run, you can set the [`HttpClientFactory`](../karate-core/src/main/java/com/intuit/karate/http/HttpClientFactory.java) using the [`Runner`](../karate-core/src/main/java/com/intuit/karate/Runner.java) "builder" API.
3030

3131
## Mocking Your Servlet
32-
Creating a `Servlet` and `ServletContext` instance is up to you and here is where you would manage configuration for your web-application. And then you can implement `HttpClientFactory` by using the `MockHttpClient` code provide by Karate.
32+
Creating a `Servlet` and `ServletContext` instance is up to you and here is where you would manage configuration for your web-application. And then you can implement `HttpClientFactory` by using the `MockHttpClient` code provided by Karate.
3333

3434
Once you refer to the following examples, you should be able to get up and running for your project.
3535
* Spring MVC Dispatcher Servlet

0 commit comments

Comments
 (0)