@Test public void shouldGetRightsForAUserOnFacilityAndProductCode() { Long userId = 1L; Long facilityId = 2L; String productCode = "3"; Facility facility = new Facility(facilityId); Program program = new Program(3L); program.setCode("4"); ProgramProduct programProduct = new ProgramProduct(5L); programProduct.setProgram(program); List<Right> expected = Collections.singletonList(new Right(CREATE_REQUISITION, ADMIN)); when(facilityService.getById(facilityId)).thenReturn(facility); when(programProductService.getByProductCode(productCode)).thenReturn(Collections.singletonList(programProduct)); when(programService.getByCode(any(String.class))).thenReturn(program); when(supervisoryNodeService.getFor(facility, program)).thenReturn(null); when(facilityService.getHomeFacility(userId)).thenReturn(facility); when(roleRightsRepository.getRightsForUserOnHomeFacilityAndProgram(userId, program)).thenReturn(expected); List<Right> result = roleRightsService.getRightsForUserFacilityAndProductCode(userId, facilityId, productCode); assertThat(result.containsAll(expected), is(true)); }
public List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode) { Facility facility = facilityService.getById(facilityId); List<ProgramProduct> programProducts = programProductService.getByProductCode(productCode); List<Program> programs = new ArrayList<>(); for (ProgramProduct programProduct : programProducts) { Program program = programService.getByCode(programProduct.getProgram().getCode()); programs.add(program); } List<Right> rights = new ArrayList<>(); for (Program program : programs) { rights.addAll(getRightsForUserAndFacilityProgram(userId, facility, program)); } return rights; }
RoleRightsService { public List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode) { Facility facility = facilityService.getById(facilityId); List<ProgramProduct> programProducts = programProductService.getByProductCode(productCode); List<Program> programs = new ArrayList<>(); for (ProgramProduct programProduct : programProducts) { Program program = programService.getByCode(programProduct.getProgram().getCode()); programs.add(program); } List<Right> rights = new ArrayList<>(); for (Program program : programs) { rights.addAll(getRightsForUserAndFacilityProgram(userId, facility, program)); } return rights; } }
RoleRightsService { public List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode) { Facility facility = facilityService.getById(facilityId); List<ProgramProduct> programProducts = programProductService.getByProductCode(productCode); List<Program> programs = new ArrayList<>(); for (ProgramProduct programProduct : programProducts) { Program program = programService.getByCode(programProduct.getProgram().getCode()); programs.add(program); } List<Right> rights = new ArrayList<>(); for (Program program : programs) { rights.addAll(getRightsForUserAndFacilityProgram(userId, facility, program)); } return rights; } @Autowired  RoleRightsService(RoleRightsRepository roleRightsRepository,
                           SupervisoryNodeService supervisoryNodeService,
                           FacilityService facilityService,
                           ProgramService programService,
                           ProgramProductService programProductService); }
RoleRightsService { public List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode) { Facility facility = facilityService.getById(facilityId); List<ProgramProduct> programProducts = programProductService.getByProductCode(productCode); List<Program> programs = new ArrayList<>(); for (ProgramProduct programProduct : programProducts) { Program program = programService.getByCode(programProduct.getProgram().getCode()); programs.add(program); } List<Right> rights = new ArrayList<>(); for (Program program : programs) { rights.addAll(getRightsForUserAndFacilityProgram(userId, facility, program)); } return rights; } @Autowired  RoleRightsService(RoleRightsRepository roleRightsRepository,
                           SupervisoryNodeService supervisoryNodeService,
                           FacilityService facilityService,
                           ProgramService programService,
                           ProgramProductService programProductService); @Transactional void saveRole(Role role); Map<String, List<Role>> getAllRolesMap(); Role getRole(Long id); List<Role> getAllRoles(); void updateRole(Role role); List<Right> getRights(Long userId); List<Right> getRightsForUserAndFacilityProgram(Long userId, Facility facility, Program program); List<Right> getRightsForUserAndWarehouse(Long userId, Long warehouseId); List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode); RightType getRightTypeForRoleId(Long roleId); }
RoleRightsService { public List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode) { Facility facility = facilityService.getById(facilityId); List<ProgramProduct> programProducts = programProductService.getByProductCode(productCode); List<Program> programs = new ArrayList<>(); for (ProgramProduct programProduct : programProducts) { Program program = programService.getByCode(programProduct.getProgram().getCode()); programs.add(program); } List<Right> rights = new ArrayList<>(); for (Program program : programs) { rights.addAll(getRightsForUserAndFacilityProgram(userId, facility, program)); } return rights; } @Autowired  RoleRightsService(RoleRightsRepository roleRightsRepository,
                           SupervisoryNodeService supervisoryNodeService,
                           FacilityService facilityService,
                           ProgramService programService,
                           ProgramProductService programProductService); @Transactional void saveRole(Role role); Map<String, List<Role>> getAllRolesMap(); Role getRole(Long id); List<Role> getAllRoles(); void updateRole(Role role); List<Right> getRights(Long userId); List<Right> getRightsForUserAndFacilityProgram(Long userId, Facility facility, Program program); List<Right> getRightsForUserAndWarehouse(Long userId, Long warehouseId); List<Right> getRightsForUserFacilityAndProductCode(Long userId, Long facilityId, String productCode); RightType getRightTypeForRoleId(Long roleId);  }