|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 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 | + * https://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.r2dbc.repository; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import io.r2dbc.mssql.util.Assert; |
| 21 | +import io.r2dbc.spi.ConnectionFactory; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.test.StepVerifier; |
| 24 | + |
| 25 | +import javax.sql.DataSource; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.context.annotation.ComponentScan; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.context.annotation.FilterType; |
| 35 | +import org.springframework.dao.DataAccessException; |
| 36 | +import org.springframework.data.annotation.Id; |
| 37 | +import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration; |
| 38 | +import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories; |
| 39 | +import org.springframework.data.r2dbc.testing.H2TestSupport; |
| 40 | +import org.springframework.data.relational.core.mapping.Table; |
| 41 | +import org.springframework.data.repository.reactive.ReactiveCrudRepository; |
| 42 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 43 | +import org.springframework.lang.Nullable; |
| 44 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 45 | + |
| 46 | +/** |
| 47 | + * Integration tests projections. |
| 48 | + * |
| 49 | + * @author Mark Paluch |
| 50 | + */ |
| 51 | +@ExtendWith(SpringExtension.class) |
| 52 | +public class ProjectingRepositoryIntegrationTests { |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private ImmutableObjectRepository repository; |
| 56 | + private JdbcTemplate jdbc; |
| 57 | + |
| 58 | + @Configuration |
| 59 | + @EnableR2dbcRepositories( |
| 60 | + includeFilters = @ComponentScan.Filter(value = ImmutableObjectRepository.class, type = FilterType.ASSIGNABLE_TYPE), |
| 61 | + considerNestedRepositories = true) |
| 62 | + static class TestConfiguration extends AbstractR2dbcConfiguration { |
| 63 | + @Override |
| 64 | + public ConnectionFactory connectionFactory() { |
| 65 | + return H2TestSupport.createConnectionFactory(); |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @BeforeEach |
| 71 | + void before() { |
| 72 | + |
| 73 | + this.jdbc = new JdbcTemplate(createDataSource()); |
| 74 | + |
| 75 | + try { |
| 76 | + this.jdbc.execute("DROP TABLE immutable_non_null"); |
| 77 | + } |
| 78 | + catch (DataAccessException e) { |
| 79 | + } |
| 80 | + |
| 81 | + this.jdbc.execute("CREATE TABLE immutable_non_null (id serial PRIMARY KEY, name varchar(255), email varchar(255))"); |
| 82 | + this. jdbc. execute( "INSERT INTO immutable_non_null VALUES (42, 'Walter', '[email protected]')"); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Creates a {@link DataSource} to be used in this test. |
| 87 | + * |
| 88 | + * @return the {@link DataSource} to be used in this test. |
| 89 | + */ |
| 90 | + protected DataSource createDataSource() { |
| 91 | + return H2TestSupport.createDataSource(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Creates a {@link ConnectionFactory} to be used in this test. |
| 96 | + * |
| 97 | + * @return the {@link ConnectionFactory} to be used in this test. |
| 98 | + */ |
| 99 | + protected ConnectionFactory createConnectionFactory() { |
| 100 | + return H2TestSupport.createConnectionFactory(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + // GH-1687 |
| 105 | + void shouldApplyProjectionDirectly() { |
| 106 | + |
| 107 | + repository. findProjectionByEmail( "[email protected]") // |
| 108 | + .as(StepVerifier::create) // |
| 109 | + .consumeNextWith(actual -> { |
| 110 | + assertThat(actual.getName()).isEqualTo("Walter"); |
| 111 | + }).verifyComplete(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + // GH-1687 |
| 116 | + void shouldApplyEntityQueryProjectionDirectly() { |
| 117 | + |
| 118 | + repository. findAllByEmail( "[email protected]") // |
| 119 | + .as(StepVerifier::create) // |
| 120 | + .consumeNextWith(actual -> { |
| 121 | + assertThat(actual.getName()).isEqualTo("Walter"); |
| 122 | + assertThat(actual).isInstanceOf(ImmutableNonNullEntity.class); |
| 123 | + }).verifyComplete(); |
| 124 | + } |
| 125 | + |
| 126 | + interface ImmutableObjectRepository extends ReactiveCrudRepository<ImmutableNonNullEntity, Integer> { |
| 127 | + |
| 128 | + Flux<ProjectionOnNonNull> findProjectionByEmail(String email); |
| 129 | + |
| 130 | + Flux<Person> findAllByEmail(String email); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + @Table("immutable_non_null") |
| 135 | + static class ImmutableNonNullEntity implements Person { |
| 136 | + |
| 137 | + final @Nullable |
| 138 | + @Id Integer id; |
| 139 | + final String name; |
| 140 | + final String email; |
| 141 | + |
| 142 | + ImmutableNonNullEntity(@Nullable Integer id, String name, String email) { |
| 143 | + |
| 144 | + Assert.notNull(name, "Name must not be null"); |
| 145 | + Assert.notNull(email, "Email must not be null"); |
| 146 | + |
| 147 | + this.id = id; |
| 148 | + this.name = name; |
| 149 | + this.email = email; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String getName() { |
| 154 | + return name; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + interface Person { |
| 159 | + |
| 160 | + String getName(); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + interface ProjectionOnNonNull { |
| 165 | + |
| 166 | + String getName(); |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments