|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 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 org.springframework.data.jdbc.repository.support; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import io.r2dbc.spi.ConnectionFactory; |
| 21 | +import lombok.AllArgsConstructor; |
| 22 | +import lombok.Data; |
| 23 | +import lombok.NoArgsConstructor; |
| 24 | +import reactor.core.publisher.Flux; |
| 25 | +import reactor.core.publisher.Hooks; |
| 26 | +import reactor.core.publisher.Mono; |
| 27 | +import reactor.test.StepVerifier; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Ignore; |
| 35 | +import org.junit.Test; |
| 36 | +import org.springframework.data.annotation.Id; |
| 37 | +import org.springframework.data.convert.EntityInstantiators; |
| 38 | +import org.springframework.data.jdbc.core.function.DatabaseClient; |
| 39 | +import org.springframework.data.jdbc.core.function.DefaultReactiveDataAccessStrategy; |
| 40 | +import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; |
| 41 | +import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity; |
| 42 | +import org.springframework.data.jdbc.core.mapping.Table; |
| 43 | +import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport; |
| 44 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 45 | + |
| 46 | +/** |
| 47 | + * Integration tests for {@link SimpleR2dbcRepository}. |
| 48 | + * |
| 49 | + * @author Mark Paluch |
| 50 | + */ |
| 51 | +public class SimpleR2dbcRepositoryIntegrationTests extends R2dbcIntegrationTestSupport { |
| 52 | + |
| 53 | + private static JdbcMappingContext mappingContext = new JdbcMappingContext(); |
| 54 | + |
| 55 | + private ConnectionFactory connectionFactory; |
| 56 | + private DatabaseClient databaseClient; |
| 57 | + private SimpleR2dbcRepository<LegoSet, Integer> repository; |
| 58 | + private JdbcTemplate jdbc; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void before() { |
| 62 | + |
| 63 | + Hooks.onOperatorDebug(); |
| 64 | + |
| 65 | + this.connectionFactory = createConnectionFactory(); |
| 66 | + this.databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory) |
| 67 | + .dataAccessStrategy(new DefaultReactiveDataAccessStrategy(mappingContext, new EntityInstantiators())).build(); |
| 68 | + this.repository = new SimpleR2dbcRepository<>(databaseClient, |
| 69 | + (JdbcPersistentEntity<LegoSet>) mappingContext.getRequiredPersistentEntity(LegoSet.class)); |
| 70 | + |
| 71 | + this.jdbc = createJdbcTemplate(createDataSource()); |
| 72 | + |
| 73 | + String tableToCreate = "CREATE TABLE IF NOT EXISTS repo_legoset (\n" + " id SERIAL PRIMARY KEY,\n" |
| 74 | + + " name varchar(255) NOT NULL,\n" + " manual integer NULL\n" + ");"; |
| 75 | + |
| 76 | + this.jdbc.execute("DROP TABLE IF EXISTS repo_legoset"); |
| 77 | + this.jdbc.execute(tableToCreate); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void shouldSaveNewObject() { |
| 82 | + |
| 83 | + LegoSet legoSet = new LegoSet(null, "SCHAUFELRADBAGGER", 12); |
| 84 | + |
| 85 | + repository.save(legoSet) // |
| 86 | + .as(StepVerifier::create) // |
| 87 | + .expectNextCount(1) // |
| 88 | + .verifyComplete(); |
| 89 | + |
| 90 | + Map<String, Object> map = jdbc.queryForMap("SELECT * FROM repo_legoset"); |
| 91 | + assertThat(map).containsEntry("name", "SCHAUFELRADBAGGER").containsEntry("manual", 12).containsKey("id"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @Ignore("Implement me") |
| 96 | + public void shouldUpdateObject() { |
| 97 | + |
| 98 | + LegoSet legoSet = new LegoSet(null, "SCHAUFELRADBAGGER", 12); |
| 99 | + |
| 100 | + repository.save(legoSet) // |
| 101 | + .as(StepVerifier::create) // |
| 102 | + .expectNextCount(1) // |
| 103 | + .verifyComplete(); |
| 104 | + |
| 105 | + legoSet.setManual(14); |
| 106 | + |
| 107 | + repository.save(legoSet) // |
| 108 | + .as(StepVerifier::create) // |
| 109 | + .expectNextCount(1) // |
| 110 | + .verifyComplete(); |
| 111 | + |
| 112 | + Map<String, Object> map = jdbc.queryForMap("SELECT * FROM repo_legoset"); |
| 113 | + assertThat(map).containsEntry("name", "SCHAUFELRADBAGGER").containsEntry("manual", 14).containsKey("id"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void shouldSaveObjectsUsingIterable() { |
| 118 | + |
| 119 | + LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12); |
| 120 | + LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13); |
| 121 | + |
| 122 | + repository.saveAll(Arrays.asList(legoSet1, legoSet2)) // |
| 123 | + .as(StepVerifier::create) // |
| 124 | + .expectNextCount(2) // |
| 125 | + .verifyComplete(); |
| 126 | + |
| 127 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 128 | + assertThat(map).containsEntry("count", 2L); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void shouldSaveObjectsUsingPublisher() { |
| 133 | + |
| 134 | + LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12); |
| 135 | + LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13); |
| 136 | + |
| 137 | + repository.saveAll(Flux.just(legoSet1, legoSet2)) // |
| 138 | + .as(StepVerifier::create) // |
| 139 | + .expectNextCount(2) // |
| 140 | + .verifyComplete(); |
| 141 | + |
| 142 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 143 | + assertThat(map).containsEntry("count", 2L); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void shouldFindById() { |
| 148 | + |
| 149 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 150 | + |
| 151 | + repository.findById(42055) // |
| 152 | + .as(StepVerifier::create) // |
| 153 | + .assertNext(actual -> { |
| 154 | + |
| 155 | + assertThat(actual.getId()).isEqualTo(42055); |
| 156 | + assertThat(actual.getName()).isEqualTo("SCHAUFELRADBAGGER"); |
| 157 | + assertThat(actual.getManual()).isEqualTo(12); |
| 158 | + }).verifyComplete(); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void shouldExistsById() { |
| 163 | + |
| 164 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 165 | + |
| 166 | + repository.existsById(42055) // |
| 167 | + .as(StepVerifier::create) // |
| 168 | + .expectNext(true)// |
| 169 | + .verifyComplete(); |
| 170 | + |
| 171 | + repository.existsById(42) // |
| 172 | + .as(StepVerifier::create) // |
| 173 | + .expectNext(false)// |
| 174 | + .verifyComplete(); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void shouldExistsByIdPublisher() { |
| 179 | + |
| 180 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 181 | + |
| 182 | + repository.existsById(Mono.just(42055)) // |
| 183 | + .as(StepVerifier::create) // |
| 184 | + .expectNext(true)// |
| 185 | + .verifyComplete(); |
| 186 | + |
| 187 | + repository.existsById(Mono.just(42)) // |
| 188 | + .as(StepVerifier::create) // |
| 189 | + .expectNext(false)// |
| 190 | + .verifyComplete(); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void shouldFindByAll() { |
| 195 | + |
| 196 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 197 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)"); |
| 198 | + |
| 199 | + repository.findAll() // |
| 200 | + .map(LegoSet::getName) // |
| 201 | + .collectList() // |
| 202 | + .as(StepVerifier::create) // |
| 203 | + .assertNext(actual -> { |
| 204 | + |
| 205 | + assertThat(actual).hasSize(2).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF"); |
| 206 | + }).verifyComplete(); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void shouldFindAllByIdUsingIterable() { |
| 211 | + |
| 212 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 213 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)"); |
| 214 | + |
| 215 | + repository.findAllById(Arrays.asList(42055, 42064)) // |
| 216 | + .map(LegoSet::getName) // |
| 217 | + .collectList() // |
| 218 | + .as(StepVerifier::create) // |
| 219 | + .assertNext(actual -> { |
| 220 | + |
| 221 | + assertThat(actual).hasSize(2).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF"); |
| 222 | + }).verifyComplete(); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void shouldFindAllByIdUsingPublisher() { |
| 227 | + |
| 228 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 229 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)"); |
| 230 | + |
| 231 | + repository.findAllById(Flux.just(42055, 42064)) // |
| 232 | + .map(LegoSet::getName) // |
| 233 | + .collectList() // |
| 234 | + .as(StepVerifier::create) // |
| 235 | + .assertNext(actual -> { |
| 236 | + |
| 237 | + assertThat(actual).hasSize(2).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF"); |
| 238 | + }).verifyComplete(); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + public void shouldCount() { |
| 243 | + |
| 244 | + repository.count() // |
| 245 | + .as(StepVerifier::create) // |
| 246 | + .expectNext(0L) // |
| 247 | + .verifyComplete(); |
| 248 | + |
| 249 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 250 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)"); |
| 251 | + |
| 252 | + repository.count() // |
| 253 | + .as(StepVerifier::create) // |
| 254 | + .expectNext(2L) // |
| 255 | + .verifyComplete(); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + public void shouldDeleteById() { |
| 260 | + |
| 261 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 262 | + |
| 263 | + repository.deleteById(42055) // |
| 264 | + .as(StepVerifier::create) // |
| 265 | + .verifyComplete(); |
| 266 | + |
| 267 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 268 | + assertThat(map).containsEntry("count", 0L); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void shouldDeleteByIdPublisher() { |
| 273 | + |
| 274 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 275 | + |
| 276 | + repository.deleteById(Mono.just(42055)) // |
| 277 | + .as(StepVerifier::create) // |
| 278 | + .verifyComplete(); |
| 279 | + |
| 280 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 281 | + assertThat(map).containsEntry("count", 0L); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void shouldDelete() { |
| 286 | + |
| 287 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 288 | + |
| 289 | + LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12); |
| 290 | + |
| 291 | + repository.delete(legoSet) // |
| 292 | + .as(StepVerifier::create) // |
| 293 | + .verifyComplete(); |
| 294 | + |
| 295 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 296 | + assertThat(map).containsEntry("count", 0L); |
| 297 | + } |
| 298 | + |
| 299 | + @Test |
| 300 | + public void shouldDeleteAllUsingIterable() { |
| 301 | + |
| 302 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 303 | + |
| 304 | + LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12); |
| 305 | + |
| 306 | + repository.deleteAll(Collections.singletonList(legoSet)) // |
| 307 | + .as(StepVerifier::create) // |
| 308 | + .verifyComplete(); |
| 309 | + |
| 310 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 311 | + assertThat(map).containsEntry("count", 0L); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + public void shouldDeleteAllUsingPublisher() { |
| 316 | + |
| 317 | + jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)"); |
| 318 | + |
| 319 | + LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12); |
| 320 | + |
| 321 | + repository.deleteAll(Mono.just(legoSet)) // |
| 322 | + .as(StepVerifier::create) // |
| 323 | + .verifyComplete(); |
| 324 | + |
| 325 | + Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset"); |
| 326 | + assertThat(map).containsEntry("count", 0L); |
| 327 | + } |
| 328 | + |
| 329 | + @Data |
| 330 | + @Table("repo_legoset") |
| 331 | + @AllArgsConstructor |
| 332 | + @NoArgsConstructor |
| 333 | + static class LegoSet { |
| 334 | + @Id Integer id; |
| 335 | + String name; |
| 336 | + Integer manual; |
| 337 | + } |
| 338 | +} |
0 commit comments