Skip to content

Commit

Permalink
[rewrite] #1281 focused commit to show changes to [karate-demo] for m…
Browse files Browse the repository at this point in the history
…ock server
  • Loading branch information
ptrthomas committed Nov 10, 2020
1 parent 1c06f00 commit a83f76e
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.netty.FeatureServer;
import java.io.File;
import java.util.Collections;
import com.intuit.karate.runtime.MockServer;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
Expand All @@ -20,14 +17,16 @@ public class ConsumerUsingMockTest {

private static final Logger logger = LoggerFactory.getLogger(ConsumerUsingMockTest.class);

private static FeatureServer server;
private static MockServer server;
private static Consumer consumer;

@BeforeClass
public static void beforeClass() {
String queueName = "DEMO.MOCK";
File file = FileUtils.getFileRelativeTo(ConsumerUsingMockTest.class, "payment-service-mock.feature");
server = FeatureServer.start(file, 0, false, Collections.singletonMap("queueName", queueName));
server = MockServer
.feature("classpath:mock/contract/payment-service-mock.feature")
.arg("queueName", queueName)
.http(0).build();
String paymentServiceUrl = "http://localhost:" + server.getPort();
consumer = new Consumer(paymentServiceUrl, queueName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.netty.FeatureServer;
import java.io.File;
import java.util.Collections;
import java.util.Map;
import com.intuit.karate.runtime.MockServer;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
Expand All @@ -17,26 +13,27 @@
* @author pthomas3
*/
public class ConsumerUsingProxyHttpTest {

private static ConfigurableApplicationContext context;
private static FeatureServer server;
private static MockServer server;
private static Consumer consumer;

@BeforeClass
public static void beforeClass() {
// actual service
String queueName = "DEMO.PROXY.HTTP";
context = PaymentService.start(queueName, false);
String paymentServiceUrl = "http://localhost:" + PaymentService.getPort(context);
String queueName = "DEMO.PROXY.HTTP";
context = PaymentService.start(queueName, false);
String paymentServiceUrl = "http://localhost:" + PaymentService.getPort(context);
// proxy
File file = FileUtils.getFileRelativeTo(ConsumerUsingProxyHttpTest.class, "payment-service-proxy.feature");
// setting 'paymentServiceUrl' to null uses request url as-is (no re-writing) - so acts as an http proxy
Map config = Collections.singletonMap("paymentServiceUrl", null);
server = FeatureServer.start(file, 0, false, config);
server = MockServer
.feature("classpath:mock/contract/payment-service-proxy.feature")
// setting 'paymentServiceUrl' to null uses request url as-is (no re-writing) - so acts as an http proxy
.arg("paymentServiceUrl", null)
.http(0).build();
// consumer (using http proxy)
consumer = new Consumer(paymentServiceUrl, "localhost", server.getPort(), queueName);
}
consumer = new Consumer(paymentServiceUrl, "localhost", server.getPort(), queueName);
}

@Test
public void testPaymentCreate() throws Exception {
Payment payment = new Payment();
Expand All @@ -49,21 +46,21 @@ public void testPaymentCreate() throws Exception {
consumer.listen(json -> {
Shipment shipment = JsonUtils.fromJson(json, Shipment.class);
assertEquals(result.getId(), shipment.getPaymentId());
assertEquals("shipped", shipment.getStatus());
synchronized(this) {
assertEquals("shipped", shipment.getStatus());
synchronized (this) {
notify();
}
});
synchronized(this) {
synchronized (this) {
wait(10000);
}
}
}

@AfterClass
public static void afterClass() {
server.stop();
PaymentService.stop(context);
consumer.stopQueueConsumer();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.netty.FeatureServer;
import java.io.File;
import java.util.Collections;
import java.util.Map;
import com.intuit.karate.runtime.MockServer;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
Expand All @@ -17,27 +13,28 @@
* @author pthomas3
*/
public class ConsumerUsingProxyRewriteSslTest {

private static ConfigurableApplicationContext context;
private static FeatureServer server;
private static MockServer server;
private static Consumer consumer;

@BeforeClass
public static void beforeClass() {
// actual service
String queueName = "DEMO.PROXY.REWRITE.SSL";
String queueName = "DEMO.PROXY.REWRITE.SSL";
context = PaymentService.start(queueName, true);
String paymentServiceUrl = "https://localhost:" + PaymentService.getPort(context);
// proxy
File file = FileUtils.getFileRelativeTo(ConsumerUsingProxyRewriteSslTest.class, "payment-service-proxy.feature");
Map config = Collections.singletonMap("paymentServiceUrl", paymentServiceUrl);
// requests will be forwarded / url re-written to paymentServiceUrl
server = FeatureServer.start(file, 0, false, config);
server = MockServer
.feature("classpath:mock/contract/payment-service-proxy.feature")
// requests will be forwarded / url re-written to paymentServiceUrl
.arg("paymentServiceUrl", paymentServiceUrl)
.http(0).build();
// consumer
String proxyUrl = "http://localhost:" + server.getPort();
consumer = new Consumer(proxyUrl, queueName);
}
String proxyUrl = "http://localhost:" + server.getPort();
consumer = new Consumer(proxyUrl, queueName);
}

@Test
public void testPaymentCreate() throws Exception {
Payment payment = new Payment();
Expand All @@ -50,21 +47,21 @@ public void testPaymentCreate() throws Exception {
consumer.listen(json -> {
Shipment shipment = JsonUtils.fromJson(json, Shipment.class);
assertEquals(result.getId(), shipment.getPaymentId());
assertEquals("shipped", shipment.getStatus());
synchronized(this) {
assertEquals("shipped", shipment.getStatus());
synchronized (this) {
notify();
}
});
synchronized(this) {
synchronized (this) {
wait(10000);
}
}
}

@AfterClass
public static void afterClass() {
server.stop();
PaymentService.stop(context);
consumer.stopQueueConsumer();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.netty.FeatureServer;
import java.io.File;
import java.util.Collections;
import java.util.Map;
import com.intuit.karate.runtime.MockServer;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
Expand All @@ -17,27 +13,28 @@
* @author pthomas3
*/
public class ConsumerUsingProxyRewriteTest {

private static ConfigurableApplicationContext context;
private static FeatureServer server;
private static MockServer server;
private static Consumer consumer;

@BeforeClass
public static void beforeClass() {
// actual service
String queueName = "DEMO.PROXY.REWRITE";
String queueName = "DEMO.PROXY.REWRITE";
context = PaymentService.start(queueName, false);
String paymentServiceUrl = "http://localhost:" + PaymentService.getPort(context);
// proxy
File file = FileUtils.getFileRelativeTo(ConsumerUsingProxyRewriteTest.class, "payment-service-proxy.feature");
Map config = Collections.singletonMap("paymentServiceUrl", paymentServiceUrl);
// requests will be forwarded / url re-written to paymentServiceUrl
server = FeatureServer.start(file, 0, false, config);
server = MockServer
.feature("classpath:mock/contract/payment-service-proxy.feature")
// requests will be forwarded / url re-written to paymentServiceUrl
.arg("paymentServiceUrl", paymentServiceUrl)
.http(0).build();
// consumer
String proxyUrl = "http://localhost:" + server.getPort();
consumer = new Consumer(proxyUrl, queueName);
}
String proxyUrl = "http://localhost:" + server.getPort();
consumer = new Consumer(proxyUrl, queueName);
}

@Test
public void testPaymentCreate() throws Exception {
Payment payment = new Payment();
Expand All @@ -50,21 +47,21 @@ public void testPaymentCreate() throws Exception {
consumer.listen(json -> {
Shipment shipment = JsonUtils.fromJson(json, Shipment.class);
assertEquals(result.getId(), shipment.getPaymentId());
assertEquals("shipped", shipment.getStatus());
synchronized(this) {
assertEquals("shipped", shipment.getStatus());
synchronized (this) {
notify();
}
});
synchronized(this) {
synchronized (this) {
wait(10000);
}
}
}

@AfterClass
public static void afterClass() {
server.stop();
PaymentService.stop(context);
consumer.stopQueueConsumer();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.junit4.Karate;
import com.intuit.karate.netty.FeatureServer;
import com.intuit.karate.KarateOptions;
import java.io.File;
import java.util.Collections;
import com.intuit.karate.runtime.MockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
Expand All @@ -18,14 +15,16 @@
@KarateOptions(features = "classpath:mock/contract/payment-service.feature")
public class PaymentServiceContractUsingMockSslTest {

private static FeatureServer server;
private static MockServer server;

@BeforeClass
public static void beforeClass() {
String queueName = "DEMO.CONTRACT.MOCK.SSL";
System.setProperty("karate.env", "contract");
File file = FileUtils.getFileRelativeTo(PaymentServiceContractUsingMockSslTest.class, "payment-service-mock.feature");
server = FeatureServer.start(file, 0, true, Collections.singletonMap("queueName", queueName));
server = MockServer
.feature("classpath:mock/contract/payment-service-mock.feature")
.arg("queueName", queueName)
.https(0).build();
String paymentServiceUrl = "https://localhost:" + server.getPort();
System.setProperty("payment.service.url", paymentServiceUrl);
System.setProperty("shipping.queue.name", queueName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.junit4.Karate;
import com.intuit.karate.netty.FeatureServer;
import com.intuit.karate.KarateOptions;
import java.io.File;
import java.util.Collections;
import com.intuit.karate.runtime.MockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
Expand All @@ -17,23 +14,25 @@
@RunWith(Karate.class)
@KarateOptions(features = "classpath:mock/contract/payment-service.feature")
public class PaymentServiceContractUsingMockTest {
private static FeatureServer server;

private static MockServer server;

@BeforeClass
public static void beforeClass() {
String queueName = "DEMO.CONTRACT.MOCK";
System.setProperty("karate.env", "contract");
File file = FileUtils.getFileRelativeTo(PaymentServiceContractUsingMockTest.class, "payment-service-mock.feature");
server = FeatureServer.start(file, 0, false, Collections.singletonMap("queueName", queueName));
System.setProperty("karate.env", "contract");
server = MockServer
.feature("classpath:mock/contract/payment-service-mock.feature")
.arg("queueName", queueName)
.http(0).build();
String paymentServiceUrl = "http://localhost:" + server.getPort();
System.setProperty("payment.service.url", paymentServiceUrl);
System.setProperty("shipping.queue.name", queueName);
}

@AfterClass
public static void afterClass() {
server.stop();
}
server.stop();
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package mock.contract;

import com.intuit.karate.FileUtils;
import com.intuit.karate.netty.FeatureServer;
import java.io.File;
import java.util.Collections;
import com.intuit.karate.runtime.MockServer;

/**
*
* @author pthomas3
*/
public class PaymentServiceMockMain {

public static void main(String[] args) {
File file = FileUtils.getFileRelativeTo(PaymentServiceMockMain.class, "payment-service-mock.feature");
FeatureServer server = FeatureServer.start(file, 8080, false, Collections.singletonMap("queueName", "DEMO.MOCK.8080"));
MockServer server = MockServer
.feature("classpath:mock/contract/payment-service-mock.feature")
.arg("queueName", "DEMO.MOCK.8080")
.http(8080).build();
server.waitSync();
}

}
Loading

0 comments on commit a83f76e

Please sign in to comment.