Program: 164538
Project: 164538
Buggy source:
/** 
 * Create a ticket reservation. It will create a reservation _only_ if it can find enough tickets. Note that it will not do date/validity validation. This must be ensured by the caller.
 * @param event
 * @param list
 * @param reservationExpiration
 * @param forWaitingQueue
 * @return
 */
public String createTicketReservation(Event event,List<TicketReservationWithOptionalCodeModification> list,List<ASReservationWithOptionalCodeModification> additionalServices,Date reservationExpiration,Optional<String> specialPriceSessionId,Optional<String> promotionCodeDiscount,Locale locale,boolean forWaitingQueue) throws NotEnoughTicketsException, MissingSpecialPriceTokenException, InvalidSpecialPriceTokenException {
  String reservationId=UUID.randomUUID().toString();
  Optional<PromoCodeDiscount> discount=promotionCodeDiscount.flatMap((promoCodeDiscount) -> optionally(() -> promoCodeDiscountRepository.findPromoCodeInEventOrOrganization(event.getId(),promoCodeDiscount)));
  ticketReservationRepository.createNewReservation(reservationId,reservationExpiration,discount.map(PromoCodeDiscount::getId).orElse(null),locale.getLanguage(),event.getId());
  list.forEach(t -> reserveTicketsForCategory(event,specialPriceSessionId,reservationId,t,locale,forWaitingQueue,discount.orElse(null)));
  int ticketCount=list.stream().map(TicketReservationWithOptionalCodeModification::getAmount).mapToInt(Integer::intValue).sum();
  additionalServiceRepository.findAllInEventWithPolicy(event.getId(),AdditionalService.SupplementPolicy.MANDATORY_ONE_FOR_TICKET).stream().filter(AdditionalService::getSaleable).forEach(as -> {
    AdditionalServiceReservationModification asrm=new AdditionalServiceReservationModification();
    asrm.setAdditionalServiceId(as.getId());
    asrm.setQuantity(ticketCount);
    additionalServices.add(new ASReservationWithOptionalCodeModification(asrm,Optional.empty()));
  }
);
  additionalServices.forEach(as -> reserveAdditionalServicesForReservation(event.getId(),reservationId,as,discount.orElse(null)));
  TicketReservation reservation=ticketReservationRepository.findReservationById(reservationId);
  OrderSummary orderSummary=orderSummaryForReservationId(reservation.getId(),event,Locale.forLanguageTag(reservation.getUserLanguage()));
  ticketReservationRepository.addReservationInvoiceOrReceiptModel(reservationId,Json.toJson(orderSummary));
  return reservationId;
}

Fixed version (ground truth):
/** 
 * Create a ticket reservation. It will create a reservation _only_ if it can find enough tickets. Note that it will not do date/validity validation. This must be ensured by the caller.
 * @param event
 * @param list
 * @param reservationExpiration
 * @param forWaitingQueue
 * @return
 */
public String createTicketReservation(Event event,List<TicketReservationWithOptionalCodeModification> list,List<ASReservationWithOptionalCodeModification> additionalServices,Date reservationExpiration,Optional<String> specialPriceSessionId,Optional<String> promotionCodeDiscount,Locale locale,boolean forWaitingQueue) throws NotEnoughTicketsException, MissingSpecialPriceTokenException, InvalidSpecialPriceTokenException {
  String reservationId=UUID.randomUUID().toString();
  Optional<PromoCodeDiscount> discount=promotionCodeDiscount.flatMap((promoCodeDiscount) -> optionally(() -> promoCodeDiscountRepository.findPromoCodeInEventOrOrganization(event.getId(),promoCodeDiscount)));
  ticketReservationRepository.createNewReservation(reservationId,reservationExpiration,discount.map(PromoCodeDiscount::getId).orElse(null),locale.getLanguage(),event.getId());
  list.forEach(t -> reserveTicketsForCategory(event,specialPriceSessionId,reservationId,t,locale,forWaitingQueue,discount.orElse(null)));
  int ticketCount=list.stream().map(TicketReservationWithOptionalCodeModification::getAmount).mapToInt(Integer::intValue).sum();
  additionalServiceRepository.findAllInEventWithPolicy(event.getId(),AdditionalService.SupplementPolicy.MANDATORY_ONE_FOR_TICKET).stream().filter(AdditionalService::getSaleable).forEach(as -> {
    AdditionalServiceReservationModification asrm=new AdditionalServiceReservationModification();
    asrm.setAdditionalServiceId(as.getId());
    asrm.setQuantity(ticketCount);
    additionalServices.add(new ASReservationWithOptionalCodeModification(asrm,Optional.empty()));
  }
);
  additionalServices.forEach(as -> reserveAdditionalServicesForReservation(event.getId(),reservationId,as,discount.orElse(null)));
  TicketReservation reservation=ticketReservationRepository.findReservationById(reservationId);
  OrderSummary orderSummary=orderSummaryForReservationId(reservation.getId(),event,Locale.forLanguageTag(reservation.getUserLanguage()));
  ticketReservationRepository.addReservationInvoiceOrReceiptModel(reservationId,Json.toJson(orderSummary));
  auditingRepository.insert(null,null,Audit.EventType.RESERVATION_CREATE,new Date(),Audit.EntityType.RESERVATION,reservationId,null);
  return reservationId;
}