Implement and extend unit tests for @CreatedBy.
Signed-off-by: Juri Berlanda <juriberlanda@hotmail.com>
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java
index 7422573..78d8b32 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/audit/PrincipalProvider.java
@@ -89,7 +89,7 @@
return true;
}
- private Object resolvePrincipal(Object entity, Property<Object> property)
+ protected Object resolvePrincipal(Object entity, Property<Object> property)
{
CurrentUser principal = AnnotationInstanceProvider.of(CurrentUser.class);
Class<?> propertyClass = property.getJavaClass();
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java
index 4c1d2ee..2e0a1fc 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/AuditEntityListenerTest.java
@@ -21,6 +21,7 @@
import static org.apache.deltaspike.data.test.util.TestDeployments.initDeployment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import javax.enterprise.inject.Produces;
@@ -129,4 +130,26 @@
assertEquals(who, entity.getChangerOnlyPrincipal().getName());
}
+ @Test
+ public void should_set_creating_principal()
+ {
+ // given
+ AuditedEntity entity = new AuditedEntity();
+
+ // when
+ getEntityManager().persist(entity);
+ getEntityManager().flush();
+
+ // then
+ assertNotNull(entity.getCreator());
+ assertEquals(who, entity.getCreator());
+ assertNotNull(entity.getCreatorPrincipal());
+ assertEquals(who, entity.getCreatorPrincipal().getName());
+ assertNotNull(entity.getChanger());
+ assertEquals(who, entity.getChanger());
+ assertNotNull(entity.getPrincipal());
+ assertEquals(who, entity.getPrincipal().getName());
+ assertNull(entity.getChangerOnly());
+ assertNull(entity.getChangerOnlyPrincipal());
+ }
}
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/PrincipalProviderTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/PrincipalProviderTest.java
new file mode 100644
index 0000000..1672a62
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/audit/PrincipalProviderTest.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.audit;
+
+import org.apache.deltaspike.data.api.audit.CreatedBy;
+import org.apache.deltaspike.data.impl.property.Property;
+import org.apache.deltaspike.data.test.domain.AuditedEntity;
+import org.apache.deltaspike.data.test.domain.Principal;
+import org.apache.deltaspike.data.test.domain.Simple;
+import org.junit.Test;
+
+import java.util.Date;
+
+import static org.junit.Assert.*;
+
+public class PrincipalProviderTest {
+
+ public static class MockPrincipalProvider extends PrincipalProvider {
+ private final String who;
+
+ MockPrincipalProvider(String who) {
+ this.who = who;
+ }
+
+ @Override
+ protected Object resolvePrincipal(Object entity, Property<Object> property)
+ {
+ if (property.getJavaClass().isAssignableFrom(Principal.class))
+ {
+ return new Principal(who);
+ }
+ return who;
+ }
+ }
+
+ @Test
+ public void should_set_users_for_creation()
+ {
+ // given
+ String creator = "creator";
+ MockPrincipalProvider provider = new MockPrincipalProvider(creator);
+ AuditedEntity entity = new AuditedEntity();
+
+ // when
+ provider.prePersist(entity);
+
+ // then
+ assertNotNull(entity.getCreator());
+ assertNotNull(entity.getCreatorPrincipal());
+ assertNotNull(entity.getChanger());
+ assertEquals(entity.getCreator(), creator);
+ assertEquals(entity.getCreatorPrincipal().getName(), creator);
+ assertEquals(entity.getChanger(), creator);
+ assertNull(entity.getChangerOnly());
+ assertNull(entity.getChangerOnlyPrincipal());
+ }
+
+ @Test
+ public void should_set_users_for_update()
+ {
+ // given
+ String changer = "changer";
+ MockPrincipalProvider provider = new MockPrincipalProvider(changer);
+ AuditedEntity entity = new AuditedEntity();
+
+ // when
+ provider.preUpdate(entity);
+
+ // then
+ assertNotNull(entity.getChanger());
+ assertNotNull(entity.getChangerOnly());
+ assertNotNull(entity.getChangerOnlyPrincipal());
+ assertEquals(entity.getChanger(), changer);
+ assertEquals(entity.getChangerOnly(), changer);
+ assertEquals(entity.getChangerOnlyPrincipal().getName(), changer);
+ assertNull(entity.getCreator());
+ assertNull(entity.getCreatorPrincipal());
+ }
+
+ @Test
+ public void should_not_fail_on_non_audited_entity()
+ {
+ // given
+ Simple entity = new Simple("should_not_fail_on_non_audited_entity");
+
+ // when
+ PrincipalProvider provider = new MockPrincipalProvider("");
+ provider.prePersist(entity);
+ provider.preUpdate(entity);
+
+ // then finish the test
+ }
+
+ @Test(expected = AuditPropertyException.class)
+ public void should_fail_on_invalid_entity()
+ {
+ // given
+ PrincipalProviderTest.InvalidEntity entity = new PrincipalProviderTest.InvalidEntity();
+
+ // when
+ new MockPrincipalProvider("").prePersist(entity);
+
+ // then
+ fail();
+ }
+
+ private static class InvalidEntity
+ {
+ @CreatedBy
+ private Date nonUser;
+ }
+}
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/domain/AuditedEntity.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/domain/AuditedEntity.java
index 99e2b6b..c747e73 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/domain/AuditedEntity.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/domain/AuditedEntity.java
@@ -31,6 +31,7 @@
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
+import org.apache.deltaspike.data.api.audit.CreatedBy;
import org.apache.deltaspike.data.api.audit.CreatedOn;
import org.apache.deltaspike.data.api.audit.ModifiedBy;
import org.apache.deltaspike.data.api.audit.ModifiedOn;
@@ -50,6 +51,13 @@
private String name;
+ @CreatedBy
+ private String creator;
+
+ @CreatedBy
+ @ManyToOne(targetEntity = Principal.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
+ private Principal creatorPrincipal;
+
@ModifiedBy
private String changer;
@@ -134,6 +142,14 @@
return principal;
}
+ public String getCreator() {
+ return creator;
+ }
+
+ public Principal getCreatorPrincipal() {
+ return creatorPrincipal;
+ }
+
public String getChangerOnly() {
return changerOnly;
}