Merge pull request #11616 from hkhan/JAVA-8983-fix-spring-data-integration-test

[JAVA-8983] Fix envers integration test
This commit is contained in:
kwoyke 2021-12-30 10:32:03 +01:00 committed by GitHub
commit 9b41f73c47
3 changed files with 44 additions and 70 deletions

View File

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

View File

@ -16,7 +16,10 @@ import javax.persistence.NamedNativeQuery;
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
@Audited
// @Proxy(lazy = false)
@ -89,17 +92,13 @@ public class Foo implements Serializable {
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
return other.name == null;
} else
return name.equals(other.name);
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
return "Foo [name=" + name + "]";
}
}

View File

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