Elffuss BraidHub

kikocis/java-driver public

d844cea208b1 · 5.9 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.CCMTestsSupport;
19import com.datastax.driver.core.ResultSet;
20import com.datastax.driver.core.utils.CassandraVersion;
21import com.datastax.driver.mapping.annotations.*;
22import com.google.common.collect.ImmutableMap;
23import com.google.common.collect.Lists;
24import com.google.common.collect.Sets;
25import org.testng.annotations.Test;
26
27import java.util.*;
28
29import static com.datastax.driver.core.Assertions.assertThat;
30
31@CassandraVersion(major = 2.1, minor = 3)
32@SuppressWarnings("unused")
33public class MapperNestedCollectionsTest extends CCMTestsSupport {
34
35 @Override
36 public Collection<String> createTestFixtures() {
37 return Lists.newArrayList(
38 "CREATE TYPE testType(i int, ls list<frozen<set<int>>>)",
39 "CREATE TABLE testTable (k int primary key, "
40 + "m1 map<text, frozen<map<int, int>>>, " // nested collection
41 + "m2 map<text, frozen<map<int, frozen<testType>>>>)" // nested collection with UDT
42 );
43 }
44
45 @Test(groups = "short")
46 public void should_map_fields_to_nested_collections() {
47 Mapper<TestTable> mapper = new MappingManager(session).mapper(TestTable.class);
48
49 TestTable testTable = new TestTable();
50 testTable.setK(1);
51 testTable.setM1(ImmutableMap.<String, Map<Integer, Integer>>of("bar", ImmutableMap.of(1, 2)));
52
53 Set<Integer> s1 = Sets.newHashSet(1, 2, 3);
54 Set<Integer> s2 = Sets.newHashSet(4, 5, 6);
55 List<Set<Integer>> ls = new ArrayList<Set<Integer>>();
56 ls.add(s1);
57 ls.add(s2);
58
59 testTable.setM2(ImmutableMap.<String, Map<Integer, TestType>>of("bar", ImmutableMap.of(1, new TestType(2, ls))));
60
61 mapper.save(testTable);
62
63 TestTable testTable2 = mapper.get(1);
64 assertThat(testTable2.getM1()).isEqualTo(testTable.getM1());
65 assertThat(testTable2.getM2()).isEqualTo(testTable.getM2());
66 }
67
68 @Test(groups = "short")
69 public void should_map_accessor_params_to_nested_collections() {
70 MappingManager manager = new MappingManager(session);
71 Mapper<TestTable> mapper = manager.mapper(TestTable.class);
72 TestAccessor accessor = manager.createAccessor(TestAccessor.class);
73
74 TestTable testTable = new TestTable();
75 testTable.setK(1);
76 mapper.save(testTable);
77
78 Map<String, Map<Integer, Integer>> m1 = ImmutableMap.<String, Map<Integer, Integer>>of("bar", ImmutableMap.of(1, 2));
79 Map<String, Map<Integer, TestType>> m2 = ImmutableMap.<String, Map<Integer, TestType>>of("bar", ImmutableMap.of(1, new TestType(2)));
80 accessor.setM1(1, m1);
81 accessor.setM2(1, m2);
82
83 TestTable testTable2 = mapper.get(1);
84 assertThat(testTable2.getM1()).isEqualTo(m1);
85 assertThat(testTable2.getM2()).isEqualTo(m2);
86 }
87
88 @UDT(name = "testType")
89 public static class TestType {
90 private int i;
91
92 @Frozen("list<frozen<set<int>>>")
93 private List<Set<Integer>> ls;
94
95 public TestType() {
96 ls = Lists.newArrayList();
97 }
98
99 public TestType(int i) {
100 this();
101 this.i = i;
102 }
103
104 public TestType(int i, List<Set<Integer>> ls) {
105 this(i);
106 this.ls = ls;
107 }
108
109 public int getI() {
110 return i;
111 }
112
113 public void setI(int i) {
114 this.i = i;
115 }
116
117 public List<Set<Integer>> getLs() {
118 return ls;
119 }
120
121 public void setLs(List<Set<Integer>> ls) {
122 this.ls = ls;
123 }
124
125 @Override
126 public boolean equals(Object o) {
127 if (this == o)
128 return true;
129 if (!(o instanceof TestType))
130 return false;
131
132 TestType testType = (TestType) o;
133
134 return i == testType.i && ls.equals(testType.ls);
135
136 }
137
138 @Override
139 public int hashCode() {
140 int result = i;
141 result = 31 * result + ls.hashCode();
142 return result;
143 }
144 }
145
146 @Table(name = "testTable")
147 public static class TestTable {
148 @PartitionKey
149 private int k;
150
151 @Frozen("map<text, frozen<map<int, int>>>")
152 private Map<String, Map<Integer, Integer>> m1;
153
154 @Frozen("map<text, frozen<map<int, frozen<testType>>>>")
155 private Map<String, Map<Integer, TestType>> m2;
156
157 public int getK() {
158 return k;
159 }
160
161 public void setK(int k) {
162 this.k = k;
163 }
164
165 public Map<String, Map<Integer, Integer>> getM1() {
166 return m1;
167 }
168
169 public void setM1(Map<String, Map<Integer, Integer>> m1) {
170 this.m1 = m1;
171 }
172
173 public Map<String, Map<Integer, TestType>> getM2() {
174 return m2;
175 }
176
177 public void setM2(Map<String, Map<Integer, TestType>> m2) {
178 this.m2 = m2;
179 }
180 }
181
182 @Accessor
183 public interface TestAccessor {
184 @Query("UPDATE testTable SET m1 = :m1 WHERE k = :k")
185 ResultSet setM1(@Param("k") int k, @Param("m1") Map<String, Map<Integer, Integer>> m1);
186
187 @Query("UPDATE testTable SET m2 = :m2 WHERE k = :k")
188 ResultSet setM2(@Param("k") int k, @Param("m2") Map<String, Map<Integer, TestType>> m2);
189 }
190}