Search in sources :

Example 11 with AuditEvent

use of org.springframework.boot.actuate.audit.AuditEvent in project spring-boot by spring-projects.

the class AuditListenerTests method testStoredEvents.

@Test
public void testStoredEvents() {
    AuditEventRepository repository = mock(AuditEventRepository.class);
    AuditEvent event = new AuditEvent("principal", "type", Collections.<String, Object>emptyMap());
    AuditListener listener = new AuditListener(repository);
    listener.onApplicationEvent(new AuditApplicationEvent(event));
    verify(repository).add(event);
}
Also used : AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) AuditEventRepository(org.springframework.boot.actuate.audit.AuditEventRepository) Test(org.junit.Test)

Example 12 with AuditEvent

use of org.springframework.boot.actuate.audit.AuditEvent in project uplace.es by Uplace.

the class AuditResource method getByDates.

/**
 * GET  /audits : get a page of AuditEvents between the fromDate and toDate.
 *
 * @param fromDate the start of the time period of AuditEvents to get
 * @param toDate the end of the time period of AuditEvents to get
 * @param pageable the pagination information
 * @return the ResponseEntity with status 200 (OK) and the list of AuditEvents in body
 */
@GetMapping(params = { "fromDate", "toDate" })
public ResponseEntity<List<AuditEvent>> getByDates(@RequestParam(value = "fromDate") LocalDate fromDate, @RequestParam(value = "toDate") LocalDate toDate, Pageable pageable) {
    Page<AuditEvent> page = auditEventService.findByDates(fromDate.atStartOfDay(ZoneId.systemDefault()).toInstant(), toDate.atStartOfDay(ZoneId.systemDefault()).plusDays(1).toInstant(), pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/management/audits");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) AuditEvent(org.springframework.boot.actuate.audit.AuditEvent)

Example 13 with AuditEvent

use of org.springframework.boot.actuate.audit.AuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method addAuditEventWithAuthorizationFailureType.

@Test
public void addAuditEventWithAuthorizationFailureType() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "AUTHORIZATION_FAILURE", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(0);
}
Also used : PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) HashMap(java.util.HashMap) AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 14 with AuditEvent

use of org.springframework.boot.actuate.audit.AuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method testFindByPrincipal.

@Test
public void testFindByPrincipal() {
    persistenceAuditEventRepository.save(testUserEvent);
    persistenceAuditEventRepository.save(testOldUserEvent);
    persistenceAuditEventRepository.save(testOtherUserEvent);
    List<AuditEvent> events = customAuditEventRepository.find("test-user", Date.from(testUserEvent.getAuditEventDate().minusSeconds(3600)));
    assertThat(events).hasSize(1);
    AuditEvent event = events.get(0);
    assertThat(event.getPrincipal()).isEqualTo(testUserEvent.getPrincipal());
    assertThat(event.getType()).isEqualTo(testUserEvent.getAuditEventType());
    assertThat(event.getData()).containsKey("test-key");
    assertThat(event.getData().get("test-key").toString()).isEqualTo("test-value");
    assertThat(event.getTimestamp()).isEqualTo(Date.from(testUserEvent.getAuditEventDate()));
}
Also used : AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 15 with AuditEvent

use of org.springframework.boot.actuate.audit.AuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method addAuditEventTruncateLargeData.

@Test
public void addAuditEventTruncateLargeData() {
    Map<String, Object> data = new HashMap<>();
    StringBuilder largeData = new StringBuilder();
    for (int i = 0; i < EVENT_DATA_COLUMN_MAX_LENGTH + 10; i++) {
        largeData.append("a");
    }
    data.put("test-key", largeData);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    String actualData = persistentAuditEvent.getData().get("test-key");
    assertThat(actualData.length()).isEqualTo(EVENT_DATA_COLUMN_MAX_LENGTH);
    assertThat(actualData).isSubstringOf(largeData);
    assertThat(persistentAuditEvent.getAuditEventDate()).isEqualTo(event.getTimestamp().toInstant());
}
Also used : PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) HashMap(java.util.HashMap) AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

AuditEvent (org.springframework.boot.actuate.audit.AuditEvent)19 HashMap (java.util.HashMap)12 Test (org.junit.Test)10 PersistentAuditEvent (com.arnaugarcia.uplace.domain.PersistentAuditEvent)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 ReflectiveMethodInvocation (org.springframework.aop.framework.ReflectiveMethodInvocation)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 FilterInvocation (org.springframework.security.web.FilterInvocation)2 WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)2 HttpSession (javax.servlet.http.HttpSession)1 AuditEventRepository (org.springframework.boot.actuate.audit.AuditEventRepository)1 EventListener (org.springframework.context.event.EventListener)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpSession (org.springframework.mock.web.MockHttpSession)1