forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentService.java
120 lines (101 loc) · 4.16 KB
/
PaymentService.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package mock.contract;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.demo.config.ServerStartedInitializingBean;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
/**
*
* @author pthomas3
*/
@Configuration
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class})
public class PaymentService {
private static final Logger logger = LoggerFactory.getLogger(PaymentService.class);
@Value("${queue.name}")
private String queueName;
@RestController
@RequestMapping("/payments")
class PaymentController {
private final AtomicInteger counter = new AtomicInteger();
private final Map<Integer, Payment> payments = new ConcurrentHashMap();
@PostMapping
public Payment create(@RequestBody Payment payment) {
int id = counter.incrementAndGet();
payment.setId(id);
payments.put(id, payment);
Shipment shipment = new Shipment();
shipment.setPaymentId(id);
shipment.setStatus("shipped");
QueueUtils.send(queueName, JsonUtils.toJson(shipment), 25);
return payment;
}
@PutMapping("/{id:.+}")
public Payment update(@PathVariable int id, @RequestBody Payment payment) {
payments.put(id, payment);
return payment;
}
@GetMapping
public Collection<Payment> list() {
return payments.values();
}
@GetMapping("/{id:.+}")
public Payment get(@PathVariable int id) {
Payment payment = payments.get(id);
if (payment == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return payment;
}
@DeleteMapping("/{id:.+}")
public void delete(@PathVariable int id) {
Payment payment = payments.remove(id);
if (payment == null) {
throw new RuntimeException("payment not found, id: " + id);
}
}
}
public static ConfigurableApplicationContext start(String queueName, boolean ssl) {
return start(queueName, ssl, 0);
}
public static ConfigurableApplicationContext start(String queueName, boolean ssl, int port) {
Stream<String> args = Stream.of("--server.port=" + port, "--queue.name=" + queueName);
if (ssl) {
args = Stream.concat(args, Stream.of(
"--server.ssl.key-store=src/test/java/server-keystore.p12",
"--server.ssl.key-store-password=karate-mock",
"--server.ssl.keyStoreType=PKCS12",
"--server.ssl.keyAlias=karate-mock"));
}
return SpringApplication.run(PaymentService.class, args.toArray(String[]::new));
}
public static void stop(ConfigurableApplicationContext context) {
SpringApplication.exit(context, () -> 0);
}
public static int getPort(ConfigurableApplicationContext context) {
ServerStartedInitializingBean ss = context.getBean(ServerStartedInitializingBean.class);
return ss.getLocalPort();
}
@Bean
public ServerStartedInitializingBean getInitializingBean() {
return new ServerStartedInitializingBean();
}
public static void main(String[] args) {
start("TEMP", false, 8090);
}
}