16
16
package com .datastax .oss .driver .mapper ;
17
17
18
18
import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .assertj .core .api .Assertions .catchThrowable ;
19
20
20
21
import com .datastax .oss .driver .api .core .CqlIdentifier ;
21
22
import com .datastax .oss .driver .api .core .CqlSession ;
25
26
import com .datastax .oss .driver .api .core .cql .ResultSet ;
26
27
import com .datastax .oss .driver .api .core .cql .Row ;
27
28
import com .datastax .oss .driver .api .core .cql .SimpleStatement ;
29
+ import com .datastax .oss .driver .api .core .data .UdtValue ;
30
+ import com .datastax .oss .driver .api .core .type .UserDefinedType ;
28
31
import com .datastax .oss .driver .api .mapper .annotations .Dao ;
29
32
import com .datastax .oss .driver .api .mapper .annotations .DaoFactory ;
30
33
import com .datastax .oss .driver .api .mapper .annotations .DaoKeyspace ;
38
41
import com .datastax .oss .driver .categories .ParallelizableTests ;
39
42
import com .datastax .oss .driver .internal .core .util .concurrent .CompletableFutures ;
40
43
import com .datastax .oss .driver .shaded .guava .common .collect .Sets ;
44
+ import java .util .UUID ;
41
45
import java .util .stream .Stream ;
42
46
import org .junit .BeforeClass ;
43
47
import org .junit .ClassRule ;
@@ -56,6 +60,8 @@ public class GetEntityIT extends InventoryITBase {
56
60
@ ClassRule
57
61
public static final TestRule CHAIN = RuleChain .outerRule (CCM_RULE ).around (SESSION_RULE );
58
62
63
+ private static final UUID PRODUCT_2D_ID = UUID .randomUUID ();
64
+
59
65
private static ProductDao dao ;
60
66
61
67
@ BeforeClass
@@ -67,6 +73,18 @@ public static void setup() {
67
73
SimpleStatement .builder (query ).setExecutionProfile (SESSION_RULE .slowProfile ()).build ());
68
74
}
69
75
76
+ UserDefinedType dimensions2d =
77
+ session
78
+ .getKeyspace ()
79
+ .flatMap (ks -> session .getMetadata ().getKeyspace (ks ))
80
+ .flatMap (ks -> ks .getUserDefinedType ("dimensions2d" ))
81
+ .orElseThrow (AssertionError ::new );
82
+ session .execute (
83
+ "INSERT INTO product2d (id, description, dimensions) VALUES (?, ?, ?)" ,
84
+ PRODUCT_2D_ID ,
85
+ "2D product" ,
86
+ dimensions2d .newValue (12 , 34 ));
87
+
70
88
InventoryMapper inventoryMapper = new GetEntityIT_InventoryMapperBuilder (session ).build ();
71
89
dao = inventoryMapper .productDao (SESSION_RULE .keyspace ());
72
90
@@ -75,19 +93,98 @@ public static void setup() {
75
93
}
76
94
77
95
@ Test
78
- public void should_get_entity_from_row () {
96
+ public void should_get_entity_from_complete_row () {
79
97
CqlSession session = SESSION_RULE .session ();
80
98
ResultSet rs =
81
99
session .execute (
82
100
SimpleStatement .newInstance (
83
- "SELECT * FROM product WHERE id = ?" , FLAMETHROWER .getId ()));
101
+ "SELECT id, description, dimensions, now() FROM product WHERE id = ?" ,
102
+ FLAMETHROWER .getId ()));
84
103
Row row = rs .one ();
85
104
assertThat (row ).isNotNull ();
86
105
87
106
Product product = dao .get (row );
88
107
assertThat (product ).isEqualTo (FLAMETHROWER );
89
108
}
90
109
110
+ @ Test
111
+ public void should_not_get_entity_from_partial_row_when_not_lenient () {
112
+ CqlSession session = SESSION_RULE .session ();
113
+ ResultSet rs =
114
+ session .execute (
115
+ SimpleStatement .newInstance (
116
+ "SELECT id, description, now() FROM product WHERE id = ?" , FLAMETHROWER .getId ()));
117
+ Row row = rs .one ();
118
+ assertThat (row ).isNotNull ();
119
+
120
+ Throwable error = catchThrowable (() -> dao .get (row ));
121
+ assertThat (error ).hasMessage ("dimensions is not a column in this row" );
122
+ }
123
+
124
+ @ Test
125
+ public void should_get_entity_from_partial_row_when_lenient () {
126
+ CqlSession session = SESSION_RULE .session ();
127
+ ResultSet rs =
128
+ session .execute (
129
+ SimpleStatement .newInstance (
130
+ "SELECT id, dimensions FROM product2d WHERE id = ?" , PRODUCT_2D_ID ));
131
+ Row row = rs .one ();
132
+ assertThat (row ).isNotNull ();
133
+
134
+ Product product = dao .getLenient (row );
135
+ assertThat (product .getId ()).isEqualTo (PRODUCT_2D_ID );
136
+ assertThat (product .getDescription ()).isNull ();
137
+ assertThat (product .getDimensions ()).isNotNull ();
138
+ assertThat (product .getDimensions ().getWidth ()).isEqualTo (12 );
139
+ assertThat (product .getDimensions ().getHeight ()).isEqualTo (34 );
140
+ assertThat (product .getDimensions ().getLength ()).isZero ();
141
+ }
142
+
143
+ @ Test
144
+ public void should_get_entity_from_complete_udt_value () {
145
+ CqlSession session = SESSION_RULE .session ();
146
+ ResultSet rs =
147
+ session .execute (
148
+ SimpleStatement .newInstance (
149
+ "SELECT dimensions FROM product WHERE id = ?" , FLAMETHROWER .getId ()));
150
+ Row row = rs .one ();
151
+ assertThat (row ).isNotNull ();
152
+
153
+ Dimensions dimensions = dao .get (row .getUdtValue (0 ));
154
+ assertThat (dimensions ).isEqualTo (FLAMETHROWER .getDimensions ());
155
+ }
156
+
157
+ @ Test
158
+ public void should_not_get_entity_from_partial_udt_value_when_not_lenient () {
159
+ CqlSession session = SESSION_RULE .session ();
160
+ ResultSet rs =
161
+ session .execute (
162
+ SimpleStatement .newInstance (
163
+ "SELECT dimensions FROM product2d WHERE id = ?" , PRODUCT_2D_ID ));
164
+ Row row = rs .one ();
165
+ assertThat (row ).isNotNull ();
166
+
167
+ Throwable error = catchThrowable (() -> dao .get (row .getUdtValue (0 )));
168
+ assertThat (error ).hasMessage ("length is not a field in this UDT" );
169
+ }
170
+
171
+ @ Test
172
+ public void should_get_entity_from_partial_udt_value_when_lenient () {
173
+ CqlSession session = SESSION_RULE .session ();
174
+ ResultSet rs =
175
+ session .execute (
176
+ SimpleStatement .newInstance (
177
+ "SELECT dimensions FROM product2d WHERE id = ?" , PRODUCT_2D_ID ));
178
+ Row row = rs .one ();
179
+ assertThat (row ).isNotNull ();
180
+
181
+ Dimensions dimensions = dao .getLenient (row .getUdtValue (0 ));
182
+ assertThat (dimensions ).isNotNull ();
183
+ assertThat (dimensions .getWidth ()).isEqualTo (12 );
184
+ assertThat (dimensions .getHeight ()).isEqualTo (34 );
185
+ assertThat (dimensions .getLength ()).isZero ();
186
+ }
187
+
91
188
@ Test
92
189
public void should_get_entity_from_first_row_of_result_set () {
93
190
CqlSession session = SESSION_RULE .session ();
@@ -144,9 +241,19 @@ public interface InventoryMapper {
144
241
@ Dao
145
242
@ DefaultNullSavingStrategy (NullSavingStrategy .SET_TO_NULL )
146
243
public interface ProductDao {
244
+
147
245
@ GetEntity
148
246
Product get (Row row );
149
247
248
+ @ GetEntity (lenient = true )
249
+ Product getLenient (Row row );
250
+
251
+ @ GetEntity
252
+ Dimensions get (UdtValue row );
253
+
254
+ @ GetEntity (lenient = true )
255
+ Dimensions getLenient (UdtValue row );
256
+
150
257
@ GetEntity
151
258
PagingIterable <Product > get (ResultSet resultSet );
152
259
0 commit comments