@Test public void should_save_when_overlapped_slot_is_itself() { SlotDto slotDto = new SlotDto() .setId(1L) .setRoom(Room.Amphi1) .setStart("2016-04-21 10:30:00") .setIdSession(1L); when(slotRepository.findOne(1L)).thenReturn(slotDto.convert()); when(sessionRepository.findOne(1L)).thenReturn(Session.createTalk().setFormat(Format.Talk)); when(eventService.getEvent(2016)).thenReturn(event); when(slotRepository.findAllSlotsByRoom(event.getId(), Room.Amphi1)).thenReturn( Arrays.asList( new Slot() .setId(1L) .setRoom(Room.Amphi1) .setStart(LocalDateTime.of(2016, 4, 21, 10, 40)) .setEnd(LocalDateTime.of(2016, 4, 21, 11, 21) ) )); when(slotRepository.save(any(Slot.class))).thenReturn(new Slot()); assertThat(slotService.save(slotDto)).isNotNull(); }
public Slot save(SlotDto slotDto) { Slot slot; Session session = null; Event event = eventService.getEvent(slotDto.getStartDate().getYear()); if (slotDto.getId() != null) { slot = slotRepository.findOne(slotDto.getId()); slot .setLabel(slotDto.getLabel()) .setRoom(slotDto.getRoom()) .setEnd(slotDto.getEndDate()) .setStart(slotDto.getStartDate()); } else{ slot = slotDto.convert(); } slot.setEvent(event); if (slotDto.getIdSession() != null) { session = sessionRepository.findOne(slotDto.getIdSession()); slot.setSession(session); slot.setEnd(LocalDateTime.from(slot.getStart()).plusMinutes(slot.getSession().getFormat().getDuration())); } List<Slot> slots = slotRepository.findAllSlotsByRoom(event.getId(), slot.getRoom()); Optional<Slot> concurrent = slots .stream() .filter(s -> !s.getId().equals(slot.getId())) .filter(s -> (slot.getStart().isAfter(s.getStart()) && slot.getStart().isBefore(s.getEnd())) || (slot.getStart().isEqual(s.getStart())) || (slot.getEnd().isEqual(s.getEnd())) || (slot.getEnd().isAfter(s.getStart()) && slot.getEnd().isBefore(s.getEnd())) ) .findAny(); if(concurrent.isPresent()){ throw new SlotOverlapException(); } Slot slotPersisted = slotRepository.save(slot); if (session != null) { session.setSlot(slot); } return slotPersisted; }
SlotService { public Slot save(SlotDto slotDto) { Slot slot; Session session = null; Event event = eventService.getEvent(slotDto.getStartDate().getYear()); if (slotDto.getId() != null) { slot = slotRepository.findOne(slotDto.getId()); slot .setLabel(slotDto.getLabel()) .setRoom(slotDto.getRoom()) .setEnd(slotDto.getEndDate()) .setStart(slotDto.getStartDate()); } else{ slot = slotDto.convert(); } slot.setEvent(event); if (slotDto.getIdSession() != null) { session = sessionRepository.findOne(slotDto.getIdSession()); slot.setSession(session); slot.setEnd(LocalDateTime.from(slot.getStart()).plusMinutes(slot.getSession().getFormat().getDuration())); } List<Slot> slots = slotRepository.findAllSlotsByRoom(event.getId(), slot.getRoom()); Optional<Slot> concurrent = slots .stream() .filter(s -> !s.getId().equals(slot.getId())) .filter(s -> (slot.getStart().isAfter(s.getStart()) && slot.getStart().isBefore(s.getEnd())) || (slot.getStart().isEqual(s.getStart())) || (slot.getEnd().isEqual(s.getEnd())) || (slot.getEnd().isAfter(s.getStart()) && slot.getEnd().isBefore(s.getEnd())) ) .findAny(); if(concurrent.isPresent()){ throw new SlotOverlapException(); } Slot slotPersisted = slotRepository.save(slot); if (session != null) { session.setSlot(slot); } return slotPersisted; } }
SlotService { public Slot save(SlotDto slotDto) { Slot slot; Session session = null; Event event = eventService.getEvent(slotDto.getStartDate().getYear()); if (slotDto.getId() != null) { slot = slotRepository.findOne(slotDto.getId()); slot .setLabel(slotDto.getLabel()) .setRoom(slotDto.getRoom()) .setEnd(slotDto.getEndDate()) .setStart(slotDto.getStartDate()); } else{ slot = slotDto.convert(); } slot.setEvent(event); if (slotDto.getIdSession() != null) { session = sessionRepository.findOne(slotDto.getIdSession()); slot.setSession(session); slot.setEnd(LocalDateTime.from(slot.getStart()).plusMinutes(slot.getSession().getFormat().getDuration())); } List<Slot> slots = slotRepository.findAllSlotsByRoom(event.getId(), slot.getRoom()); Optional<Slot> concurrent = slots .stream() .filter(s -> !s.getId().equals(slot.getId())) .filter(s -> (slot.getStart().isAfter(s.getStart()) && slot.getStart().isBefore(s.getEnd())) || (slot.getStart().isEqual(s.getStart())) || (slot.getEnd().isEqual(s.getEnd())) || (slot.getEnd().isAfter(s.getStart()) && slot.getEnd().isBefore(s.getEnd())) ) .findAny(); if(concurrent.isPresent()){ throw new SlotOverlapException(); } Slot slotPersisted = slotRepository.save(slot); if (session != null) { session.setSlot(slot); } return slotPersisted; }  }
SlotService { public Slot save(SlotDto slotDto) { Slot slot; Session session = null; Event event = eventService.getEvent(slotDto.getStartDate().getYear()); if (slotDto.getId() != null) { slot = slotRepository.findOne(slotDto.getId()); slot .setLabel(slotDto.getLabel()) .setRoom(slotDto.getRoom()) .setEnd(slotDto.getEndDate()) .setStart(slotDto.getStartDate()); } else{ slot = slotDto.convert(); } slot.setEvent(event); if (slotDto.getIdSession() != null) { session = sessionRepository.findOne(slotDto.getIdSession()); slot.setSession(session); slot.setEnd(LocalDateTime.from(slot.getStart()).plusMinutes(slot.getSession().getFormat().getDuration())); } List<Slot> slots = slotRepository.findAllSlotsByRoom(event.getId(), slot.getRoom()); Optional<Slot> concurrent = slots .stream() .filter(s -> !s.getId().equals(slot.getId())) .filter(s -> (slot.getStart().isAfter(s.getStart()) && slot.getStart().isBefore(s.getEnd())) || (slot.getStart().isEqual(s.getStart())) || (slot.getEnd().isEqual(s.getEnd())) || (slot.getEnd().isAfter(s.getStart()) && slot.getEnd().isBefore(s.getEnd())) ) .findAny(); if(concurrent.isPresent()){ throw new SlotOverlapException(); } Slot slotPersisted = slotRepository.save(slot); if (session != null) { session.setSlot(slot); } return slotPersisted; }  Slot save(SlotDto slotDto); void delete(Long id); }
SlotService { public Slot save(SlotDto slotDto) { Slot slot; Session session = null; Event event = eventService.getEvent(slotDto.getStartDate().getYear()); if (slotDto.getId() != null) { slot = slotRepository.findOne(slotDto.getId()); slot .setLabel(slotDto.getLabel()) .setRoom(slotDto.getRoom()) .setEnd(slotDto.getEndDate()) .setStart(slotDto.getStartDate()); } else{ slot = slotDto.convert(); } slot.setEvent(event); if (slotDto.getIdSession() != null) { session = sessionRepository.findOne(slotDto.getIdSession()); slot.setSession(session); slot.setEnd(LocalDateTime.from(slot.getStart()).plusMinutes(slot.getSession().getFormat().getDuration())); } List<Slot> slots = slotRepository.findAllSlotsByRoom(event.getId(), slot.getRoom()); Optional<Slot> concurrent = slots .stream() .filter(s -> !s.getId().equals(slot.getId())) .filter(s -> (slot.getStart().isAfter(s.getStart()) && slot.getStart().isBefore(s.getEnd())) || (slot.getStart().isEqual(s.getStart())) || (slot.getEnd().isEqual(s.getEnd())) || (slot.getEnd().isAfter(s.getStart()) && slot.getEnd().isBefore(s.getEnd())) ) .findAny(); if(concurrent.isPresent()){ throw new SlotOverlapException(); } Slot slotPersisted = slotRepository.save(slot); if (session != null) { session.setSlot(slot); } return slotPersisted; }  Slot save(SlotDto slotDto); void delete(Long id);  }