1
+ package org .springframework .data .jdbc .core .mapping ;
2
+
3
+ import static org .mockito .ArgumentMatchers .anyMap ;
4
+ import static org .mockito .ArgumentMatchers .anyString ;
5
+ import static org .mockito .Mockito .any ;
6
+ import static org .mockito .Mockito .mock ;
7
+ import static org .mockito .Mockito .when ;
8
+
9
+ import org .assertj .core .api .Assertions ;
10
+ import org .junit .jupiter .api .Test ;
11
+ import org .springframework .data .annotation .Id ;
12
+ import org .springframework .data .relational .core .conversion .MutableAggregateChange ;
13
+ import org .springframework .data .relational .core .dialect .MySqlDialect ;
14
+ import org .springframework .data .relational .core .dialect .PostgresDialect ;
15
+ import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
16
+ import org .springframework .data .relational .core .mapping .Table ;
17
+ import org .springframework .data .relational .core .mapping .TargetSequence ;
18
+ import org .springframework .data .relational .core .sql .IdentifierProcessing ;
19
+ import org .springframework .jdbc .core .RowMapper ;
20
+ import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
21
+
22
+ /**
23
+ * Unit tests for {@link IdGeneratingBeforeSaveCallback}
24
+ *
25
+ * @author Mikhail Polivakha
26
+ */
27
+ class IdGeneratingBeforeSaveCallbackTest {
28
+
29
+ @ Test
30
+ void test_mySqlDialect_sequenceGenerationIsNotSupported () {
31
+ // given
32
+ RelationalMappingContext relationalMappingContext = new RelationalMappingContext ();
33
+ MySqlDialect mySqlDialect = new MySqlDialect (IdentifierProcessing .NONE );
34
+ NamedParameterJdbcOperations operations = mock (NamedParameterJdbcOperations .class );
35
+
36
+ // and
37
+ IdGeneratingBeforeSaveCallback subject = new IdGeneratingBeforeSaveCallback (relationalMappingContext , mySqlDialect , operations );
38
+
39
+ NoSequenceEntity entity = new NoSequenceEntity ();
40
+
41
+ // when
42
+ Object processed = subject .onBeforeSave (entity , MutableAggregateChange .forSave (entity ));
43
+
44
+ // then
45
+ Assertions .assertThat (processed ).isSameAs (entity );
46
+ Assertions .assertThat (processed ).usingRecursiveComparison ().isEqualTo (entity );
47
+ }
48
+
49
+ @ Test
50
+ void test_EntityIsNotMarkedWithTargetSequence () {
51
+ // given
52
+ RelationalMappingContext relationalMappingContext = new RelationalMappingContext ();
53
+ PostgresDialect mySqlDialect = PostgresDialect .INSTANCE ;
54
+ NamedParameterJdbcOperations operations = mock (NamedParameterJdbcOperations .class );
55
+
56
+ // and
57
+ IdGeneratingBeforeSaveCallback subject = new IdGeneratingBeforeSaveCallback (relationalMappingContext , mySqlDialect , operations );
58
+
59
+ NoSequenceEntity entity = new NoSequenceEntity ();
60
+
61
+ // when
62
+ Object processed = subject .onBeforeSave (entity , MutableAggregateChange .forSave (entity ));
63
+
64
+ // then
65
+ Assertions .assertThat (processed ).isSameAs (entity );
66
+ Assertions .assertThat (processed ).usingRecursiveComparison ().isEqualTo (entity );
67
+ }
68
+
69
+ @ Test
70
+ void test_EntityIdIsPopulatedFromSequence () {
71
+ // given
72
+ RelationalMappingContext relationalMappingContext = new RelationalMappingContext ();
73
+ relationalMappingContext .getRequiredPersistentEntity (EntityWithSequence .class );
74
+
75
+ PostgresDialect mySqlDialect = PostgresDialect .INSTANCE ;
76
+ NamedParameterJdbcOperations operations = mock (NamedParameterJdbcOperations .class );
77
+
78
+ // and
79
+ long generatedId = 112L ;
80
+ when (operations .queryForObject (anyString (), anyMap (), any (RowMapper .class ))).thenReturn (generatedId );
81
+
82
+ // and
83
+ IdGeneratingBeforeSaveCallback subject = new IdGeneratingBeforeSaveCallback (relationalMappingContext , mySqlDialect , operations );
84
+
85
+ EntityWithSequence entity = new EntityWithSequence ();
86
+
87
+ // when
88
+ Object processed = subject .onBeforeSave (entity , MutableAggregateChange .forSave (entity ));
89
+
90
+ // then
91
+ Assertions .assertThat (processed ).isSameAs (entity );
92
+ Assertions
93
+ .assertThat (processed )
94
+ .usingRecursiveComparison ()
95
+ .ignoringFields ("id" )
96
+ .isEqualTo (entity );
97
+ Assertions .assertThat (entity .getId ()).isEqualTo (generatedId );
98
+ }
99
+
100
+ @ Table
101
+ static class NoSequenceEntity {
102
+
103
+ @ Id
104
+ private Long id ;
105
+ private Long name ;
106
+ }
107
+
108
+ @ Table
109
+ static class EntityWithSequence {
110
+
111
+ @ Id
112
+ @ TargetSequence (value = "id_seq" , schema = "public" )
113
+ private Long id ;
114
+
115
+ private Long name ;
116
+
117
+ public Long getId () {
118
+ return id ;
119
+ }
120
+ }
121
+ }
0 commit comments