MemberShareService+MemberShareRemote

This commit is contained in:
Michael Hoennig 2017-04-14 18:25:17 +02:00
parent 2c9785af14
commit 87d698f828
7 changed files with 322 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,14 @@
package de.hsadmin.service.customer;
import de.hsadmin.xmlrpc.AbstractRemote;
import de.hsadmin.xmlrpc.Remote;
public class MemberShareRemote extends AbstractRemote<MemberShareVO> implements Remote {
@Override
protected String getModuleLookup() {
return MemberShareServiceLocal.class.getSimpleName();
}
}

View File

@ -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<MemberShareVO> 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<MemberShareVO> read(final RequestContext requestContext, final MemberShareVO criteria)
throws UserException, TechnicalException {
final List<MemberShareVO> emptyList = super.read(requestContext, criteria);
final List<MemberShare> 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<MemberShareVO> 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<MemberShare> runCriteriaQuery(final MemberShareVO criteria) throws UserException, TechnicalException {
return QueryBuilder.newBuilder(entityManager, MemberShare.class).getResultList(criteria);
}
}

View File

@ -0,0 +1,10 @@
package de.hsadmin.service.customer;
import javax.ejb.Local;
import de.hsadmin.module.Module;
@Local
public interface MemberShareServiceLocal extends Module<MemberShareVO> {
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<MemberShare> memberSharesCriteriaQueryResult;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
memberShareService = new MemberShareService(emMock, customerDaoMock, memberShareDao) {
@Override
List<MemberShare> 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<MemberShareVO> 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);
}
}