Elffuss BraidHub

kikocis/java-driver public

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