Elffuss BraidHub

kikocis/java-driver public

ced5ba920359 · 10.7 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.CCMTestsSupport;
19import com.datastax.driver.core.utils.CassandraVersion;
20import com.datastax.driver.mapping.annotations.Accessor;
21import com.datastax.driver.mapping.annotations.Param;
22import com.datastax.driver.mapping.annotations.Query;
23import com.datastax.driver.mapping.annotations.Table;
24import org.testng.annotations.BeforeMethod;
25import org.testng.annotations.Test;
26
27import java.util.Iterator;
28
29import static org.assertj.core.api.Assertions.assertThat;
30
31@SuppressWarnings("unused")
32@CassandraVersion(major = 3.0)
33public class MapperMaterializedViewTest extends CCMTestsSupport {
34
35
36 @Override
37 public void onTestContextInitialized() {
38 // Example schema taken from: http://www.datastax.com/dev/blog/new-in-cassandra-3-0-materialized-views
39 execute(
40 "CREATE TABLE scores (user TEXT, game TEXT, year INT, month INT, day INT, score INT, PRIMARY KEY(user, game, year, month, day))",
41 "CREATE MATERIALIZED VIEW alltimehigh AS\n"
42 + " SELECT * FROM scores\n"
43 + " WHERE game IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL\n"
44 + " PRIMARY KEY (game, score, user, year, month, day)\n"
45 + " WITH CLUSTERING ORDER BY (score desc)",
46 "CREATE MATERIALIZED VIEW dailyhigh AS\n"
47 + " SELECT * FROM scores\n"
48 + " WHERE game IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL\n"
49 + " PRIMARY KEY ((game, year, month, day), score, user)\n"
50 + " WITH CLUSTERING ORDER BY (score DESC)",
51 "CREATE MATERIALIZED VIEW monthlyhigh AS\n"
52 + " SELECT * FROM scores\n"
53 + " WHERE game IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND day IS NOT NULL\n"
54 + " PRIMARY KEY ((game, year, month), score, user, day)\n"
55 + " WITH CLUSTERING ORDER BY (score DESC)",
56 "CREATE MATERIALIZED VIEW filtereduserhigh AS\n"
57 + " SELECT * FROM scores\n"
58 + " WHERE user in ('jbellis', 'pcmanus') AND game IS NOT NULL AND score IS NOT NULL AND year is NOT NULL AND day is not NULL and month IS NOT NULL\n"
59 + " PRIMARY KEY (game, score, user, year, month, day)\n"
60 + " WITH CLUSTERING ORDER BY (score DESC)",
61 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Coup', 2015, 5, 1, 4000)",
62 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Coup', 2015, 5, 3, 1750)",
63 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('yukim', 'Coup', 2015, 5, 3, 2250)",
64 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('tjake', 'Coup', 2015, 5, 3, 500)",
65 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('iamaleksey', 'Coup', 2015, 6, 1, 2500)",
66 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('tjake', 'Coup', 2015, 6, 2, 1000)",
67 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Coup', 2015, 6, 2, 2000)",
68 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jmckenzie', 'Coup', 2015, 6, 9, 2700)",
69 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Coup', 2015, 6, 20, 3500)",
70 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Checkers', 2015, 6, 20, 1200)",
71 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Chess', 2015, 6, 21, 3500)",
72 "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Chess', 2015, 1, 25, 3200)"
73 );
74 }
75
76 private ScoreAccessor accessor;
77
78 @BeforeMethod(groups = "short")
79 public void setUp() {
80 accessor = new MappingManager(session()).createAccessor(ScoreAccessor.class);
81 }
82
83 /**
84 * Validates that an accessor properly maps a single entity over a result from a materialized
85 * view.
86 *
87 * @test_category materialized_view, mapper
88 */
89 @Test(groups = "short")
90 public void should_access_single_entity() {
91 Score score = accessor.allTimeHigh("Coup");
92
93 assertThat(score).isEqualTo(new Score("pcmanus", "Coup", 2015, 5, 1, 4000));
94 }
95
96 /**
97 * Validates that an accessor properly maps to a Result from a materialized
98 * view.
99 *
100 * @test_category materialized_view, mapper
101 */
102 @Test(groups = "short")
103 public void should_access_mapped_result() {
104 Result<Score> scores = accessor.dailyHigh("Coup", 2015, 6, 2);
105
106 Iterator<Score> iterator = scores.iterator();
107 assertThat(iterator.next()).isEqualTo(new Score("pcmanus", "Coup", 2015, 6, 2, 2000));
108 assertThat(iterator.next()).isEqualTo(new Score("tjake", "Coup", 2015, 6, 2, 1000));
109 assertThat(scores.isExhausted());
110 }
111
112 /**
113 * Validates that an accessor properly maps to a Result from a materialized
114 * view given a range criteria query.
115 *
116 * @test_category materialized_view, mapper
117 */
118 @Test(groups = "short")
119 public void should_access_mapped_result_range() {
120 Result<Score> scores = accessor.monthlyHighRange("Coup", 2015, 6, 2500, 3500);
121
122 Iterator<Score> iterator = scores.iterator();
123 assertThat(iterator.next()).isEqualTo(new Score("jbellis", "Coup", 2015, 6, 20, 3500));
124 assertThat(iterator.next()).isEqualTo(new Score("jmckenzie", "Coup", 2015, 6, 9, 2700));
125 assertThat(iterator.next()).isEqualTo(new Score("iamaleksey", "Coup", 2015, 6, 1, 2500));
126 assertThat(scores.isExhausted());
127 }
128
129 /**
130 * Validates that an accessor properly maps to a Result from a materialized
131 * view that is filtered on a primary key.
132 *
133 * @test_category materialized_view, mapper
134 */
135 @Test(groups = "short")
136 public void should_access_filtered_user_high() {
137 Result<Score> scores = accessor.filteredUserHigh("Chess");
138
139 Iterator<Score> iterator = scores.iterator();
140 assertThat(iterator.next()).isEqualTo(new Score("jbellis", "Chess", 2015, 6, 21, 3500));
141 assertThat(iterator.next()).isEqualTo(new Score("pcmanus", "Chess", 2015, 1, 25, 3200));
142 assertThat(scores.isExhausted());
143 }
144
145 @Accessor
146 public interface ScoreAccessor {
147 @Query("select * from alltimehigh where game=:game")
148 Score allTimeHigh(@Param("game") String game);
149
150 @Query("select * from monthlyhigh where game=? and year=? and month=? and score >= ? and score <= ?")
151 Result<Score> monthlyHighRange(String game, int year, int month, int lowScore, int highScore);
152
153 @Query("select * from dailyhigh where game=? and year=? and month=? and day=?")
154 Result<Score> dailyHigh(String game, int year, int month, int day);
155
156 @Query("select * from filtereduserhigh where game=:game")
157 Result<Score> filteredUserHigh(String game);
158 }
159
160 @Table(name = "scores")
161 public static class Score {
162 String user;
163
164 String game;
165
166 int year;
167
168 int month;
169
170 int day;
171
172 int score;
173
174 public Score() {
175 }
176
177 public Score(String user, String game, int year, int month, int day, int score) {
178 this.user = user;
179 this.game = game;
180 this.year = year;
181 this.month = month;
182 this.day = day;
183 this.score = score;
184 }
185
186 public String getUser() {
187 return user;
188 }
189
190 public void setUser(String user) {
191 this.user = user;
192 }
193
194 public String getGame() {
195 return game;
196 }
197
198 public void setGame(String game) {
199 this.game = game;
200 }
201
202 public int getYear() {
203 return year;
204 }
205
206 public void setYear(int year) {
207 this.year = year;
208 }
209
210 public int getMonth() {
211 return month;
212 }
213
214 public void setMonth(int month) {
215 this.month = month;
216 }
217
218 public int getDay() {
219 return day;
220 }
221
222 public void setDay(int day) {
223 this.day = day;
224 }
225
226 public int getScore() {
227 return score;
228 }
229
230 public void setScore(int score) {
231 this.score = score;
232 }
233
234 @Override
235 public String toString() {
236 return "Score{" +
237 "user='" + user + '\'' +
238 ", game='" + game + '\'' +
239 ", year=" + year +
240 ", month=" + month +
241 ", day=" + day +
242 ", score=" + score +
243 '}';
244 }
245
246 @SuppressWarnings("SimplifiableIfStatement")
247 @Override
248 public boolean equals(Object o) {
249 if (this == o)
250 return true;
251 if (!(o instanceof Score))
252 return false;
253
254 Score score1 = (Score) o;
255
256 if (year != score1.year)
257 return false;
258 if (month != score1.month)
259 return false;
260 if (day != score1.day)
261 return false;
262 if (score != score1.score)
263 return false;
264 if (!user.equals(score1.user))
265 return false;
266 return game.equals(score1.game);
267 }
268
269 @Override
270 public int hashCode() {
271 int result = user.hashCode();
272 result = 31 * result + game.hashCode();
273 result = 31 * result + year;
274 result = 31 * result + month;
275 result = 31 * result + day;
276 result = 31 * result + score;
277 return result;
278 }
279 }
280}