diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResource.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResource.java index 222b74f801..d5a068ad51 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResource.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResource.java @@ -1,6 +1,14 @@ package org.bahmni.module.bahmnicore.web.v1_0.controller; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + import org.apache.commons.beanutils.ConversionException; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -47,14 +55,6 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - /** * Controller for REST web service access to * the Search resource. @@ -154,18 +154,18 @@ private ResponseEntity getIdentifierErrorMessageResponseEntity() throws @RequestMapping(method = RequestMethod.POST, value = "/{uuid}") @ResponseBody public ResponseEntity update(@PathVariable("uuid") String uuid, @RequestBody SimpleObject propertiesToUpdate) throws Exception { - PatientProfile delegate = null; + PatientProfile patientProfile = null; try { - delegate = mapForUpdatePatient(uuid, propertiesToUpdate); + patientProfile = mapForUpdatePatient(uuid, propertiesToUpdate); } catch (APIAuthenticationException e) { return new ResponseEntity(RestUtil.wrapErrorResponse(e, "User is logged in but doesn't have the relevant privilege "), HttpStatus.FORBIDDEN); } - setConvertedProperties(delegate, propertiesToUpdate, getUpdatableProperties(), true); - delegate.setRelationships(getRelationships(propertiesToUpdate, delegate.getPatient())); + setConvertedProperties(patientProfile, propertiesToUpdate, getUpdatableProperties(), true); + patientProfile.setRelationships(getRelationships(propertiesToUpdate, patientProfile.getPatient())); try { - delegate = emrPatientProfileService.save(delegate); - setRelationships(delegate); - return new ResponseEntity<>(ConversionUtil.convertToRepresentation(delegate, Representation.FULL), HttpStatus.OK); + patientProfile = emrPatientProfileService.save(patientProfile); + setRelationships(patientProfile); + return new ResponseEntity<>(ConversionUtil.convertToRepresentation(patientProfile, Representation.FULL), HttpStatus.OK); } catch (ContextAuthenticationException e) { return new ResponseEntity(RestUtil.wrapErrorResponse(e, e.getMessage()), HttpStatus.FORBIDDEN); } catch (ValidationException e) { @@ -203,11 +203,11 @@ private PatientProfile mapForCreatePatient(SimpleObject propertiesToCreate) { } private PatientProfile mapForUpdatePatient(String uuid, SimpleObject propertiesToUpdate) { - if (propertiesToUpdate.get("patient") == null || !(propertiesToUpdate.get("patient") instanceof Map)) { + if (propertiesToUpdate.get("patient") == null || !(propertiesToUpdate.get("patient") instanceof Map)) { throw new ConversionException("The patient property is missing"); } - PatientProfile delegate = new PatientProfile(); + PatientProfile patientProfile = new PatientProfile(); PatientResource1_8 patientResource1_9 = (PatientResource1_8) Context.getService(RestService.class).getResourceBySupportedClass(Patient.class); Patient patient = patientResource1_9.getPatientForUpdate(uuid, (Map) propertiesToUpdate.get("patient")); @@ -219,10 +219,10 @@ private PatientProfile mapForUpdatePatient(String uuid, SimpleObject propertiesT PatientIdentifier patientIdentifier = (PatientIdentifier) ConversionUtil.convert(identifierProperties, PatientIdentifier.class); patient.addIdentifier(patientIdentifier); } - delegate.setPatient(patient); + patientProfile.setPatient(patient); propertiesToUpdate.removeProperty("patient"); - return delegate; + return patientProfile; } private List getRelationships(SimpleObject propertiesToCreate, Person currentPerson) { diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResourceTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResourceTest.java index 3370598943..f22982fac0 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResourceTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniPatientProfileResourceTest.java @@ -1,5 +1,26 @@ package org.bahmni.module.bahmnicore.web.v1_0.controller; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.doReturn; +import static org.powermock.api.mockito.PowerMockito.doThrow; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.spy; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.Before; @@ -10,13 +31,16 @@ import org.openmrs.Patient; import org.openmrs.PatientIdentifier; import org.openmrs.Person; +import org.openmrs.PersonName; import org.openmrs.Relationship; +import org.openmrs.RelationshipType; import org.openmrs.api.APIAuthenticationException; import org.openmrs.api.AdministrationService; import org.openmrs.api.PatientService; import org.openmrs.api.PersonService; import org.openmrs.api.context.Context; import org.openmrs.messagesource.MessageSourceService; +import org.openmrs.module.emrapi.encounter.DateMapper; import org.openmrs.module.emrapi.patient.EmrPatientProfileService; import org.openmrs.module.emrapi.patient.PatientProfile; import org.openmrs.module.idgen.webservices.services.IdentifierSourceServiceWrapper; @@ -24,143 +48,161 @@ import org.openmrs.module.webservices.rest.web.api.RestService; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PersonResource1_8; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.RelationShipTypeResource1_8; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.ResponseEntity; -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.spy; -import static org.powermock.api.mockito.PowerMockito.doThrow; - // TODO: 13/09/16 This is wrong way of writing test. We should mock the external dependency in resource but we ended up mocking all internal dependencies. For eg: MessageSourceService @PrepareForTest({Context.class, BahmniPatientProfileResource.class}) @RunWith(PowerMockRunner.class) public class BahmniPatientProfileResourceTest { - @Mock - private EmrPatientProfileService emrPatientProfileService; - - @Mock - private RestService restService; - - @Mock - PatientResource1_8 patientResource1_8; - - @Mock - private AdministrationService administrationService; - - @Mock - private PatientService patientService; - - @Mock - private PersonService personService; - @Mock - private MessageSourceService messageSourceService; - - @Mock - private IdentifierSourceServiceWrapper identifierSourceServiceWrapper; - - private BahmniPatientProfileResource bahmniPatientProfileResource; - private SimpleObject propertiesToCreate; - - @Before - public void setUp() throws IOException { - MockitoAnnotations.initMocks(this); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("patient.json").getFile()); - String jsonString = FileUtils.readFileToString(file); - propertiesToCreate = new SimpleObject().parseJson(jsonString); - Patient patient = new Patient(); - patient.setGender("M"); - mockStatic(Context.class); - PowerMockito.when(Context.getService(RestService.class)).thenReturn(restService); - PowerMockito.when(Context.getPersonService()).thenReturn(personService); - PowerMockito.when(Context.getMessageSourceService()).thenReturn(messageSourceService); - PowerMockito.when(restService.getResourceBySupportedClass(Patient.class)).thenReturn(patientResource1_8); - PowerMockito.when(patientResource1_8.getPatient(any(SimpleObject.class))).thenReturn(patient); - PowerMockito.when(patientResource1_8.getPatientForUpdate(anyString(), any(SimpleObject.class))).thenReturn(patient); - } - - @Test - public void createPatient() throws Exception { - bahmniPatientProfileResource = new BahmniPatientProfileResource(emrPatientProfileService, identifierSourceServiceWrapper); - BahmniPatientProfileResource bahmniPatientProfileResourceSpy = spy(this.bahmniPatientProfileResource); - PatientProfile delegate = mock(PatientProfile.class); - when(identifierSourceServiceWrapper.generateIdentifierUsingIdentifierSourceUuid("dead-cafe", "")).thenReturn("BAH300010"); - doReturn(delegate).when(bahmniPatientProfileResourceSpy, "mapForCreatePatient", propertiesToCreate); - when(emrPatientProfileService.save(delegate)).thenReturn(delegate); - when(Context.getAdministrationService()).thenReturn(administrationService); - when(Context.getPatientService()).thenReturn(patientService); - Patient patient = mock(Patient.class); - when(patient.getUuid()).thenReturn("patientUuid"); - when(delegate.getPatient()).thenReturn(patient); - PatientIdentifier patientIdentifier = mock(PatientIdentifier.class); - Set patientIdentifiers = new HashSet<>(); - patientIdentifiers.add(patientIdentifier); - when(patient.getIdentifiers()).thenReturn(patientIdentifiers); - doNothing().when(bahmniPatientProfileResourceSpy).setConvertedProperties(any(PatientProfile.class), any(SimpleObject.class), any(DelegatingResourceDescription.class), any(Boolean.class)); - Person person = new Person(); - person.setUuid("personUuid"); - when(personService.getPersonByUuid("patientUuid")).thenReturn(person); - List relationships = Arrays.asList(); - when(personService.getRelationshipsByPerson(person)).thenReturn(relationships); - - ResponseEntity response = bahmniPatientProfileResourceSpy.create(false, propertiesToCreate); - - Assert.assertEquals(200, response.getStatusCode().value()); - verify(identifierSourceServiceWrapper, times(1)).generateIdentifierUsingIdentifierSourceUuid("dead-cafe", ""); - verify(personService, times(1)).getPersonByUuid("patientUuid"); - verify(delegate, times(1)).setRelationships(relationships); - } - - @Test - public void updatePatient() throws Exception { - bahmniPatientProfileResource = new BahmniPatientProfileResource(emrPatientProfileService, identifierSourceServiceWrapper); - BahmniPatientProfileResource spy = spy(bahmniPatientProfileResource); - PatientProfile delegate = mock(PatientProfile.class); - doReturn(delegate).when(spy, "mapForUpdatePatient", anyString(), any(SimpleObject.class)); - when(emrPatientProfileService.save(delegate)).thenReturn(delegate); - doNothing().when(spy).setConvertedProperties(any(PatientProfile.class), any(SimpleObject.class), any(DelegatingResourceDescription.class), any(Boolean.class)); - Person person = new Person(); - person.setUuid("personUuid"); - when(personService.getPersonByUuid("patientUuid")).thenReturn(person); - List relationships = Collections.emptyList(); - when(personService.getRelationshipsByPerson(person)).thenReturn(relationships); - Patient patient = mock(Patient.class); - when(patient.getUuid()).thenReturn("patientUuid"); - when(delegate.getPatient()).thenReturn(patient); - - ResponseEntity response = spy.update("someUuid", propertiesToCreate); - - Assert.assertEquals(200, response.getStatusCode().value()); - verify(personService, times(1)).getPersonByUuid("patientUuid"); - verify(delegate, times(2)).setRelationships(relationships); - } - - @Test - public void shouldThrowExceptionWhenPatientIsNotHavingProperPrivilege() throws Exception { - bahmniPatientProfileResource = new BahmniPatientProfileResource(emrPatientProfileService, identifierSourceServiceWrapper); - BahmniPatientProfileResource spy = spy(bahmniPatientProfileResource); - doThrow(new APIAuthenticationException()).when(spy, "mapForUpdatePatient", anyString(), any(SimpleObject.class)); - - ResponseEntity response = spy.update("someUuid", propertiesToCreate); - Assert.assertEquals(403,response.getStatusCode().value()); - } -} \ No newline at end of file + @Mock + private EmrPatientProfileService emrPatientProfileService; + + @Mock + private RestService restService; + + @Mock + PatientResource1_8 patientResource1_8; + + @Mock + PersonResource1_8 personResource1_8; + + @Mock + RelationShipTypeResource1_8 relationShipTypeResource1_8; + + @Mock + private AdministrationService administrationService; + + @Mock + private PatientService patientService; + + @Mock + private PersonService personService; + + @Mock + private MessageSourceService messageSourceService; + + @Mock + private IdentifierSourceServiceWrapper identifierSourceServiceWrapper; + + private BahmniPatientProfileResource bahmniPatientProfileResource; + private SimpleObject propertiesToCreate; + + @Before + public void setUp() throws IOException { + MockitoAnnotations.initMocks(this); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("patient.json").getFile()); + String jsonString = FileUtils.readFileToString(file); + propertiesToCreate = new SimpleObject().parseJson(jsonString); + Patient patient = new Patient(); + patient.setGender("M"); + mockStatic(Context.class); + PowerMockito.when(Context.getService(RestService.class)).thenReturn(restService); + PowerMockito.when(Context.getPersonService()).thenReturn(personService); + PowerMockito.when(Context.getMessageSourceService()).thenReturn(messageSourceService); + PowerMockito.when(restService.getResourceBySupportedClass(Patient.class)).thenReturn(patientResource1_8); + PowerMockito.when(patientResource1_8.getPatient(any(SimpleObject.class))).thenReturn(patient); + PowerMockito.when(patientResource1_8.getPatientForUpdate(anyString(), any(SimpleObject.class))).thenReturn(patient); + PowerMockito.when(Context.getService(RestService.class).getResourceBySupportedClass(Person.class)).thenReturn(personResource1_8); + PowerMockito.when(personResource1_8.getByUniqueId("patientUuid")).thenReturn(patient); + PowerMockito.when(Context.getService(RestService.class).getResourceBySupportedClass(RelationshipType.class)).thenReturn(relationShipTypeResource1_8); + + RelationshipType rt = new RelationshipType(); + rt.setUuid("2a5f4ff4-a179-4b8a-aa4c-40f71956eabc"); + PowerMockito.when(relationShipTypeResource1_8.getByUniqueId(anyString())).thenReturn(rt); + } + + @Test + public void createPatient() throws Exception { + bahmniPatientProfileResource = new BahmniPatientProfileResource(emrPatientProfileService, identifierSourceServiceWrapper); + BahmniPatientProfileResource bahmniPatientProfileResourceSpy = spy(this.bahmniPatientProfileResource); + PatientProfile delegate = mock(PatientProfile.class); + when(identifierSourceServiceWrapper.generateIdentifierUsingIdentifierSourceUuid("dead-cafe", "")).thenReturn("BAH300010"); + doReturn(delegate).when(bahmniPatientProfileResourceSpy, "mapForCreatePatient", propertiesToCreate); + when(emrPatientProfileService.save(delegate)).thenReturn(delegate); + when(Context.getAdministrationService()).thenReturn(administrationService); + when(Context.getPatientService()).thenReturn(patientService); + Patient patient = mock(Patient.class); + when(patient.getUuid()).thenReturn("patientUuid"); + when(delegate.getPatient()).thenReturn(patient); + PatientIdentifier patientIdentifier = mock(PatientIdentifier.class); + Set patientIdentifiers = new HashSet<>(); + patientIdentifiers.add(patientIdentifier); + when(patient.getIdentifiers()).thenReturn(patientIdentifiers); + doNothing().when(bahmniPatientProfileResourceSpy).setConvertedProperties(any(PatientProfile.class), any(SimpleObject.class), any(DelegatingResourceDescription.class), any(Boolean.class)); + Person person = new Person(); + person.setUuid("personUuid"); + when(personService.getPersonByUuid("patientUuid")).thenReturn(person); + List relationships = Arrays.asList(); + when(personService.getRelationshipsByPerson(person)).thenReturn(relationships); + + ResponseEntity response = bahmniPatientProfileResourceSpy.create(false, propertiesToCreate); + + Assert.assertEquals(200, response.getStatusCode().value()); + verify(identifierSourceServiceWrapper, times(1)).generateIdentifierUsingIdentifierSourceUuid("dead-cafe", ""); + verify(personService, times(1)).getPersonByUuid("patientUuid"); + verify(delegate, times(1)).setRelationships(relationships); + } + + @Test + public void updatePatient() throws Exception { + bahmniPatientProfileResource = new BahmniPatientProfileResource(emrPatientProfileService, identifierSourceServiceWrapper); + BahmniPatientProfileResource spy = spy(bahmniPatientProfileResource); + PatientProfile mockPatientProfile = mock(PatientProfile.class); + when(emrPatientProfileService.save(mockPatientProfile)).thenReturn(mockPatientProfile); + doNothing().when(spy).setConvertedProperties(any(PatientProfile.class), any(SimpleObject.class), any(DelegatingResourceDescription.class), any(Boolean.class)); + + Person person = new Person(); + when(personService.getPersonByUuid("patientUuid")).thenReturn(person); + + // Create PersonB + Person personB = new Person(); + // "592b29e1-b3f5-423e-83cb-0d2c9b80867f" is the UUID of PersonB as specified in the patient.json payload + PowerMockito.when(personResource1_8.getByUniqueId("592b29e1-b3f5-423e-83cb-0d2c9b80867f")).thenReturn(personB); + + Patient patient = new Patient(); + patient.setUuid("patientUuid"); + when(mockPatientProfile.getPatient()).thenReturn(patient); + + doReturn(mockPatientProfile).when(spy, "mapForUpdatePatient", anyString(), any(SimpleObject.class)); + + // Create the Relationship that corresponds to the one specified in patient.json payload + Relationship relationship = new Relationship(); + relationship.setEndDate(new DateMapper().convertUTCToDate( "2020-04-06T21:00:00.000Z")); + relationship.setPersonA(patient); + relationship.setPersonB(personB); + relationship.setUuid("1234-5678"); + relationship.setRelationshipType(new RelationshipType()); + // Return it when calling "createRelationship" method. + doReturn(relationship).when(spy, "createRelationship", any(), any()); + + // Create the list of relationships + List relationships = new ArrayList(); + relationships.add(relationship); + // and return it when personService.getRelationshipsByPerson() is called + when(personService.getRelationshipsByPerson(person)).thenReturn(relationships); + + // Replay + ResponseEntity response = spy.update("patientUuid", propertiesToCreate); + + Assert.assertEquals(200, response.getStatusCode().value()); + verify(personService, times(1)).getPersonByUuid("patientUuid"); + verify(mockPatientProfile, times(2)).setRelationships(relationships); + } + + @Test + public void shouldThrowExceptionWhenPatientIsNotHavingProperPrivilege() throws Exception { + bahmniPatientProfileResource = new BahmniPatientProfileResource(emrPatientProfileService, identifierSourceServiceWrapper); + BahmniPatientProfileResource spy = spy(bahmniPatientProfileResource); + doThrow(new APIAuthenticationException()).when(spy, "mapForUpdatePatient", anyString(), any(SimpleObject.class)); + + ResponseEntity response = spy.update("someUuid", propertiesToCreate); + Assert.assertEquals(403,response.getStatusCode().value()); + } +} diff --git a/bahmnicore-omod/src/test/resources/patient.json b/bahmnicore-omod/src/test/resources/patient.json index c9395cb2bd..812147ed57 100644 --- a/bahmnicore-omod/src/test/resources/patient.json +++ b/bahmnicore-omod/src/test/resources/patient.json @@ -43,5 +43,16 @@ ] }, - "relationships": [] -} \ No newline at end of file + "relationships": [ + { + "relationshipType": { + "uuid": "2a5f4ff4-a179-4b8a-aa4c-40f71956eabc" + }, + "personB": { + "display": "abishek kumar Anand", + "uuid": "592b29e1-b3f5-423e-83cb-0d2c9b80867f" + }, + "endDate": "2020-04-06T21:00:00.000Z" + } + ] +}