Elffuss BraidHub

kikocis/java-driver public

bf1b4ac3f151 · 8.3 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.CCMBridge;
19import com.datastax.driver.core.Cluster;
20import com.datastax.driver.core.Session;
21import com.datastax.driver.core.utils.CassandraVersion;
22import com.datastax.driver.mapping.annotations.*;
23import com.google.common.collect.ImmutableMap;
24import org.testng.annotations.Test;
25
26import java.util.HashMap;
27import java.util.Map;
28
29@CassandraVersion(major = 2.1)
30public class UDTFieldMapperTest {
31
32 @Test(groups = "short")
33 public void udt_and_tables_with_ks_created_in_another_session_should_be_mapped() {
34 CCMBridge ccm = null;
35 Cluster cluster = null;
36
37 try {
38 // Create type and table
39 ccm = CCMBridge.builder("test").build();
40 cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
41 Session session1 = cluster.connect();
42 session1.execute("create schema if not exists java_509 " +
43 "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
44 session1.execute("create type java_509.my_tuple (" +
45 "type text, " +
46 "value text);");
47 session1.execute("create table java_509.my_hash (" +
48 "key int primary key, " +
49 "properties map<text, frozen<java_509.my_tuple>>);");
50 cluster.close();
51
52 // Create entities with another connection
53 cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
54 Session session2 = cluster.newSession();
55 Mapper<MyHashWithKeyspace> hashMapper = new MappingManager(session2).mapper(MyHashWithKeyspace.class);
56 hashMapper.save(new MyHashWithKeyspace(
57 1,
58 ImmutableMap.of("key-1", new MyTupleWithKeyspace("first-half-1", "second-half-1"))));
59 hashMapper.save(new MyHashWithKeyspace(
60 2,
61 ImmutableMap.of("key-2", new MyTupleWithKeyspace("first-half-2", null))));
62 hashMapper.save(new MyHashWithKeyspace(
63 3,
64 ImmutableMap.of("key-3", new MyTupleWithKeyspace(null, "second-half-3"))));
65 hashMapper.save(new MyHashWithKeyspace(
66 4,
67 new HashMap<String, MyTupleWithKeyspace>()));
68 hashMapper.save(new MyHashWithKeyspace(
69 5,
70 null));
71 } finally {
72 if (cluster != null)
73 cluster.close();
74 if (ccm != null)
75 ccm.remove();
76 }
77 }
78
79 @Test(groups = "short")
80 public void udt_and_tables_without_ks_created_in_another_session_should_be_mapped() {
81 CCMBridge ccm = null;
82 Cluster cluster = null;
83
84 try {
85 // Create type and table
86 ccm = CCMBridge.builder("test").build();
87 cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
88 Session session1 = cluster.connect();
89 session1.execute("create schema if not exists java_509 " +
90 "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
91 session1.execute("use java_509");
92 session1.execute("create type my_tuple (" +
93 "type text, " +
94 "value text);");
95 session1.execute("create table my_hash (" +
96 "key int primary key, " +
97 "properties map<text, frozen<my_tuple>>);");
98 cluster.close();
99
100 // Create entities with another connection
101 cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
102 Session session2 = cluster.newSession();
103
104 session2.execute("use java_509");
105 Mapper<MyHash> hashMapper = new MappingManager(session2).mapper(MyHash.class);
106 hashMapper.save(new MyHash(
107 1,
108 ImmutableMap.of("key-1", new MyTuple("first-half-1", "second-half-1"))));
109 hashMapper.save(new MyHash(
110 2,
111 ImmutableMap.of("key-2", new MyTuple("first-half-2", null))));
112 hashMapper.save(new MyHash(
113 3,
114 ImmutableMap.of("key-3", new MyTuple(null, "second-half-3"))));
115 hashMapper.save(new MyHash(
116 4,
117 new HashMap<String, MyTuple>()));
118 hashMapper.save(new MyHash(
119 5,
120 null));
121 } finally {
122 if (cluster != null)
123 cluster.close();
124 if (ccm != null)
125 ccm.remove();
126 }
127 }
128
129 @UDT(name = "my_tuple")
130 static class MyTuple {
131 @Field(name = "type")
132 private String type;
133 @Field(name = "value")
134 private String value;
135
136 public MyTuple() {
137 }
138
139 public MyTuple(String type, String value) {
140 this.type = type;
141 this.value = value;
142 }
143
144 public String getType() {
145 return type;
146 }
147
148 public void setType(String type) {
149 this.type = type;
150 }
151
152 public String getValue() {
153 return value;
154 }
155
156 public void setValue(String value) {
157 this.value = value;
158 }
159 }
160
161 @Table(name = "my_hash")
162 static class MyHash {
163 @PartitionKey
164 @Column(name = "key")
165 private int key;
166
167 @FrozenValue
168 @Column(name = "properties")
169 private Map<String, MyTuple> properties;
170
171 public MyHash() {
172
173 }
174
175 public MyHash(int key, Map<String, MyTuple> properties) {
176
177 this.key = key;
178 this.properties = properties;
179 }
180
181 public int getKey() {
182 return key;
183 }
184
185 public void setKey(int key) {
186 this.key = key;
187 }
188
189 public Map<String, MyTuple> getProperties() {
190 return properties;
191 }
192
193 public void setProperties(Map<String, MyTuple> properties) {
194 this.properties = properties;
195 }
196 }
197
198 @UDT(name = "my_tuple", keyspace = "java_509")
199 static class MyTupleWithKeyspace {
200 @Field(name = "type")
201 private String type;
202 @Field(name = "value")
203 private String value;
204
205 public MyTupleWithKeyspace() {
206 }
207
208 public MyTupleWithKeyspace(String type, String value) {
209 this.type = type;
210 this.value = value;
211 }
212
213 public String getType() {
214 return type;
215 }
216
217 public void setType(String type) {
218 this.type = type;
219 }
220
221 public String getValue() {
222 return value;
223 }
224
225 public void setValue(String value) {
226 this.value = value;
227 }
228 }
229
230 @Table(name = "my_hash", keyspace = "java_509")
231 static class MyHashWithKeyspace {
232 @PartitionKey
233 @Column(name = "key")
234 private int key;
235
236 @FrozenValue
237 @Column(name = "properties")
238 private Map<String, MyTupleWithKeyspace> properties;
239
240 public MyHashWithKeyspace() {
241 }
242
243 public MyHashWithKeyspace(int key, Map<String, MyTupleWithKeyspace> properties) {
244 this.key = key;
245 this.properties = properties;
246 }
247
248 public int getKey() {
249 return key;
250 }
251
252 public void setKey(int key) {
253 this.key = key;
254 }
255
256 public Map<String, MyTupleWithKeyspace> getProperties() {
257 return properties;
258 }
259
260 public void setProperties(Map<String, MyTupleWithKeyspace> properties) {
261 this.properties = properties;
262 }
263 }
264}