@Test(expected = IllegalArgumentException.class) public void shouldNotDeleteTheDefaultGroupOfAProject() throws NotFoundException { User user = new User(); user.setId(USER_ID); Project project = new Project(); project.setId(PROJECT_ID); project.addOwner(user); SymbolGroup group = new SymbolGroup(); group.setId(DEFAULT_GROUP_ID); group.setProject(project); project.getGroups().add(group); given(projectRepository.findById(PROJECT_ID)).willReturn(Optional.of(project)); given(symbolGroupRepository.findFirstByProject_IdOrderByIdAsc(PROJECT_ID)).willReturn(group); given(symbolGroupRepository.findById(DEFAULT_GROUP_ID)).willReturn(Optional.of(group)); symbolGroupDAO.delete(user, PROJECT_ID, DEFAULT_GROUP_ID); }
public void delete(User user, long projectId, Long groupId) throws IllegalArgumentException, NotFoundException { final Project project = projectRepository.findById(projectId).orElse(null); final SymbolGroup group = symbolGroupRepository.findById(groupId).orElse(null); checkAccess(user, project, group); final SymbolGroup defaultGroup = symbolGroupRepository.findFirstByProject_IdOrderByIdAsc(projectId); if (defaultGroup.equals(group)) { throw new IllegalArgumentException("You can not delete the default group of a project."); } group.walk(g -> { hideSymbols(g, defaultGroup); return null; }, s -> null); if (group.getParent() != null) { group.getParent().getGroups().remove(group); symbolGroupRepository.save(group.getParent()); } group.setSymbols(null); symbolGroupRepository.delete(group); }
SymbolGroupDAO { public void delete(User user, long projectId, Long groupId) throws IllegalArgumentException, NotFoundException { final Project project = projectRepository.findById(projectId).orElse(null); final SymbolGroup group = symbolGroupRepository.findById(groupId).orElse(null); checkAccess(user, project, group); final SymbolGroup defaultGroup = symbolGroupRepository.findFirstByProject_IdOrderByIdAsc(projectId); if (defaultGroup.equals(group)) { throw new IllegalArgumentException("You can not delete the default group of a project."); } group.walk(g -> { hideSymbols(g, defaultGroup); return null; }, s -> null); if (group.getParent() != null) { group.getParent().getGroups().remove(group); symbolGroupRepository.save(group.getParent()); } group.setSymbols(null); symbolGroupRepository.delete(group); } }
SymbolGroupDAO { public void delete(User user, long projectId, Long groupId) throws IllegalArgumentException, NotFoundException { final Project project = projectRepository.findById(projectId).orElse(null); final SymbolGroup group = symbolGroupRepository.findById(groupId).orElse(null); checkAccess(user, project, group); final SymbolGroup defaultGroup = symbolGroupRepository.findFirstByProject_IdOrderByIdAsc(projectId); if (defaultGroup.equals(group)) { throw new IllegalArgumentException("You can not delete the default group of a project."); } group.walk(g -> { hideSymbols(g, defaultGroup); return null; }, s -> null); if (group.getParent() != null) { group.getParent().getGroups().remove(group); symbolGroupRepository.save(group.getParent()); } group.setSymbols(null); symbolGroupRepository.delete(group); } @Autowired  SymbolGroupDAO(ProjectRepository projectRepository, ProjectDAO projectDAO,
                          SymbolGroupRepository symbolGroupRepository, SymbolRepository symbolRepository,
                          @Lazy SymbolDAO symbolDAO, ObjectMapper objectMapper); }
SymbolGroupDAO { public void delete(User user, long projectId, Long groupId) throws IllegalArgumentException, NotFoundException { final Project project = projectRepository.findById(projectId).orElse(null); final SymbolGroup group = symbolGroupRepository.findById(groupId).orElse(null); checkAccess(user, project, group); final SymbolGroup defaultGroup = symbolGroupRepository.findFirstByProject_IdOrderByIdAsc(projectId); if (defaultGroup.equals(group)) { throw new IllegalArgumentException("You can not delete the default group of a project."); } group.walk(g -> { hideSymbols(g, defaultGroup); return null; }, s -> null); if (group.getParent() != null) { group.getParent().getGroups().remove(group); symbolGroupRepository.save(group.getParent()); } group.setSymbols(null); symbolGroupRepository.delete(group); } @Autowired  SymbolGroupDAO(ProjectRepository projectRepository, ProjectDAO projectDAO,
                          SymbolGroupRepository symbolGroupRepository, SymbolRepository symbolRepository,
                          @Lazy SymbolDAO symbolDAO, ObjectMapper objectMapper); List<SymbolGroup> importGroups(User user, Long projectId, SymbolGroupsImportableEntity importableEntity); List<SymbolGroup> importGroups(User user, Project project, List<SymbolGroup> groups, Map<String, SymbolImportConflictResolutionStrategy> conflictResolutions); void create(User user, SymbolGroup group); List<SymbolGroup> create(User user, Long projectId, List<SymbolGroup> groups); List<SymbolGroup> getAll(User user, long projectId); SymbolGroup get(User user, long projectId, Long groupId); void update(User user, SymbolGroup group); SymbolGroup move(User user, SymbolGroup group); void delete(User user, long projectId, Long groupId); void checkAccess(User user, Project project, SymbolGroup group); }
SymbolGroupDAO { public void delete(User user, long projectId, Long groupId) throws IllegalArgumentException, NotFoundException { final Project project = projectRepository.findById(projectId).orElse(null); final SymbolGroup group = symbolGroupRepository.findById(groupId).orElse(null); checkAccess(user, project, group); final SymbolGroup defaultGroup = symbolGroupRepository.findFirstByProject_IdOrderByIdAsc(projectId); if (defaultGroup.equals(group)) { throw new IllegalArgumentException("You can not delete the default group of a project."); } group.walk(g -> { hideSymbols(g, defaultGroup); return null; }, s -> null); if (group.getParent() != null) { group.getParent().getGroups().remove(group); symbolGroupRepository.save(group.getParent()); } group.setSymbols(null); symbolGroupRepository.delete(group); } @Autowired  SymbolGroupDAO(ProjectRepository projectRepository, ProjectDAO projectDAO,
                          SymbolGroupRepository symbolGroupRepository, SymbolRepository symbolRepository,
                          @Lazy SymbolDAO symbolDAO, ObjectMapper objectMapper); List<SymbolGroup> importGroups(User user, Long projectId, SymbolGroupsImportableEntity importableEntity); List<SymbolGroup> importGroups(User user, Project project, List<SymbolGroup> groups, Map<String, SymbolImportConflictResolutionStrategy> conflictResolutions); void create(User user, SymbolGroup group); List<SymbolGroup> create(User user, Long projectId, List<SymbolGroup> groups); List<SymbolGroup> getAll(User user, long projectId); SymbolGroup get(User user, long projectId, Long groupId); void update(User user, SymbolGroup group); SymbolGroup move(User user, SymbolGroup group); void delete(User user, long projectId, Long groupId); void checkAccess(User user, Project project, SymbolGroup group);  }