|
| 1 | +package org.jds.edgar4j.controller; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.hasSize; |
| 4 | + |
| 5 | +import java.util.Calendar; |
| 6 | +import java.util.Date; |
| 7 | + |
| 8 | +import org.jds.edgar4j.model.Form3; |
| 9 | +import org.jds.edgar4j.repository.Form3Repository; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Nested; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 18 | +import org.springframework.test.context.ActiveProfiles; |
| 19 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 20 | + |
| 21 | +/** |
| 22 | + * Integration tests for Form3Controller REST endpoints. |
| 23 | + * Uses WebTestClient and embedded MongoDB. |
| 24 | + */ |
| 25 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 26 | +@ActiveProfiles("test") |
| 27 | +class Form3ControllerTest { |
| 28 | + |
| 29 | + private WebTestClient webTestClient; |
| 30 | + |
| 31 | + @LocalServerPort |
| 32 | + private int port; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private Form3Repository repository; |
| 36 | + |
| 37 | + private static final String BASE_URL = "/api/form3"; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUp() { |
| 41 | + webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build(); |
| 42 | + repository.deleteAll(); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterEach |
| 46 | + void tearDown() { |
| 47 | + repository.deleteAll(); |
| 48 | + } |
| 49 | + |
| 50 | + private Form3 createForm3(String accessionNumber, String cik, String symbol, Date filedDate) { |
| 51 | + return Form3.builder() |
| 52 | + .accessionNumber(accessionNumber) |
| 53 | + .cik(cik) |
| 54 | + .issuerName("ACME CORP") |
| 55 | + .tradingSymbol(symbol) |
| 56 | + .documentType("3") |
| 57 | + .filedDate(filedDate) |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + @Nested |
| 62 | + @DisplayName("GET /api/form3/{id}") |
| 63 | + class GetById { |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("should return Form3 when found by ID") |
| 67 | + void shouldReturnForm3WhenFound() { |
| 68 | + Form3 saved = repository.save(createForm3("0000555555-25-000001", "0000555555", "ACME", new Date())); |
| 69 | + |
| 70 | + webTestClient.get() |
| 71 | + .uri(BASE_URL + "/" + saved.getId()) |
| 72 | + .exchange() |
| 73 | + .expectStatus().isOk() |
| 74 | + .expectBody() |
| 75 | + .jsonPath("$.accessionNumber").isEqualTo("0000555555-25-000001") |
| 76 | + .jsonPath("$.tradingSymbol").isEqualTo("ACME"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("should return 404 when not found") |
| 81 | + void shouldReturn404WhenNotFound() { |
| 82 | + webTestClient.get() |
| 83 | + .uri(BASE_URL + "/nonexistent-id") |
| 84 | + .exchange() |
| 85 | + .expectStatus().isNotFound(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Nested |
| 90 | + @DisplayName("GET /api/form3/accession/{accessionNumber}") |
| 91 | + class GetByAccessionNumber { |
| 92 | + |
| 93 | + @Test |
| 94 | + @DisplayName("should return Form3 by accession number") |
| 95 | + void shouldReturnByAccessionNumber() { |
| 96 | + repository.save(createForm3("0000555555-25-000002", "0000555555", "ACME", new Date())); |
| 97 | + |
| 98 | + webTestClient.get() |
| 99 | + .uri(BASE_URL + "/accession/0000555555-25-000002") |
| 100 | + .exchange() |
| 101 | + .expectStatus().isOk() |
| 102 | + .expectBody() |
| 103 | + .jsonPath("$.cik").isEqualTo("0000555555"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Nested |
| 108 | + @DisplayName("GET /api/form3/cik/{cik}") |
| 109 | + class GetByCik { |
| 110 | + |
| 111 | + @Test |
| 112 | + @DisplayName("should return paginated Form3 by CIK") |
| 113 | + void shouldReturnPaginatedByCik() { |
| 114 | + repository.save(createForm3("0000555555-25-000003", "0000555555", "ACME", new Date())); |
| 115 | + repository.save(createForm3("0000555555-25-000004", "0000555555", "ACME", new Date())); |
| 116 | + |
| 117 | + webTestClient.get() |
| 118 | + .uri(uriBuilder -> uriBuilder |
| 119 | + .path(BASE_URL + "/cik/0000555555") |
| 120 | + .queryParam("page", "0") |
| 121 | + .queryParam("size", "10") |
| 122 | + .build()) |
| 123 | + .exchange() |
| 124 | + .expectStatus().isOk() |
| 125 | + .expectBody() |
| 126 | + .jsonPath("$.content").value(hasSize(2)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Nested |
| 131 | + @DisplayName("GET /api/form3/symbol/{symbol}") |
| 132 | + class GetBySymbol { |
| 133 | + |
| 134 | + @Test |
| 135 | + @DisplayName("should handle case insensitive symbol") |
| 136 | + void shouldHandleCaseInsensitiveSymbol() { |
| 137 | + repository.save(createForm3("0000555555-25-000005", "0000555555", "ACME", new Date())); |
| 138 | + |
| 139 | + webTestClient.get() |
| 140 | + .uri(BASE_URL + "/symbol/acme") |
| 141 | + .exchange() |
| 142 | + .expectStatus().isOk() |
| 143 | + .expectBody() |
| 144 | + .jsonPath("$.content").value(hasSize(1)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Nested |
| 149 | + @DisplayName("GET /api/form3/date-range") |
| 150 | + class GetByDateRange { |
| 151 | + |
| 152 | + @Test |
| 153 | + @DisplayName("should return 400 for invalid date format") |
| 154 | + void shouldReturn400ForInvalidDateFormat() { |
| 155 | + webTestClient.get() |
| 156 | + .uri(uriBuilder -> uriBuilder |
| 157 | + .path(BASE_URL + "/date-range") |
| 158 | + .queryParam("startDate", "invalid-date") |
| 159 | + .queryParam("endDate", "2025-11-20") |
| 160 | + .build()) |
| 161 | + .exchange() |
| 162 | + .expectStatus().isBadRequest(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + @DisplayName("should return filings within date range") |
| 167 | + void shouldReturnByDateRange() { |
| 168 | + Calendar cal = Calendar.getInstance(); |
| 169 | + cal.set(2025, Calendar.NOVEMBER, 5); |
| 170 | + Date nov5 = cal.getTime(); |
| 171 | + |
| 172 | + repository.save(createForm3("0000555555-25-000006", "0000555555", "ACME", nov5)); |
| 173 | + |
| 174 | + webTestClient.get() |
| 175 | + .uri(uriBuilder -> uriBuilder |
| 176 | + .path(BASE_URL + "/date-range") |
| 177 | + .queryParam("startDate", "2025-11-01") |
| 178 | + .queryParam("endDate", "2025-11-10") |
| 179 | + .build()) |
| 180 | + .exchange() |
| 181 | + .expectStatus().isOk() |
| 182 | + .expectBody() |
| 183 | + .jsonPath("$.content").value(hasSize(1)); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Nested |
| 188 | + @DisplayName("GET /api/form3/recent") |
| 189 | + class GetRecent { |
| 190 | + |
| 191 | + @Test |
| 192 | + @DisplayName("should return recent filings") |
| 193 | + void shouldReturnRecentFilings() { |
| 194 | + repository.save(createForm3("0000555555-25-000007", "0000555555", "ACME", new Date())); |
| 195 | + repository.save(createForm3("0000555555-25-000008", "0000555555", "ACME", new Date())); |
| 196 | + |
| 197 | + webTestClient.get() |
| 198 | + .uri(BASE_URL + "/recent?limit=10") |
| 199 | + .exchange() |
| 200 | + .expectStatus().isOk() |
| 201 | + .expectBody() |
| 202 | + .jsonPath("$").value(hasSize(2)); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
0 commit comments