Browse Source

Handle null Principal in AuditEvent

See gh-11320
pull/11378/head
nklmish 8 years ago committed by Stephane Nicoll
parent
commit
6b6a01a7e7
  1. 4
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java
  2. 8
      spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java

4
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java

@ -41,6 +41,7 @@ import org.springframework.util.Assert; @@ -41,6 +41,7 @@ import org.springframework.util.Assert;
* (wrappers for AuditEvent).
*
* @author Dave Syer
* @author Nakul Mishra
* @see AuditEventRepository
*/
@JsonInclude(Include.NON_EMPTY)
@ -85,10 +86,9 @@ public class AuditEvent implements Serializable { @@ -85,10 +86,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);
}

8
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java

@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Dave Syer
* @author Vedran Pavic
* @author Nakul Mishra
*/
public class AuditEventTests {
@ -64,10 +65,9 @@ public class AuditEventTests { @@ -64,10 +65,9 @@ public class AuditEventTests {
}
@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"));
public void nullPrincipal() {
AuditEvent auditEvent = new AuditEvent(null, "UNKNOWN", Collections.singletonMap("a", (Object) "b"));
assertThat(auditEvent.getPrincipal()).isEmpty();
}
@Test

Loading…
Cancel
Save