|
| 1 | +package com.svix.test; |
| 2 | + |
| 3 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 4 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import com.svix.Svix; |
| 8 | +import com.svix.SvixOptions; |
| 9 | +import com.svix.api.MessageAttemptListByEndpointOptions; |
| 10 | +import com.svix.models.ListResponseMessageAttemptOut; |
| 11 | +import com.svix.models.ReplayIn; |
| 12 | +import org.junit.After; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | +import java.time.ZoneId; |
| 19 | +import java.time.OffsetDateTime; |
| 20 | +import java.time.ZoneOffset; |
| 21 | +import java.time.format.DateTimeFormatter; |
| 22 | + |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 24 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 25 | + |
| 26 | +public class WiremockTests { |
| 27 | + @Rule |
| 28 | + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().withRootDirectory("lib/src/test/resources")); |
| 29 | + |
| 30 | + |
| 31 | + @Before @After |
| 32 | + public void resetAll() { |
| 33 | + wireMockRule.resetAll(); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + private Svix testClient() { |
| 38 | + SvixOptions opts = new SvixOptions(); |
| 39 | + opts.setServerUrl(wireMockRule.baseUrl()); |
| 40 | + return new Svix("testtk_asd12.eu", opts); |
| 41 | + } |
| 42 | + |
| 43 | + @Test public void testDateUTCInQueryParam() throws Exception { |
| 44 | + Svix svx = testClient(); |
| 45 | + wireMockRule.stubFor(WireMock.get(urlMatching("/api/v1/app/ap/attempt/endpoint/endp?.*")) |
| 46 | + .willReturn(WireMock.ok().withBodyFile("ListResponseMessageAttemptOut.json")) |
| 47 | + ); |
| 48 | + |
| 49 | + Instant instant = Instant.ofEpochSecond(1740240455, 514412223); |
| 50 | + OffsetDateTime before = instant.atOffset(ZoneOffset.UTC); |
| 51 | + |
| 52 | + MessageAttemptListByEndpointOptions opts = new MessageAttemptListByEndpointOptions(); |
| 53 | + opts.setBefore(before); |
| 54 | + svx.getMessageAttempt().listByEndpoint("ap", "endp", opts); |
| 55 | + |
| 56 | + wireMockRule.verify(1, getRequestedFor(urlEqualTo("/api/v1/app/ap/attempt/endpoint/endp?before=2025-02-22T16%3A07%3A35.514412223Z"))); |
| 57 | + } |
| 58 | + |
| 59 | + @Test public void testDateESTInQueryParam() throws Exception { |
| 60 | + Svix svx = testClient(); |
| 61 | + wireMockRule.stubFor(WireMock.get(urlMatching("/api/v1/app/ap/attempt/endpoint/endp?.*")) |
| 62 | + .willReturn(WireMock.ok().withBodyFile("ListResponseMessageAttemptOut.json")) |
| 63 | + ); |
| 64 | + |
| 65 | + Instant instant = Instant.ofEpochSecond(1740240455, 514412223); |
| 66 | + OffsetDateTime before = instant.atZone(ZoneId.of("America/New_York")).toOffsetDateTime(); |
| 67 | + MessageAttemptListByEndpointOptions opts = new MessageAttemptListByEndpointOptions(); |
| 68 | + opts.setBefore(before); |
| 69 | + svx.getMessageAttempt().listByEndpoint("ap","endp",opts); |
| 70 | + |
| 71 | + wireMockRule.verify(1,getRequestedFor(urlEqualTo("/api/v1/app/ap/attempt/endpoint/endp?before=2025-02-22T11%3A07%3A35.514412223-05%3A00"))); |
| 72 | + } |
| 73 | + @Test public void testDateESTRequestBody() throws Exception { |
| 74 | + Svix svx = testClient(); |
| 75 | + wireMockRule.stubFor(WireMock.post(urlEqualTo("/api/v1/app/ap/endpoint/ep/replay-missing")) |
| 76 | + .willReturn(WireMock.ok().withBodyFile("ReplayOut.json")) |
| 77 | + ); |
| 78 | + |
| 79 | + Instant instant = Instant.ofEpochSecond(1740240455, 514412223); |
| 80 | + OffsetDateTime since = instant.atZone(ZoneId.of("America/New_York")).toOffsetDateTime(); |
| 81 | + |
| 82 | + ReplayIn in = new ReplayIn(); |
| 83 | + in.setSince(since); |
| 84 | + svx.getEndpoint().replayMissing("ap","ep", in); |
| 85 | + |
| 86 | + wireMockRule.verify(1,postRequestedFor(urlEqualTo("/api/v1/app/ap/endpoint/ep/replay-missing")).withRequestBody(equalTo("{\"since\":\"2025-02-22T11:07:35.514412223-05:00\"}"))); |
| 87 | + |
| 88 | + |
| 89 | + } |
| 90 | + @Test public void testDateUTCRequestBody() throws Exception { |
| 91 | + Svix svx = testClient(); |
| 92 | + wireMockRule.stubFor(WireMock.post(urlEqualTo("/api/v1/app/ap/endpoint/ep/replay-missing")) |
| 93 | + .willReturn(WireMock.ok().withBodyFile("ReplayOut.json")) |
| 94 | + ); |
| 95 | + Instant instant = Instant.ofEpochSecond(1740240455, 514412223); |
| 96 | + OffsetDateTime since = instant.atOffset(ZoneOffset.UTC); |
| 97 | + |
| 98 | + ReplayIn in = new ReplayIn(); |
| 99 | + in.setSince(since); |
| 100 | + svx.getEndpoint().replayMissing("ap","ep", in); |
| 101 | + wireMockRule.verify(1,postRequestedFor(urlEqualTo("/api/v1/app/ap/endpoint/ep/replay-missing")).withRequestBody(equalTo("{\"since\":\"2025-02-22T16:07:35.514412223Z\"}"))); |
| 102 | + |
| 103 | + } |
| 104 | + @Test public void testDateResponseBody() throws Exception { |
| 105 | + Svix svx = testClient(); |
| 106 | + wireMockRule.stubFor(WireMock.get(urlMatching("/api/v1/app/ap/attempt/endpoint/endp?.*")) |
| 107 | + .willReturn(WireMock.ok().withBodyFile("ListResponseMessageAttemptOut.json")) |
| 108 | + ); |
| 109 | + |
| 110 | + Instant instant = Instant.ofEpochSecond(1740240455, 514412223); |
| 111 | + OffsetDateTime timestamp = instant.atOffset(ZoneOffset.UTC); |
| 112 | + ListResponseMessageAttemptOut out = svx.getMessageAttempt().listByEndpoint("ap", "endp"); |
| 113 | + |
| 114 | + assertEquals(timestamp,out.getData().get(0).getTimestamp()); |
| 115 | + } |
| 116 | + @Test public void testListResponseOutDeserializesCorrectly() throws Exception { |
| 117 | + Svix svx = testClient(); |
| 118 | + } |
| 119 | + @Test public void testEnumAsQueryParam() throws Exception { |
| 120 | + Svix svx = testClient(); |
| 121 | + } |
| 122 | + @Test public void testListAsQueryParam() throws Exception { |
| 123 | + Svix svx = testClient(); |
| 124 | + } |
| 125 | + @Test public void testHeaderParamSent() throws Exception { |
| 126 | + Svix svx = testClient(); |
| 127 | + } |
| 128 | + @Test public void testRetryForStatus500() throws Exception { |
| 129 | + Svix svx = testClient(); |
| 130 | + } |
| 131 | + @Test public void noBodyInResponseDoesNotReturnAnything() throws Exception { |
| 132 | + Svix svx = testClient(); |
| 133 | + } |
| 134 | + @Test public void status422ReturnsValidationError() throws Exception { |
| 135 | + Svix svx = testClient(); |
| 136 | + } |
| 137 | + @Test public void status400ReturnsApiException() throws Exception { |
| 138 | + Svix svx = testClient(); |
| 139 | + } |
| 140 | + @Test public void subResourceWorks() throws Exception { |
| 141 | + Svix svx = testClient(); |
| 142 | + } |
| 143 | + @Test public void integerEnumSerialization() throws Exception { |
| 144 | + Svix svx = testClient(); |
| 145 | + } |
| 146 | + @Test public void stringEnumDeSerialization() throws Exception { |
| 147 | + Svix svx = testClient(); |
| 148 | + } |
| 149 | + @Test public void nonCamelCaseFieldName() throws Exception { |
| 150 | + Svix svx = testClient(); |
| 151 | + } |
| 152 | + @Test public void patchRequestBody() throws Exception { |
| 153 | + Svix svx = testClient(); |
| 154 | + } |
| 155 | + @Test public void arbitraryJsonObjectBody() throws Exception { |
| 156 | + Svix svx = testClient(); |
| 157 | + } |
| 158 | + @Test public void tokenAndUserAgentIsSent() throws Exception { |
| 159 | + Svix svx = testClient(); |
| 160 | + } |
| 161 | +} |
0 commit comments