Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: actions/setup-java@v6
with:
distribution: 'temurin'
java-version: '25'
java-version: '21'
cache: 'maven'

- name: Cache SonarCloud Packages
Expand All @@ -47,7 +47,7 @@ jobs:
CI: true

- name: SonarCloud Code Analysis & Quality Gate Check
if: env.SONAR_TOKEN != ''
if: ${{ env.SONAR_TOKEN != '' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
@Repository
public interface ExcursionReviewRepository extends JpaRepository<ExcursionReview, Long> {

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
<S extends ExcursionReview> S save(S entity);

Optional<ExcursionReview> findByExcursionIdAndPassengerId(Long excursionId, Long passengerId);

boolean existsByExcursionIdAndPassengerId(Long excursionId, Long passengerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ExcursionReviewService {
private final ExcursionReviewRepository reviewRepository;
private final ShoreExcursionRepository excursionRepository;
private final ExcursionPassengerRepository passengerRepository;
private final ExcursionReviewWriter reviewWriter;

@Transactional
public ExcursionReviewResponse submitReview(Long excursionId, ExcursionReviewRequest request, String idempotencyKey) {
Expand Down Expand Up @@ -84,7 +85,7 @@ public ExcursionReviewResponse submitReview(Long excursionId, ExcursionReviewReq
.build();

try {
ExcursionReview saved = reviewRepository.save(review);
ExcursionReview saved = reviewWriter.writeItem(review);
log.info("Excursion review submitted: excursionId={}, passengerId={}, overallRating={}, flagged={}",
excursionId, request.passengerId(), request.ratingOverall(), isNegativeOrSafetyConcern);
return mapToResponse(saved);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cruise.activity.service;

import com.cruise.activity.entity.ExcursionReview;
import com.cruise.activity.repository.ExcursionReviewRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* Helper writer that persists a single ExcursionReview
* in an isolated REQUIRES_NEW transaction. This ensures that any
* database constraint violation (duplicate transaction or idempotency key) is isolated
* and does not mark the caller batch's transaction as rollback-only.
*/
@Service
@RequiredArgsConstructor
public class ExcursionReviewWriter {

private final ExcursionReviewRepository reviewRepository;

@Transactional(propagation = Propagation.REQUIRES_NEW)
public ExcursionReview writeItem(ExcursionReview review) {
return reviewRepository.save(review);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class ExcursionReviewAndAnalyticsTest {
@Mock
private ExcursionGuideRepository guideRepository;

@Mock
private ExcursionReviewWriter reviewWriter;

@InjectMocks
private ExcursionReviewService reviewService;

Expand All @@ -77,6 +80,7 @@ class ExcursionReviewAndAnalyticsTest {
void setUp() {
fleetAggregator = new FleetOccupancyAggregator(excursionRepository, vehicleRepository);
opsAnalyticsService = new ExcursionOpsAnalyticsService(excursionRepository, reviewRepository, guideRepository);
reviewService = new ExcursionReviewService(reviewRepository, excursionRepository, passengerRepository, reviewWriter);

mockExcursion = ShoreExcursion.builder()
.tourId(10L)
Expand Down Expand Up @@ -139,14 +143,14 @@ void submitReview_WhenAttended_ShouldSucceed() {
.build();
saved.setId(99L);

when(reviewRepository.save(any(ExcursionReview.class))).thenReturn(saved);
when(reviewWriter.writeItem(any(ExcursionReview.class))).thenReturn(saved);

ExcursionReviewResponse response = reviewService.submitReview(1L, req, "IDEM-1");

assertThat(response).isNotNull();
assertThat(response.ratingOverall()).isEqualTo(5);
assertThat(response.isFlagged()).isFalse();
verify(reviewRepository, times(1)).save(any(ExcursionReview.class));
verify(reviewWriter, times(1)).writeItem(any(ExcursionReview.class));
}

@Test
Expand Down Expand Up @@ -182,7 +186,7 @@ void submitReview_WhenOneStarOrDangerous_ShouldFlagReview() {
.build();
saved.setId(100L);

when(reviewRepository.save(any(ExcursionReview.class))).thenReturn(saved);
when(reviewWriter.writeItem(any(ExcursionReview.class))).thenReturn(saved);

ExcursionReviewResponse response = reviewService.submitReview(1L, req, null);

Expand Down Expand Up @@ -237,7 +241,7 @@ void submitReview_DataIntegrityViolation_Fallback() {
when(excursionRepository.findById(1L)).thenReturn(Optional.of(mockExcursion));
when(passengerRepository.findByExcursionIdAndActiveTrue(1L)).thenReturn(List.of(attendedPassenger));
when(reviewRepository.existsByExcursionIdAndPassengerId(1L, 201L)).thenReturn(false);
when(reviewRepository.save(any())).thenThrow(new DataIntegrityViolationException("duplicate key"));
when(reviewWriter.writeItem(any())).thenThrow(new DataIntegrityViolationException("duplicate key"));

ExcursionReview existing = ExcursionReview.builder()
.excursionId(1L)
Expand Down
Loading