Skip to content

Commit c8a4357

Browse files
authored
Merge pull request #97 from ozgliderpilot/refactor/java-datetime-alignment
Refactor/java datetime alignment
2 parents 21bbf34 + 7e5d3b8 commit c8a4357

10 files changed

Lines changed: 214 additions & 115 deletions

File tree

mflix/server/java-spring/pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>org.springframework.boot</groupId>
1010
<artifactId>spring-boot-starter-parent</artifactId>
11-
<version>3.5.7</version>
11+
<version>3.5.13</version>
1212
<relativePath/>
1313
</parent>
1414

@@ -88,6 +88,20 @@
8888
<scope>test</scope>
8989
</dependency>
9090

91+
<!-- Testcontainers JUnit Jupiter integration -->
92+
<dependency>
93+
<groupId>org.testcontainers</groupId>
94+
<artifactId>junit-jupiter</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
98+
<!-- MongoDB Atlas Local container for Testcontainers -->
99+
<dependency>
100+
<groupId>org.testcontainers</groupId>
101+
<artifactId>mongodb</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
91105
<!-- Jackson Databind for JSON serialization/deserialization -->
92106
<dependency>
93107
<groupId>com.fasterxml.jackson.core</groupId>

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/config/MongoConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.context.annotation.Bean;
1010
import org.springframework.context.annotation.Configuration;
1111
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
12+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
1213
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
1314
import org.springframework.lang.NonNull;
1415

@@ -119,4 +120,9 @@ public MongoDatabase mongoDatabase() {
119120

120121
return client.getDatabase(databaseName);
121122
}
123+
124+
@Bean
125+
public MongoCustomConversions customConversions() {
126+
return MongoCustomConversions.create(MongoCustomConversions.MongoConverterConfigurationAdapter::useNativeDriverJavaTimeCodecs);
127+
}
122128
}

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/model/Movie.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.mongodb.samplemflix.model;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4-
import java.util.Date;
4+
import java.time.Instant;
5+
import java.time.LocalDate;
56
import java.util.List;
67
import lombok.AccessLevel;
78
import lombok.AllArgsConstructor;
@@ -111,9 +112,14 @@ private Fields() {
111112
private String fullplot;
112113

113114
/**
114-
* Release date.
115+
* Release date as a calendar date (no time-of-day or time zone).
116+
*
117+
* <p>A movie release date is a "date only" concept (e.g. "1999-03-31"), not a specific
118+
* moment in time. We use {@link LocalDate} because it represents exactly that: a date
119+
* without time-of-day or time zone information. Spring Data MongoDB maps this via the
120+
* Jsr310 {@code LocalDateCodec}.
115121
*/
116-
private Date released;
122+
private LocalDate released;
117123

118124
/**
119125
* Runtime in minutes.
@@ -270,9 +276,12 @@ public static class Tomatoes {
270276
private String production;
271277

272278
/**
273-
* Last updated date.
279+
* Timestamp of the last update to Tomatoes ratings.
280+
*
281+
* <p>Stored as BSON DateTime in MongoDB. Uses {@link Instant} for an immutable,
282+
* UTC-only representation of this point-in-time event.
274283
*/
275-
private Date lastUpdated;
284+
private Instant lastUpdated;
276285

277286
/**
278287
* Nested class for viewer ratings.

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/model/dto/MovieWithCommentsResult.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.mongodb.samplemflix.model.dto;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4-
import java.util.Date;
4+
import java.time.Instant;
55
import java.util.List;
66
import lombok.Builder;
77

@@ -61,9 +61,12 @@ public record MovieWithCommentsResult (
6161
Integer totalComments,
6262

6363
/**
64-
* Date of the most recent comment.
64+
* Timestamp of the most recent comment as a UTC instant.
65+
*
66+
* <p>Uses {@link Instant} for an immutable, unambiguous UTC representation.
67+
* BSON DateTime values are converted via {@code Date.toInstant()}.
6568
*/
66-
Date mostRecentCommentDate) {
69+
Instant mostRecentCommentDate) {
6770

6871
/**
6972
* Nested record for comment information.
@@ -91,8 +94,11 @@ public record CommentInfo (
9194
String text,
9295

9396
/**
94-
* Comment date.
97+
* Comment timestamp as a UTC instant.
98+
*
99+
* <p>Stored as BSON DateTime in MongoDB. Uses {@link Instant} for immutability
100+
* and unambiguous UTC semantics.
95101
*/
96-
Date date) {}
102+
Instant date) {}
97103
}
98104

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/service/MovieServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ private MovieWithCommentsResult mapToMovieWithCommentsResult(Document doc) {
574574
.name(commentDoc.getString("name"))
575575
.email(commentDoc.getString("email"))
576576
.text(commentDoc.getString("text"))
577-
.date(commentDoc.getDate("date"))
577+
.date(commentDoc.get("date") != null ? commentDoc.getDate("date").toInstant() : null)
578578
.build())
579579
.collect(Collectors.toList());
580580
}
@@ -598,7 +598,7 @@ private MovieWithCommentsResult mapToMovieWithCommentsResult(Document doc) {
598598
.imdbRating(imdbRating)
599599
.recentComments(recentComments)
600600
.totalComments(doc.getInteger("totalComments"))
601-
.mostRecentCommentDate(doc.getDate("mostRecentCommentDate"))
601+
.mostRecentCommentDate(doc.getDate("mostRecentCommentDate") != null ? doc.getDate("mostRecentCommentDate").toInstant() : null)
602602
.build();
603603
}
604604

mflix/server/java-spring/src/test/java/com/mongodb/samplemflix/controller/MovieControllerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.mongodb.samplemflix.model.dto.UpdateMovieRequest;
2323
import com.mongodb.samplemflix.model.dto.VectorSearchResult;
2424
import com.mongodb.samplemflix.service.MovieService;
25+
import java.time.Instant;
2526
import java.util.Arrays;
26-
import java.util.Date;
2727
import java.util.HashMap;
2828
import java.util.List;
2929
import java.util.Map;
@@ -343,7 +343,7 @@ void testGetMoviesWithMostComments_Success() throws Exception {
343343
.name("John Doe")
344344
.email("john@example.com")
345345
.text("Great movie!")
346-
.date(new Date())
346+
.date(Instant.now())
347347
.build();
348348

349349
MovieWithCommentsResult result = MovieWithCommentsResult.builder()
@@ -356,7 +356,7 @@ void testGetMoviesWithMostComments_Success() throws Exception {
356356
.imdbRating(8.5)
357357
.recentComments(Arrays.asList(comment))
358358
.totalComments(5)
359-
.mostRecentCommentDate(new Date())
359+
.mostRecentCommentDate(Instant.now())
360360
.build();
361361

362362
when(movieService.getMoviesWithMostRecentComments(anyInt(), isNull())).thenReturn(Arrays.asList(result));

0 commit comments

Comments
 (0)