Elffuss BraidHub

kikocis/java-driver public

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