[JAVA-8983] Fix envers integration test

This commit is contained in:
Haroon Khan 2021-12-24 00:06:01 +00:00
parent a7e7caaadf
commit 7bce366f78
3 changed files with 44 additions and 70 deletions

View File

@ -1,8 +1,14 @@
package com.baeldung.persistence.model; package com.baeldung.persistence.model;
import java.io.Serializable; import com.google.common.collect.Sets;
import java.util.Date; import org.hibernate.annotations.OrderBy;
import java.util.Set; import org.hibernate.envers.Audited;
import org.jboss.logging.Logger;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
@ -17,17 +23,9 @@ import javax.persistence.OneToMany;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.PreRemove; import javax.persistence.PreRemove;
import javax.persistence.PreUpdate; import javax.persistence.PreUpdate;
import java.io.Serializable;
import org.hibernate.annotations.OrderBy; import java.util.Date;
import org.hibernate.envers.Audited; import java.util.Set;
import org.jboss.logging.Logger;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.google.common.collect.Sets;
@Entity @Entity
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") @NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
@ -35,11 +33,11 @@ import com.google.common.collect.Sets;
@EntityListeners(AuditingEntityListener.class) @EntityListeners(AuditingEntityListener.class)
public class Bar implements Serializable { public class Bar implements Serializable {
private static Logger logger = Logger.getLogger(Bar.class); private static final Logger LOGGER = Logger.getLogger(Bar.class);
public enum OPERATION { public enum OPERATION {
INSERT, UPDATE, DELETE; INSERT, UPDATE, DELETE;
private String value; private final String value;
OPERATION() { OPERATION() {
value = toString(); value = toString();
@ -70,7 +68,7 @@ public class Bar implements Serializable {
private String name; private String name;
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OrderBy(clause = "NAME DESC") @OrderBy(clause = "name DESC")
// @NotAudited // @NotAudited
private Set<Foo> fooSet = Sets.newHashSet(); private Set<Foo> fooSet = Sets.newHashSet();
@ -102,7 +100,6 @@ public class Bar implements Serializable {
public Bar(final String name) { public Bar(final String name) {
super(); super();
this.name = name; this.name = name;
} }
@ -202,35 +199,31 @@ public class Bar implements Serializable {
return false; return false;
final Bar other = (Bar) obj; final Bar other = (Bar) obj;
if (name == null) { if (name == null) {
if (other.name != null) return other.name == null;
return false; } else
} else if (!name.equals(other.name)) return name.equals(other.name);
return false;
return true;
} }
@Override @Override
public String toString() { public String toString() {
final StringBuilder builder = new StringBuilder(); return "Bar [name=" + name + "]";
builder.append("Bar [name=").append(name).append("]");
return builder.toString();
} }
@PrePersist @PrePersist
public void onPrePersist() { public void onPrePersist() {
logger.info("@PrePersist"); LOGGER.info("@PrePersist");
audit(OPERATION.INSERT); audit(OPERATION.INSERT);
} }
@PreUpdate @PreUpdate
public void onPreUpdate() { public void onPreUpdate() {
logger.info("@PreUpdate"); LOGGER.info("@PreUpdate");
audit(OPERATION.UPDATE); audit(OPERATION.UPDATE);
} }
@PreRemove @PreRemove
public void onPreRemove() { public void onPreRemove() {
logger.info("@PreRemove"); LOGGER.info("@PreRemove");
audit(OPERATION.DELETE); audit(OPERATION.DELETE);
} }

View File

@ -16,7 +16,10 @@ import javax.persistence.NamedNativeQuery;
import org.hibernate.envers.Audited; import org.hibernate.envers.Audited;
@NamedNativeQueries({ @NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) }) @NamedNativeQueries({
@NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class),
@NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class)
})
@Entity @Entity
@Audited @Audited
// @Proxy(lazy = false) // @Proxy(lazy = false)
@ -89,17 +92,13 @@ public class Foo implements Serializable {
return false; return false;
final Foo other = (Foo) obj; final Foo other = (Foo) obj;
if (name == null) { if (name == null) {
if (other.name != null) return other.name == null;
return false; } else
} else if (!name.equals(other.name)) return name.equals(other.name);
return false;
return true;
} }
@Override @Override
public String toString() { public String toString() {
final StringBuilder builder = new StringBuilder(); return "Foo [name=" + name + "]";
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
} }
} }

View File

@ -1,18 +1,14 @@
package com.baeldung.persistence.audit; package com.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals; import com.baeldung.persistence.model.Bar;
import static org.junit.Assert.assertNotNull; import com.baeldung.persistence.model.Foo;
import static org.junit.Assert.assertTrue; import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IFooAuditableService;
import java.util.List; import com.baeldung.spring.config.PersistenceTestConfig;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -24,28 +20,17 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Bar; import java.util.List;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IBarAuditableService; import static org.junit.Assert.assertEquals;
import com.baeldung.persistence.service.IFooAuditableService; import static org.junit.Assert.assertNotNull;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS) @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
public class EnversFooBarAuditIntegrationTest { public class EnversFooBarAuditIntegrationTest {
private static Logger logger = LoggerFactory.getLogger(EnversFooBarAuditIntegrationTest.class); private static final Logger LOGGER = LoggerFactory.getLogger(EnversFooBarAuditIntegrationTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired @Autowired
@Qualifier("fooHibernateAuditableService") @Qualifier("fooHibernateAuditableService")
@ -61,15 +46,15 @@ public class EnversFooBarAuditIntegrationTest {
private Session session; private Session session;
@Before @Before
public void setUp() throws Exception { public void setUp() {
logger.info("setUp()"); LOGGER.info("setUp()");
makeRevisions(); makeRevisions();
session = sessionFactory.openSession(); session = sessionFactory.openSession();
} }
@After @After
public void tearDown() throws Exception { public void tearDown() {
logger.info("tearDown()"); LOGGER.info("tearDown()");
session.close(); session.close();
} }
@ -98,20 +83,17 @@ public class EnversFooBarAuditIntegrationTest {
// REV #3: update BAR // REV #3: update BAR
private void rev3(final Bar bar) { private void rev3(final Bar bar) {
bar.setName("BAR1"); bar.setName("BAR1");
barService.update(bar); barService.update(bar);
} }
// REV #4: insert FOO3 & update BAR // REV #4: insert FOO3 & update BAR
private void rev4(final Bar bar) { private void rev4(final Bar bar) {
final Foo foo3 = new Foo("FOO3"); final Foo foo3 = new Foo("FOO3");
foo3.setBar(bar); foo3.setBar(bar);
fooService.create(foo3); fooService.create(foo3);
} }
@Ignore("Fixing after Spring Boot 2.6.1 upgrade")
@Test @Test
public final void whenFooBarsModified_thenFooBarsAudited() { public final void whenFooBarsModified_thenFooBarsAudited() {