Skip to content

Commit

Permalink
Merge pull request #449 from OpenSRP/769-practitioners-end-point
Browse files Browse the repository at this point in the history
769 : Return list of practitioners via another practitioner's identifier and code

Failing tests to be fixed as part of [#448](#448)
  • Loading branch information
ndegwamartin authored Mar 18, 2021
2 parents 253aad3 + bbf2752 commit c989a45
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.9.6-SNAPSHOT</version>
<version>2.9.7-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>http://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/opensrp/repository/PractitionerRepository.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface PractitionerRepository extends BaseRepository<Practitioner> {
Practitioner getPractitionerByUsername(String username);

List<Practitioner> getAllPractitioners(PractitionerSearchBean practitionerSearchBean);

List<Practitioner> getAllPractitionersByIdentifiers(List<String> practitionerIdentifiers);
}
2 changes: 2 additions & 0 deletions src/main/java/org/opensrp/repository/PractitionerRoleRepository.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ void assignPractitionerRole(Long organizationId,Long practitionerId, String prac
PractitionerRole practitionerRole);

List<PractitionerRole> getAllPractitionerRoles(PractitionerRoleSearchBean practitionerRoleSearchBean);

public List<PractitionerRole> getPractitionerRolesByOrgIdAndCode(Long organizationId, String code);
}
12 changes: 11 additions & 1 deletion src/main/java/org/opensrp/repository/postgres/PractitionerRepositoryImpl.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ public List<Practitioner> getAllPractitioners(PractitionerSearchBean practitione
return convert(pgPractitionerList);
}

@Override
@Override
public List<Practitioner> getAllPractitionersByIdentifiers(List<String> practitionerIdentifiers) {
PractitionerSearchBean practitionerSearchBean = new PractitionerSearchBean();
Pair<Integer, Integer> pageSizeAndOffset = RepositoryUtil.getPageSizeAndOffset(practitionerSearchBean);
PractitionerExample practitionerExample = new PractitionerExample();
practitionerExample.createCriteria().andDateDeletedIsNull().andIdentifierIn(practitionerIdentifiers);
List<org.opensrp.domain.postgres.Practitioner> pgPractitionerList = practitionerMapper.selectMany(practitionerExample, pageSizeAndOffset.getRight(), pageSizeAndOffset.getLeft());
return convert(pgPractitionerList);
}

