From 863804adf49c70284813af689d232f90139e4568 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Thu, 3 Jun 2021 11:18:03 +0300 Subject: [PATCH 1/3] Fixes issue: Changes to the product-catalogue endpoint #841 --- pom.xml | 4 ++-- .../opensrp/web/rest/ProductCatalogueResource.java | 13 +++++++++++-- .../web/rest/ProductCatalogueResourceTest.java | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 8f0608543..daa053742 100755 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ opensrp-server-web war - 2.8.4-SNAPSHOT + 2.8.5-SNAPSHOT opensrp-server-web OpenSRP Server Web Application https://github.com/OpenSRP/opensrp-server-web @@ -26,7 +26,7 @@ 5.2.2.RELEASE always 1.5.1 - 2.11.2-SNAPSHOT + 2.11.4-SNAPSHOT 2.3.0-SNAPSHOT 2.0.1-SNAPSHOT 2.0.3-SNAPSHOT diff --git a/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java b/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java index 3dfe09c58..7e0f7dc7e 100644 --- a/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java +++ b/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java @@ -1,5 +1,6 @@ package org.opensrp.web.rest; +import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensrp.domain.Multimedia; @@ -48,7 +49,8 @@ public class ProductCatalogueResource { public List getAll( @RequestParam(value = "productName", defaultValue = "", required = false) String productName , @RequestParam(value = "uniqueId", defaultValue = "0", required = false) Long uniqueId, - @RequestParam(value = "serverVersion", required = false) String serverVersion) { + @RequestParam(value = "serverVersion", required = false) String serverVersion, + @RequestParam(value = "limit", required = false) String limit) { final String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString(); @@ -62,7 +64,14 @@ public List getAll( productCatalogueSearchBean.setUniqueId(uniqueId); productCatalogueSearchBean.setServerVersion(lastSyncedServerVersion); - return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, baseUrl); + if(StringUtils.isBlank(limit)){ + return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, Integer.MAX_VALUE,baseUrl); + } else { + return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, Integer.parseInt(limit), baseUrl); + } + + + } @PostMapping(headers = { "Accept=multipart/form-data" }) diff --git a/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java b/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java index 51e382da0..59fbdcd7f 100644 --- a/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java @@ -97,7 +97,7 @@ public void testGetAll() throws Exception { productCatalogue.setUniqueId(1l); List productCatalogues = new ArrayList<>(); productCatalogues.add(productCatalogue); - when(productCatalogueService.getProductCatalogues(any(ProductCatalogueSearchBean.class), anyString())) + when(productCatalogueService.getProductCatalogues(any(ProductCatalogueSearchBean.class), any(Integer.class), anyString())) .thenReturn(productCatalogues); MvcResult result = mockMvc.perform(get(BASE_URL)) .andExpect(status().isOk()) From 8d94506260ef080b6c48ad76926fa012a7e7fdd1 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Fri, 4 Jun 2021 00:59:09 +0300 Subject: [PATCH 2/3] Overloaded getAll and added test: Fixes#841 --- .../web/rest/ProductCatalogueResource.java | 30 +++++++++++++++---- .../rest/ProductCatalogueResourceTest.java | 26 +++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java b/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java index 7e0f7dc7e..fbf89ebe8 100644 --- a/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java +++ b/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java @@ -45,15 +45,38 @@ public class ProductCatalogueResource { private static final String DOWNLOAD_PHOTO_END_POINT = "/multimedia/media/"; + @Deprecated @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE }) public List getAll( @RequestParam(value = "productName", defaultValue = "", required = false) String productName , @RequestParam(value = "uniqueId", defaultValue = "0", required = false) Long uniqueId, - @RequestParam(value = "serverVersion", required = false) String serverVersion, - @RequestParam(value = "limit", required = false) String limit) { + @RequestParam(value = "serverVersion", required = false) String serverVersion) { + + final String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString(); + + Long lastSyncedServerVersion = null; + if (serverVersion != null) { + lastSyncedServerVersion = Long.parseLong(serverVersion); + } + + ProductCatalogueSearchBean productCatalogueSearchBean = new ProductCatalogueSearchBean(); + productCatalogueSearchBean.setProductName(productName); + productCatalogueSearchBean.setUniqueId(uniqueId); + productCatalogueSearchBean.setServerVersion(lastSyncedServerVersion); + + return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, baseUrl); + + } + @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE }, params = "limit") + public List getAll( + @RequestParam(value = "productName", defaultValue = "", required = false) String productName + , @RequestParam(value = "uniqueId", defaultValue = "0", required = false) Long uniqueId, + @RequestParam(value = "serverVersion", required = false) String serverVersion, + @RequestParam(value = "limit") String limit) { final String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString(); + Long lastSyncedServerVersion = null; if (serverVersion != null) { lastSyncedServerVersion = Long.parseLong(serverVersion); @@ -69,9 +92,6 @@ public List getAll( } else { return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, Integer.parseInt(limit), baseUrl); } - - - } @PostMapping(headers = { "Accept=multipart/form-data" }) diff --git a/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java b/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java index 59fbdcd7f..ce27fed5b 100644 --- a/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java @@ -97,7 +97,7 @@ public void testGetAll() throws Exception { productCatalogue.setUniqueId(1l); List productCatalogues = new ArrayList<>(); productCatalogues.add(productCatalogue); - when(productCatalogueService.getProductCatalogues(any(ProductCatalogueSearchBean.class), any(Integer.class), anyString())) + when(productCatalogueService.getProductCatalogues(any(ProductCatalogueSearchBean.class), anyString())) .thenReturn(productCatalogues); MvcResult result = mockMvc.perform(get(BASE_URL)) .andExpect(status().isOk()) @@ -115,6 +115,30 @@ public void testGetAll() throws Exception { assertEquals("MT-123", response.get(0).getMaterialNumber()); } + @Test + public void testGetAllWithLimitParam() throws Exception { + ProductCatalogue productCatalogue = createProductCatalog(); + productCatalogue.setUniqueId(1l); + List productCatalogues = new ArrayList<>(); + productCatalogues.add(productCatalogue); + when(productCatalogueService.getProductCatalogues(any(ProductCatalogueSearchBean.class), any(Integer.class),anyString())) + .thenReturn(productCatalogues); + MvcResult result = mockMvc.perform(get(BASE_URL+"?limit=10")) + .andExpect(status().isOk()) + .andReturn(); + + List response = (List) result.getModelAndView().getModel().get("productCatalogueList"); + + if (response.size() == 0) { + fail("Test case failed"); + } + + assertEquals(response.size(), 1); + assertEquals(new Long(1), response.get(0).getUniqueId()); + assertEquals("Scale", response.get(0).getProductName()); + assertEquals("MT-123", response.get(0).getMaterialNumber()); + } + @Test public void testCreate() throws Exception { From 2cc88fe9a98c16b6ae518893f5871b3f0594ed4c Mon Sep 17 00:00:00 2001 From: hilpitome Date: Fri, 4 Jun 2021 12:28:12 +0300 Subject: [PATCH 3/3] applied opensrp code formatting --- .../web/rest/ProductCatalogueResource.java | 15 +++++++------ .../rest/ProductCatalogueResourceTest.java | 22 +++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java b/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java index fbf89ebe8..e69fae4a7 100644 --- a/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java +++ b/src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java @@ -66,8 +66,8 @@ public List getAll( return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, baseUrl); - } + @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE }, params = "limit") public List getAll( @RequestParam(value = "productName", defaultValue = "", required = false) String productName @@ -76,7 +76,6 @@ public List getAll( @RequestParam(value = "limit") String limit) { final String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString(); - Long lastSyncedServerVersion = null; if (serverVersion != null) { lastSyncedServerVersion = Long.parseLong(serverVersion); @@ -87,10 +86,11 @@ public List getAll( productCatalogueSearchBean.setUniqueId(uniqueId); productCatalogueSearchBean.setServerVersion(lastSyncedServerVersion); - if(StringUtils.isBlank(limit)){ - return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, Integer.MAX_VALUE,baseUrl); + if (StringUtils.isBlank(limit)) { + return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, Integer.MAX_VALUE, baseUrl); } else { - return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, Integer.parseInt(limit), baseUrl); + return productCatalogueService + .getProductCatalogues(productCatalogueSearchBean, Integer.parseInt(limit), baseUrl); } } @@ -100,7 +100,8 @@ public ResponseEntity create(@RequestPart(required = false) MultipartFil try { productCatalogueService.add(productCatalogue); - ProductCatalogue createdProductCatalogue = productCatalogueService.getProductCatalogueByName(productCatalogue.getProductName()); + ProductCatalogue createdProductCatalogue = productCatalogueService + .getProductCatalogueByName(productCatalogue.getProductName()); Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String userName = authentication.getName(); @@ -136,7 +137,7 @@ public ResponseEntity update(@PathVariable("id") Long uniqueId, @RequestPart ProductCatalogue productCatalogue) { try { - if(file != null) { + if (file != null) { productCatalogue.setPhotoURL(DOWNLOAD_PHOTO_END_POINT + productCatalogue.getUniqueId()); } diff --git a/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java b/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java index ce27fed5b..4db8c0fef 100644 --- a/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java +++ b/src/test/java/org/opensrp/web/rest/ProductCatalogueResourceTest.java @@ -35,6 +35,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.Mockito; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyLong; @@ -43,6 +44,7 @@ import static org.mockito.Mockito.when; import static org.mockito.Mockito.times; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -103,7 +105,8 @@ public void testGetAll() throws Exception { .andExpect(status().isOk()) .andReturn(); - List response = (List) result.getModelAndView().getModel().get("productCatalogueList"); + List response = (List) result.getModelAndView().getModel() + .get("productCatalogueList"); if (response.size() == 0) { fail("Test case failed"); @@ -121,13 +124,15 @@ public void testGetAllWithLimitParam() throws Exception { productCatalogue.setUniqueId(1l); List productCatalogues = new ArrayList<>(); productCatalogues.add(productCatalogue); - when(productCatalogueService.getProductCatalogues(any(ProductCatalogueSearchBean.class), any(Integer.class),anyString())) + when(productCatalogueService + .getProductCatalogues(any(ProductCatalogueSearchBean.class), any(Integer.class), anyString())) .thenReturn(productCatalogues); - MvcResult result = mockMvc.perform(get(BASE_URL+"?limit=10")) + MvcResult result = mockMvc.perform(get(BASE_URL + "?limit=10")) .andExpect(status().isOk()) .andReturn(); - List response = (List) result.getModelAndView().getModel().get("productCatalogueList"); + List response = (List) result.getModelAndView().getModel() + .get("productCatalogueList"); if (response.size() == 0) { fail("Test case failed"); @@ -159,9 +164,9 @@ public void testCreate() throws Exception { when(multipartFile.getContentType()).thenReturn(""); when(multipartFile.getBytes()).thenReturn(bytes); when(multipartFile.getOriginalFilename()).thenReturn("Midwifery kit image"); - when(multimediaService.saveFile(any(MultimediaDTO.class),any(byte[].class),anyString())).thenReturn("Success"); + when(multimediaService.saveFile(any(MultimediaDTO.class), any(byte[].class), anyString())).thenReturn("Success"); - productCatalogueResource.create(multipartFile,productCatalogue); + productCatalogueResource.create(multipartFile, productCatalogue); verify(productCatalogueService).add(argumentCaptor.capture()); @@ -199,9 +204,9 @@ public void testUpdate() throws Exception { when(multipartFile.getContentType()).thenReturn(""); when(multipartFile.getBytes()).thenReturn(bytes); when(multipartFile.getOriginalFilename()).thenReturn("Midwifery kit image"); - when(multimediaService.saveFile(any(MultimediaDTO.class),any(byte[].class),anyString())).thenReturn("Success"); + when(multimediaService.saveFile(any(MultimediaDTO.class), any(byte[].class), anyString())).thenReturn("Success"); - productCatalogueResource.update(1l,multipartFile,productCatalogue); + productCatalogueResource.update(1l, multipartFile, productCatalogue); verify(productCatalogueService).update(argumentCaptor.capture()); @@ -246,7 +251,6 @@ public void testDelete() throws Exception { assertEquals(argumentCaptor.getValue().longValue(), 1); } - private ProductCatalogue createProductCatalog() { ProductCatalogue productCatalogue = new ProductCatalogue(); productCatalogue.setProductName("Scale");