From 809f1ddefda22a332857d265b4973367a7c11bc6 Mon Sep 17 00:00:00 2001 From: Peter Stieglecker Date: Fri, 18 Aug 2023 12:12:26 +0200 Subject: [PATCH] APS-156 added test but found no problem --- .../config/persistence.xml | 3 +- .../utils/persistence/daos/InvoiceDAO.java | 42 +++++++++++++++++ .../utils/persistence/entities/Invoice.java | 41 ++++++++++++++++ .../persistence/entities/InvoicePosition.java | 31 ++++++++++++ .../persistence/test/dao/Aps156Test.java | 47 +++++++++++++++++++ .../utils/persistence/dao/GenericDAO.java | 5 +- 6 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 persistence-utils-demo-test/src/com/axonivy/utils/persistence/daos/InvoiceDAO.java create mode 100644 persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/Invoice.java create mode 100644 persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/InvoicePosition.java create mode 100644 persistence-utils-demo-test/src_test/com/axonivy/utils/persistence/test/dao/Aps156Test.java diff --git a/persistence-utils-demo-test/config/persistence.xml b/persistence-utils-demo-test/config/persistence.xml index 4c16201..1b4eafd 100644 --- a/persistence-utils-demo-test/config/persistence.xml +++ b/persistence-utils-demo-test/config/persistence.xml @@ -11,12 +11,13 @@ persistence-utils-test com.axonivy.utils.persistence.entities.Car + com.axonivy.utils.persistence.entities.Invoice + com.axonivy.utils.persistence.entities.InvoicePosition com.axonivy.utils.persistence.entities.Producer com.axonivy.utils.persistence.entities.Product com.axonivy.utils.persistence.history.beans.History - diff --git a/persistence-utils-demo-test/src/com/axonivy/utils/persistence/daos/InvoiceDAO.java b/persistence-utils-demo-test/src/com/axonivy/utils/persistence/daos/InvoiceDAO.java new file mode 100644 index 0000000..f1e0210 --- /dev/null +++ b/persistence-utils-demo-test/src/com/axonivy/utils/persistence/daos/InvoiceDAO.java @@ -0,0 +1,42 @@ +package com.axonivy.utils.persistence.daos; + +import javax.persistence.criteria.Expression; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.MessageFormatMessageFactory; + +import com.axonivy.utils.persistence.dao.CriteriaQueryContext; +import com.axonivy.utils.persistence.dao.GenericIdEntityDAO; +import com.axonivy.utils.persistence.entities.Invoice; +import com.axonivy.utils.persistence.entities.Invoice_; + +public class InvoiceDAO extends GenericIdEntityDAO implements BaseDAO{ + private static final Logger LOG = LogManager.getLogger(new MessageFormatMessageFactory()); + private static final InvoiceDAO INSTANCE = new InvoiceDAO(); + + public static InvoiceDAO get() { + return INSTANCE; + } + + @Override + protected Class getType() { + return Invoice.class; + } + + public int countPositions(String invoiceId) { + int result = 0; + + Invoice invoice = null; + try (CriteriaQueryContext ctx = initializeQuery()) { + Expression idExpr = getExpression(null, ctx.r, Invoice_.id); + ctx.whereEq(idExpr, invoiceId); + invoice = forceSingleResult(findByCriteria(ctx)); + // Do we have a session here? + LOG.info("Found invoice position: {0}", invoice.getPositions().get(0).getDescription()); + result = invoice.getPositions().size(); + } + return result; + } + +} diff --git a/persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/Invoice.java b/persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/Invoice.java new file mode 100644 index 0000000..1e082b7 --- /dev/null +++ b/persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/Invoice.java @@ -0,0 +1,41 @@ +package com.axonivy.utils.persistence.entities; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.OneToMany; + +import com.axonivy.utils.persistence.beans.GenericIdEntity; + +@Entity +public class Invoice extends GenericIdEntity { + private static final long serialVersionUID = 1L; + + @Column + private String name; + + @OneToMany(mappedBy = "invoice", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) + private List positions; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getPositions() { + return positions; + } + + public void setPositions(List positions) { + this.positions = positions; + } + + + +} diff --git a/persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/InvoicePosition.java b/persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/InvoicePosition.java new file mode 100644 index 0000000..6327bf8 --- /dev/null +++ b/persistence-utils-demo-test/src/com/axonivy/utils/persistence/entities/InvoicePosition.java @@ -0,0 +1,31 @@ +package com.axonivy.utils.persistence.entities; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; + +import com.axonivy.utils.persistence.beans.GenericIdEntity; + +@Entity +public class InvoicePosition extends GenericIdEntity { + private static final long serialVersionUID = 1L; + + @Column + private String description; + + @ManyToOne + private Invoice invoice; + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public Invoice getInvoice() { + return invoice; + } + public void setInvoice(Invoice invoice) { + this.invoice = invoice; + } +} diff --git a/persistence-utils-demo-test/src_test/com/axonivy/utils/persistence/test/dao/Aps156Test.java b/persistence-utils-demo-test/src_test/com/axonivy/utils/persistence/test/dao/Aps156Test.java new file mode 100644 index 0000000..20f4a26 --- /dev/null +++ b/persistence-utils-demo-test/src_test/com/axonivy/utils/persistence/test/dao/Aps156Test.java @@ -0,0 +1,47 @@ +package com.axonivy.utils.persistence.test.dao; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.axonivy.utils.persistence.daos.InvoiceDAO; +import com.axonivy.utils.persistence.entities.Invoice; +import com.axonivy.utils.persistence.entities.InvoicePosition; + +import ch.ivyteam.ivy.environment.IvyTest; + +@IvyTest +public class Aps156Test { + + private Invoice invoice; + + @BeforeEach + public void prepare() { + invoice = new Invoice(); + invoice.setName("Invoice 1"); + + ArrayList positions = new ArrayList<>(); + + InvoicePosition ip1 = new InvoicePosition(); + ip1.setDescription("Invoice Position 1"); + ip1.setInvoice(invoice); + positions.add(ip1); + + invoice.setPositions(positions); + + invoice = InvoiceDAO.get().save(invoice); + } + + @Test + public void test1() { + assertThat(invoice).as("Prepared test").isNotNull(); + + int count = InvoiceDAO.get().countPositions(invoice.getId()); + + assertThat(count).as("Found one").isEqualTo(1); + } +} + diff --git a/persistence-utils/src/com/axonivy/utils/persistence/dao/GenericDAO.java b/persistence-utils/src/com/axonivy/utils/persistence/dao/GenericDAO.java index b5dbc04..6dba57e 100644 --- a/persistence-utils/src/com/axonivy/utils/persistence/dao/GenericDAO.java +++ b/persistence-utils/src/com/axonivy/utils/persistence/dao/GenericDAO.java @@ -810,11 +810,10 @@ public CriteriaQueryGenericContext initializeTupleQuery() { * @param the type of the represented object * @return criteria query for combination of T and U */ - public CriteriaQueryGenericContext initializeQuery( - Class rootType, Class queryType) { + public CriteriaQueryGenericContext initializeQuery(Class rootType, Class queryType) { CriteriaBuilder cb = getEM().getCriteriaBuilder(); - beginSession();// try(AutoCloseTransaction autoSession = beginSession() + beginSession(); CriteriaQuery query = cb.createQuery(queryType); Root root = query.from(rootType);