@Test public void shouldUpdateACHWFacility() throws Exception { Agent agent = make(a(defaultCHW)); Facility baseFacility = getBaseFacility(agent); when(facilityService.getFacilityWithReferenceDataForCode(agent.getParentFacilityCode())).thenReturn(baseFacility); Date currentTimeStamp = mock(Date.class); whenNew(Date.class).withNoArguments().thenReturn(currentTimeStamp); Facility chwFacility = spy(new Facility()); chwFacility.setVirtualFacility(true); chwFacility.setEnabled(true); chwFacility.setParentFacilityId(1l); whenNew(Facility.class).withNoArguments().thenReturn(chwFacility); when(facilityService.getByCode(chwFacility)).thenReturn(chwFacility); when(userService.getByUserName(user.getUserName())).thenReturn(user); restAgentService.update(agent, user.getId()); verify(chwFacility).setName(agent.getAgentName()); verify(chwFacility).setMainPhone(agent.getPhoneNumber()); verify(chwFacility).setActive(Boolean.parseBoolean(agent.getActive())); verify(chwFacility, times(2)).setParentFacilityId(baseFacility.getId()); verify(chwFacility).setGeographicZone(baseFacility.getGeographicZone()); verify(chwFacility).setFacilityType(baseFacility.getFacilityType()); verify(facilityService).update(chwFacility); verify(requisitionGroupMemberService, never()).deleteMembersFor(chwFacility); }
public void update(Agent agent, Long userId) { if (agent.getActive() == null) { throw new DataException("error.mandatory.fields.missing"); } agent.validate(); Facility chwFacility = getExistingFacilityForCode(agent.getAgentCode()); validateCHWForUpdate(chwFacility); chwFacility.setName(agent.getAgentName()); chwFacility.setMainPhone(agent.getPhoneNumber() == null ? chwFacility.getMainPhone() : agent.getPhoneNumber()); chwFacility.setActive(Boolean.parseBoolean(agent.getActive())); Long previousParent = chwFacility.getParentFacilityId(); fillBaseFacility(agent, chwFacility); if (!previousParent.equals(chwFacility.getParentFacilityId())) { requisitionGroupMemberService.deleteMembersFor(chwFacility); saveRequisitionGroupMembers(chwFacility, userId); } chwFacility.setModifiedDate(new Date()); chwFacility.setModifiedBy(userId); facilityService.update(chwFacility); }
RestAgentService { public void update(Agent agent, Long userId) { if (agent.getActive() == null) { throw new DataException("error.mandatory.fields.missing"); } agent.validate(); Facility chwFacility = getExistingFacilityForCode(agent.getAgentCode()); validateCHWForUpdate(chwFacility); chwFacility.setName(agent.getAgentName()); chwFacility.setMainPhone(agent.getPhoneNumber() == null ? chwFacility.getMainPhone() : agent.getPhoneNumber()); chwFacility.setActive(Boolean.parseBoolean(agent.getActive())); Long previousParent = chwFacility.getParentFacilityId(); fillBaseFacility(agent, chwFacility); if (!previousParent.equals(chwFacility.getParentFacilityId())) { requisitionGroupMemberService.deleteMembersFor(chwFacility); saveRequisitionGroupMembers(chwFacility, userId); } chwFacility.setModifiedDate(new Date()); chwFacility.setModifiedBy(userId); facilityService.update(chwFacility); } }
RestAgentService { public void update(Agent agent, Long userId) { if (agent.getActive() == null) { throw new DataException("error.mandatory.fields.missing"); } agent.validate(); Facility chwFacility = getExistingFacilityForCode(agent.getAgentCode()); validateCHWForUpdate(chwFacility); chwFacility.setName(agent.getAgentName()); chwFacility.setMainPhone(agent.getPhoneNumber() == null ? chwFacility.getMainPhone() : agent.getPhoneNumber()); chwFacility.setActive(Boolean.parseBoolean(agent.getActive())); Long previousParent = chwFacility.getParentFacilityId(); fillBaseFacility(agent, chwFacility); if (!previousParent.equals(chwFacility.getParentFacilityId())) { requisitionGroupMemberService.deleteMembersFor(chwFacility); saveRequisitionGroupMembers(chwFacility, userId); } chwFacility.setModifiedDate(new Date()); chwFacility.setModifiedBy(userId); facilityService.update(chwFacility); }  }
RestAgentService { public void update(Agent agent, Long userId) { if (agent.getActive() == null) { throw new DataException("error.mandatory.fields.missing"); } agent.validate(); Facility chwFacility = getExistingFacilityForCode(agent.getAgentCode()); validateCHWForUpdate(chwFacility); chwFacility.setName(agent.getAgentName()); chwFacility.setMainPhone(agent.getPhoneNumber() == null ? chwFacility.getMainPhone() : agent.getPhoneNumber()); chwFacility.setActive(Boolean.parseBoolean(agent.getActive())); Long previousParent = chwFacility.getParentFacilityId(); fillBaseFacility(agent, chwFacility); if (!previousParent.equals(chwFacility.getParentFacilityId())) { requisitionGroupMemberService.deleteMembersFor(chwFacility); saveRequisitionGroupMembers(chwFacility, userId); } chwFacility.setModifiedDate(new Date()); chwFacility.setModifiedBy(userId); facilityService.update(chwFacility); }  @Transactional void create(Agent agent, Long userId); void update(Agent agent, Long userId); }
RestAgentService { public void update(Agent agent, Long userId) { if (agent.getActive() == null) { throw new DataException("error.mandatory.fields.missing"); } agent.validate(); Facility chwFacility = getExistingFacilityForCode(agent.getAgentCode()); validateCHWForUpdate(chwFacility); chwFacility.setName(agent.getAgentName()); chwFacility.setMainPhone(agent.getPhoneNumber() == null ? chwFacility.getMainPhone() : agent.getPhoneNumber()); chwFacility.setActive(Boolean.parseBoolean(agent.getActive())); Long previousParent = chwFacility.getParentFacilityId(); fillBaseFacility(agent, chwFacility); if (!previousParent.equals(chwFacility.getParentFacilityId())) { requisitionGroupMemberService.deleteMembersFor(chwFacility); saveRequisitionGroupMembers(chwFacility, userId); } chwFacility.setModifiedDate(new Date()); chwFacility.setModifiedBy(userId); facilityService.update(chwFacility); }  @Transactional void create(Agent agent, Long userId); void update(Agent agent, Long userId);  }