DocumentDao { public List<DocumentMetadata> findDocuments(DocumentQueryParams params) { LOG.debug("Beginning document query"); DocumentQueryParams parameters = params; if (parameters == null) { parameters = new DocumentQueryParams(); } List<DocumentMetadata> documents = new ArrayList<>(); Session sess = null; try { sess = getSession(); if (sess != null) { Criteria criteria = sess.createCriteria(DocumentMetadata.class); String patientId = parameters.getPatientId(); if (patientId != null) { criteria.add(Restrictions.eq("patientId", patientId)); } List<String> statuses = parameters.getStatuses(); if (CollectionUtils.isNotEmpty(statuses)) { LOG.debug("Document query - statuses: {}", statuses); criteria.add(Restrictions.in("status", statuses)); } List<String> documentUniqueIds = parameters.getDocumentUniqueIds(); if (CollectionUtils.isNotEmpty(documentUniqueIds)) { LOG.debug("Document query - document unique ids: {}", documentUniqueIds); criteria.add(Restrictions.in(DOCUMENT_UNIQUE_ID, documentUniqueIds)); } Boolean onDemand = parameters.getOnDemand(); if (onDemand != null) { LOG.debug("Document query - onDemand: {}", onDemand); criteria.add(Restrictions.eq("onDemand", onDemand)); } populateClassCodeCritera(parameters, criteria); populateServiceDateCriteria(parameters, criteria); populateCreationDateCriteria(parameters, criteria); documents = criteria.list(); LOG.debug("Completed retrieve of document query. {} results returned.", documents.size()); } else { LOG.error("Failed to obtain a session from the sessionFactory"); } } finally { GenericDBUtils.closeSession(sess); } return documents; }  boolean save(DocumentMetadata document); DocumentMetadata saveAndGetDocument(DocumentMetadata document); boolean saveAll(List<DocumentMetadata> documents); boolean delete(DocumentMetadata document); boolean deleteAll(List<DocumentMetadata> documents); DocumentMetadata findById(Long documentId); List<DocumentMetadata> findAll(); List<DocumentMetadata> findAllByPatientId(long patientId); int getNextID(); List<DocumentMetadata> findDocuments(DocumentQueryParams params);  }
@Test public void testFindDocuments() { DocumentQueryParams params = new DocumentQueryParams(); params.setPatientId("patientID"); List<String> classCodes = new ArrayList<>(); classCodes.add("class^^Code"); params.setClassCodes(classCodes); params.setClassCodeScheme("class code scheme"); Date creationTimeFrom = new Date(); creationTimeFrom.setTime(01012222); params.setCreationTimeFrom(creationTimeFrom); Date creationTimeTo = new Date(); creationTimeTo.setTime(01012222); params.setCreationTimeTo(creationTimeTo); Date serviceStartTimeFrom = new Date(); serviceStartTimeFrom.setTime(01012222); params.setServiceStartTimeFrom(serviceStartTimeFrom); Date serviceStartTimeTo = new Date(); serviceStartTimeTo.setTime(01012222); params.setServiceStartTimeTo(serviceStartTimeTo); Date serviceStopTimeFrom = new Date(); serviceStopTimeFrom.setTime(01012222); params.setServiceStopTimeFrom(serviceStopTimeFrom); Date serviceStopTimeTo = new Date(); serviceStopTimeTo.setTime(01012222); params.setServiceStopTimeTo(serviceStopTimeTo); List<String> statuses = new ArrayList<>(); statuses.add("status"); params.setStatuses(statuses); List<String> documentUniqueIds = new ArrayList<>(); documentUniqueIds.add("doc unique id"); params.setDocumentUniqueId(documentUniqueIds); params.setOnDemandParams(false); DocumentMetadata doc1 = new DocumentMetadata(); final long ID_1 = 12345; doc1.setDocumentid(ID_1); DocumentMetadata doc2 = new DocumentMetadata(); final long ID_2 = 12345; doc2.setDocumentid(ID_2); List<DocumentMetadata> documents = new ArrayList<>(); documents.add(doc1); documents.add(doc2); Criteria criteria = mock(Criteria.class); when(session.createCriteria(DocumentMetadata.class)).thenReturn(criteria); when(criteria.list()).thenReturn(documents); List<DocumentMetadata> resultsDocuments = documentDao.findDocuments(params); assertEquals(2, resultsDocuments.size()); assertEquals((Long) ID_1, resultsDocuments.get(0).getDocumentid()); assertEquals((Long) ID_2, resultsDocuments.get(1).getDocumentid()); }