@Override
protected Long retrievePrimaryKey(Practitioner practitioner) {
Object uniqueId = getUniqueField(practitioner);
if (uniqueId == null) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/opensrp/repository/postgres/PractitionerRoleRepositoryImpl.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ public List<PractitionerRole> getAllPractitionerRoles(PractitionerRoleSearchBean
return convert(pgPractitionerRoleList);
}

@Override
public List<PractitionerRole> getPractitionerRolesByOrgIdAndCode(Long organizationId, String code) {
PractitionerRoleSearchBean practitionerRoleSearchBean = new PractitionerRoleSearchBean();
Pair<Integer, Integer> pageSizeAndOffset = RepositoryUtil.getPageSizeAndOffset(practitionerRoleSearchBean);
PractitionerRoleExample practitionerRoleExample = new PractitionerRoleExample();
practitionerRoleExample.createCriteria().andOrganizationIdEqualTo(organizationId).andCodeEqualTo(code);
List<org.opensrp.domain.postgres.PractitionerRole> pgPractitionerRoleList = practitionerRoleMapper.selectMany(practitionerRoleExample, pageSizeAndOffset.getRight(), pageSizeAndOffset.getLeft());
return convert(pgPractitionerRoleList);
}

private boolean isExistingPractitionerRole(Long organizationId, Long practitionerId, String code,
org.opensrp.domain.postgres.PractitionerRole practitionerRole) {
if (organizationId != null && practitionerId != null) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/opensrp/service/PractitionerRoleService.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@ public void validateIdentifier(String identifier) {
throw new IllegalArgumentException("Practitioner Role Identifier not specified");
}

public List<PractitionerRole> getPractitionerRolesByOrgIdAndCode(Long organizationId, String code) {
return practitionerRoleRepository.getPractitionerRolesByOrgIdAndCode(organizationId, code);
}

}
22 changes: 22 additions & 0 deletions src/main/java/org/opensrp/service/PractitionerService.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,26 @@ public Long getPractitionerIdByIdentifier(String identifier) {
org.opensrp.domain.postgres.Practitioner pgPractitioner = getPgPractitioner(identifier);
return pgPractitioner != null ? pgPractitioner.getId() : null;
}

public List<Practitioner> getPractitionersByIdentifiers(List<String> practitionerIdentifiers) {
return practitionerRepository.getAllPractitionersByIdentifiers(practitionerIdentifiers);
}

public List<Practitioner> getAssignedPractitionersByIdentifierAndCode(String practitionerIdentifier, String code) {
List<Long> organizationIds = new ArrayList<>();
for (PractitionerRole practitionerRole : practitionerRoleService.getPgRolesForPractitioner(practitionerIdentifier)) {
organizationIds.add(practitionerRole.getOrganizationId());
}

// Retrieved teams, now get all members of the team
List<org.smartregister.domain.PractitionerRole> practitionerRoles = new ArrayList<>();
List<String> practitionerIdentifiers = new ArrayList<>();
for(Long organizationId : organizationIds) {
practitionerRoles = practitionerRoleService.getPractitionerRolesByOrgIdAndCode(organizationId,code);
practitionerRoles.stream().forEach(practitionerRole -> practitionerIdentifiers.add(practitionerRole.getPractitionerIdentifier()));
}

// Now get all practitioners by the given Ids
return getPractitionersByIdentifiers(practitionerIdentifiers);
}
}
18 changes: 18 additions & 0 deletions src/test/java/org/opensrp/repository/postgres/PractitionerRepositoryTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

import org.junit.BeforeClass;
Expand Down Expand Up @@ -355,4 +356,21 @@ public void testGetAllPractitioners() {
assertEquals("practitoner-2-identifier",practitioners.get(1).getIdentifier());
}

