/
CouponServiceTest.java
94 lines (71 loc) · 3.09 KB
/
CouponServiceTest.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
import com.despegar.http.client.GetMethod;
import com.despegar.http.client.HttpResponse;
import com.despegar.sparkjava.test.SparkServer;
import com.gojek.ApplicationConfiguration;
import com.gojek.Figaro;
import com.testpyramid.CouponService;
import helpers.TestDataHelper;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import spark.servlet.SparkApplication;
import java.sql.Timestamp;
import java.time.Instant;
import static org.junit.Assert.assertEquals;
public class CouponServiceTest {
private static final ApplicationConfiguration config = Figaro.configure(null);
public static class CouponServiceTestSparkApplication implements SparkApplication {
@Override
public void init() {
CouponService.main(new String[]{});
}
}
@ClassRule
public static SparkServer<CouponServiceTestSparkApplication> testServer =
new SparkServer<>(CouponServiceTestSparkApplication.class, config.getValueAsInt("PORT"));
@Before
public void setUp() {
TestDataHelper.cleanDb();
}
@Test
public void testPing() throws Exception {
GetMethod get = testServer.get("/ping", false);
HttpResponse httpResponse = testServer.execute(get);
assertEquals(200, httpResponse.code());
assertEquals("pong", new String(httpResponse.body()));
}
@Test
public void couponReturnsCouponDetailsIfActiveAndAvailable() throws Exception {
String couponId = "ABCXYZ";
TestDataHelper.createCoupon(couponId, Timestamp.from(Instant.now().plusSeconds(86400)).toString(), 10, 1);
GetMethod get = testServer.get("/coupons/" + couponId, false);
HttpResponse httpResponse = testServer.execute(get);
assertEquals(200, httpResponse.code());
assertEquals("{\"id\":\"ABCXYZ\"}", new String(httpResponse.body()));
}
@Test
public void couponReturns404IfNotFound() throws Exception {
GetMethod get = testServer.get("/coupons/QWERTY", false);
HttpResponse httpResponse = testServer.execute(get);
assertEquals(404, httpResponse.code());
assertEquals("", new String(httpResponse.body()));
}
@Test
public void couponReturns404IfCouponExpired() throws Exception {
String couponId = "ABCXYZ";
TestDataHelper.createCoupon(couponId, Timestamp.from(Instant.now().minusSeconds(86400)).toString(), 10, 1);
GetMethod get = testServer.get("/coupons/" + couponId, false);
HttpResponse httpResponse = testServer.execute(get);
assertEquals(404, httpResponse.code());
assertEquals("", new String(httpResponse.body()));
}
@Test
public void couponReturns404IfCouponExhausted() throws Exception {
String couponId = "ABCXYZ";
TestDataHelper.createCoupon(couponId, Timestamp.from(Instant.now().plusSeconds(86400)).toString(), 10, 0);
GetMethod get = testServer.get("/coupons/" + couponId, false);
HttpResponse httpResponse = testServer.execute(get);
assertEquals(404, httpResponse.code());
assertEquals("", new String(httpResponse.body()));
}
}