Skip to content

Commit

Permalink
Date Tests need to handle delayed audit records (#5599)
Browse files Browse the repository at this point in the history
* Added another pause when setting up the data for the tests.
* Set the date tests to be a bit more robust to make sure that
   their data is present vs an absolute count check.
  • Loading branch information
cppwfs authored Dec 9, 2023
1 parent c40910e commit 107d46b
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
Expand All @@ -78,7 +79,9 @@
@AutoConfigureTestDatabase(replace = Replace.ANY)
public class AuditRecordControllerTests {

private static final int INITIAL_AUDIT_CREATE_COUNT = 7;
private static final int INITIAL_AUDIT_CREATE_COUNT = 6;

private static final int FULL_AUDIT_CREATE_COUNT = 7;

@Autowired
private StreamDefinitionRepository streamDefinitionRepository;
Expand Down Expand Up @@ -126,14 +129,16 @@ public void setupMocks() throws Exception {
mockMvc.perform(post("/streams/definitions/").param("name", "myStream1").param("definition", "time | log")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isCreated());

// Verify that the 4 app create and 2 stream create audit records have been recorded before setting the between date.
Awaitility.await().atMost(Duration.ofMillis(30000)).until(() -> auditRecordRepository.count() == INITIAL_AUDIT_CREATE_COUNT);

betweenDate = ZonedDateTime.now();

mockMvc.perform(post("/streams/definitions/").param("name", "myStream2").param("definition", "time | log")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isCreated());

// Verify that the 4 app create and 3 stream create audit records have been recorded before setting the end date.
Awaitility.await().atMost(Duration.ofMillis(30000)).until(() -> auditRecordRepository.count() == INITIAL_AUDIT_CREATE_COUNT);

Awaitility.await().atMost(Duration.ofMillis(30000)).until(() -> auditRecordRepository.count() == FULL_AUDIT_CREATE_COUNT);

endDate = ZonedDateTime.now();

Expand All @@ -159,7 +164,7 @@ public void tearDown() {
* {@link StreamService#undeployStream(String)} too.
*/
@Test
public void testVerifyNumberOfAuditRecords() throws Exception {
public void testVerifyNumberOfAuditRecords() {
assertEquals(4, appRegistrationRepository.count());
assertEquals(2, streamDefinitionRepository.count());
assertEquals(9, auditRecordRepository.count());
Expand Down Expand Up @@ -386,7 +391,7 @@ public void testRetrieveAuditRecordsFromGivenDateToNull() throws Exception {
mockMvc.perform(get("/audit-records?fromDate=" + fromDate).accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(2)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(greaterThanOrEqualTo(2))))

.andExpect(jsonPath("$._embedded.auditRecordResourceList[0].auditRecordId", is(12)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList[0].correlationId", is("myStream")))
Expand All @@ -409,7 +414,7 @@ public void testRetrieveAuditRecordsBetweenTwoGivenDates() throws Exception {
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(1)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(greaterThanOrEqualTo(1))))

.andExpect(jsonPath("$._embedded.auditRecordResourceList[0].auditRecordId", is(11)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList[0].correlationId", is("myStream2")))
Expand All @@ -434,7 +439,7 @@ public void testRetrieveAuditRecordsBetweenTwoNullDates() throws Exception {
mockMvc.perform(get("/audit-records").accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(9)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(greaterThanOrEqualTo(9))))
.andExpect(jsonPath("$._embedded.auditRecordResourceList[4].auditRecordId", is(9)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList[4].correlationId", is("myStream")))
.andExpect(jsonPath("$._embedded.auditRecordResourceList[4].auditAction", is("CREATE")));
Expand Down Expand Up @@ -534,7 +539,7 @@ public void testRetrieveDataByOperationsAndActionsAndDate() throws Exception {
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(2)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList.*", hasSize(greaterThanOrEqualTo(2))))

.andExpect(jsonPath("$._embedded.auditRecordResourceList[0].auditRecordId", is(9)))
.andExpect(jsonPath("$._embedded.auditRecordResourceList[0].correlationId", is("myStream")))
Expand Down

0 comments on commit 107d46b

Please sign in to comment.