### Question:
ImageDAOJpa implements ImageDAO { @Override @SuppressWarnings("unchecked") public List<Image> getCacheImagesForApplication(Application application) { if(application == null) { return new ArrayList<Image>(); } try { Query query = entityManager.createQuery("from Image where demo = true and application = :applicationIdentifier"); query.setParameter("applicationIdentifier", application); List<Image> images = (List<Image>) query.getResultList(); return images; } catch(IllegalStateException ex) { logger.debug("Trying to get information for an application that does not exists in the databse"); return new ArrayList<Image>(); } }  @PersistenceContext (unitName = "applicationManagerDB") void setEntityManager(EntityManager entityManager); EntityManager getEntityManager(); @Override @Transactional(readOnly=false, propagation=Propagation.REQUIRED) Image getById(int id); @Override @Transactional(readOnly=false, propagation=Propagation.REQUIRED) List<Image> getAll(); @Override @Transactional(readOnly=false, propagation=Propagation.REQUIRED) boolean save(Image image); @Override @Transactional(readOnly=false, propagation=Propagation.REQUIRED) boolean update(Image image); @Override @Transactional(readOnly=false, propagation=Propagation.REQUIRED) boolean delete(Image image); @Override Image getLastImageWithOvfId(String ovfId); @Override Image getDemoCacheImage(String ovfId, String ovfHref); @Override @SuppressWarnings("unchecked") List<Image> getCacheImagesForApplication(Application application);  }### Answer:
@Test public void getCacheImagesForApplicationNotInDB() { Application application = new Application(); List<Image> images = imageDAO.getCacheImagesForApplication(application); assertEquals(0, images.size()); images = imageDAO.getCacheImagesForApplication(null); assertEquals(0, images.size()); }
@Test public void getCacheImagesForApplicationTest() { Image image1 = new Image(); image1.setOvfId("ovf-id1"); image1.setProviderImageId("provider-image-id1"); image1.setDemo(true); image1.setOvfHref("href1"); boolean saved = imageDAO.save(image1); assertTrue(saved); Image image2 = new Image(); image2.setOvfId("ovf-id2"); image2.setProviderImageId("provider-image-id2"); image2.setDemo(false); image2.setOvfHref("href2"); saved = imageDAO.save(image2); assertTrue(saved); Image image3 = new Image(); image3.setOvfId("ovf-id3"); image3.setProviderImageId("provider-image-id3"); image3.setDemo(true); image3.setOvfHref("href1"); saved = imageDAO.save(image3); assertTrue(saved); Image image4 = new Image(); image4.setOvfId("ovf-id1"); image4.setProviderImageId("provider-image-id4"); image4.setDemo(true); image4.setOvfHref("href4"); saved = imageDAO.save(image4); assertTrue(saved); Image image5 = new Image(); image5.setOvfId("ovf-id1"); image5.setProviderImageId("provider-image-id5"); image5.setDemo(false); image5.setOvfHref("href1"); saved = imageDAO.save(image5); assertTrue(saved); int size = applicationDAO.getAll().size(); Application application = new Application(); application.setName("TestX"); applicationDAO.save(application); application = applicationDAO.getAll().get(size); application.addImage(image1); application.addImage(image2); application.addImage(image4); applicationDAO.update(application); List<Image> images = imageDAO.getCacheImagesForApplication(application); assertEquals(2, images.size()); assertEquals("href1", images.get(0).getOvfHref()); assertEquals("href4", images.get(1).getOvfHref()); }