@Test public void shouldCreateACounter() { User user = new User(); Project project = new Project(); Counter counter = new Counter(); counter.setProject(project); counter.setName(COUNTER_NAME); counter.setValue(COUNTER_VALUE); try { counterDAO.create(user, counter); } catch (NotFoundException e) { e.printStackTrace(); } verify(counterRepository).save(counter); }
public void create(User user, Counter counter) throws NotFoundException, ValidationException { final Project project = projectRepository.findById(counter.getProjectId()).orElse(null); projectDAO.checkAccess(user, project); if (counterRepository.findByProjectAndName(project, counter.getName()) != null) { throw new ValidationException("A counter with the name already exists."); } counter.setId(null); counter.setProject(project); counterRepository.save(counter); }
CounterDAO { public void create(User user, Counter counter) throws NotFoundException, ValidationException { final Project project = projectRepository.findById(counter.getProjectId()).orElse(null); projectDAO.checkAccess(user, project); if (counterRepository.findByProjectAndName(project, counter.getName()) != null) { throw new ValidationException("A counter with the name already exists."); } counter.setId(null); counter.setProject(project); counterRepository.save(counter); } }
CounterDAO { public void create(User user, Counter counter) throws NotFoundException, ValidationException { final Project project = projectRepository.findById(counter.getProjectId()).orElse(null); projectDAO.checkAccess(user, project); if (counterRepository.findByProjectAndName(project, counter.getName()) != null) { throw new ValidationException("A counter with the name already exists."); } counter.setId(null); counter.setProject(project); counterRepository.save(counter); } @Autowired  CounterDAO(ProjectDAO projectDAO,
                      CounterRepository counterRepository,
                      ProjectRepository projectRepository); }
CounterDAO { public void create(User user, Counter counter) throws NotFoundException, ValidationException { final Project project = projectRepository.findById(counter.getProjectId()).orElse(null); projectDAO.checkAccess(user, project); if (counterRepository.findByProjectAndName(project, counter.getName()) != null) { throw new ValidationException("A counter with the name already exists."); } counter.setId(null); counter.setProject(project); counterRepository.save(counter); } @Autowired  CounterDAO(ProjectDAO projectDAO,
                      CounterRepository counterRepository,
                      ProjectRepository projectRepository); void create(User user, Counter counter); List<Counter> getAll(User user, Long projectId); Counter update(User user, Counter counter); void delete(User user, Long projectId, List<Long> counterIds); void checkAccess(User user, Project project, Counter counter); }
CounterDAO { public void create(User user, Counter counter) throws NotFoundException, ValidationException { final Project project = projectRepository.findById(counter.getProjectId()).orElse(null); projectDAO.checkAccess(user, project); if (counterRepository.findByProjectAndName(project, counter.getName()) != null) { throw new ValidationException("A counter with the name already exists."); } counter.setId(null); counter.setProject(project); counterRepository.save(counter); } @Autowired  CounterDAO(ProjectDAO projectDAO,
                      CounterRepository counterRepository,
                      ProjectRepository projectRepository); void create(User user, Counter counter); List<Counter> getAll(User user, Long projectId); Counter update(User user, Counter counter); void delete(User user, Long projectId, List<Long> counterIds); void checkAccess(User user, Project project, Counter counter);  }