ReserveServiceImpl implements ReserveService { @Override public void cancel(String reserveNo) throws BusinessException { Reserve reserve = findOne(reserveNo); String transfer = reserve.getTransfer(); if (Reserve.TRANSFERED.equals(transfer)) { ResultMessages message = ResultMessages.error().add( MessageId.E_TR_0001); throw new BusinessException(message); } TourInfo info = reserve.getTourInfo(); if (tourInfoSharedService.isOverPaymentLimit(info)) { ResultMessages message = ResultMessages.error().add( MessageId.E_TR_0002); throw new BusinessException(message); } reserve = reserveRepository.findOneForUpdate(reserveNo); if (reserve != null) { reserveRepository.deleteById(reserveNo); } else { ResultMessages message = ResultMessages.error().add( MessageId.E_TR_0003); throw new BusinessException(message); } log.debug("canceled reserveNo={}", reserveNo); }  @Override Reserve findOne(String reserveNo); @Override List<Reserve> findAllByCustomerCode(String customerCode); @Override ReserveTourOutput reserve(
            ReserveTourInput input); @Override void cancel(String reserveNo); @Override ReservationUpdateOutput update(
            ReservationUpdateInput input);  }
@Test public void testCancel01() { Reserve reserve = new Reserve("001"); reserve.setAdultCount(1); reserve.setChildCount(2); TourInfo tour = new TourInfo("01"); reserve.setTourInfo(tour); reserve.setTransfer(Reserve.NOT_TRANSFERED); when(reserveRepository.findById("001")).thenReturn(Optional.of( reserve)); when(reserveRepository.findOneForUpdate("001")).thenReturn(reserve); when(tourInfoSharedService.isOverPaymentLimit(tour)).thenReturn(false); reserveService.cancel("001"); ArgumentCaptor<String> argOfDelete = ArgumentCaptor.forClass( String.class); verify(reserveRepository, times(1)).deleteById(argOfDelete.capture()); assertThat(argOfDelete.getValue(), is("001")); }
@Test(expected = BusinessException.class) public void testCancel02() { Reserve reserve = new Reserve("001"); reserve.setAdultCount(1); reserve.setChildCount(2); TourInfo tour = new TourInfo("01"); reserve.setTourInfo(tour); reserve.setTransfer(Reserve.TRANSFERED); when(reserveRepository.findById("001")).thenReturn(Optional.of( reserve)); when(reserveRepository.findOneForUpdate("001")).thenReturn(reserve); when(tourInfoSharedService.isOverPaymentLimit(tour)).thenReturn(false); try { reserveService.cancel("001"); fail("error route"); } catch (BusinessException e) { ResultMessages messages = e.getResultMessages(); assertThat(messages.isNotEmpty(), is(true)); assertThat(messages.getType(), is((ResultMessageType) ERROR)); assertThat(messages.getList().size(), is(1)); assertThat(messages.getList().get(0).getCode(), is( MessageId.E_TR_0001)); throw e; } }
@Test(expected = BusinessException.class) public void testCancel03() { Reserve reserve = new Reserve("001"); reserve.setAdultCount(1); reserve.setChildCount(2); TourInfo tour = new TourInfo("01"); reserve.setTourInfo(tour); reserve.setTransfer(Reserve.NOT_TRANSFERED); when(reserveRepository.findById("001")).thenReturn(Optional.of( reserve)); when(reserveRepository.findOneForUpdate("001")).thenReturn(reserve); when(tourInfoSharedService.isOverPaymentLimit(tour)).thenReturn(true); try { reserveService.cancel("001"); fail("error route"); } catch (BusinessException e) { ResultMessages messages = e.getResultMessages(); assertThat(messages.isNotEmpty(), is(true)); assertThat(messages.getType(), is((ResultMessageType) ERROR)); assertThat(messages.getList().size(), is(1)); assertThat(messages.getList().get(0).getCode(), is( MessageId.E_TR_0002)); throw e; } }
@Test(expected = BusinessException.class) public void testCancel04() { Reserve reserve = new Reserve("001"); reserve.setAdultCount(1); reserve.setChildCount(2); TourInfo tour = new TourInfo("01"); reserve.setTourInfo(tour); reserve.setTransfer(Reserve.NOT_TRANSFERED); when(reserveRepository.findById("001")).thenReturn(Optional.of(reserve)) .thenReturn(Optional.empty()); when(reserveRepository.findOneForUpdate("001")).thenReturn( (Reserve) null); when(tourInfoSharedService.isOverPaymentLimit(tour)).thenReturn(false); try { reserveService.cancel("001"); fail("error route"); } catch (BusinessException e) { ResultMessages messages = e.getResultMessages(); assertThat(messages.isNotEmpty(), is(true)); assertThat(messages.getType(), is((ResultMessageType) ERROR)); assertThat(messages.getList().size(), is(1)); assertThat(messages.getList().get(0).getCode(), is( MessageId.E_TR_0003)); throw e; } }