Skip to content

Commit

Permalink
update as per reviewed recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
hilpitome committed Feb 21, 2022
2 parents 0a3ad8c + 335dd80 commit aa7eca4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>opensrp-server-web</artifactId>
<packaging>war</packaging>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
<name>opensrp-server-web</name>
<description>OpenSRP Server Web Application</description>
<url>https://github.com/OpenSRP/opensrp-server-web</url>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/opensrp/web/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ public interface Constants {

public static final String PAGE_SIZE = "pageSize";

public static final String SERVER_VERSION = "serverVersion";

public static final String ORDER_BY_TYPE = "orderByType";

public static final String ORDER_BY_FIELD_NAME = "orderByFieldName";

public static final String DATETIME_IN_UTC_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

String LOCATIONS = "locations";

String NULL = "null";
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/opensrp/web/rest/OrganizationResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.opensrp.web.Constants.ORDER_BY_FIELD_NAME;
import static org.opensrp.web.Constants.ORDER_BY_TYPE;
import static org.opensrp.web.Constants.PAGE_NUMBER;
import static org.opensrp.web.Constants.PAGE_SIZE;
import static org.opensrp.web.Constants.ORDER_BY_TYPE;
import static org.opensrp.web.Constants.TOTAL_RECORDS;
import static org.opensrp.web.Constants.ORDER_BY_FIELD_NAME;
import static org.opensrp.web.Constants.SERVER_VERSION;


