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