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