Skip to content

Commit

Permalink
Merge pull request #1114 from opensrp/alternate_phone_number
Browse files Browse the repository at this point in the history
Enable Client search by alternate phone number attribute and fix search by phone number (v2.2)
  • Loading branch information
SebaMutuku authored Jul 4, 2022
2 parents 4a2f5e9 + 5bd71a6 commit 90c9432
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 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>2.10.1-SNAPSHOT</version>
<version>2.10.2-SNAPSHOT</version>
<name>opensrp-server-web</name>
<description>OpenSRP Server Web Application</description>
<url>https://github.com/OpenSRP/opensrp-server-web</url>
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/opensrp/web/rest/SearchResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public List<Client> search(HttpServletRequest request) throws ParseException {//
String middleName = getStringFilter(MIDDLE_NAME, request);
String lastName = getStringFilter(LAST_NAME, request);
Optional<String> phoneNumber = Optional.ofNullable(getStringFilter(PHONE_NUMBER, request));
Optional<String> altPhoneNumber = Optional.ofNullable(getStringFilter(ALT_PHONE_NUMBER, request));
Optional<String> alternateName = Optional.ofNullable(getStringFilter(ALT_NAME, request));
ClientSearchBean searchBean = new ClientSearchBean();
searchBean.setNameLike(getStringFilter(NAME, request));
Expand All @@ -92,23 +93,16 @@ public List<Client> search(HttpServletRequest request) throws ParseException {//
searchBean.setLastEditFrom(lastEdit[0]);
searchBean.setLastEditTo(lastEdit[1]);
}
Map<String, String> attributeMap = null;
Map<String, String> attributeMap = new HashMap<>();
String attributes = getStringFilter(ATTRIBUTE, request);
if (!StringUtils.isBlank(attributes)) {
String attributeType = StringUtils.isBlank(attributes) ? null : attributes.split(":", -1)[0];
String attributeValue = StringUtils.isBlank(attributes) ? null : attributes.split(":", -1)[1];

attributeMap = new HashMap<>();
attributeMap.put(attributeType, attributeValue);
}
if (phoneNumber.isPresent()) {
attributeMap = new HashMap<>();
attributeMap.put(ALT_PHONE_NUMBER, phoneNumber.get());
}
if (alternateName.isPresent()) {
attributeMap = new HashMap<>();
attributeMap.put(ALT_NAME, alternateName.get());
}
phoneNumber.ifPresent(phoneValue -> attributeMap.put(PHONE_NUMBER, phoneValue));
altPhoneNumber.ifPresent(altPhoneValue -> attributeMap.put(ALT_PHONE_NUMBER, altPhoneValue));
alternateName.ifPresent(altNameValue -> attributeMap.put(ALT_NAME, altNameValue));
searchBean.setAttributes(attributeMap);

Map<String, String> identifierMap = null;
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/org/opensrp/web/rest/SearchResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.opensrp.repository.PlanRepository;
import org.opensrp.service.TaskGenerator;
import org.opensrp.service.ExportEventDataMapper;
import org.smartregister.domain.Client;
import org.opensrp.repository.ClientsRepository;
import org.opensrp.repository.EventsRepository;
import org.opensrp.repository.PlanRepository;
import org.opensrp.repository.SearchRepository;
import org.opensrp.service.ClientService;
import org.opensrp.service.EventService;
import org.opensrp.service.SearchService;
import org.opensrp.service.*;
import org.opensrp.web.rest.it.TestWebContextLoader;
import org.opensrp.web.utils.SearchHelper;
import org.smartregister.domain.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -95,9 +91,11 @@ public void shouldSearchClient() throws ParseException {
mockHttpServletRequest = new MockHttpServletRequest();
mockHttpServletRequest.addParameter("ff", "ona");
mockHttpServletRequest.addParameter("phone_number", phoneNumber);
mockHttpServletRequest.addParameter("alt_phone_number", phoneNumber);
mockHttpServletRequest.addParameter("alt_name", firstName);
mockHttpServletRequest.addParameter("attribute", "next_contact_date:2022-06-15");
mockHttpServletRequest.addParameter("dob", String.valueOf(birthDate));
mockHttpServletRequest.addParameter("identifier", "fsdf"+":"+ "sfdf");
SearchResource searchResource=new SearchResource(searchService,clientService,eventService);
List<Client> clients = searchResource.search(mockHttpServletRequest);
Assert.assertNotNull(clients);
Expand Down
60 changes: 55 additions & 5 deletions src/test/java/org/opensrp/web/rest/it/SearchResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import org.springframework.mock.web.MockHttpServletRequest;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -156,6 +155,7 @@ public void shouldSearchClientWithBirthDateWithoutColons() throws Exception {
Client actualClient = mapper.treeToValue(actualObj.get(0), Client.class);
assertEquals(expectedClient, actualClient);
}

@Test
public void shouldSearchClientWithMobileNumber() throws Exception {
Client expectedClient = createOneSearchableClient();
Expand All @@ -164,6 +164,27 @@ public void shouldSearchClientWithMobileNumber() throws Exception {
Client actualClient = mapper.treeToValue(actualObj.get(0), Client.class);
assertEquals(expectedClient, actualClient);
}

@Test
public void shouldSearchClientWithoutAltMobileNumber() throws Exception {
Client expectedClient = createDifferentClient();
String searchQuery = "alt_phone_number=" + phoneNumber;
JsonNode actualObj = searchClient(searchQuery);
Client actualClient = mapper.treeToValue(actualObj.get(1), Client.class);
assertNotEquals(expectedClient.toString().length(), actualClient.toString().length());
assertNotEquals(expectedClient.getAttributes().get("alt_phone_number"),
actualClient.getAttributes().get("alt_phone_number"));
}

@Test
public void shouldSearchClientAltWithMobileNumber() throws Exception {
Client expectedClient = createOneSearchableClient();
String searchQuery = "alt_phone_number=" + phoneNumber;
JsonNode actualObj = searchClient(searchQuery);
Client actualClient = mapper.treeToValue(actualObj.get(0), Client.class);
assertEquals(expectedClient, actualClient);
}

@Test
public void shouldSearchClientByAltName() throws Exception {
mockHttpServletRequest= new MockHttpServletRequest();
Expand Down Expand Up @@ -226,19 +247,48 @@ private Client createOneSearchableClient() {
otherClient.withIdentifier("fsdf", "sfdf");
otherClient.withAttribute("sfdf", "sfdf");
otherClient.withAttribute("alt_phone_number",phoneNumber);
otherClient.withAttribute("phone_number",phoneNumber);
otherClient.withAttribute("alt_name","ona");
Client otherClient2 = (Client) new Client("3").withFirstName("dd").withMiddleName("fdf").withLastName("sfd")
.withGender(FEMALE).withBirthdate(birthDate, false).withDeathdate(deathDate, true).withAddress(address);
;
otherClient2.setDateCreated(DATE_CREATED);
otherClient2.withIdentifier("hg", "ghgh");
otherClient2.withAttribute("hg", "hgh");
otherClient2.withAttribute("alt_phone_number",phoneNumber);
otherClient2.withAttribute("alt_name","ona");
otherClient2.withAttribute("alt_phone_number", phoneNumber);
otherClient2.withAttribute("phone_number", phoneNumber);
otherClient2.withAttribute("alt_name", "ona");

addObjectToRepository(asList(expectedClient, otherClient, otherClient2), allClients);

return expectedClient;
}

private Client createDifferentClient() {
Client expectedClient = (Client) new Client("1").withFirstName(firstName).withMiddleName(MIDDLE_NAME)
.withLastName(LAST_NAME).withGender(male).withBirthdate(birthDate, false).withDeathdate(deathDate, true)
.withAddress(address);
expectedClient.setDateCreated(DATE_CREATED);
expectedClient.withIdentifier(IDENTIFIER_TYPE, IDENTIFIER);
expectedClient.withAttribute(ATTRIBUTES_NAME, ATTRIBUTES_VALUE);

Client otherClient = (Client) new Client("2").withFirstName("ff").withMiddleName("fd").withLastName("sfdf")
.withGender(FEMALE).withBirthdate(birthDate, false).withDeathdate(deathDate, true).withAddress(address);
otherClient.setDateCreated(DATE_CREATED);
otherClient.withIdentifier("fsdf", "sfdf");
otherClient.withAttribute("sfdf", "sfdf");
otherClient.withAttribute("phone_number", "0727000000");
otherClient.withAttribute("alt_name", "ona");
Client otherClient2 = (Client) new Client("3").withFirstName("dd").withMiddleName("fdf").withLastName("sfd")
.withGender(FEMALE).withBirthdate(birthDate, false).withDeathdate(deathDate, true).withAddress(address);
otherClient2.setDateCreated(DATE_CREATED);
otherClient2.withIdentifier("hg", "ghgh");
otherClient2.withAttribute("hg", "hgh");
otherClient2.withAttribute("alt_phone_number", "0727000000");
otherClient2.withAttribute("phone_number", "0727000000");
otherClient2.withAttribute("alt_name", "ona");

addObjectToRepository(asList(expectedClient, otherClient, otherClient2), allClients);
return expectedClient;
}

}

0 comments on commit 90c9432

Please sign in to comment.