@Test public void shouldCreateFacilityForCHW() throws Exception { Agent agent = make(a(defaultCHW)); Facility baseFacility = getBaseFacility(agent); Facility facility = mock(Facility.class); when(facilityService.getFacilityWithReferenceDataForCode(agent.getParentFacilityCode())).thenReturn(baseFacility); whenNew(Facility.class).withNoArguments().thenReturn(facility); when(userService.getByUserName(user.getUserName())).thenReturn(user); Date currentTimeStamp = mock(Date.class); whenNew(Date.class).withNoArguments().thenReturn(currentTimeStamp); when(facility.getParentFacilityId()).thenReturn(baseFacility.getId()); RequisitionGroup requisitionGroup = new RequisitionGroup(); when(requisitionGroupMemberService.getAllRequisitionGroupMembersByFacility(baseFacility.getId())). thenReturn(asList(new RequisitionGroupMember(requisitionGroup, baseFacility))); restAgentService.create(agent, user.getId()); verify(facility, times(2)).setCode(agent.getAgentCode()); verify(facility).setParentFacilityId(baseFacility.getId()); verify(facility).setName(agent.getAgentName()); verify(facility).setFacilityType(baseFacility.getFacilityType()); verify(facility).setMainPhone(agent.getPhoneNumber()); verify(facility).setGeographicZone(baseFacility.getGeographicZone()); verify(facility).setActive(Boolean.parseBoolean(agent.getActive())); verify(facility).setVirtualFacility(true); verify(facility).setSdp(true); verify(facility).setEnabled(true); verify(facility).setGoLiveDate(currentTimeStamp); verify(facilityService).save(facility); verify(requisitionGroupMemberService).getAllRequisitionGroupMembersByFacility(baseFacility.getId()); ArgumentCaptor<RequisitionGroupMember> captor = ArgumentCaptor.forClass(RequisitionGroupMember.class); verify(requisitionGroupMemberService).save(captor.capture()); assertThat(captor.getValue().getRequisitionGroup(), is(requisitionGroup)); assertThat(captor.getValue().getFacility(), is(facility)); assertThat(captor.getValue().getCreatedBy(), is(user.getId())); assertThat(captor.getValue().getModifiedBy(), is(user.getId())); verify(programSupportedService).updateSupportedPrograms(facility); }
@Transactional public void create(Agent agent, Long userId) { agent.validate(); if (getExistingFacilityForCode(agent.getAgentCode()) != null) { throw new DataException("error.agent.already.registered"); } Facility facility = createFacilityFrom(agent); facility.setCreatedBy(userId); facility.setModifiedBy(userId); facilityService.save(facility); programSupportedService.updateSupportedPrograms(facility); saveRequisitionGroupMembers(facility, userId); }
RestAgentService { @Transactional public void create(Agent agent, Long userId) { agent.validate(); if (getExistingFacilityForCode(agent.getAgentCode()) != null) { throw new DataException("error.agent.already.registered"); } Facility facility = createFacilityFrom(agent); facility.setCreatedBy(userId); facility.setModifiedBy(userId); facilityService.save(facility); programSupportedService.updateSupportedPrograms(facility); saveRequisitionGroupMembers(facility, userId); } }
RestAgentService { @Transactional public void create(Agent agent, Long userId) { agent.validate(); if (getExistingFacilityForCode(agent.getAgentCode()) != null) { throw new DataException("error.agent.already.registered"); } Facility facility = createFacilityFrom(agent); facility.setCreatedBy(userId); facility.setModifiedBy(userId); facilityService.save(facility); programSupportedService.updateSupportedPrograms(facility); saveRequisitionGroupMembers(facility, userId); }  }
RestAgentService { @Transactional public void create(Agent agent, Long userId) { agent.validate(); if (getExistingFacilityForCode(agent.getAgentCode()) != null) { throw new DataException("error.agent.already.registered"); } Facility facility = createFacilityFrom(agent); facility.setCreatedBy(userId); facility.setModifiedBy(userId); facilityService.save(facility); programSupportedService.updateSupportedPrograms(facility); saveRequisitionGroupMembers(facility, userId); }  @Transactional void create(Agent agent, Long userId); void update(Agent agent, Long userId); }
RestAgentService { @Transactional public void create(Agent agent, Long userId) { agent.validate(); if (getExistingFacilityForCode(agent.getAgentCode()) != null) { throw new DataException("error.agent.already.registered"); } Facility facility = createFacilityFrom(agent); facility.setCreatedBy(userId); facility.setModifiedBy(userId); facilityService.save(facility); programSupportedService.updateSupportedPrograms(facility); saveRequisitionGroupMembers(facility, userId); }  @Transactional void create(Agent agent, Long userId); void update(Agent agent, Long userId);  }