Elffuss BraidHub

kikocis/java-driver public

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