Elffuss BraidHub

kikocis/java-driver public

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