| 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.ResultSet; |
| 20 | import com.datastax.driver.core.Row; |
| 21 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 22 | import com.datastax.driver.core.utils.CassandraVersion; |
| 23 | import com.datastax.driver.mapping.annotations.*; |
| 24 | import org.testng.annotations.Test; |
| 25 | import org.testng.collections.Lists; |
| 26 | |
| 27 | import java.util.List; |
| 28 | |
| 29 | import static org.assertj.core.api.Assertions.assertThat; |
| 30 | |
| 31 | @SuppressWarnings("unused") |
| 32 | public class MapperAccessorParamsTest extends CCMTestsSupport { |
| 33 | |
| 34 | @Override |
| 35 | public void onTestContextInitialized() { |
| 36 | execute( |
| 37 | "CREATE TABLE user ( key int primary key, gender int, home_phone text, work_phone text)", |
| 38 | "CREATE INDEX on user(gender)", |
| 39 | "CREATE TABLE user_str ( key int primary key, gender text)", |
| 40 | "CREATE INDEX on user_str(gender)" |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | @Test(groups = "short") |
| 45 | public void should_allow_enum_as_int_in_accessor_params() { |
| 46 | UserAccessor accessor = new MappingManager(session()) |
| 47 | .createAccessor(UserAccessor.class); |
| 48 | |
| 49 | accessor.addUser(0, Enum.FEMALE); |
| 50 | accessor.addUser(1, Enum.MALE); |
| 51 | |
| 52 | assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE); |
| 53 | assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1); |
| 54 | |
| 55 | assertThat(accessor.getUser(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE); |
| 56 | assertThat(accessor.getUser(Enum.FEMALE).one().getKey()).isEqualTo(0); |
| 57 | } |
| 58 | |
| 59 | @Test(groups = "short") |
| 60 | public void should_allow_enum_as_string_in_accessor_params() { |
| 61 | UserAccessor accessor = new MappingManager(session()) |
| 62 | .createAccessor(UserAccessor.class); |
| 63 | |
| 64 | accessor.addUserStr(0, Enum.FEMALE); |
| 65 | accessor.addUserStr(1, Enum.MALE); |
| 66 | |
| 67 | assertThat(accessor.getUserStr(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE); |
| 68 | assertThat(accessor.getUserStr(Enum.MALE).one().getKey()).isEqualTo(1); |
| 69 | |
| 70 | assertThat(accessor.getUserStr(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE); |
| 71 | assertThat(accessor.getUserStr(Enum.FEMALE).one().getKey()).isEqualTo(0); |
| 72 | } |
| 73 | |
| 74 | @Test(groups = "short") |
| 75 | @CassandraVersion(major = 2.0, description = "Uses named parameters") |
| 76 | public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() { |
| 77 | UserPhoneAccessor accessor = new MappingManager(session()) |
| 78 | .createAccessor(UserPhoneAccessor.class); |
| 79 | |
| 80 | session().execute("delete from user where key = 0"); |
| 81 | accessor.updatePhones_positional("1111", "2222", 0); |
| 82 | assertPhonesEqual(0, "1111", "2222"); |
| 83 | |
| 84 | session().execute("delete from user where key = 0"); |
| 85 | accessor.updatePhones_named(0, "1111", "2222"); |
| 86 | assertPhonesEqual(0, "1111", "2222"); |
| 87 | |
| 88 | session().execute("delete from user where key = 0"); |
| 89 | accessor.updatePhones_fallback("1111", "2222", 0); |
| 90 | assertPhonesEqual(0, "1111", "2222"); |
| 91 | |
| 92 | session().execute("delete from user where key = 0"); |
| 93 | accessor.updateBothPhones(0, "1111"); |
| 94 | assertPhonesEqual(0, "1111", "1111"); |
| 95 | |
| 96 | session().execute("delete from user where key = 0"); |
| 97 | accessor.updatePhones_fallback2("1111", "2222", 0); |
| 98 | assertPhonesEqual(0, "1111", "2222"); |
| 99 | } |
| 100 | |
| 101 | @Test(groups = "short", expectedExceptions = RuntimeException.class) |
| 102 | public void should_fail_if_not_enough_parameters() { |
| 103 | new MappingManager(session()) |
| 104 | .createAccessor(UserPhoneAccessor_NotEnoughParams.class); |
| 105 | } |
| 106 | |
| 107 | @Test(groups = "short", expectedExceptions = RuntimeException.class) |
| 108 | public void should_fail_if_too_many_parameters() { |
| 109 | new MappingManager(session()) |
| 110 | .createAccessor(UserPhoneAccessor_TooManyParams.class); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Ensures that a wrong parameter type is detected when |
| 115 | * the accessor is called with a wrong data type. |
| 116 | * |
| 117 | * @jira_ticket JAVA-974 |
| 118 | */ |
| 119 | @Test(groups = "short", expectedExceptions = InvalidTypeException.class) |
| 120 | public void should_fail_if_wrong_parameter_type() { |
| 121 | UserPhoneAccessor_WrongParameterTypes accessor = new MappingManager(session()) |
| 122 | .createAccessor(UserPhoneAccessor_WrongParameterTypes.class); |
| 123 | accessor.findUsersBykeys(Lists.newArrayList(1, 2, 3)); |
| 124 | } |
| 125 | |
| 126 | private void assertPhonesEqual(int key, String home, String work) { |
| 127 | Row row = session().execute("select * from user where key = " + key).one(); |
| 128 | assertThat(row.getString("home_phone")).isEqualTo(home); |
| 129 | assertThat(row.getString("work_phone")).isEqualTo(work); |
| 130 | } |
| 131 | |
| 132 | enum Enum { |
| 133 | MALE, FEMALE |
| 134 | } |
| 135 | |
| 136 | @Accessor |
| 137 | public interface UserAccessor { |
| 138 | @Query("select * from user where gender=?") |
| 139 | Result<User> getUser(@Enumerated(EnumType.ORDINAL) Enum value); |
| 140 | |
| 141 | @Query("select * from user_str where gender=?") |
| 142 | Result<UserStr> getUserStr(@Enumerated(EnumType.STRING) Enum value); |
| 143 | |
| 144 | @Query("insert into user (key, gender) values (?,?)") |
| 145 | ResultSet addUser(int key, @Enumerated(EnumType.ORDINAL) Enum value); |
| 146 | |
| 147 | @Query("insert into user_str (key, gender) values (?,?)") |
| 148 | ResultSet addUserStr(int key, @Enumerated(EnumType.STRING) Enum value); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests various ways to match method parameters to query bind markers. |
| 153 | */ |
| 154 | @Accessor |
| 155 | public interface UserPhoneAccessor { |
| 156 | /** |
| 157 | * Standard positional markers |
| 158 | */ |
| 159 | @Query("update user set home_phone = ?, work_phone = ? where key = ?") |
| 160 | void updatePhones_positional(String homePhone, String workPhone, int key); |
| 161 | |
| 162 | /** |
| 163 | * Standard named markers |
| 164 | */ |
| 165 | @Query("update user set home_phone = :home, work_phone = :work where key = :key") |
| 166 | void updatePhones_named(@Param("key") int key, @Param("home") String homePhone, @Param("work") String workPhone); |
| 167 | |
| 168 | /** |
| 169 | * Named markers with no @Param. Should fallback to positional matching |
| 170 | */ |
| 171 | @Query("update user set home_phone = :home, work_phone = :work where key = :key") |
| 172 | void updatePhones_fallback(String homePhone, String workPhone, int key); |
| 173 | |
| 174 | /** |
| 175 | * Named markers with repeated names |
| 176 | */ |
| 177 | @Query("update user set home_phone = :phone, work_phone = :phone where key = :key") |
| 178 | void updateBothPhones(@Param("key") int key, @Param("phone") String uniquePhone); |
| 179 | |
| 180 | /** |
| 181 | * Named markers with repeated names, but no @Param. Should fallback to positional matching |
| 182 | */ |
| 183 | @Query("update user set home_phone = :phone, work_phone = :phone where key = :key") |
| 184 | void updatePhones_fallback2(String homePhone, String workPhone, int key); |
| 185 | } |
| 186 | |
| 187 | @Accessor |
| 188 | public interface UserPhoneAccessor_NotEnoughParams { |
| 189 | @SuppressWarnings("unused") |
| 190 | @Query("update user set home_phone = :phone, work_phone = :phone where key = :key") |
| 191 | void updateBothPhones(@Param("key") int key); |
| 192 | } |
| 193 | |
| 194 | @Accessor |
| 195 | public interface UserPhoneAccessor_TooManyParams { |
| 196 | @SuppressWarnings("unused") |
| 197 | @Query("update user set home_phone = ?, work_phone = ? where key = ?") |
| 198 | void updatePhones(String homePhone, String workPhone, int key, int extra); |
| 199 | } |
| 200 | |
| 201 | @Table(name = "user") |
| 202 | public static class User { |
| 203 | @PartitionKey |
| 204 | private int key; |
| 205 | |
| 206 | @Enumerated(EnumType.ORDINAL) |
| 207 | private Enum gender; |
| 208 | |
| 209 | public User() { |
| 210 | } |
| 211 | |
| 212 | public User(int k, Enum val) { |
| 213 | this.key = k; |
| 214 | this.gender = val; |
| 215 | } |
| 216 | |
| 217 | public int getKey() { |
| 218 | return this.key; |
| 219 | } |
| 220 | |
| 221 | public void setKey(int pk) { |
| 222 | this.key = pk; |
| 223 | } |
| 224 | |
| 225 | public Enum getGender() { |
| 226 | return this.gender; |
| 227 | } |
| 228 | |
| 229 | public void setGender(Enum val) { |
| 230 | this.gender = val; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Table(name = "user") |
| 235 | public static class UserStr { |
| 236 | @PartitionKey |
| 237 | private int key; |
| 238 | |
| 239 | private Enum gender; |
| 240 | |
| 241 | public UserStr() { |
| 242 | } |
| 243 | |
| 244 | public UserStr(int k, Enum val) { |
| 245 | this.key = k; |
| 246 | this.gender = val; |
| 247 | } |
| 248 | |
| 249 | public int getKey() { |
| 250 | return this.key; |
| 251 | } |
| 252 | |
| 253 | public void setKey(int pk) { |
| 254 | this.key = pk; |
| 255 | } |
| 256 | |
| 257 | public Enum getGender() { |
| 258 | return this.gender; |
| 259 | } |
| 260 | |
| 261 | public void setGender(Enum val) { |
| 262 | this.gender = val; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | @Accessor |
| 267 | public interface UserPhoneAccessor_WrongParameterTypes { |
| 268 | @Query("select * from user where key IN (?)") |
| 269 | // WRONG, should be "where key IN ?" |
| 270 | void findUsersBykeys(List<Integer> keys); |
| 271 | } |
| 272 | |
| 273 | } |