@Test
public void testGetAllPractitionersByIdentifiers() {
Practitioner practitioner1 = initTestPractitioner1();
practitionerRepository.add(practitioner1);
Practitioner practitioner2 = initTestPractitioner2();
practitionerRepository.add(practitioner2);

List<String> practitionerIdentifiers = new ArrayList<>();
practitionerIdentifiers.add("practitoner-1-identifier");
practitionerIdentifiers.add("practitoner-2-identifier");
List<Practitioner> practitioners = practitionerRepository.getAllPractitionersByIdentifiers(practitionerIdentifiers);
assertNotNull(practitioners);
assertEquals(2,practitioners.size());
assertEquals("Practitioner",practitioners.get(0).getName());
assertEquals("Second Practitioner",practitioners.get(1).getName());
}

}
36 changes: 36 additions & 0 deletions src/test/java/org/opensrp/repository/postgres/PractitionerRoleRepositoryTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.opensrp.domain.Organization;
import org.opensrp.repository.OrganizationRepository;
import org.smartregister.domain.PractitionerRole;
import org.smartregister.domain.PractitionerRoleCode;
import org.opensrp.repository.PractitionerRoleRepository;
Expand All @@ -13,6 +15,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -24,6 +27,9 @@ public class PractitionerRoleRepositoryTest extends BaseRepositoryTest {
@Autowired
private PractitionerRoleRepository practitionerRoleRepository;

@Autowired
private OrganizationRepository organizationRepository;

@BeforeClass
public static void bootStrap() {
tableNames= Arrays.asList("team.organization","team.practitioner","team.practitioner_role");
Expand Down Expand Up @@ -292,6 +298,36 @@ public void testGetAllPractitionerRoles() {
assertEquals("pr2-identifier", practitionerRoles.get(1).getIdentifier());
}

@Test
public void testGetPractitionerRolesByOrgIdAndCode() {
String identifier = UUID.randomUUID().toString();
Organization organization = new Organization();
organization.setIdentifier(identifier);
organization.setName("Ateam");
organization.setPartOf(1l);
organization.setActive(true);
organizationRepository.add(organization);

Organization addedOrganization = organizationRepository.get(identifier);

PractitionerRole practitionerRole1 = initTestPractitionerRole1();
practitionerRole1.setOrganizationIdentifier(identifier);
practitionerRoleRepository.add(practitionerRole1);

PractitionerRole practitionerRole2 = initTestPractitionerRole2();
practitionerRole2.setOrganizationIdentifier(identifier);
practitionerRoleRepository.add(practitionerRole2);

List<PractitionerRole> practitionerRoles = practitionerRoleRepository.getPractitionerRolesByOrgIdAndCode(addedOrganization.getId(), "pr2Code");
assertNotNull(practitionerRoles);
assertEquals(1l, practitionerRoles.size());
assertEquals("pr2-identifier", practitionerRoles.get(0).getIdentifier());
assertEquals("p2-identifier", practitionerRoles.get(0).getPractitionerIdentifier());
assertEquals(identifier, practitionerRoles.get(0).getOrganizationIdentifier());
assertEquals("pr2Code", practitionerRoles.get(0).getCode().getText());

}

private static PractitionerRole initTestPractitionerRole1(){
PractitionerRole practitionerRole = new PractitionerRole();
practitionerRole.setIdentifier("pr1-identifier");
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/opensrp/service/PractitionerRoleServiceTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ public void testGetPgRolesForPractitionerWithNullIdentifier() {
verify(practitionerRoleRepository, never()).getPgRolesForPractitioner(anyString());
}

@Test
public void testGetPractitionerRolesByOrgIdAndCode() {
PractitionerRole practitionerRole1 = initTestPractitionerRole();
practitionerRoleRepository.add(practitionerRole1);

PractitionerRole practitionerRole2 = initTestPractitionerRole2();
practitionerRoleRepository.add(practitionerRole2);

List<PractitionerRole> practitionerRoles = new ArrayList<>();
practitionerRoles.add(practitionerRole1);
practitionerRoles.add(practitionerRole2);

when(practitionerRoleRepository.getPractitionerRolesByOrgIdAndCode(anyLong(), anyString())).thenReturn(practitionerRoles);
assertNotNull(practitionerRoles);
assertEquals(2l, practitionerRoles.size());
assertEquals("pr1-identifier", practitionerRoles.get(0).getIdentifier());
assertEquals("p1-identifier", practitionerRoles.get(0).getPractitionerIdentifier());
assertEquals("pr2-identifier", practitionerRoles.get(1).getIdentifier());
assertEquals("p2-identifier", practitionerRoles.get(1).getPractitionerIdentifier());

}

private static PractitionerRole initTestPractitionerRole(){
PractitionerRole practitionerRole = new PractitionerRole();
practitionerRole.setIdentifier("pr1-identifier");
Expand All @@ -207,4 +229,16 @@ private static PractitionerRole initTestPractitionerRole(){
practitionerRole.setCode(code);
return practitionerRole;
}

private static PractitionerRole initTestPractitionerRole2(){
PractitionerRole practitionerRole = new PractitionerRole();
practitionerRole.setIdentifier("pr2-identifier");
practitionerRole.setActive(true);
practitionerRole.setOrganizationIdentifier("org1");
practitionerRole.setPractitionerIdentifier("p2-identifier");
PractitionerRoleCode code = new PractitionerRoleCode();
code.setText("pr2Code");
practitionerRole.setCode(code);
return practitionerRole;
}
}
91 changes: 91 additions & 0 deletions src/test/java/org/opensrp/service/PractitionerServiceTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import org.opensrp.repository.PractitionerRepository;
import org.opensrp.search.PractitionerSearchBean;
import org.powermock.modules.junit4.PowerMockRunner;
import org.smartregister.domain.PractitionerRole;
import org.smartregister.domain.PractitionerRoleCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -165,6 +168,60 @@ public void testGetPractionerByUsername() {

}

@Test
public void testGetPractitionersByIdentifiers() {
Practitioner practitioner1 = initTestPractitioner();
Practitioner practitioner2 = initTestPractitioner2();
List<Practitioner> practitioners = new ArrayList<>();
practitioners.add(practitioner1);
practitioners.add(practitioner2);

when(practitionerRepository.getAllPractitionersByIdentifiers(any(List.class))).thenReturn(practitioners);

List<String> identifiers = new ArrayList<>();
identifiers.add("practitoner-1-identifier");
identifiers.add("practitoner-2-identifier");
List<Practitioner> actual = practitionerService.getPractitionersByIdentifiers(identifiers);

verify(practitionerRepository).getAllPractitionersByIdentifiers(identifiers);
assertEquals(practitioners.size(), actual.size());
assertEquals(practitioners.get(0).getName(), actual.get(0).getName());
assertEquals(practitioners.get(1).getName(), actual.get(1).getName());
}

@Test
public void testGetAssignedPractitionersByIdentifierAndCode() {
PractitionerRole practitionerRole = initTestPractitionerRole1();
PractitionerRole practitionerRole2 = initTestPractitionerRole2();

List<PractitionerRole> practitionerRoles = new ArrayList<>();
practitionerRoles.add(practitionerRole);
practitionerRoles.add(practitionerRole2);

List<org.opensrp.domain.postgres.PractitionerRole> roles = new ArrayList<>();
List<Long> organizationIds = Arrays.asList(11l, 40l,7667l);
for(long id:organizationIds) {
org.opensrp.domain.postgres.PractitionerRole role = new org.opensrp.domain.postgres.PractitionerRole();
role.setOrganizationId(id);
roles.add(role);
}

Practitioner practitioner1 = initTestPractitioner();
Practitioner practitioner2 = initTestPractitioner2();
List<Practitioner> practitioners = new ArrayList<>();
practitioners.add(practitioner1);
practitioners.add(practitioner2);
when(practitionerRoleService.getPgRolesForPractitioner(anyString())).thenReturn(roles);
when(practitionerRoleService.getPractitionerRolesByOrgIdAndCode(anyLong(),anyString())).thenReturn(practitionerRoles);
when(practitionerRepository.getAllPractitionersByIdentifiers(any(List.class))).thenReturn(practitioners);

List<Practitioner> actual = practitionerService.getAssignedPractitionersByIdentifierAndCode("test-identifier","pr1Code");

assertEquals(practitioners.size(), actual.size());
assertEquals(practitioners.get(0).getName(), actual.get(0).getName());
assertEquals(practitioners.get(1).getName(), actual.get(1).getName());
}

private Practitioner initTestPractitioner(){
Practitioner practitioner = new Practitioner();
practitioner.setIdentifier("practitoner-1-identifier");
Expand All @@ -174,4 +231,38 @@ private Practitioner initTestPractitioner(){
practitioner.setUserId("user1");
return practitioner;
}

private Practitioner initTestPractitioner2(){
Practitioner practitioner = new Practitioner();
practitioner.setIdentifier("practitoner-2-identifier");
practitioner.setActive(true);
practitioner.setName("Practitioner 2");
practitioner.setUsername("Practioner2");
practitioner.setUserId("user2");
return practitioner;
}

private static PractitionerRole initTestPractitionerRole1(){
PractitionerRole practitionerRole = new PractitionerRole();
practitionerRole.setIdentifier("pr1-identifier");
practitionerRole.setActive(true);
practitionerRole.setOrganizationIdentifier("org1");
practitionerRole.setPractitionerIdentifier("p1-identifier");
PractitionerRoleCode code = new PractitionerRoleCode();
code.setText("pr1Code");
practitionerRole.setCode(code);
return practitionerRole;
}

private static PractitionerRole initTestPractitionerRole2(){
PractitionerRole practitionerRole = new PractitionerRole();
practitionerRole.setIdentifier("pr2-identifier");
practitionerRole.setActive(true);
practitionerRole.setOrganizationIdentifier("org1");
practitionerRole.setPractitionerIdentifier("p2-identifier");
PractitionerRoleCode code = new PractitionerRoleCode();
code.setText("pr2Code");
practitionerRole.setCode(code);
return practitionerRole;
}
}

0 comments on commit c989a45

Please sign in to comment.