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