Search in sources :

Example 1 with PersistentAuditEvent

use of com.arnaugarcia.uplace.domain.PersistentAuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method addAuditEventWithAnonymousUser.

@Test
public void addAuditEventWithAnonymousUser() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent(Constants.ANONYMOUS_USER, "test-type", 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 2 with PersistentAuditEvent

use of com.arnaugarcia.uplace.domain.PersistentAuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method findByPrincipalAndType.

@Test
public void findByPrincipalAndType() {
    persistenceAuditEventRepository.save(testUserEvent);
    persistenceAuditEventRepository.save(testOldUserEvent);
    testOtherUserEvent.setAuditEventType(testUserEvent.getAuditEventType());
    persistenceAuditEventRepository.save(testOtherUserEvent);
    PersistentAuditEvent testUserOtherTypeEvent = new PersistentAuditEvent();
    testUserOtherTypeEvent.setPrincipal(testUserEvent.getPrincipal());
    testUserOtherTypeEvent.setAuditEventType("test-other-type");
    testUserOtherTypeEvent.setAuditEventDate(testUserEvent.getAuditEventDate());
    persistenceAuditEventRepository.save(testUserOtherTypeEvent);
    List<AuditEvent> events = customAuditEventRepository.find("test-user", Date.from(testUserEvent.getAuditEventDate().minusSeconds(3600)), "test-type");
    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 : PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) 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 3 with PersistentAuditEvent

use of com.arnaugarcia.uplace.domain.PersistentAuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method testAddEventWithNullData.

@Test
public void testAddEventWithNullData() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", null);
    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.getData().get("test-key")).isEqualTo("null");
}
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 4 with PersistentAuditEvent

use of com.arnaugarcia.uplace.domain.PersistentAuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method setup.

@Before
public void setup() {
    customAuditEventRepository = new CustomAuditEventRepository(persistenceAuditEventRepository, auditEventConverter);
    persistenceAuditEventRepository.deleteAll();
    Instant oneHourAgo = Instant.now().minusSeconds(3600);
    testUserEvent = new PersistentAuditEvent();
    testUserEvent.setPrincipal("test-user");
    testUserEvent.setAuditEventType("test-type");
    testUserEvent.setAuditEventDate(oneHourAgo);
    Map<String, String> data = new HashMap<>();
    data.put("test-key", "test-value");
    testUserEvent.setData(data);
    testOldUserEvent = new PersistentAuditEvent();
    testOldUserEvent.setPrincipal("test-user");
    testOldUserEvent.setAuditEventType("test-type");
    testOldUserEvent.setAuditEventDate(oneHourAgo.minusSeconds(10000));
    testOtherUserEvent = new PersistentAuditEvent();
    testOtherUserEvent.setPrincipal("other-test-user");
    testOtherUserEvent.setAuditEventType("test-type");
    testOtherUserEvent.setAuditEventDate(oneHourAgo);
}
Also used : PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) HashMap(java.util.HashMap) Instant(java.time.Instant) Before(org.junit.Before)

Example 5 with PersistentAuditEvent

use of com.arnaugarcia.uplace.domain.PersistentAuditEvent in project uplace.es by Uplace.

the class CustomAuditEventRepositoryIntTest method testAddEventWithWebAuthenticationDetails.

@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    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.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
Also used : PersistentAuditEvent(com.arnaugarcia.uplace.domain.PersistentAuditEvent) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) MockHttpSession(org.springframework.mock.web.MockHttpSession) 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

PersistentAuditEvent (com.arnaugarcia.uplace.domain.PersistentAuditEvent)10 HashMap (java.util.HashMap)7 Test (org.junit.Test)7 AuditEvent (org.springframework.boot.actuate.audit.AuditEvent)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 Before (org.junit.Before)2 Instant (java.time.Instant)1 HttpSession (javax.servlet.http.HttpSession)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpSession (org.springframework.mock.web.MockHttpSession)1 WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)1 Transactional (org.springframework.transaction.annotation.Transactional)1