1515 */
1616package com .datastax .driver .mapping ;
1717
18+ import static org .assertj .core .api .Assertions .assertThat ;
1819import static org .testng .Assert .assertEquals ;
1920
2021import com .datastax .driver .core .CCMTestsSupport ;
22+ import com .datastax .driver .core .DataType ;
23+ import com .datastax .driver .core .TupleType ;
24+ import com .datastax .driver .core .TupleValue ;
25+ import com .datastax .driver .core .UserType ;
2126import com .datastax .driver .core .utils .CassandraVersion ;
2227import com .datastax .driver .core .utils .MoreObjects ;
2328import com .datastax .driver .mapping .annotations .FrozenKey ;
3035import java .util .List ;
3136import java .util .Map ;
3237import java .util .Set ;
38+ import java .util .UUID ;
3339import org .testng .annotations .Test ;
3440import org .testng .collections .Lists ;
3541
@@ -47,7 +53,8 @@ public void onTestContextInitialized() {
4753 + "s set<frozen<\" Sub\" >>, "
4854 + "m1 map<int,frozen<\" Sub\" >>, "
4955 + "m2 map<frozen<\" Sub\" >,int>, "
50- + "m3 map<frozen<\" Sub\" >,frozen<\" Sub\" >>)" );
56+ + "m3 map<frozen<\" Sub\" >,frozen<\" Sub\" >>)" ,
57+ "CREATE TABLE user_with_tuple (id uuid PRIMARY KEY, sub tuple<text,\" Sub\" >)" );
5158 }
5259
5360 @ UDT (name = "Sub" , caseSensitiveType = true )
@@ -198,4 +205,108 @@ public void testNullCollection() {
198205
199206 assertEquals (m .get (c .getId ()), c );
200207 }
208+
209+ @ Table (name = "user_with_tuple" )
210+ public static class UserWithTuple {
211+ @ PartitionKey private UUID id ;
212+
213+ private TupleValue sub ;
214+
215+ public UUID getId () {
216+ return id ;
217+ }
218+
219+ public void setId (UUID id ) {
220+ this .id = id ;
221+ }
222+
223+ public TupleValue getSub () {
224+ return sub ;
225+ }
226+
227+ public void setSub (TupleValue sub ) {
228+ this .sub = sub ;
229+ }
230+
231+ @ Override
232+ public boolean equals (Object o ) {
233+ if (this == o ) return true ;
234+ if (o == null || getClass () != o .getClass ()) return false ;
235+
236+ UserWithTuple that = (UserWithTuple ) o ;
237+
238+ if (id != null ? !id .equals (that .id ) : that .id != null ) return false ;
239+ return sub != null ? sub .equals (that .sub ) : that .sub == null ;
240+ }
241+
242+ @ Override
243+ public int hashCode () {
244+ int result = id != null ? id .hashCode () : 0 ;
245+ result = 31 * result + (sub != null ? sub .hashCode () : 0 );
246+ return result ;
247+ }
248+ }
249+
250+ @ UDT (name = "Sub" , caseSensitiveType = true )
251+ // Create a separate class with the same structure to test effectiveness of udtCodec
252+ public static class SubInTuple {
253+ private int i ;
254+
255+ public SubInTuple () {}
256+
257+ public SubInTuple (int i ) {
258+ this .i = i ;
259+ }
260+
261+ public int getI () {
262+ return i ;
263+ }
264+
265+ public void setI (int i ) {
266+ this .i = i ;
267+ }
268+
269+ @ Override
270+ public boolean equals (Object other ) {
271+ if (other instanceof SubInTuple ) {
272+ SubInTuple that = (SubInTuple ) other ;
273+ return this .i == that .i ;
274+ }
275+ return false ;
276+ }
277+
278+ @ Override
279+ public int hashCode () {
280+ return MoreObjects .hashCode (i );
281+ }
282+ }
283+
284+ /**
285+ * Validates that tables having a tuple that has a UDT field can be handled by the object mapper
286+ * assuming a udt codec has been registered with {@link MappingManager#udtCodec(Class)}
287+ *
288+ * @jira_ticket JAVA-1159
289+ * @test_category object_mapper
290+ */
291+ @ Test (groups = "short" )
292+ public void should_be_able_to_create_entity_from_table_having_tuple_with_udt () {
293+ MappingManager manager = new MappingManager (session ());
294+ Mapper <UserWithTuple > mapper = manager .mapper (UserWithTuple .class );
295+
296+ // register a codec using udtCodec, this is mandatory.
297+ manager .udtCodec (SubInTuple .class );
298+
299+ UserType subType = cluster ().getMetadata ().getKeyspace (keyspace ).getUserType ("\" Sub\" " );
300+ TupleType tt = cluster ().getMetadata ().newTupleType (DataType .text (), subType );
301+ TupleValue tv = tt .newValue ("seven" , new SubInTuple (7 ));
302+
303+ UserWithTuple user = new UserWithTuple ();
304+ user .setId (UUID .randomUUID ());
305+ user .setSub (tv );
306+
307+ mapper .save (user );
308+
309+ UserWithTuple retrieved = mapper .get (user .getId ());
310+ assertThat (retrieved ).isEqualTo (user );
311+ }
201312}
0 commit comments