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