| 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.BoundStatement; |
| 19 | import com.datastax.driver.core.CCMTestsSupport; |
| 20 | import com.datastax.driver.core.utils.CassandraVersion; |
| 21 | import com.datastax.driver.mapping.Mapper.Option; |
| 22 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 23 | import com.datastax.driver.mapping.annotations.Table; |
| 24 | import org.testng.annotations.BeforeMethod; |
| 25 | import org.testng.annotations.Test; |
| 26 | |
| 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 28 | |
| 29 | @SuppressWarnings("unused") |
| 30 | public class MapperSaveNullFieldsTest extends CCMTestsSupport { |
| 31 | |
| 32 | Mapper<User> mapper; |
| 33 | |
| 34 | @Override |
| 35 | public void onTestContextInitialized() { |
| 36 | execute("CREATE TABLE user (login text primary key, name text, phone text)"); |
| 37 | } |
| 38 | |
| 39 | @BeforeMethod(groups = "short") |
| 40 | public void setup() { |
| 41 | mapper = new MappingManager(session()).mapper(User.class); |
| 42 | } |
| 43 | |
| 44 | @CassandraVersion("2.1.0") |
| 45 | @Test(groups = "short") |
| 46 | void should_save_null_fields_if_requested() { |
| 47 | should_save_null_fields(true, Option.saveNullFields(true)); |
| 48 | |
| 49 | mapper.setDefaultSaveOptions(Option.saveNullFields(true)); |
| 50 | should_save_null_fields(true); |
| 51 | } |
| 52 | |
| 53 | @Test(groups = "short") |
| 54 | void should_save_null_fields_by_default() { |
| 55 | should_save_null_fields(true); |
| 56 | } |
| 57 | |
| 58 | @Test(groups = "short") |
| 59 | void should_ignore_null_fields_if_requested() { |
| 60 | should_save_null_fields(false, Option.saveNullFields(false)); |
| 61 | |
| 62 | mapper.setDefaultSaveOptions(Option.saveNullFields(false)); |
| 63 | should_save_null_fields(false); |
| 64 | } |
| 65 | |
| 66 | private void should_save_null_fields(boolean saveExpected, Option... options) { |
| 67 | // Try different combinations of null fields |
| 68 | should_save_null_fields(true, true, saveExpected, options); |
| 69 | should_save_null_fields(true, false, saveExpected, options); |
| 70 | should_save_null_fields(false, true, saveExpected, options); |
| 71 | should_save_null_fields(false, false, saveExpected, options); |
| 72 | } |
| 73 | |
| 74 | private void should_save_null_fields(boolean nullName, boolean nullPhone, boolean saveExpected, Option... options) { |
| 75 | // Start with clean data |
| 76 | session().execute("insert into user(login, name, phone) " |
| 77 | + "values ('test_login', 'previous_name', 'previous_phone')"); |
| 78 | |
| 79 | String newName = nullName ? null : "new_name"; |
| 80 | String newPhone = nullPhone ? null : "new_phone"; |
| 81 | String description = String.format("update with name=%s, phone = %s", newName, newPhone); |
| 82 | User newUser = new User("test_login", newName, newPhone); |
| 83 | |
| 84 | // Check if null fields are included in generated statement: |
| 85 | BoundStatement bs = (BoundStatement) mapper.saveQuery(newUser, options); |
| 86 | String queryString = bs.preparedStatement().getQueryString(); |
| 87 | if (nullName && !saveExpected) |
| 88 | assertThat(queryString).as(description).doesNotContain("name"); |
| 89 | else |
| 90 | assertThat(queryString).as(description).contains("name"); |
| 91 | |
| 92 | if (nullPhone && !saveExpected) |
| 93 | assertThat(queryString).as(description).doesNotContain("phone"); |
| 94 | else |
| 95 | assertThat(queryString).as(description).contains("phone"); |
| 96 | |
| 97 | // Save entity and check the data |
| 98 | mapper.save(newUser, options); |
| 99 | User savedUser = mapper.get("test_login"); |
| 100 | String expectedName = nullName |
| 101 | ? (saveExpected ? null : "previous_name") |
| 102 | : "new_name"; |
| 103 | String expectedPhone = nullPhone |
| 104 | ? (saveExpected ? null : "previous_phone") |
| 105 | : "new_phone"; |
| 106 | assertThat(savedUser.getName()).as(description).isEqualTo(expectedName); |
| 107 | assertThat(savedUser.getPhone()).as(description).isEqualTo(expectedPhone); |
| 108 | } |
| 109 | |
| 110 | @Table(name = "user") |
| 111 | public static class User { |
| 112 | @PartitionKey |
| 113 | private String login; |
| 114 | private String name; |
| 115 | private String phone; |
| 116 | |
| 117 | public User() { |
| 118 | } |
| 119 | |
| 120 | public User(String login, String name, String phone) { |
| 121 | this.login = login; |
| 122 | this.name = name; |
| 123 | this.phone = phone; |
| 124 | } |
| 125 | |
| 126 | public String getLogin() { |
| 127 | return login; |
| 128 | } |
| 129 | |
| 130 | public void setLogin(String login) { |
| 131 | this.login = login; |
| 132 | } |
| 133 | |
| 134 | public String getName() { |
| 135 | return name; |
| 136 | } |
| 137 | |
| 138 | public void setName(String name) { |
| 139 | this.name = name; |
| 140 | } |
| 141 | |
| 142 | public String getPhone() { |
| 143 | return phone; |
| 144 | } |
| 145 | |
| 146 | public void setPhone(String phone) { |
| 147 | this.phone = phone; |
| 148 | } |
| 149 | } |
| 150 | } |