Commit 788fd777 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #11320 from nklmish:gh-11241

* pr/11320:
  Polish "Handle null Principal in AuditEvent"
  Handle null Principal in AuditEvent
parents 37437a0f 7fb0bd77
...@@ -85,10 +85,9 @@ public class AuditEvent implements Serializable { ...@@ -85,10 +85,9 @@ public class AuditEvent implements Serializable {
public AuditEvent(Date timestamp, String principal, String type, public AuditEvent(Date timestamp, String principal, String type,
Map<String, Object> data) { Map<String, Object> data) {
Assert.notNull(timestamp, "Timestamp must not be null"); Assert.notNull(timestamp, "Timestamp must not be null");
Assert.notNull(principal, "Principal must not be null");
Assert.notNull(type, "Type must not be null"); Assert.notNull(type, "Type must not be null");
this.timestamp = timestamp; this.timestamp = timestamp;
this.principal = principal; this.principal = principal != null ? principal : "";
this.type = type; this.type = type;
this.data = Collections.unmodifiableMap(data); this.data = Collections.unmodifiableMap(data);
} }
...@@ -117,7 +116,8 @@ public class AuditEvent implements Serializable { ...@@ -117,7 +116,8 @@ public class AuditEvent implements Serializable {
} }
/** /**
* Returns the user principal responsible for the event. * Returns the user principal responsible for the event or an empty String if the
* principal is not available.
* @return the principal * @return the principal
*/ */
public String getPrincipal() { public String getPrincipal() {
......
...@@ -56,18 +56,18 @@ public class AuditEventTests { ...@@ -56,18 +56,18 @@ public class AuditEventTests {
} }
@Test @Test
public void nullTimestamp() throws Exception { public void nullPrincipalIsMappedToEmptyString() {
this.thrown.expect(IllegalArgumentException.class); AuditEvent auditEvent = new AuditEvent(null, "UNKNOWN",
this.thrown.expectMessage("Timestamp must not be null");
new AuditEvent(null, "phil", "UNKNOWN",
Collections.singletonMap("a", (Object) "b")); Collections.singletonMap("a", (Object) "b"));
assertThat(auditEvent.getPrincipal()).isEmpty();
} }
@Test @Test
public void nullPrincipal() throws Exception { public void nullTimestamp() throws Exception {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Principal must not be null"); this.thrown.expectMessage("Timestamp must not be null");
new AuditEvent(null, "UNKNOWN", Collections.singletonMap("a", (Object) "b")); new AuditEvent(null, "phil", "UNKNOWN",
Collections.singletonMap("a", (Object) "b"));
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment