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