|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 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.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import io.r2dbc.spi.ConnectionFactory; |
| 21 | +import lombok.Data; |
| 22 | +import reactor.test.StepVerifier; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +import javax.sql.DataSource; |
| 29 | + |
| 30 | +import org.junit.ClassRule; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import org.springframework.core.convert.converter.Converter; |
| 34 | +import org.springframework.dao.DataAccessException; |
| 35 | +import org.springframework.data.annotation.Id; |
| 36 | +import org.springframework.data.convert.ReadingConverter; |
| 37 | +import org.springframework.data.convert.WritingConverter; |
| 38 | +import org.springframework.data.r2dbc.dialect.MySqlDialect; |
| 39 | +import org.springframework.data.r2dbc.query.Criteria; |
| 40 | +import org.springframework.data.r2dbc.testing.ExternalDatabase; |
| 41 | +import org.springframework.data.r2dbc.testing.MariaDbTestSupport; |
| 42 | +import org.springframework.data.relational.core.mapping.Table; |
| 43 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 44 | + |
| 45 | +/** |
| 46 | + * Integration tests for {@link DatabaseClient} against MariaDB. |
| 47 | + * |
| 48 | + * @author Mark Paluch |
| 49 | + */ |
| 50 | +public class MariaDbDatabaseClientIntegrationTests extends AbstractDatabaseClientIntegrationTests { |
| 51 | + |
| 52 | + @ClassRule public static final ExternalDatabase database = MariaDbTestSupport.database(); |
| 53 | + |
| 54 | + @Override |
| 55 | + protected DataSource createDataSource() { |
| 56 | + return MariaDbTestSupport.createDataSource(database); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected ConnectionFactory createConnectionFactory() { |
| 61 | + return MariaDbTestSupport.createConnectionFactory(database); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected String getCreateTableStatement() { |
| 66 | + return MariaDbTestSupport.CREATE_TABLE_LEGOSET; |
| 67 | + } |
| 68 | + |
| 69 | + @Test // gh-166 |
| 70 | + public void considersBuiltInConverters() { |
| 71 | + |
| 72 | + ConnectionFactory connectionFactory = createConnectionFactory(); |
| 73 | + JdbcTemplate jdbc = createJdbcTemplate(createDataSource()); |
| 74 | + |
| 75 | + try { |
| 76 | + jdbc.execute("DROP TABLE boolean_mapping"); |
| 77 | + } catch (DataAccessException e) {} |
| 78 | + jdbc.execute("CREATE TABLE boolean_mapping (id int, flag1 TINYINT, flag2 TINYINT)"); |
| 79 | + |
| 80 | + BooleanMapping mapping = new BooleanMapping(); |
| 81 | + mapping.setId(42); |
| 82 | + mapping.setFlag1(true); |
| 83 | + |
| 84 | + DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); |
| 85 | + |
| 86 | + databaseClient.insert().into(BooleanMapping.class).using(mapping).then() // |
| 87 | + .as(StepVerifier::create) // |
| 88 | + .verifyComplete(); |
| 89 | + |
| 90 | + databaseClient.select().from(BooleanMapping.class).fetch().first() // |
| 91 | + .as(StepVerifier::create) // |
| 92 | + .consumeNextWith(actual -> assertThat(actual.isFlag1()).isTrue()) // |
| 93 | + .verifyComplete(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test // gh-305 |
| 97 | + public void shouldApplyCustomConverters() { |
| 98 | + |
| 99 | + ConnectionFactory connectionFactory = createConnectionFactory(); |
| 100 | + JdbcTemplate jdbc = createJdbcTemplate(createDataSource()); |
| 101 | + ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(MySqlDialect.INSTANCE, |
| 102 | + Arrays.asList(UuidToStringConverter.INSTANCE, StringToUuidConverter.INSTANCE)); |
| 103 | + |
| 104 | + try { |
| 105 | + jdbc.execute("DROP TABLE uuid_type"); |
| 106 | + } catch (DataAccessException e) {} |
| 107 | + jdbc.execute("CREATE TABLE uuid_type (id varchar(255), uuid_value varchar(255))"); |
| 108 | + |
| 109 | + UuidType uuidType = new UuidType(); |
| 110 | + uuidType.setId(UUID.randomUUID()); |
| 111 | + uuidType.setUuidValue(UUID.randomUUID()); |
| 112 | + |
| 113 | + DatabaseClient databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory) |
| 114 | + .dataAccessStrategy(strategy).build(); |
| 115 | + |
| 116 | + databaseClient.insert().into(UuidType.class).using(uuidType).then() // |
| 117 | + .as(StepVerifier::create) // |
| 118 | + .verifyComplete(); |
| 119 | + |
| 120 | + databaseClient.select().from(UuidType.class).matching(Criteria.where("id").is(uuidType.getId())) // |
| 121 | + .fetch().first() // |
| 122 | + .as(StepVerifier::create) // |
| 123 | + .consumeNextWith(actual -> assertThat(actual.getUuidValue()).isEqualTo(uuidType.getUuidValue())) // |
| 124 | + .verifyComplete(); |
| 125 | + |
| 126 | + uuidType.setUuidValue(null); |
| 127 | + databaseClient.update().table(UuidType.class).using(uuidType).then() // |
| 128 | + .as(StepVerifier::create) // |
| 129 | + .verifyComplete(); |
| 130 | + |
| 131 | + databaseClient.execute("SELECT * FROM uuid_type WHERE id = ?") // |
| 132 | + .bind(0, uuidType.getId()) // |
| 133 | + .as(UuidType.class) // |
| 134 | + .fetch().first() // |
| 135 | + .as(StepVerifier::create) // |
| 136 | + .consumeNextWith(actual -> assertThat(actual.getUuidValue()).isNull()) // |
| 137 | + .verifyComplete(); |
| 138 | + |
| 139 | + databaseClient.execute("SELECT * FROM uuid_type WHERE id in (:ids)") // |
| 140 | + .bind("ids", Collections.singleton(uuidType.getId())) // |
| 141 | + .as(UuidType.class) // |
| 142 | + .fetch().first() // |
| 143 | + .as(StepVerifier::create) // |
| 144 | + .consumeNextWith(actual -> assertThat(actual.getUuidValue()).isNull()) // |
| 145 | + .verifyComplete(); |
| 146 | + } |
| 147 | + |
| 148 | + @Table("boolean_mapping") |
| 149 | + @Data |
| 150 | + static class BooleanMapping { |
| 151 | + |
| 152 | + int id; |
| 153 | + boolean flag1; |
| 154 | + boolean flag2; |
| 155 | + } |
| 156 | + |
| 157 | + @Table("uuid_type") |
| 158 | + @Data |
| 159 | + static class UuidType { |
| 160 | + |
| 161 | + @Id UUID id; |
| 162 | + UUID uuidValue; |
| 163 | + } |
| 164 | + |
| 165 | + @WritingConverter |
| 166 | + enum UuidToStringConverter implements Converter<UUID, String> { |
| 167 | + INSTANCE; |
| 168 | + |
| 169 | + @Override |
| 170 | + public String convert(UUID uuid) { |
| 171 | + return uuid.toString(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @ReadingConverter |
| 176 | + enum StringToUuidConverter implements Converter<String, UUID> { |
| 177 | + INSTANCE; |
| 178 | + |
| 179 | + @Override |
| 180 | + public UUID convert(String value) { |
| 181 | + return UUID.fromString(value); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments