Elffuss BraidHub

kikocis/java-driver public

9b39ff6a48bf · 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.ArrayList;
28import java.util.List;
29import java.util.Map;
30import java.util.Set;
31
32import static com.datastax.driver.core.Assertions.assertThat;
33
34@CassandraVersion("2.1.3")
35@SuppressWarnings("unused")
36public class MapperNestedCollectionsTest extends CCMTestsSupport {
37
38 @Override
39 public void onTestContextInitialized() {
40 execute(
41 "CREATE TYPE testType(i int, ls list<frozen<set<int>>>)",
42 "CREATE TABLE testTable (k int primary key, "
43 + "m1 map<text, frozen<map<int, int>>>, " // nested collection
44 + "m2 map<text, frozen<map<int, frozen<testType>>>>)" // nested collection with UDT
45 );
46 }
47
48 @Test(groups = "short")
49 public void should_map_fields_to_nested_collections() {
50 Mapper<TestTable> mapper = new MappingManager(session()).mapper(TestTable.class);
51
52 TestTable testTable = new TestTable();
53 testTable.setK(1);
54 testTable.setM1(ImmutableMap.<String, Map<Integer, Integer>>of("bar", ImmutableMap.of(1, 2)));
55
56 Set<Integer> s1 = Sets.newHashSet(1, 2, 3);
57 Set<Integer> s2 = Sets.newHashSet(4, 5, 6);
58 List<Set<Integer>> ls = new ArrayList<Set<Integer>>();
59 ls.add(s1);
60 ls.add(s2);
61
62 testTable.setM2(ImmutableMap.<String, Map<Integer, TestType>>of("bar", ImmutableMap.of(1, new TestType(2, ls))));
63
64 mapper.save(testTable);
65
66 TestTable testTable2 = mapper.get(1);
67 assertThat(testTable2.getM1()).isEqualTo(testTable.getM1());
68 assertThat(testTable2.getM2()).isEqualTo(testTable.getM2());
69 }
70
71 @Test(groups = "short")
72 public void should_map_accessor_params_to_nested_collections() {
73 MappingManager manager = new MappingManager(session());
74 Mapper<TestTable> mapper = manager.mapper(TestTable.class);
75 TestAccessor accessor = manager.createAccessor(TestAccessor.class);
76
77 TestTable testTable = new TestTable();
78 testTable.setK(1);
79 mapper.save(testTable);
80
81 Map<String, Map<Integer, Integer>> m1 = ImmutableMap.<String, Map<Integer, Integer>>of("bar", ImmutableMap.of(1, 2));
82 Map<String, Map<Integer, TestType>> m2 = ImmutableMap.<String, Map<Integer, TestType>>of("bar", ImmutableMap.of(1, new TestType(2)));
83 accessor.setM1(1, m1);
84 accessor.setM2(1, m2);
85
86 TestTable testTable2 = mapper.get(1);
87 assertThat(testTable2.getM1()).isEqualTo(m1);
88 assertThat(testTable2.getM2()).isEqualTo(m2);
89 }
90
91 @UDT(name = "testType")
92 public static class TestType {
93 private int i;
94
95 @Frozen("list<frozen<set<int>>>")
96 private List<Set<Integer>> ls;
97
98 public TestType() {
99 ls = Lists.newArrayList();
100 }
101
102 public TestType(int i) {
103 this();
104 this.i = i;
105 }
106
107 public TestType(int i, List<Set<Integer>> ls) {
108 this(i);
109 this.ls = ls;
110 }
111
112 public int getI() {
113 return i;
114 }
115
116 public void setI(int i) {
117 this.i = i;
118 }
119
120 public List<Set<Integer>> getLs() {
121 return ls;
122 }
123
124 public void setLs(List<Set<Integer>> ls) {
125 this.ls = ls;
126 }
127
128 @Override
129 public boolean equals(Object o) {
130 if (this == o)
131 return true;
132 if (!(o instanceof TestType))
133 return false;
134
135 TestType testType = (TestType) o;
136
137 return i == testType.i && ls.equals(testType.ls);
138
139 }
140
141 @Override
142 public int hashCode() {
143 int result = i;
144 result = 31 * result + ls.hashCode();
145 return result;
146 }
147 }
148
149 @Table(name = "testTable")
150 public static class TestTable {
151 @PartitionKey
152 private int k;
153
154 @Frozen("map<text, frozen<map<int, int>>>")
155 private Map<String, Map<Integer, Integer>> m1;
156
157 @Frozen("map<text, frozen<map<int, frozen<testType>>>>")
158 private Map<String, Map<Integer, TestType>> m2;
159
160 public int getK() {
161 return k;
162 }
163
164 public void setK(int k) {
165 this.k = k;
166 }
167
168 public Map<String, Map<Integer, Integer>> getM1() {
169 return m1;
170 }
171
172 public void setM1(Map<String, Map<Integer, Integer>> m1) {
173 this.m1 = m1;
174 }
175
176 public Map<String, Map<Integer, TestType>> getM2() {
177 return m2;
178 }
179
180 public void setM2(Map<String, Map<Integer, TestType>> m2) {
181 this.m2 = m2;
182 }
183 }
184
185 @Accessor
186 public interface TestAccessor {
187 @Query("UPDATE testTable SET m1 = :m1 WHERE k = :k")
188 ResultSet setM1(@Param("k") int k, @Param("m1") Map<String, Map<Integer, Integer>> m1);
189
190 @Query("UPDATE testTable SET m2 = :m2 WHERE k = :k")
191 ResultSet setM2(@Param("k") int k, @Param("m2") Map<String, Map<Integer, TestType>> m2);
192 }
193}