/**
* @author Samuel Githengi created on 09/10/19
Expand Down Expand Up @@ -115,10 +117,17 @@ public List<Organization> getAllOrganizations(@RequestParam(value = "location_id
@RequestParam(value = PAGE_NUMBER, required = false) Integer pageNumber,
@RequestParam(value = PAGE_SIZE, required = false) Integer pageSize,
@RequestParam(value = ORDER_BY_TYPE, required = false) String orderByType,
@RequestParam(value = ORDER_BY_FIELD_NAME, required = false) String orderByFieldName
@RequestParam(value = ORDER_BY_FIELD_NAME, required = false) String orderByFieldName,
@RequestParam(value = SERVER_VERSION, required = false) String serverVersionParam
) {

Long serverVersion = null;
if (serverVersionParam != null) {
serverVersion = Long.parseLong(serverVersionParam);
}

OrganizationSearchBean organizationSearchBean = createOrganizationSearchBeanForPagination(pageNumber, pageSize,
orderByType, orderByFieldName);
orderByType, orderByFieldName, serverVersion);

if (StringUtils.isNotBlank(locationID)) {
return organizationService.selectOrganizationsEncompassLocations(locationID);
Expand Down Expand Up @@ -329,7 +338,7 @@ public ResponseEntity<String> searchOrganization(OrganizationSearchBean organiza
}

private OrganizationSearchBean createOrganizationSearchBeanForPagination(Integer pageNumber, Integer pageSize,
String orderByType, String orderByFieldName) {
String orderByType, String orderByFieldName, Long serverVersion) {
OrganizationSearchBean organizationSearchBean = new OrganizationSearchBean();
OrganizationSearchBean.OrderByType orderByTypeEnum = orderByType != null ?
OrganizationSearchBean.OrderByType.valueOf(orderByType) :
Expand All @@ -341,6 +350,7 @@ private OrganizationSearchBean createOrganizationSearchBeanForPagination(Integer
organizationSearchBean.setPageSize(pageSize);
organizationSearchBean.setOrderByFieldName(fieldName);
organizationSearchBean.setOrderByType(orderByTypeEnum);
organizationSearchBean.setServerVersion(serverVersion);

return organizationSearchBean;
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/opensrp/web/rest/PractitionerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import org.opensrp.search.BaseSearchBean;
import org.opensrp.search.PractitionerSearchBean;
import org.opensrp.service.PractitionerService;
import org.opensrp.util.DateTypeConverter;
import org.smartregister.domain.Practitioner;
import org.smartregister.utils.TaskDateTimeTypeConverter;
import org.smartregister.utils.DateTimeTypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -26,22 +25,24 @@

import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.opensrp.web.Constants.ORDER_BY_FIELD_NAME;
import static org.opensrp.web.Constants.ORDER_BY_TYPE;
import static org.opensrp.web.Constants.DATETIME_IN_UTC_FORMAT_STRING;
import static org.opensrp.web.Constants.PAGE_NUMBER;
import static org.opensrp.web.Constants.PAGE_SIZE;
import static org.opensrp.web.Constants.ORDER_BY_TYPE;
import static org.opensrp.web.Constants.TOTAL_RECORDS;
import static org.opensrp.web.Constants.ORDER_BY_FIELD_NAME;
import static org.opensrp.web.Constants.SERVER_VERSION;

@Controller
@RequestMapping(value = "/rest/practitioner")
public class PractitionerResource {

private static Logger logger = LogManager.getLogger(PractitionerResource.class.toString());

public static Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new TaskDateTimeTypeConverter())
.registerTypeAdapter(LocalDate.class, new DateTypeConverter()).create();
public static Gson gson = new GsonBuilder().setDateFormat(DATETIME_IN_UTC_FORMAT_STRING)
.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()).create();

private PractitionerService practitionerService;

Expand Down Expand Up @@ -72,10 +73,16 @@ public ResponseEntity<String> getPractitionerByUniqueId(@PathVariable(IDENTIFIER
public ResponseEntity<String> getPractitioners(@RequestParam(value = PAGE_NUMBER, required = false) Integer pageNumber,
@RequestParam(value = PAGE_SIZE, required = false) Integer pageSize,
@RequestParam(value = ORDER_BY_TYPE, required = false) String orderByType,
@RequestParam(value = ORDER_BY_FIELD_NAME, required = false) String orderByFieldName) {
@RequestParam(value = ORDER_BY_FIELD_NAME, required = false) String orderByFieldName,
@RequestParam(value = SERVER_VERSION, required = false) String serverVersionParam) {

Long serverVersion = null;
if (serverVersionParam != null) {
serverVersion = Long.parseLong(serverVersionParam);
}

PractitionerSearchBean practitionerSearchBean = createPractitionerSearchBean(pageNumber, pageSize, orderByType,
orderByFieldName);
orderByFieldName, serverVersion);
return new ResponseEntity<>(gson.toJson(
practitionerService.getAllPractitioners(practitionerSearchBean)),
RestUtils.getJSONUTF8Headers(), HttpStatus.OK);
Expand Down Expand Up @@ -149,7 +156,7 @@ public List<Practitioner> getPractitionersByPractitionerRoleIdentifierAndCode(
}

private PractitionerSearchBean createPractitionerSearchBean(Integer pageNumber, Integer pageSize, String orderByType,
String orderByFieldName) {
String orderByFieldName, Long serverVersion) {

BaseSearchBean.OrderByType orderByTypeEnum;
BaseSearchBean.FieldName fieldName;
Expand All @@ -163,6 +170,7 @@ private PractitionerSearchBean createPractitionerSearchBean(Integer pageNumber,
.pageSize(pageSize)
.orderByType(orderByTypeEnum)
.orderByFieldName(fieldName)
.serverVersion(serverVersion)
.build();

}
Expand Down
17 changes: 11 additions & 6 deletions src/test/java/org/opensrp/web/rest/OrganizationResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -113,7 +115,8 @@ public class OrganizationResourceTest {

private String BASE_URL = "/rest/organization/";

private String organizationJSON = "{\"identifier\":\"801874c0-d963-11e9-8a34-2a2ae2dbcce4\",\"active\":true,\"name\":\"B Team\",\"partOf\":1123,\"type\":{\"coding\":[{\"system\":\"http://terminology.hl7.org/CodeSystem/organization-type\",\"code\":\"team\",\"display\":\"Team\"}]},\"serverVersion\":0}";

private String organizationJSON = "{\"identifier\":\"801874c0-d963-11e9-8a34-2a2ae2dbcce4\",\"active\":true,\"name\":\"B Team\",\"partOf\":1123,\"type\":{\"coding\":[{\"system\":\"http://terminology.hl7.org/CodeSystem/organization-type\",\"code\":\"team\",\"display\":\"Team\"}]},\"dateCreated\":\"2021-12-20T13:16:02.457Z\",\"dateEdited\":\"2021-12-20T13:16:02.457Z\",\"serverVersion\":1}";

private ObjectMapper objectMapper;

Expand All @@ -130,6 +133,8 @@ public void setUp() {
organizationResource.setLocationService(locationService);
organizationResource.setPlanService(planService);
objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.registerModule(new JodaModule());
}

@Test
Expand Down Expand Up @@ -272,6 +277,7 @@ public void testAssignLocationAndPlanWithInternalError() throws Exception {

@Test
public void testGetAssignedLocationAndPlan() throws Exception {
objectMapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String identifier = UUID.randomUUID().toString();
List<AssignedLocations> expected = getOrganizationLocationsAssigned(true);
when(organizationService.findAssignedLocationsAndPlans(identifier, true, null, null, null, null))
Expand All @@ -282,7 +288,6 @@ public void testGetAssignedLocationAndPlan() throws Exception {
verify(organizationService).findAssignedLocationsAndPlans(identifier, true, null, null, null, null);
verifyNoMoreInteractions(organizationService);
assertEquals(objectMapper.writeValueAsString(expected), result.getResponse().getContentAsString());

}

@Test
Expand Down Expand Up @@ -322,6 +327,7 @@ public void testGetAssignedLocationAndPlanWithInternalError() throws Exception {

@Test
public void testGetAssignedLocationsAndPlansByPlanId() throws Exception {
objectMapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String identifier = UUID.randomUUID().toString();
List<AssignedLocations> expected = getOrganizationLocationsAssigned(true);
when(organizationService.findAssignedLocationsAndPlansByPlanIdentifier(identifier, null, null, null, null))
Expand All @@ -332,7 +338,6 @@ public void testGetAssignedLocationsAndPlansByPlanId() throws Exception {
verify(organizationService).findAssignedLocationsAndPlansByPlanIdentifier(identifier, null, null, null, null);
verifyNoMoreInteractions(organizationService);
assertEquals(objectMapper.writeValueAsString(expected), result.getResponse().getContentAsString());

}

@Test
Expand Down Expand Up @@ -495,7 +500,7 @@ private OrganizationAssigmentBean[] getOrganizationAssignment() {
}

private List<AssignedLocations> getOrganizationLocationsAssigned(boolean includePlans) {
List<AssignedLocations> organizationAssigmentBeans = new ArrayList<>();
List<AssignedLocations> organizationAssignmentBeans = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 5; i++) {
AssignedLocations bean = new AssignedLocations();
Expand All @@ -508,10 +513,10 @@ private List<AssignedLocations> getOrganizationLocationsAssigned(boolean include
bean.setFromDate(new Date());
if (random.nextBoolean())
bean.setToDate(new Date());
organizationAssigmentBeans.add(bean);
organizationAssignmentBeans.add(bean);

}
return organizationAssigmentBeans;
return organizationAssignmentBeans;

}

Expand Down
Loading

0 comments on commit aa7eca4

Please sign in to comment.