From 87d698f82883b4f334637e2ae0d09a0ced19231e Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 14 Apr 2017 18:25:17 +0200 Subject: [PATCH] MemberShareService+MemberShareRemote --- .../hsadmin/dao/customer/MemberShareDao.java | 33 ++++ .../service/customer/MemberShareRemote.java | 14 ++ .../service/customer/MemberShareService.java | 96 +++++++++++ .../customer/MemberShareServiceLocal.java | 10 ++ .../xmlrpc/webserver/XmlRpcServlet.properties | 3 +- .../customer/MemberShareRemoteTest.java | 15 ++ .../customer/MemberShareServiceTest.java | 152 ++++++++++++++++++ 7 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 cust-services/src/main/java/de/hsadmin/dao/customer/MemberShareDao.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/MemberShareRemote.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/MemberShareService.java create mode 100644 cust-services/src/main/java/de/hsadmin/service/customer/MemberShareServiceLocal.java create mode 100644 cust-services/src/test/java/de/hsadmin/service/customer/MemberShareRemoteTest.java create mode 100644 cust-services/src/test/java/de/hsadmin/service/customer/MemberShareServiceTest.java diff --git a/cust-services/src/main/java/de/hsadmin/dao/customer/MemberShareDao.java b/cust-services/src/main/java/de/hsadmin/dao/customer/MemberShareDao.java new file mode 100644 index 0000000..2529dec --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/dao/customer/MemberShareDao.java @@ -0,0 +1,33 @@ +package de.hsadmin.dao.customer; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import de.hsadmin.bo.customer.MemberShare; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserException; +import de.hsadmin.service.customer.MemberShareVO; + +public class MemberShareDao { + + @PersistenceContext(name="hsar") + private EntityManager entityManager; + + public MemberShare findMemberShareByValues(MemberShareVO prototype) throws UserException, TechnicalException { + // TODO MHOENNIG -> PHORMANNS: warum werden immer alle verglichen? OptimisticLocking? + // (p.s. code analog zu Beispielen von dir) + final Query query = entityManager.createQuery("SELECT s FROM MemberShare s " + + "WHERE s.customer.name = :customer " + + " AND m.action = :action " + + " AND m.date.name = :date" + + " AND m.quantity = :quantity"+ + " AND m.comment = :comment"); + query.setParameter("customer", prototype.get("customer").getValue()); + query.setParameter("action", prototype.get("action").getValue()); + query.setParameter("date", prototype.get("date").getValue()); + query.setParameter("quantity", prototype.get("quantity").getValue()); + query.setParameter("comment", prototype.get("comment").getValue()); + return (MemberShare) query.getSingleResult(); + } +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareRemote.java b/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareRemote.java new file mode 100644 index 0000000..6b6703c --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareRemote.java @@ -0,0 +1,14 @@ +package de.hsadmin.service.customer; + +import de.hsadmin.xmlrpc.AbstractRemote; +import de.hsadmin.xmlrpc.Remote; + + +public class MemberShareRemote extends AbstractRemote implements Remote { + + @Override + protected String getModuleLookup() { + return MemberShareServiceLocal.class.getSimpleName(); + } + +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareService.java b/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareService.java new file mode 100644 index 0000000..15c8d01 --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareService.java @@ -0,0 +1,96 @@ +package de.hsadmin.service.customer; + +import java.util.List; + +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.commons.lang3.NotImplementedException; + +import de.hsadmin.bo.customer.MemberShare; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserException; +import de.hsadmin.dao.customer.CustomerDao; +import de.hsadmin.dao.customer.MemberShareDao; +import de.hsadmin.login.RequestContext; +import de.hsadmin.login.RequiredScope; +import de.hsadmin.login.Role; +import de.hsadmin.login.ScopePolicy; +import de.hsadmin.module.impl.AbstractModule; +import de.hsadmin.module.util.QueryBuilder; + +@Stateless +public class MemberShareService extends AbstractModule implements MemberShareServiceLocal { + + @PersistenceContext(name="hsar") + private EntityManager entityManager; + + @EJB + private CustomerDao customerDao; + + @EJB + private MemberShareDao memberShareDao; + + public MemberShareService() { + } + + public MemberShareService(final EntityManager em, final CustomerDao customerDao, final MemberShareDao memberShareDao) { + this.entityManager = em; + this.customerDao = customerDao; + this.memberShareDao = memberShareDao; + } + + @Override + public MemberShareVO buildVO() throws TechnicalException { + return new MemberShareVO(); + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM)}) + public MemberShareVO create(final RequestContext requestContext, final MemberShareVO prototype) + throws UserException, TechnicalException { + final MemberShareVO vo = super.create(requestContext, prototype); + final MemberShare bo = new MemberShare(); + bo.setCustomer(customerDao.findCustomerByName(prototype.getCustomer())); + vo.copyPropertiesToPersistentObject(bo); + entityManager.persist(bo); + final MemberShareVO newVO = new MemberShareVO(); + newVO.copyPropertiesFromPersistentObject(bo); + return newVO; + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM), @ScopePolicy(value=Role.CUSTOMER, property="customer")}) + public List read(final RequestContext requestContext, final MemberShareVO criteria) + throws UserException, TechnicalException { + final List emptyList = super.read(requestContext, criteria); + final List list = runCriteriaQuery(criteria); + for (MemberShare c : list) { + final MemberShareVO vo = new MemberShareVO(); + vo.copyPropertiesFromPersistentObject(c); + emptyList.add(vo); + } + return emptyList; + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM)}) + public List update(final RequestContext requestContext, final MemberShareVO criteria, final MemberShareVO prototype) + throws UserException, TechnicalException { + throw new TechnicalException(new NotImplementedException("MemberShares are immuatable")); + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM)}) + public void delete(final RequestContext requestContext, final MemberShareVO criteria) + throws UserException, TechnicalException { + throw new TechnicalException(new NotImplementedException("MemberShares are immuatable")); + } + + // macht diese Klasse Unit-testbar + List runCriteriaQuery(final MemberShareVO criteria) throws UserException, TechnicalException { + return QueryBuilder.newBuilder(entityManager, MemberShare.class).getResultList(criteria); + } +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareServiceLocal.java b/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareServiceLocal.java new file mode 100644 index 0000000..3964659 --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/MemberShareServiceLocal.java @@ -0,0 +1,10 @@ +package de.hsadmin.service.customer; + +import javax.ejb.Local; + +import de.hsadmin.module.Module; + +@Local +public interface MemberShareServiceLocal extends Module { + +} diff --git a/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties b/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties index f5864b8..dbc3837 100644 --- a/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties +++ b/cust-services/src/main/resources/org/apache/xmlrpc/webserver/XmlRpcServlet.properties @@ -1,6 +1,7 @@ customer=de.hsadmin.service.customer.CustomerRemote contact=de.hsadmin.service.customer.ContactRemote mandat=de.hsadmin.service.customer.SEPADirectDebitRemote +memberShare=de.hsadmin.service.customer.MemberShareRemote hive=de.hsadmin.service.pac.HiveRemote pac=de.hsadmin.service.pac.PacRemote -property=de.hsadmin.service.property.PropertyRemote \ No newline at end of file +property=de.hsadmin.service.property.PropertyRemote diff --git a/cust-services/src/test/java/de/hsadmin/service/customer/MemberShareRemoteTest.java b/cust-services/src/test/java/de/hsadmin/service/customer/MemberShareRemoteTest.java new file mode 100644 index 0000000..84f8e8d --- /dev/null +++ b/cust-services/src/test/java/de/hsadmin/service/customer/MemberShareRemoteTest.java @@ -0,0 +1,15 @@ +package de.hsadmin.service.customer; + +import org.junit.Test; + +import de.hsadmin.test.BaseRemoteTest; + +public class MemberShareRemoteTest extends BaseRemoteTest { + + @Test + public void canInstantiateMemberShareServiceLocalViaRemoteRegistry() throws Exception { + givenRegisteredModuleLookupFor(MemberShareServiceLocal.class); + whenServiceIsRetrievedFromRemoteRegistryKey("memberShare"); + thenModuleInstantiatesServiceOfClass(MemberShareService.class); + } +} diff --git a/cust-services/src/test/java/de/hsadmin/service/customer/MemberShareServiceTest.java b/cust-services/src/test/java/de/hsadmin/service/customer/MemberShareServiceTest.java new file mode 100644 index 0000000..51ac1f1 --- /dev/null +++ b/cust-services/src/test/java/de/hsadmin/service/customer/MemberShareServiceTest.java @@ -0,0 +1,152 @@ +package de.hsadmin.service.customer; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; + +import org.apache.commons.lang3.NotImplementedException; +import org.joda.time.LocalDate; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import de.hsadmin.bo.customer.Customer; +import de.hsadmin.bo.customer.MemberShare; +import de.hsadmin.bo.customer.ShareAction; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserException; +import de.hsadmin.dao.customer.CustomerDao; +import de.hsadmin.dao.customer.MemberShareDao; +import de.hsadmin.login.RequestContext; +import de.hsadmin.login.Role; +import de.hsadmin.test.CauseMatcher; + +public class MemberShareServiceTest { + + @Mock + private RequestContext requestContextMock; + + @Mock + private EntityManager emMock; + + @Mock + private CustomerDao customerDaoMock; + + @Mock + private MemberShareDao memberShareDao; + + @Rule + public ExpectedException expectedExeption = ExpectedException.none(); + + private MemberShareService memberShareService; + + protected List memberSharesCriteriaQueryResult; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + memberShareService = new MemberShareService(emMock, customerDaoMock, memberShareDao) { + @Override + List runCriteriaQuery(MemberShareVO criteria) throws UserException, TechnicalException { + return memberSharesCriteriaQueryResult; + } + }; + } + + @Test + public void buildVO() throws TechnicalException { + MemberShareVO newVO = memberShareService.buildVO(); + assertEquals(new MemberShareVO().toString(), newVO.toString()); + } + + @Test + public void create() throws TechnicalException, UserException { + // given + givenLoginRole(Role.SYSTEM); + MemberShareVO prototypeVO = MemberShareVOTest.givenInitializedMemberShareVOForCustomer(givenFakeCustomerWithName("testCust").getName()); + + // when + MemberShareVO createdVO = memberShareService.create(requestContextMock, prototypeVO); + + // then + assertEquals(prototypeVO.toString(), createdVO.toString()); + Mockito.verify(emMock, Mockito.only()).persist(Mockito.any(MemberShare.class)); + } + + @Test + public void read() throws TechnicalException, UserException { + // given + givenLoginRole(Role.SYSTEM); + Customer fakeCustomer = givenFakeCustomerWithName("testCust"); + MemberShareVO prototypeVO = MemberShareVOTest.givenEmptyMemberShareVOForCustomer(fakeCustomer.getName()); + MemberShare memberShare1 = givenFakeMemberShare(fakeCustomer, ShareAction.SUBSCRIPTION, 4 ); + MemberShare memberShare2 = givenFakeMemberShare(fakeCustomer, ShareAction.UNSUBSCRIPTION, 2 ); + givenCriteriaQueryResult(prototypeVO, memberShare1, memberShare2); + + // when + List readVOs = memberShareService.read(requestContextMock, prototypeVO); + + // then + assertEquals(2, readVOs.size()); + assertEquals("MemberShareVO [customer=testCust, action=SUBSCRIPTION, date=Fri Apr 14 00:00:00 CEST 2017, quantity=4, comment=some comment]", readVOs.get(0).toString()); + assertEquals("MemberShareVO [customer=testCust, action=UNSUBSCRIPTION, date=Fri Apr 14 00:00:00 CEST 2017, quantity=2, comment=some comment]", readVOs.get(1).toString()); + } + + @Test + public void update() throws TechnicalException, UserException { + MemberShareVO prototypeVO = MemberShareVOTest.givenInitializedMemberShareVOForCustomer("testCust"); + + // then + expectedExeption.expect(TechnicalException.class); + expectedExeption.expectCause(new CauseMatcher(NotImplementedException.class, "MemberShares are immuatable")); + + // when + memberShareService.update(requestContextMock, prototypeVO, prototypeVO); + } + + @Test + public void delete() throws TechnicalException, UserException { + MemberShareVO prototypeVO = MemberShareVOTest.givenInitializedMemberShareVOForCustomer("testCust"); + + // then + expectedExeption.expect(TechnicalException.class); + expectedExeption.expectCause(new CauseMatcher(NotImplementedException.class, "MemberShares are immuatable")); + + // when + memberShareService.delete(requestContextMock, prototypeVO); + } + + // === test fixture === + + private void givenLoginRole(Role role) { + Mockito.when(requestContextMock.getLoginRole()).thenReturn(role); + } + + private Customer givenFakeCustomerWithName(String name) throws UserException { + Customer fakeCustomer = new Customer(); + fakeCustomer.setName(name); + Mockito.when(customerDaoMock.findCustomerByName(name)).thenReturn(fakeCustomer); + return fakeCustomer; + } + + private MemberShare givenFakeMemberShare(Customer customer, ShareAction action, int quantity) { + MemberShare share = new MemberShare(); + share.setCustomer(customer); + share.setAction(action); + share.setQuantity(quantity); + share.setDate(new LocalDate(2017, 4, 14).toDate()); + share.setComment("some comment"); + return share; + } + + private void givenCriteriaQueryResult(MemberShareVO prototypeVO, MemberShare... memberShare) { + memberSharesCriteriaQueryResult = Arrays.asList(memberShare); + } +}