Skip to content

Commit

Permalink
Merge branch 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rehammuzzamil committed Jun 8, 2021
2 parents 48092de + 2cc88fe commit 6d4045f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<redis.lettuce.version>5.2.2.RELEASE</redis.lettuce.version>
<opensrp.updatePolicy>always</opensrp.updatePolicy>
<nexus-staging-maven-plugin.version>1.5.1</nexus-staging-maven-plugin.version>
<opensrp.core.version>2.11.5-SNAPSHOT</opensrp.core.version>
<opensrp.core.version>2.11.4-SNAPSHOT</opensrp.core.version>
<opensrp.connector.version>2.3.0-SNAPSHOT</opensrp.connector.version>
<opensrp.interface.version>2.0.1-SNAPSHOT</opensrp.interface.version>
<opensrp.common.version>2.0.3-SNAPSHOT</opensrp.common.version>
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/org/opensrp/web/rest/ProductCatalogueResource.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class ProductCatalogueResource {

private static final String DOWNLOAD_PHOTO_END_POINT = "/multimedia/media/";

@Deprecated
@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
public List<ProductCatalogue> getAll(
@RequestParam(value = "productName", defaultValue = "", required = false) String productName
Expand All @@ -63,6 +65,33 @@ public List<ProductCatalogue> getAll(
productCatalogueSearchBean.setServerVersion(lastSyncedServerVersion);

return productCatalogueService.getProductCatalogues(productCatalogueSearchBean, baseUrl);

}

@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE }, params = "limit")
public List<ProductCatalogue> 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);
}

ProductCatalogueSearchBean productCatalogueSearchBean = new ProductCatalogueSearchBean();
productCatalogueSearchBean.setProductName(productName);
productCatalogueSearchBean.setUniqueId(uniqueId);
productCatalogueSearchBean.setServerVersion(lastSyncedServerVersion);

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" })
Expand All @@ -71,7 +100,8 @@ public ResponseEntity<String> 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();
Expand Down Expand Up @@ -107,7 +137,7 @@ public ResponseEntity<String> update(@PathVariable("id") Long uniqueId,
@RequestPart ProductCatalogue productCatalogue) {

try {
if(file != null) {
if (file != null) {
productCatalogue.setPhotoURL(DOWNLOAD_PHOTO_END_POINT + productCatalogue.getUniqueId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -103,7 +105,34 @@ public void testGetAll() throws Exception {
.andExpect(status().isOk())
.andReturn();

List<ProductCatalogue> response = (List<ProductCatalogue>) result.getModelAndView().getModel().get("productCatalogueList");
List<ProductCatalogue> response = (List<ProductCatalogue>) 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 testGetAllWithLimitParam() throws Exception {
ProductCatalogue productCatalogue = createProductCatalog();
productCatalogue.setUniqueId(1l);
List<ProductCatalogue> 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<ProductCatalogue> response = (List<ProductCatalogue>) result.getModelAndView().getModel()
.get("productCatalogueList");

if (response.size() == 0) {
fail("Test case failed");
Expand Down Expand Up @@ -135,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());

Expand Down Expand Up @@ -175,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());

Expand Down Expand Up @@ -222,7 +251,6 @@ public void testDelete() throws Exception {
assertEquals(argumentCaptor.getValue().longValue(), 1);
}


private ProductCatalogue createProductCatalog() {
ProductCatalogue productCatalogue = new ProductCatalogue();
productCatalogue.setProductName("Scale");
Expand Down

0 comments on commit 6d4045f

Please sign in to comment.