@SuppressWarnings("unchecked") @Test public void testExecuteNoneFound() { final EntityManager entityManager = context.mock(EntityManager.class); final QueryOptimizer queryOptimizer = context.mock(QueryOptimizer.class); final FindByIdRequest req = context.mock(FindByIdRequest.class); final Query query = context.mock(Query.class); final List<Activity> activities = context.mock(List.class); FindByIdMapper<Activity> sut = new FindByIdMapper(); sut.setEntityManager(entityManager); sut.setQueryOptimizer(queryOptimizer); context.checking(new Expectations() { { oneOf(req).getEntityName(); will(returnValue("Activity")); oneOf(req).getEntityId(); will(returnValue(1L)); oneOf(entityManager).createQuery(with(any(String.class))); will(returnValue(query)); allowing(query).setParameter(with(any(String.class)), with(any(Object.class))); will(returnValue(query)); oneOf(query).getResultList(); will(returnValue(activities)); oneOf(activities).isEmpty(); will(returnValue(true)); } }); assertTrue(sut.execute(req) == null); context.assertIsSatisfied(); }
public TDomainEntityType execute(final FindByIdRequest inRequest) { Query q = getEntityManager().createQuery("from " + inRequest.getEntityName() + " where id = :domainEntityId") .setParameter("domainEntityId", inRequest.getEntityId()); List<TDomainEntityType> results = q.getResultList(); if (!results.isEmpty() && results.size() == 1) { return results.get(0); } else { return null; } }
FindByIdMapper extends
        ReadMapper<FindByIdRequest, TDomainEntityType> { public TDomainEntityType execute(final FindByIdRequest inRequest) { Query q = getEntityManager().createQuery("from " + inRequest.getEntityName() + " where id = :domainEntityId") .setParameter("domainEntityId", inRequest.getEntityId()); List<TDomainEntityType> results = q.getResultList(); if (!results.isEmpty() && results.size() == 1) { return results.get(0); } else { return null; } } }
FindByIdMapper extends
        ReadMapper<FindByIdRequest, TDomainEntityType> { public TDomainEntityType execute(final FindByIdRequest inRequest) { Query q = getEntityManager().createQuery("from " + inRequest.getEntityName() + " where id = :domainEntityId") .setParameter("domainEntityId", inRequest.getEntityId()); List<TDomainEntityType> results = q.getResultList(); if (!results.isEmpty() && results.size() == 1) { return results.get(0); } else { return null; } }  }
FindByIdMapper extends
        ReadMapper<FindByIdRequest, TDomainEntityType> { public TDomainEntityType execute(final FindByIdRequest inRequest) { Query q = getEntityManager().createQuery("from " + inRequest.getEntityName() + " where id = :domainEntityId") .setParameter("domainEntityId", inRequest.getEntityId()); List<TDomainEntityType> results = q.getResultList(); if (!results.isEmpty() && results.size() == 1) { return results.get(0); } else { return null; } }  TDomainEntityType execute(final FindByIdRequest inRequest); }
FindByIdMapper extends
        ReadMapper<FindByIdRequest, TDomainEntityType> { public TDomainEntityType execute(final FindByIdRequest inRequest) { Query q = getEntityManager().createQuery("from " + inRequest.getEntityName() + " where id = :domainEntityId") .setParameter("domainEntityId", inRequest.getEntityId()); List<TDomainEntityType> results = q.getResultList(); if (!results.isEmpty() && results.size() == 1) { return results.get(0); } else { return null; } }  TDomainEntityType execute(final FindByIdRequest inRequest);  }