Elffuss BraidHub

kikocis/java-driver public

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