Merge pull request #11320 from nklmish:gh-11241

* pr/11320:
  Polish "Handle null Principal in AuditEvent"
  Handle null Principal in AuditEvent
This commit is contained in:
Stephane Nicoll 2017-12-12 08:45:42 +01:00
commit 788fd777b0
2 changed files with 10 additions and 10 deletions

View File

@ -85,10 +85,9 @@ public class AuditEvent implements Serializable {
public AuditEvent(Date timestamp, String principal, String type,
Map<String, Object> data) {
Assert.notNull(timestamp, "Timestamp must not be null");
Assert.notNull(principal, "Principal must not be null");
Assert.notNull(type, "Type must not be null");
this.timestamp = timestamp;
this.principal = principal;
this.principal = principal != null ? principal : "";
this.type = type;
this.data = Collections.unmodifiableMap(data);
}
@ -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
*/
public String getPrincipal() {

View File

@ -55,6 +55,13 @@ public class AuditEventTests {
assertThat(event.getData().get("c")).isEqualTo("d");
}
@Test
public void nullPrincipalIsMappedToEmptyString() {
AuditEvent auditEvent = new AuditEvent(null, "UNKNOWN",
Collections.singletonMap("a", (Object) "b"));
assertThat(auditEvent.getPrincipal()).isEmpty();
}
@Test
public void nullTimestamp() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
@ -63,13 +70,6 @@ public class AuditEventTests {
Collections.singletonMap("a", (Object) "b"));
}
@Test
public void nullPrincipal() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Principal must not be null");
new AuditEvent(null, "UNKNOWN", Collections.singletonMap("a", (Object) "b"));
}
@Test
public void nullType() throws Exception {
this.thrown.expect(IllegalArgumentException.class);