|
| 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 java.util.Collection; |
| 19 | +import java.util.Iterator; |
| 20 | + |
| 21 | +import org.testng.annotations.BeforeMethod; |
| 22 | +import org.testng.annotations.Test; |
| 23 | + |
| 24 | +import static com.google.common.collect.Lists.newArrayList; |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +import com.datastax.driver.core.CCMBridge; |
| 28 | +import com.datastax.driver.core.utils.CassandraVersion; |
| 29 | +import com.datastax.driver.mapping.annotations.Accessor; |
| 30 | +import com.datastax.driver.mapping.annotations.Param; |
| 31 | +import com.datastax.driver.mapping.annotations.Query; |
| 32 | +import com.datastax.driver.mapping.annotations.Table; |
| 33 | + |
| 34 | +@CassandraVersion(major=3.0) |
| 35 | +public class MapperMaterializedViewTest extends CCMBridge.PerClassSingleNodeCluster{ |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Collection<String> getTableDefinitions() { |
| 39 | + // Example schema taken from: http://www.datastax.com/dev/blog/new-in-cassandra-3-0-materialized-views |
| 40 | + return newArrayList( |
| 41 | + "CREATE TABLE scores (user TEXT, game TEXT, year INT, month INT, day INT, score INT, PRIMARY KEY(user, game, year, month, day))", |
| 42 | + "CREATE MATERIALIZED VIEW alltimehigh AS\n" |
| 43 | + + " SELECT * FROM scores\n" |
| 44 | + + " 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" |
| 45 | + + " PRIMARY KEY (game, score, user, year, month, day)\n" |
| 46 | + + " WITH CLUSTERING ORDER BY (score desc)", |
| 47 | + "CREATE MATERIALIZED VIEW dailyhigh AS\n" |
| 48 | + + " SELECT * FROM scores\n" |
| 49 | + + " 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" |
| 50 | + + " PRIMARY KEY ((game, year, month, day), score, user)\n" |
| 51 | + + " WITH CLUSTERING ORDER BY (score DESC)", |
| 52 | + "CREATE MATERIALIZED VIEW monthlyhigh AS\n" |
| 53 | + + " SELECT * FROM scores\n" |
| 54 | + + " 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" |
| 55 | + + " PRIMARY KEY ((game, year, month), score, user, day)\n" |
| 56 | + + " WITH CLUSTERING ORDER BY (score DESC)", |
| 57 | + "CREATE MATERIALIZED VIEW filtereduserhigh AS\n" |
| 58 | + + " SELECT * FROM scores\n" |
| 59 | + + " 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" |
| 60 | + + " PRIMARY KEY (game, score, user, year, month, day)\n" |
| 61 | + + " WITH CLUSTERING ORDER BY (score DESC)", |
| 62 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Coup', 2015, 5, 1, 4000)", |
| 63 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Coup', 2015, 5, 3, 1750)", |
| 64 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('yukim', 'Coup', 2015, 5, 3, 2250)", |
| 65 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('tjake', 'Coup', 2015, 5, 3, 500)", |
| 66 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('iamaleksey', 'Coup', 2015, 6, 1, 2500)", |
| 67 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('tjake', 'Coup', 2015, 6, 2, 1000)", |
| 68 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Coup', 2015, 6, 2, 2000)", |
| 69 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jmckenzie', 'Coup', 2015, 6, 9, 2700)", |
| 70 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Coup', 2015, 6, 20, 3500)", |
| 71 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Checkers', 2015, 6, 20, 1200)", |
| 72 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('jbellis', 'Chess', 2015, 6, 21, 3500)", |
| 73 | + "INSERT INTO scores (user, game, year, month, day, score) VALUES ('pcmanus', 'Chess', 2015, 1, 25, 3200)" |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private ScoreAccessor accessor; |
| 78 | + |
| 79 | + @BeforeMethod(groups="short") |
| 80 | + public void setUp() { |
| 81 | + accessor = new MappingManager(session).createAccessor(ScoreAccessor.class); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Validates that an accessor properly maps a single entity over a result from a materialized |
| 86 | + * view. |
| 87 | + * |
| 88 | + * @test_category materialized_view,mapper |
| 89 | + */ |
| 90 | + @Test(groups="short") |
| 91 | + public void should_access_single_entity() { |
| 92 | + Score score = accessor.allTimeHigh("Coup"); |
| 93 | + |
| 94 | + assertThat(score).isEqualTo(new Score("pcmanus", "Coup", 2015, 5, 1, 4000)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Validates that an accessor properly maps to a Result from a materialized |
| 99 | + * view. |
| 100 | + * |
| 101 | + * @test_category materialized_view,mapper |
| 102 | + */ |
| 103 | + @Test(groups="short") |
| 104 | + public void should_access_mapped_result() { |
| 105 | + Result<Score> scores = accessor.dailyHigh("Coup", 2015, 6, 2); |
| 106 | + |
| 107 | + Iterator<Score> iterator = scores.iterator(); |
| 108 | + assertThat(iterator.next()).isEqualTo(new Score("pcmanus", "Coup", 2015, 6, 2, 2000)); |
| 109 | + assertThat(iterator.next()).isEqualTo(new Score("tjake", "Coup", 2015, 6, 2, 1000)); |
| 110 | + assertThat(scores.isExhausted()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Validates that an accessor properly maps to a Result from a materialized |
| 115 | + * view given a range criteria query. |
| 116 | + * |
| 117 | + * @test_category materialized_view,mapper |
| 118 | + */ |
| 119 | + @Test(groups="short") |
| 120 | + public void should_access_mapped_result_range() { |
| 121 | + Result<Score> scores = accessor.monthlyHighRange("Coup", 2015, 6, 2500, 3500); |
| 122 | + |
| 123 | + Iterator<Score> iterator = scores.iterator(); |
| 124 | + assertThat(iterator.next()).isEqualTo(new Score("jbellis", "Coup", 2015, 6, 20, 3500)); |
| 125 | + assertThat(iterator.next()).isEqualTo(new Score("jmckenzie", "Coup", 2015, 6, 9, 2700)); |
| 126 | + assertThat(iterator.next()).isEqualTo(new Score("iamaleksey", "Coup", 2015, 6, 1, 2500)); |
| 127 | + assertThat(scores.isExhausted()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Validates that an accessor properly maps to a Result from a materialized |
| 132 | + * view that is filtered on a primary key. |
| 133 | + * |
| 134 | + * @test_category materialized_view,mapper |
| 135 | + */ |
| 136 | + @Test(groups="short") |
| 137 | + public void should_access_filtered_user_high() { |
| 138 | + Result<Score> scores = accessor.filteredUserHigh("Chess"); |
| 139 | + |
| 140 | + Iterator<Score> iterator = scores.iterator(); |
| 141 | + assertThat(iterator.next()).isEqualTo(new Score("jbellis", "Chess", 2015, 6, 21, 3500)); |
| 142 | + assertThat(iterator.next()).isEqualTo(new Score("pcmanus", "Chess", 2015, 1, 25, 3200)); |
| 143 | + assertThat(scores.isExhausted()); |
| 144 | + } |
| 145 | + |
| 146 | + @Accessor |
| 147 | + public interface ScoreAccessor { |
| 148 | + @Query("select * from alltimehigh where game=:game") |
| 149 | + Score allTimeHigh(@Param("game") String game); |
| 150 | + |
| 151 | + @Query("select * from monthlyhigh where game=? and year=? and month=? and score >= ? and score <= ?") |
| 152 | + Result<Score> monthlyHighRange(String game, int year, int month, int lowScore, int highScore); |
| 153 | + |
| 154 | + @Query("select * from dailyhigh where game=? and year=? and month=? and day=?") |
| 155 | + Result<Score> dailyHigh(String game, int year, int month, int day); |
| 156 | + |
| 157 | + @Query("select * from filtereduserhigh where game=:game") |
| 158 | + Result<Score> filteredUserHigh(String game); |
| 159 | + } |
| 160 | + |
| 161 | + @Table(name="scores") |
| 162 | + public static class Score { |
| 163 | + String user; |
| 164 | + |
| 165 | + String game; |
| 166 | + |
| 167 | + int year; |
| 168 | + |
| 169 | + int month; |
| 170 | + |
| 171 | + int day; |
| 172 | + |
| 173 | + int score; |
| 174 | + |
| 175 | + public Score() {} |
| 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 | + @Override |
| 247 | + public boolean equals(Object o) { |
| 248 | + if (this == o) |
| 249 | + return true; |
| 250 | + if (!(o instanceof Score)) |
| 251 | + return false; |
| 252 | + |
| 253 | + Score score1 = (Score)o; |
| 254 | + |
| 255 | + if (year != score1.year) |
| 256 | + return false; |
| 257 | + if (month != score1.month) |
| 258 | + return false; |
| 259 | + if (day != score1.day) |
| 260 | + return false; |
| 261 | + if (score != score1.score) |
| 262 | + return false; |
| 263 | + if (!user.equals(score1.user)) |
| 264 | + return false; |
| 265 | + return game.equals(score1.game); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public int hashCode() { |
| 270 | + int result = user.hashCode(); |
| 271 | + result = 31 * result + game.hashCode(); |
| 272 | + result = 31 * result + year; |
| 273 | + result = 31 * result + month; |
| 274 | + result = 31 * result + day; |
| 275 | + result = 31 * result + score; |
| 276 | + return result; |
| 277 | + } |
| 278 | + } |
| 279 | +} |
0 commit comments