From 29a89d05d9d094d9b36db696de9240f40b69c66c Mon Sep 17 00:00:00 2001 From: achabill Date: Fri, 25 Nov 2016 11:58:02 +0100 Subject: [PATCH] RAD-380: Search by report template creator --- .../HibernateMrrtReportTemplateDAO.java | 3 ++ .../MrrtReportTemplateSearchCriteria.java | 22 ++++++++ .../template/MrrtReportTemplateService.java | 2 + .../MrrtReportTemplateSearchCriteriaTest.java | 12 +++++ ...rrtReportTemplateServiceComponentTest.java | 50 +++++++++++++++++++ ...ortTemplateServiceComponentTestDataset.xml | 4 +- .../MrrtReportTemplateSearchHandler.java | 9 +++- ...ortTemplateSearchHandlerComponentTest.java | 42 ++++++++++++++++ .../MrrtReportTemplateSearchHandlerTest.java | 30 +++++++++++ ...plateSearchHandlerComponentTestDataset.xml | 4 +- 10 files changed, 173 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/HibernateMrrtReportTemplateDAO.java b/api/src/main/java/org/openmrs/module/radiology/report/template/HibernateMrrtReportTemplateDAO.java index 3f77fd17b..256851870 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/HibernateMrrtReportTemplateDAO.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/HibernateMrrtReportTemplateDAO.java @@ -88,6 +88,9 @@ public List getMrrtReportTemplates(MrrtReportTemplateSearchC if (searchCriteria.getLicense() != null) { crit.add(Restrictions.ilike("dcTermsLicense", searchCriteria.getLicense() + "%", MatchMode.ANYWHERE)); } + if (searchCriteria.getCreator() != null) { + crit.add(Restrictions.ilike("dcTermsCreator", searchCriteria.getCreator() + "%", MatchMode.ANYWHERE)); + } final List result = (List) crit.list(); return result == null ? new ArrayList<>() : result; } diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteria.java b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteria.java index 3889a534f..488bc0979 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteria.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteria.java @@ -21,6 +21,8 @@ public class MrrtReportTemplateSearchCriteria { private final String license; + private final String creator; + /** * @return the title of the mrrt report template */ @@ -44,6 +46,13 @@ public String getLicense() { return license; } + /** + * @return the creator of the mrrt report template + */ + public String getCreator() { + return creator; + } + public static class Builder { @@ -53,6 +62,8 @@ public static class Builder { private String license; + private String creator; + /** * @param title the title of the mrrt report template * @return this builder instance @@ -82,6 +93,15 @@ public Builder withLicense(String license) { return this; } + /** + * @param creator the creator of the mrrt report template + * @return this builder instance + */ + public Builder withCreator(String creator) { + this.creator = creator; + return this; + } + /** * Creates an {@code MrrtReportTemplateSearchCriteria} with properties of this builder instance. * @@ -89,6 +109,7 @@ public Builder withLicense(String license) { * @should create an mrrt report template search criteria instance with title if title is set * @should create an mrrt report template search criteria instance with publisher if publisher is set * @should create an mrrt report template search criteria instance with license if license is set + * @should create an mrrt report template search criteria instance with creator if creator is set */ public MrrtReportTemplateSearchCriteria build() { return new MrrtReportTemplateSearchCriteria(this); @@ -100,5 +121,6 @@ private MrrtReportTemplateSearchCriteria(Builder builder) { this.title = builder.title; this.publisher = builder.publisher; this.license = builder.license; + this.creator = builder.creator; } } diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateService.java b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateService.java index 0ae8017ea..0c8751c50 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateService.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateService.java @@ -122,6 +122,8 @@ public interface MrrtReportTemplateService extends OpenmrsService { * @should return an empty list if no match for publisher was found * @should return all mrrt report templates that match given license anywhere in dcterms license insensitive to case * @should return an empty list if no match for license was found + * @should return all mrrt report templates that match given creator anywhere in dcterms creator insensitive to case + * @should return an empty list if no match for creator was found */ @Authorized(RadiologyPrivileges.GET_RADIOLOGY_REPORT_TEMPLATES) public List diff --git a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteriaTest.java b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteriaTest.java index c73006ff9..4539dfd16 100644 --- a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteriaTest.java +++ b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateSearchCriteriaTest.java @@ -61,4 +61,16 @@ public void build_shouldCreateAnMrrtReportTemplateSearchCriteriaInstanceWithLice .build(); assertThat(mrrtReportTemplateSearchCriteria.getLicense(), is(license)); } + + /** + * @see MrrtReportTemplateSearchCriteria.Builder#build() + * @verifies create an mrrt report template search criteria instance with creator if creator is set + */ + @Test + public void build_shouldCreateAnMrrtReportTemplateSearchCriteriaInstanceWithCreatorIfCreatorIsSet() throws Exception { + String creator = "creator1"; + mrrtReportTemplateSearchCriteria = new MrrtReportTemplateSearchCriteria.Builder().withCreator(creator) + .build(); + assertThat(mrrtReportTemplateSearchCriteria.getCreator(), is(creator)); + } } diff --git a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateServiceComponentTest.java b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateServiceComponentTest.java index e03478b7e..49d9e24ea 100644 --- a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateServiceComponentTest.java +++ b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateServiceComponentTest.java @@ -84,6 +84,10 @@ public class MrrtReportTemplateServiceComponentTest extends BaseModuleContextSen private static final String NON_EXISTING_TEMPLATE_LICENSE = "Non existing license"; + private static final String EXISTING_TEMPLATE_CREATOR = "creator1"; + + private static final String NON_EXISTING_TEMPLATE_CREATOR = "Non existing creator"; + private static final String TEMPLATE_IDENTIFIER = "1.3.6.1.4.1.21367.13.199.1015"; private static final String NON_EXISTING_PUBLISHER = "Non existing publisher"; @@ -533,6 +537,52 @@ public void getMrrtReportTemplates_shouldReturnAnEmptyListIfNoMatchForLicenseWas assertTrue(templates.isEmpty()); } + /** + * @see MrrtReportTemplateService#getMrrtReportTemplates(MrrtReportTemplateSearchCriteria) + * @verifies return all mrrt report templates that match given creator anywhere in dcterms creator insensitive to case + */ + @Test + public void + getMrrtReportTemplates_shouldReturnAllMrrtReportTemplatesThatMatchGivenCreatorAnywhereInDctermsCreatorInsensitiveToCase() + throws Exception { + MrrtReportTemplateSearchCriteria searchCriteria = + new MrrtReportTemplateSearchCriteria.Builder().withCreator(EXISTING_TEMPLATE_CREATOR) + .build(); + List templates = mrrtReportTemplateService.getMrrtReportTemplates(searchCriteria); + + assertNotNull(templates); + assertThat(templates.size(), is(1)); + assertThat(templates.get(0) + .getDcTermsCreator(), + is(EXISTING_TEMPLATE_CREATOR)); + + searchCriteria = new MrrtReportTemplateSearchCriteria.Builder().withCreator("CREATOR") + .build(); + templates = mrrtReportTemplateService.getMrrtReportTemplates(searchCriteria); + List creators = new ArrayList<>(); + templates.forEach(template -> creators.add(template.getDcTermsCreator())); + + assertNotNull(creators); + assertThat(creators.size(), is(2)); + assertThat(creators, hasItem("creator1")); + assertThat(creators, hasItem("creator2")); + } + + /** + * @see MrrtReportTemplateService#getMrrtReportTemplates(MrrtReportTemplateSearchCriteria) + * @verifies return an empty list if no match for creator was found + */ + @Test + public void getMrrtReportTemplates_shouldReturnAnEmptyListIfNoMatchForCreatorWasFound() throws Exception { + MrrtReportTemplateSearchCriteria searchCriteria = + new MrrtReportTemplateSearchCriteria.Builder().withCreator(NON_EXISTING_TEMPLATE_CREATOR) + .build(); + List templates = mrrtReportTemplateService.getMrrtReportTemplates(searchCriteria); + + assertNotNull(templates); + assertTrue(templates.isEmpty()); + } + /** * @see MrrtReportTemplateService#getMrrtReportTemplateHtmlBody(MrrtReportTemplate) * @verifies return the body content of the mrrt report template file diff --git a/api/src/test/resources/org/openmrs/module/radiology/include/MrrtReportTemplateServiceComponentTestDataset.xml b/api/src/test/resources/org/openmrs/module/radiology/include/MrrtReportTemplateServiceComponentTestDataset.xml index 6f8ad413c..ba220a87e 100644 --- a/api/src/test/resources/org/openmrs/module/radiology/include/MrrtReportTemplateServiceComponentTestDataset.xml +++ b/api/src/test/resources/org/openmrs/module/radiology/include/MrrtReportTemplateServiceComponentTestDataset.xml @@ -13,7 +13,7 @@ - - + + diff --git a/omod/src/main/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandler.java b/omod/src/main/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandler.java index 6cb64fe7e..ac6afd861 100644 --- a/omod/src/main/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandler.java +++ b/omod/src/main/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandler.java @@ -42,12 +42,14 @@ public class MrrtReportTemplateSearchHandler implements SearchHandler { public static final String REQUEST_PARAM_LICENSE = "license"; + public static final String REQUEST_PARAM_CREATOR = "creator"; + @Autowired private MrrtReportTemplateService mrrtReportTemplateService; SearchQuery searchQuery = new SearchQuery.Builder("Allows you to search for MrrtReportTemplate's by title") .withOptionalParameters(new String[] { REQUEST_PARAM_TITLE, REQUEST_PARAM_PUBLISHER, REQUEST_PARAM_LICENSE, - REQUEST_PARAM_TOTAL_COUNT }) + REQUEST_PARAM_CREATOR, REQUEST_PARAM_TOTAL_COUNT }) .build(); private final SearchConfig searchConfig = new SearchConfig("default", RestConstants.VERSION_1 + "/mrrtreporttemplate", @@ -70,6 +72,8 @@ public SearchConfig getSearchConfig() { * @should return empty search result if publisher does not exist * @should return all report templates that match given license * @should return empty search result if license does not exist + * @should return all report templates that match given creator + * @should return empty search result if creator does not exist */ @Override public PageableResult search(RequestContext context) throws ResponseException { @@ -77,10 +81,13 @@ public PageableResult search(RequestContext context) throws ResponseException { final String templateTitle = context.getParameter("title"); final String publisher = context.getParameter("publisher"); final String templateLicense = context.getParameter("license"); + final String templateCreator = context.getParameter("creator"); + final MrrtReportTemplateSearchCriteria searchCriteria = new MrrtReportTemplateSearchCriteria.Builder().withTitle(templateTitle) .withPublisher(publisher) .withLicense(templateLicense) + .withCreator(templateCreator) .build(); final List result = mrrtReportTemplateService.getMrrtReportTemplates(searchCriteria); diff --git a/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerComponentTest.java b/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerComponentTest.java index 14aaa8bbb..39b8e422c 100644 --- a/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerComponentTest.java +++ b/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerComponentTest.java @@ -52,6 +52,10 @@ public class MrrtReportTemplateSearchHandlerComponentTest extends MainResourceCo private static final String NON_EXISTING_LICENSE = "Non existing license"; + private static final String CREATOR_QUERY = "creator1"; + + private static final String NON_EXISTING_CREATOR = "Non existing creator"; + @Autowired MrrtReportTemplateService mrrtReportTemplateService; @@ -219,4 +223,42 @@ public void search_shouldReturnEmptySearchResultIfLicenseDoesNotExist() throws E List hits = (List) resultMrrtReportTemplate.get("results"); assertThat(hits.size(), is(0)); } + + /** + * @see MrrtReportTemplateSearchHandler#search(RequestContext) + * @verifies return all report templates that match given creator + */ + @Test + public void search_shouldReturnAllReportTemplatesThatMatchGivenCreator() throws Exception { + MockHttpServletRequest mrrtReportTemplateRequest = request(RequestMethod.GET, getURI()); + mrrtReportTemplateRequest.setParameter(MrrtReportTemplateSearchHandler.REQUEST_PARAM_CREATOR, CREATOR_QUERY); + SimpleObject resultMrrtReportTemplate = deserialize(handle(mrrtReportTemplateRequest)); + + assertNotNull(resultMrrtReportTemplate); + List hits = (List) resultMrrtReportTemplate.get("results"); + MrrtReportTemplateSearchCriteria searchCriteria = + new MrrtReportTemplateSearchCriteria.Builder().withCreator(CREATOR_QUERY) + .build(); + assertThat(hits.size(), is(1)); + assertThat(PropertyUtils.getProperty(hits.get(0), "uuid"), + is(mrrtReportTemplateService.getMrrtReportTemplates(searchCriteria) + .get(0) + .getUuid())); + assertNull(PropertyUtils.getProperty(resultMrrtReportTemplate, "totalCount")); + } + + /** + * @see MrrtReportTemplateSearchHandler#search(RequestContext) + * @verifies return empty search result if creator does not exist + */ + @Test + public void search_shouldReturnEmptySearchResultIfCreatorDoesNotExist() throws Exception { + MockHttpServletRequest mockRequest = request(RequestMethod.GET, getURI()); + mockRequest.setParameter(MrrtReportTemplateSearchHandler.REQUEST_PARAM_CREATOR, NON_EXISTING_CREATOR); + SimpleObject resultMrrtReportTemplate = deserialize(handle(mockRequest)); + + assertNotNull(resultMrrtReportTemplate); + List hits = (List) resultMrrtReportTemplate.get("results"); + assertThat(hits.size(), is(0)); + } } diff --git a/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerTest.java b/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerTest.java index 0935c340e..e5770243d 100644 --- a/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerTest.java +++ b/omod/src/test/java/org/openmrs/module/radiology/report/template/web/search/MrrtReportTemplateSearchHandlerTest.java @@ -62,6 +62,10 @@ public class MrrtReportTemplateSearchHandlerTest { private static final String NON_EXISTING_LICENSE = "Non existing license"; + private static final String CREATOR_QUERY = "creator1"; + + private static final String NON_EXISTING_CREATOR = "Non existing creator"; + @Mock RestService RestService; @@ -174,4 +178,30 @@ public void search_shouldReturnEmptySearchResultIfLicenseDoesNotExist() throws E PageableResult pageableResult = mrrtReportTemplateSearchHandler.search(requestContext); assertThat(pageableResult, is(instanceOf(EmptySearchResult.class))); } + + /** + * @see MrrtReportTemplateSearchHandler#search(RequestContext) + * @verifies return all report templates that match given creator + */ + @Test + public void search_shouldReturnAllReportTemplatesThatMatchGivenCreator() throws Exception { + request.setParameter(MrrtReportTemplateSearchHandler.REQUEST_PARAM_CREATOR, CREATOR_QUERY); + when(mrrtReportTemplateService.getMrrtReportTemplates(any(MrrtReportTemplateSearchCriteria.class))) + .thenReturn(mrrtReportTemplates); + + PageableResult pageableResult = mrrtReportTemplateSearchHandler.search(requestContext); + assertThat(pageableResult, is(instanceOf(NeedsPaging.class))); + } + + /** + * @see MrrtReportTemplateSearchHandler#search(RequestContext) + * @verifies return empty search result if creator does not exist + */ + @Test + public void search_shouldReturnEmptySearchResultIfCreatorDoesNotExist() throws Exception { + request.setParameter(MrrtReportTemplateSearchHandler.REQUEST_PARAM_CREATOR, NON_EXISTING_CREATOR); + + PageableResult pageableResult = mrrtReportTemplateSearchHandler.search(requestContext); + assertThat(pageableResult, is(instanceOf(EmptySearchResult.class))); + } } diff --git a/omod/src/test/resources/MrrtReportTemplateSearchHandlerComponentTestDataset.xml b/omod/src/test/resources/MrrtReportTemplateSearchHandlerComponentTestDataset.xml index f9cb06c89..2101bfc25 100644 --- a/omod/src/test/resources/MrrtReportTemplateSearchHandlerComponentTestDataset.xml +++ b/omod/src/test/resources/MrrtReportTemplateSearchHandlerComponentTestDataset.xml @@ -11,6 +11,6 @@ --> - - + + \ No newline at end of file