1+ package com .dedicatedcode .reitti .service ;
2+
3+ import com .dedicatedcode .reitti .IntegrationTest ;
4+ import com .dedicatedcode .reitti .TestingService ;
5+ import com .dedicatedcode .reitti .model .processing .DetectionParameter ;
6+ import com .dedicatedcode .reitti .model .processing .RecalculationState ;
7+ import com .dedicatedcode .reitti .model .security .User ;
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .Test ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .jdbc .core .JdbcTemplate ;
12+
13+ import java .sql .Timestamp ;
14+ import java .time .Instant ;
15+ import java .util .UUID ;
16+
17+ import static org .junit .jupiter .api .Assertions .*;
18+
19+ @ IntegrationTest
20+ class PreviewCleanupJobTest {
21+
22+ @ Autowired
23+ private PreviewCleanupJob previewCleanupJob ;
24+
25+ @ Autowired
26+ private JdbcTemplate jdbcTemplate ;
27+
28+ @ Autowired
29+ private VisitDetectionPreviewService visitDetectionPreviewService ;
30+
31+ @ Autowired
32+ private TestingService testingService ;
33+
34+ private User user ;
35+
36+ @ BeforeEach
37+ void setUp () {
38+ this .user = testingService .randomUser ();
39+ }
40+
41+ @ Test
42+ void shouldRunCleanUpJob () {
43+ Instant now = Instant .now ();
44+ DetectionParameter config = new DetectionParameter (null ,
45+ new DetectionParameter .VisitDetection (100 , 100 ),
46+ new DetectionParameter .VisitMerging (1 , 100 , 100 ),
47+ new DetectionParameter .LocationDensity (50.0 , 100 ),
48+ now ,
49+ RecalculationState .NEEDED );
50+ String previewId = this .visitDetectionPreviewService .startPreview (user , config , now );
51+
52+ this .jdbcTemplate .update ("UPDATE preview_significant_places SET preview_created_at = ? WHERE preview_id = ?" , Timestamp .from (now .minusSeconds (259200 )), previewId );
53+ this .jdbcTemplate .update ("UPDATE preview_raw_location_points SET preview_created_at = ? WHERE preview_id = ?" , Timestamp .from (now .minusSeconds (259200 )), previewId );
54+ this .jdbcTemplate .update ("UPDATE preview_processed_visits SET preview_created_at = ? WHERE preview_id = ?" , Timestamp .from (now .minusSeconds (259200 )), previewId );
55+ this .jdbcTemplate .update ("UPDATE preview_trips SET preview_created_at = ? WHERE preview_id = ?" , Timestamp .from (now .minusSeconds (259200 )), previewId );
56+ this .jdbcTemplate .update ("UPDATE preview_visit_detection_parameters SET preview_created_at = ? WHERE preview_id = ?" , Timestamp .from (now .minusSeconds (259200 )), previewId );
57+
58+ this .previewCleanupJob .cleanUp ();
59+
60+ assertEquals (0 , jdbcTemplate .queryForObject ("SELECT COUNT(*) FROM preview_processed_visits WHERE preview_id = ?" , Integer .class , previewId ));
61+ assertEquals (0 , jdbcTemplate .queryForObject ("SELECT COUNT(*) FROM preview_raw_location_points WHERE preview_id = ?" , Integer .class , previewId ));
62+ assertEquals (0 , jdbcTemplate .queryForObject ("SELECT COUNT(*) FROM preview_significant_places WHERE preview_id = ?" , Integer .class , previewId ));
63+ assertEquals (0 , jdbcTemplate .queryForObject ("SELECT COUNT(*) FROM preview_trips WHERE preview_id = ?" , Integer .class , previewId ));
64+ assertEquals (0 , jdbcTemplate .queryForObject ("SELECT COUNT(*) FROM preview_visit_detection_parameters WHERE preview_id = ?" , Integer .class , previewId ));
65+ }
66+ }
0 commit comments