Elffuss BraidHub

kikocis/java-driver public

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