Fix InMemoryAuditEventRepository search by date
Update InMemoryAuditEventRepository to consider the date when searching for events. Also switch to a circular buffer implementation and update the capacity to limit the total number of items rather than limiting per principal. Fixes gh-2291
This commit is contained in:
@@ -16,30 +16,85 @@
|
||||
|
||||
package org.springframework.boot.actuate.audit;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link InMemoryAuditEventRepository}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class InMemoryAuditEventRepositoryTests {
|
||||
|
||||
private final InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
|
||||
@Test
|
||||
public void lessThanCapacity() throws Exception {
|
||||
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
|
||||
repository.add(new AuditEvent("dave", "a"));
|
||||
repository.add(new AuditEvent("dave", "b"));
|
||||
List<AuditEvent> events = repository.find("dave", null);
|
||||
assertThat(events.size(), equalTo(2));
|
||||
assertThat(events.get(0).getType(), equalTo("a"));
|
||||
assertThat(events.get(1).getType(), equalTo("b"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToCapacity() throws Exception {
|
||||
this.repository.setCapacity(2);
|
||||
this.repository.add(new AuditEvent("phil", "UNKNOWN"));
|
||||
this.repository.add(new AuditEvent("phil", "UNKNOWN"));
|
||||
this.repository.add(new AuditEvent("dave", "UNKNOWN"));
|
||||
this.repository.add(new AuditEvent("dave", "UNKNOWN"));
|
||||
this.repository.add(new AuditEvent("phil", "UNKNOWN"));
|
||||
assertEquals(2, this.repository.find("phil", new Date(0L)).size());
|
||||
public void capacity() throws Exception {
|
||||
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository(2);
|
||||
repository.add(new AuditEvent("dave", "a"));
|
||||
repository.add(new AuditEvent("dave", "b"));
|
||||
repository.add(new AuditEvent("dave", "c"));
|
||||
List<AuditEvent> events = repository.find("dave", null);
|
||||
assertThat(events.size(), equalTo(2));
|
||||
assertThat(events.get(0).getType(), equalTo("b"));
|
||||
assertThat(events.get(1).getType(), equalTo("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByPrincipal() throws Exception {
|
||||
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
|
||||
repository.add(new AuditEvent("dave", "a"));
|
||||
repository.add(new AuditEvent("phil", "b"));
|
||||
repository.add(new AuditEvent("dave", "c"));
|
||||
repository.add(new AuditEvent("phil", "d"));
|
||||
List<AuditEvent> events = repository.find("dave", null);
|
||||
assertThat(events.size(), equalTo(2));
|
||||
assertThat(events.get(0).getType(), equalTo("a"));
|
||||
assertThat(events.get(1).getType(), equalTo("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByDate() throws Exception {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2000, 1, 1, 0, 0, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
Map<String, Object> data = new HashMap<String, Object>();
|
||||
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
|
||||
repository.add(new AuditEvent(calendar.getTime(), "dave", "a", data));
|
||||
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
||||
repository.add(new AuditEvent(calendar.getTime(), "phil", "b", data));
|
||||
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
||||
Date after = calendar.getTime();
|
||||
repository.add(new AuditEvent(calendar.getTime(), "dave", "c", data));
|
||||
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
||||
repository.add(new AuditEvent(calendar.getTime(), "phil", "d", data));
|
||||
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
||||
List<AuditEvent> events = repository.find(null, after);
|
||||
assertThat(events.size(), equalTo(2));
|
||||
assertThat(events.get(0).getType(), equalTo("c"));
|
||||
assertThat(events.get(1).getType(), equalTo("d"));
|
||||
events = repository.find("dave", after);
|
||||
assertThat(events.size(), equalTo(1));
|
||||
assertThat(events.get(0).getType(), equalTo("c"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user