Elffuss BraidHub

kikocis/java-driver public

1c1e4c2160b4 · 7.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 java.util.*;
19
20import com.datastax.driver.core.Session;
21import com.datastax.driver.core.utils.CassandraVersion;
22import com.datastax.driver.mapping.annotations.*;
23import com.google.common.base.Objects;
24import org.testng.annotations.Test;
25
26import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT;
27import static org.testng.Assert.assertEquals;
28import static org.testng.Assert.assertNull;
29
30import com.datastax.driver.core.CCMBridge;
31import com.datastax.driver.core.utils.UUIDs;
32
33/**
34 * Tests usage of mapping annotations without specifying a keyspace.
35 */
36@CassandraVersion(major=2.1)
37public class MapperDefaultKeyspaceTest extends CCMBridge.PerClassSingleNodeCluster {
38
39 private static final String KEYSPACE = "mapper_default_keyspace_test_ks";
40
41 protected Collection<String> getTableDefinitions() {
42 return Arrays.asList("CREATE TABLE groups (group_id uuid PRIMARY KEY, name text)",
43 "CREATE TYPE IF NOT EXISTS group_name (name text)");
44 }
45
46 /*
47 * An entity that does not specify a keyspace in its @Table annotation. When a keyspace is
48 * not specified, the mapper uses the session's logged in keyspace.
49 */
50 @Table(name = "groups")
51 public static class Group {
52
53 @PartitionKey
54 @Column(name = "group_id")
55 private UUID groupId;
56
57 private String name;
58
59 public Group() {}
60
61 public Group(String name) {
62 this.name = name;
63 this.groupId = UUIDs.random();
64 }
65
66 public UUID getGroupId() {
67 return groupId;
68 }
69
70 public void setGroupId(UUID groupId) {
71 this.groupId = groupId;
72 }
73
74 public String getName() {
75 return name;
76 }
77
78 public void setName(String name) {
79 this.name = name;
80 }
81
82 @Override
83 public boolean equals(Object other) {
84 if (other == null || other.getClass() != this.getClass())
85 return false;
86
87 Group that = (Group)other;
88 return Objects.equal(groupId, that.groupId)
89 && Objects.equal(name, that.name);
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hashCode(groupId, name);
95 }
96 }
97
98 @Test(groups = "short")
99 public void testTableWithDefaultKeyspace() throws Exception {
100 // Ensure that the test session is logged into the "ks" keyspace.
101 session.execute("USE " + keyspace);
102
103 MappingManager manager = new MappingManager(session);
104 Mapper<Group> m = manager.mapper(Group.class);
105 Group group = new Group("testGroup");
106 UUID groupId = group.getGroupId();
107
108 // Check the save operation.
109 m.save(group);
110
111 // Check the select operation.
112 Group selectedGroup = m.get(groupId);
113 assertEquals(selectedGroup.getGroupId(), groupId);
114
115 // Check the delete operation.
116 m.delete(group);
117 assertNull(m.get(groupId));
118 }
119
120 @Table(keyspace = KEYSPACE, name = "groups2")
121 public static class Group2 {
122
123 @PartitionKey
124 @Column(name = "group_id")
125 private UUID groupId;
126
127 @Frozen
128 private GroupName name;
129
130 public Group2() {}
131
132 public Group2(GroupName name) {
133 this.name = name;
134 this.groupId = UUIDs.random();
135 }
136
137 public UUID getGroupId() {
138 return groupId;
139 }
140
141 public void setGroupId(UUID groupId) {
142 this.groupId = groupId;
143 }
144
145 public GroupName getName() {
146 return name;
147 }
148
149 public void setName(GroupName name) {
150 this.name = name;
151 }
152
153 @Override
154 public boolean equals(Object other) {
155 if (other == null || other.getClass() != this.getClass())
156 return false;
157
158 Group that = (Group)other;
159 return Objects.equal(groupId, that.groupId)
160 && Objects.equal(name, that.name);
161 }
162
163 @Override
164 public int hashCode() {
165 return Objects.hashCode(groupId, name);
166 }
167 }
168
169 /*
170 * User defined type without a keyspace specified. The mapper will use the session's logged
171 * keyspace when a keyspace is not specified in the @UDT annotation.
172 */
173 @UDT(name = "group_name")
174 public static class GroupName {
175 private String name;
176
177 public GroupName() {
178 }
179
180 public GroupName(String name) {
181 this.name = name;
182 }
183
184 public String getName() {
185 return name;
186 }
187
188 public void setName(String name) {
189 this.name = name;
190 }
191
192 @Override
193 public boolean equals(Object other) {
194 if (other instanceof GroupName) {
195 GroupName that = (GroupName) other;
196 return this.name == that.name;
197 }
198 return false;
199 }
200
201 @Override
202 public int hashCode() {
203 return Objects.hashCode(name);
204 }
205 }
206
207 @Test(groups = "short")
208 public void testUDTWithDefaultKeyspace() throws Exception {
209 // Create test specific keyspace and table.
210 session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }", KEYSPACE));
211 session.execute(String.format("CREATE TYPE IF NOT EXISTS %s.group_name (name text)", KEYSPACE));
212 session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.groups2 (group_id uuid PRIMARY KEY, name frozen<group_name>)", KEYSPACE));
213 // Ensure that the test session is logged into the keyspace.
214 session.execute("USE " + keyspace);
215
216 MappingManager manager = new MappingManager(session);
217 Mapper<Group2> m = manager.mapper(Group2.class);
218 Group2 group = new Group2(new GroupName("testGroup"));
219 UUID groupId = group.getGroupId();
220
221 // Check the save operation.
222 m.save(group);
223
224 // Check the select operation.
225 Group2 selectedGroup = m.get(groupId);
226 assertEquals(selectedGroup.getGroupId(), groupId);
227
228 // Check the delete operation.
229 m.delete(group);
230 assertNull(m.get(groupId));
231 }
232
233 @Test(groups = "short",
234 expectedExceptions = IllegalArgumentException.class,
235 expectedExceptionsMessageRegExp = "Error creating mapper for class Group, the @Table annotation declares no default keyspace, and the session is not currently logged to any keyspace")
236 public void should_throw_a_meaningful_error_message_when_no_default_table_keyspace_and_session_not_logged() {
237 Session session2 = cluster.connect();
238 MappingManager manager = new MappingManager(session2);
239 manager.mapper(Group.class);
240 }
241
242 @Test(groups = "short",
243 expectedExceptions = IllegalArgumentException.class,
244 expectedExceptionsMessageRegExp = "Error creating UDT mapper for class GroupName, the @UDT annotation declares no default keyspace, and the session is not currently logged to any keyspace")
245 public void should_throw_a_meaningful_error_message_when_no_default_udt_keyspace_and_session_not_logged() {
246 Session session2 = cluster.connect();
247 MappingManager manager = new MappingManager(session2);
248 manager.udtMapper(GroupName.class);
249 }
250}