Skip to content

[BUG] TransportModeService.segmentAndClassifyTrip throws NoSuchElementException for an empty trip #1020

@SuperCorleone

Description

@SuperCorleone

Describe the Bug

segmentAndClassifyTrip(points, configs) calls points.getFirst() with no empty check. When points is empty it throws java.util.NoSuchElementException. This is reachable on real paths: inferTransportMode (no-override branch) is called with tripPoints loaded from the DB, which can be empty when a trip's time window contains no surviving raw points (data gaps, pruned/cleaned points). It crashes trip creation and bulk reclassification.

Root Cause

// TransportModeService.segmentAndClassifyTrip, L44-46
List<TripSegment> segments = new ArrayList<>();
List<Double> speeds = calculateSpeeds(points);   // tolerates empty
List<RawLocationPoint> currentSegmentPoints = new ArrayList<>();
currentSegmentPoints.add(points.getFirst());      // L46: NoSuchElementException when points is empty

Reachable call paths (no empty-guard before the call):

  • UnifiedLocationProcessingService.java:770 — trip creation: tripPoints = rawLocationPointJdbcService.findByUserAndTimestampBetweenOrderByTimestampAsc(...)
  • TransportationModesController.java:208reclassifyTrips over existing trips.
    Only the transport-mode-override early-return guards inferTransportMode; absent an override, execution reaches L46.

Steps to Reproduce

  1. Call inferTransportMode/segmentAndClassifyTrip for a trip whose point list is empty (or create/reclassify a trip whose [startTime,endTime] window has no raw points).

Expected Behavior

Empty points → TransportMode.UNKNOWN (the documented "can't classify" result), no exception.

Observed Behavior (reproduced)

TestFusionUnit__2412__Test.testSegmentAndClassifyTrip_emptyPoints_returnsUnknown:79 » NoSuchElement
java.util.NoSuchElementException
  at …TransportModeService.segmentAndClassifyTrip(TransportModeService.java:46)

(testSegmentAndClassifyTrip_singlePoint_returnsUnknown passes — only the empty case fails.)

Suggested Fix

public TransportMode segmentAndClassifyTrip(List<RawLocationPoint> points, List<TransportModeConfig> configs) {
    if (points == null || points.isEmpty()) {
        return TransportMode.UNKNOWN;
    }
    ...
}

Corresponding Test (generated)

@Test
public void testSegmentAndClassifyTrip_emptyPoints_returnsUnknown() {
    // Arrange
    List<RawLocationPoint> points = new ArrayList<>();
    List<TransportModeConfig> configs = Arrays.asList(
        new TransportModeConfig(TransportMode.WALKING, 5.0),
        new TransportModeConfig(TransportMode.CYCLING, 20.0),
        new TransportModeConfig(TransportMode.DRIVING, null)
    );
    
    // Act
    TransportMode result = transportModeService.segmentAndClassifyTrip(points, configs);
    
    // Assert
    assertEquals(TransportMode.UNKNOWN, result);
}

This input was generated by the test case generator TestFusion developed in our STAR lab.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions