File tree Expand file tree Collapse file tree 4 files changed +145
-0
lines changed
customer-service/src/test/java/io/github/bsayli/customerservice/api/dto Expand file tree Collapse file tree 4 files changed +145
-0
lines changed Original file line number Diff line number Diff line change 1+ package io .github .bsayli .customerservice .api .dto ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import jakarta .validation .Validation ;
6+ import jakarta .validation .Validator ;
7+ import jakarta .validation .ValidatorFactory ;
8+ import org .junit .jupiter .api .*;
9+
10+ @ Tag ("unit" )
11+ @ DisplayName ("DTO Validation: CustomerCreateRequest" )
12+ class CustomerCreateRequestValidationTest {
13+
14+ private static Validator validator ;
15+
16+ @ BeforeAll
17+ static void setup () {
18+ ValidatorFactory factory = Validation .buildDefaultValidatorFactory ();
19+ validator = factory .getValidator ();
20+ }
21+
22+ @ Test
23+ @ DisplayName ("name/email valid -> no violations" )
24+ void validPayload_shouldPass () {
25+ var dto = new CustomerCreateRequest ("John Smith" , "john.smith@example.com" );
26+ assertThat (validator .validate (dto )).isEmpty ();
27+ }
28+
29+ @ Test
30+ @ DisplayName ("blank name -> validation fails" )
31+ void blankName_shouldFail () {
32+ var dto = new CustomerCreateRequest (" " , "john.smith@example.com" );
33+ assertThat (validator .validate (dto )).isNotEmpty ();
34+ }
35+
36+ @ Test
37+ @ DisplayName ("invalid email -> validation fails" )
38+ void invalidEmail_shouldFail () {
39+ var dto = new CustomerCreateRequest ("John Smith" , "not-an-email" );
40+ assertThat (validator .validate (dto )).isNotEmpty ();
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package io .github .bsayli .customerservice .api .dto ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import com .fasterxml .jackson .databind .SerializationFeature ;
7+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
8+ import java .time .Instant ;
9+ import org .junit .jupiter .api .*;
10+
11+ @ Tag ("unit" )
12+ @ DisplayName ("DTO JSON: CustomerCreateResponse" )
13+ class CustomerCreateResponseJsonTest {
14+
15+ private ObjectMapper om ;
16+
17+ @ BeforeEach
18+ void setUp () {
19+ om = new ObjectMapper ();
20+ om .registerModule (new JavaTimeModule ());
21+ om .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
22+ }
23+
24+ @ Test
25+ @ DisplayName ("createdAt should serialize as ISO-8601 string" )
26+ void createdAt_shouldBeIso8601 () throws Exception {
27+ var customer = new CustomerDto (10 , "Jane" , "jane@example.com" );
28+ var resp = new CustomerCreateResponse (customer , Instant .parse ("2025-01-01T12:34:56Z" ));
29+
30+ String json = om .writeValueAsString (resp );
31+ assertThat (json ).contains ("\" createdAt\" :\" 2025-01-01T12:34:56Z\" " );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package io .github .bsayli .customerservice .api .dto ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
7+ import org .junit .jupiter .api .*;
8+
9+ @ Tag ("unit" )
10+ @ DisplayName ("DTO JSON: CustomerDto" )
11+ class CustomerDtoJsonTest {
12+
13+ private ObjectMapper om ;
14+
15+ @ BeforeEach
16+ void setUp () {
17+ om = new ObjectMapper ();
18+ om .registerModule (new JavaTimeModule ());
19+ }
20+
21+ @ Test
22+ @ DisplayName ("serialize/deserialize should preserve fields" )
23+ void roundTrip_shouldPreserveFields () throws Exception {
24+ var dto = new CustomerDto (5 , "Mark Lee" , "mark.lee@example.com" );
25+
26+ String json = om .writeValueAsString (dto );
27+ CustomerDto back = om .readValue (json , CustomerDto .class );
28+
29+ assertThat (back .customerId ()).isEqualTo (5 );
30+ assertThat (back .name ()).isEqualTo ("Mark Lee" );
31+ assertThat (back .email ()).isEqualTo ("mark.lee@example.com" );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package io .github .bsayli .customerservice .api .dto ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import jakarta .validation .Validation ;
6+ import jakarta .validation .Validator ;
7+ import jakarta .validation .ValidatorFactory ;
8+ import org .junit .jupiter .api .*;
9+
10+ @ Tag ("unit" )
11+ @ DisplayName ("DTO Validation: CustomerUpdateRequest" )
12+ class CustomerUpdateRequestValidationTest {
13+
14+ private static Validator validator ;
15+
16+ @ BeforeAll
17+ static void setup () {
18+ ValidatorFactory factory = Validation .buildDefaultValidatorFactory ();
19+ validator = factory .getValidator ();
20+ }
21+
22+ @ Test
23+ @ DisplayName ("valid update -> no violations" )
24+ void validUpdate_shouldPass () {
25+ var dto = new CustomerUpdateRequest ("Jane Doe" , "jane.doe@example.com" );
26+ assertThat (validator .validate (dto )).isEmpty ();
27+ }
28+
29+ @ Test
30+ @ DisplayName ("blank name or invalid email -> violations" )
31+ void invalidUpdate_shouldFail () {
32+ var dto1 = new CustomerUpdateRequest ("" , "jane.doe@example.com" );
33+ var dto2 = new CustomerUpdateRequest ("Jane Doe" , "bad-email" );
34+ assertThat (validator .validate (dto1 )).isNotEmpty ();
35+ assertThat (validator .validate (dto2 )).isNotEmpty ();
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments