forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsumer.java
97 lines (84 loc) · 3.14 KB
/
Consumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package mock.contract;
import com.intuit.karate.JsonUtils;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class Consumer implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
private final String paymentServiceUrl;
private final String proxyHost;
private final Integer proxyPort;
private final List<Shipment> shipments = new ArrayList();
private final QueueConsumer queueConsumer;
public Consumer(String paymentServiceUrl, String queueName) {
this(paymentServiceUrl, null, null, queueName);
}
public Consumer(String paymentServiceUrl, String proxyHost, Integer proxyPort, String queueName) {
this.paymentServiceUrl = paymentServiceUrl;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
queueConsumer = new QueueConsumer(queueName);
queueConsumer.setMessageListener(this);
}
private HttpURLConnection getConnection(String path) throws Exception {
URL url = new URL(paymentServiceUrl + path);
if (proxyHost != null) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
return (HttpURLConnection) url.openConnection(proxy);
} else {
return (HttpURLConnection) url.openConnection();
}
}
public Payment create(Payment payment) {
try {
HttpURLConnection con = getConnection("/payments");
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
String json = JsonUtils.toJson(payment);
IOUtils.write(json, con.getOutputStream(), "utf-8");
int status = con.getResponseCode();
if (status != 200) {
throw new RuntimeException("status code was " + status);
}
String content = IOUtils.toString(con.getInputStream(), "utf-8");
return JsonUtils.fromJson(content, Payment.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public List<Shipment> getShipments() {
return shipments;
}
@Override
public void onMessage(Message message) {
try {
TextMessage tm = (TextMessage) message;
String json = tm.getText();
logger.info("*** received message: {}", json);
Shipment shipment = JsonUtils.fromJson(json, Shipment.class);
shipments.add(shipment);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void waitUntilFirstMessage() {
QueueUtils.waitUntilCondition(200, () -> !shipments.isEmpty());
}
public void stopQueueConsumer() {
queueConsumer.stop();
}
}