@Override protected Cohort getObjectByUuid(String uuid) { return Context.getCohortService().getCohortByUuid(uuid); } }
/** * @see CohortService#getCohortByUuid(String) */ @Test public void getCohortByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() { assertNull(Context.getCohortService().getCohortByUuid("some invalid uuid")); }
/** * @see CohortService#getCohortByUuid(String) */ @Test public void getCohortByUuid_shouldFindObjectGivenValidUuid() { executeDataSet(COHORT_XML); String uuid = "h9a9m0i6-15e6-467c-9d4b-mbi7teu9lf0f"; Cohort cohort = Context.getCohortService().getCohortByUuid(uuid); Assert.assertEquals(1, (int) cohort.getCohortId()); }
@Test public void handle_shouldVoidCohortMemberships() throws Exception { // test a corner case by letting the same patient belong to the cohort for two separate periods CohortMembership membership1 = new CohortMembership(7, parseDate("2001-01-01", "yyyy-MM-dd")); membership1.setEndDate(parseDate("2001-12-31", "yyyy-MM-dd")); CohortMembership membership2 = new CohortMembership(7, parseDate("2017-01-01", "yyyy-MM-dd")); CohortMembership membership3 = new CohortMembership(8); Cohort cohort = new Cohort(); cohort.setName("Cohort"); cohort.setDescription("Description"); cohort.setUuid(COHORT_UUID); cohort.addMembership(membership1); cohort.addMembership(membership2); cohort.addMembership(membership3); cohortService.saveCohort(cohort); PatientService patientService = Context.getPatientService(); patientService.voidPatient(patientService.getPatient(7), "void reason"); Collection<CohortMembership> memberships = cohortService.getCohortByUuid(COHORT_UUID).getMemberships(false); assertEquals(1, memberships.size()); assertEquals(8, (int) memberships.iterator().next().getPatientId()); // patientId 7 was voided } }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getByUniqueId(java.lang.String) */ @Override public Cohort getByUniqueId(String param) { Cohort result = Context.getCohortService().getCohortByUuid(param); if (result == null) return Context.getCohortService().getCohort(param); return result; }
@Override public Cohort newObject() { return Context.getCohortService().getCohortByUuid(getUuidProperty()); }
@Override public Cohort newObject() { return Context.getCohortService().getCohortByUuid(getUuidProperty()); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#retrieve(java.lang.String, * java.lang.String, org.openmrs.module.webservices.rest.web.RequestContext) */ @Override public Object retrieve(String parentUniqueId, String uuid, RequestContext context) throws ResponseException { CohortMember1_8 delegate = getByUniqueId(uuid); if (delegate == null) throw new ObjectNotFoundException(); delegate.setCohort(Context.getCohortService().getCohortByUuid(parentUniqueId)); return asRepresentation(delegate, context.getRepresentation()); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#create(java.lang.String, * org.openmrs.module.webservices.rest.SimpleObject, * org.openmrs.module.webservices.rest.web.RequestContext) */ @Override public Object create(String parentUniqueId, SimpleObject post, RequestContext context) throws ResponseException { Cohort parent = Context.getCohortService().getCohortByUuid(parentUniqueId); CohortMember1_8 delegate = newDelegate(); setParent(delegate, parent); delegate.setPatient(Context.getPatientService().getPatientByUuid(post.get("patient").toString())); delegate = save(delegate); return ConversionUtil.convertToRepresentation(delegate, Representation.DEFAULT); }
@Override public CohortMember1_8 newObject() { Cohort cohort = Context.getCohortService().getCohortByUuid(RestTestConstants1_8.COHORT_UUID); Patient patient = Context.getPatientService().getPatientByUuid(RestTestConstants1_8.PATIENT_UUID); return new CohortMember1_8(patient, cohort); }
@Test(expected = ConversionException.class) public void updateCohort_shouldFailToOverwriteMemberIdsOnACohort() throws Exception { Assert.assertEquals(3, service.getCohortByUuid(getUuid()).getMemberIds().size()); SimpleObject attributes = new SimpleObject(); attributes.add("memberIds", new Integer[] { 2, 6 }); String json = new ObjectMapper().writeValueAsString(attributes); MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); req.setContent(json.getBytes()); handle(req); }
@Test public void updateCohort_shouldChangeAPropertyOnACohort() throws Exception { SimpleObject attributes = new SimpleObject(); attributes.add("name", "Updated cohort name"); String json = new ObjectMapper().writeValueAsString(attributes); MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); req.setContent(json.getBytes()); handle(req); Cohort editedCohort = service.getCohortByUuid(getUuid()); Assert.assertEquals("Updated cohort name", editedCohort.getName()); }
@Test public void addCohortMember_shouldAddCohortMember() throws Exception { String patientId = "da7f524f-27ce-4bb2-86d6-6d1d05312bd5"; SimpleObject attributes = new SimpleObject(); attributes.add("patient", patientId); String json = new ObjectMapper().writeValueAsString(attributes); MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid() + "/member"); req.setContent(json.getBytes()); handle(req); Cohort cohort = service.getCohortByUuid(getUuid()); Patient patient = patientService.getPatientByUuid(patientId); Assert.assertTrue(cohort.contains(patient)); }
@Test public void shouldEdit() throws Exception { String newDescription = "New description"; MockHttpServletRequest request = newPostRequest(getURI() + "/" + getUuid(), new SimpleObject().add("description", newDescription)); SimpleObject result = deserialize(handle(request)); assertThat((String) result.get("description"), is(newDescription)); assertThat(cohortService.getCohortByUuid(RestTestConstants2_1.COHORT_UUID).getDescription(), is(newDescription)); }
@Test public void shouldVoid() throws Exception { MockHttpServletRequest request = newDeleteRequest(getURI() + "/" + getUuid()); handle(request); assertTrue(cohortService.getCohortByUuid(RestTestConstants2_1.COHORT_UUID).getVoided()); }
@Test public void shouldPurge() throws Exception { MockHttpServletRequest request = newDeleteRequest(getURI() + "/" + getUuid()); request.setParameter("purge", "true"); handle(request); assertNull(cohortService.getCohortByUuid(RestTestConstants2_1.COHORT_UUID)); }
@Test public void getAllCohortMembers_shouldGetARefRepresentationOfAllCohortMembers() throws Exception { int size = service.getCohortByUuid(getUuid()).getMemberIds().size(); MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid() + "/member"); SimpleObject result = deserialize(handle(req)); Util.log("Cohort member fetched (ref)", result); Assert.assertEquals(size, Util.getResultsSize(result)); }
@Test public void purgeCohort_shouldPurgeCohort() throws Exception { MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); req.addParameter("purge", "true"); handle(req); Assert.assertNull(service.getCohortByUuid(getUuid())); }
@Test public void getAllCohortMembers_shouldGetADefaultRepresentationOfAllCohortMembers() throws Exception { int size = service.getCohortByUuid(getUuid()).getMemberIds().size(); MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid() + "/member"); req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL); SimpleObject result = deserialize(handle(req)); Util.log("Cohort member fetched (default)", result); Assert.assertEquals(size, Util.getResultsSize(result)); }
@Test public void removeCohortMember_shouldRemoveCohortMember() throws Exception { MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + getUuid() + "/member/" + patientUuid); req.addParameter("!purge", ""); handle(req); Cohort cohort = service.getCohortByUuid(getUuid()); Patient patient = patientService.getPatientByUuid(patientUuid); Assert.assertTrue(!cohort.contains(patient)); }