|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 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 | + |
| 17 | +package org.springframework.data.r2dbc.core.mapping; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import java.util.function.BiFunction; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.mockito.Mockito; |
| 28 | +import org.reactivestreams.Publisher; |
| 29 | +import org.springframework.data.annotation.Id; |
| 30 | +import org.springframework.data.r2dbc.dialect.MySqlDialect; |
| 31 | +import org.springframework.data.r2dbc.dialect.PostgresDialect; |
| 32 | +import org.springframework.data.r2dbc.mapping.OutboundRow; |
| 33 | +import org.springframework.data.r2dbc.mapping.R2dbcMappingContext; |
| 34 | +import org.springframework.data.relational.core.mapping.Sequence; |
| 35 | +import org.springframework.data.relational.core.sql.SqlIdentifier; |
| 36 | +import org.springframework.r2dbc.core.DatabaseClient; |
| 37 | +import org.springframework.r2dbc.core.Parameter; |
| 38 | + |
| 39 | +import reactor.core.publisher.Mono; |
| 40 | +import reactor.test.StepVerifier; |
| 41 | + |
| 42 | +/** |
| 43 | + * Unit tests for {@link IdGeneratingBeforeSaveCallback}. |
| 44 | + * |
| 45 | + * @author Mikhail Polivakha |
| 46 | + */ |
| 47 | +class IdGeneratingBeforeSaveCallbackTest { |
| 48 | + |
| 49 | + @Test |
| 50 | + void testIdGenerationIsNotSupported() { |
| 51 | + R2dbcMappingContext r2dbcMappingContext = new R2dbcMappingContext(); |
| 52 | + r2dbcMappingContext.getPersistentEntity(SimpleEntity.class); |
| 53 | + MySqlDialect dialect = MySqlDialect.INSTANCE; |
| 54 | + DatabaseClient databaseClient = mock(DatabaseClient.class); |
| 55 | + |
| 56 | + IdGeneratingBeforeSaveCallback callback = new IdGeneratingBeforeSaveCallback(r2dbcMappingContext, dialect, |
| 57 | + databaseClient); |
| 58 | + |
| 59 | + OutboundRow row = new OutboundRow("name", Parameter.from("my_name")); |
| 60 | + SimpleEntity entity = new SimpleEntity(); |
| 61 | + Publisher<Object> publisher = callback.onBeforeSave(entity, row, SqlIdentifier.unquoted("simple_entity")); |
| 62 | + |
| 63 | + StepVerifier.create(publisher).expectNext(entity).expectComplete().verify(); |
| 64 | + assertThat(row).hasSize(1); // id is not added |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testEntityIsNotAnnotatedWithSequence() { |
| 69 | + R2dbcMappingContext r2dbcMappingContext = new R2dbcMappingContext(); |
| 70 | + r2dbcMappingContext.getPersistentEntity(SimpleEntity.class); |
| 71 | + PostgresDialect dialect = PostgresDialect.INSTANCE; |
| 72 | + DatabaseClient databaseClient = mock(DatabaseClient.class); |
| 73 | + |
| 74 | + IdGeneratingBeforeSaveCallback callback = new IdGeneratingBeforeSaveCallback(r2dbcMappingContext, dialect, |
| 75 | + databaseClient); |
| 76 | + |
| 77 | + OutboundRow row = new OutboundRow("name", Parameter.from("my_name")); |
| 78 | + SimpleEntity entity = new SimpleEntity(); |
| 79 | + Publisher<Object> publisher = callback.onBeforeSave(entity, row, SqlIdentifier.unquoted("simple_entity")); |
| 80 | + |
| 81 | + StepVerifier.create(publisher).expectNext(entity).expectComplete().verify(); |
| 82 | + assertThat(row).hasSize(1); // id is not added |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testIdGeneratedFromSequenceHappyPath() { |
| 87 | + R2dbcMappingContext r2dbcMappingContext = new R2dbcMappingContext(); |
| 88 | + r2dbcMappingContext.getPersistentEntity(WithSequence.class); |
| 89 | + PostgresDialect dialect = PostgresDialect.INSTANCE; |
| 90 | + DatabaseClient databaseClient = mock(DatabaseClient.class, RETURNS_DEEP_STUBS); |
| 91 | + long generatedId = 1L; |
| 92 | + |
| 93 | + when(databaseClient.sql(Mockito.anyString()).map(Mockito.any(BiFunction.class)).one()).thenReturn( |
| 94 | + Mono.just(generatedId)); |
| 95 | + |
| 96 | + IdGeneratingBeforeSaveCallback callback = new IdGeneratingBeforeSaveCallback(r2dbcMappingContext, dialect, |
| 97 | + databaseClient); |
| 98 | + |
| 99 | + OutboundRow row = new OutboundRow("name", Parameter.from("my_name")); |
| 100 | + WithSequence entity = new WithSequence(); |
| 101 | + Publisher<Object> publisher = callback.onBeforeSave(entity, row, SqlIdentifier.unquoted("simple_entity")); |
| 102 | + |
| 103 | + StepVerifier.create(publisher).expectNext(entity).expectComplete().verify(); |
| 104 | + assertThat(row).hasSize(2) |
| 105 | + .containsEntry(SqlIdentifier.unquoted("id"), Parameter.from(generatedId)); |
| 106 | + } |
| 107 | + |
| 108 | + static class SimpleEntity { |
| 109 | + |
| 110 | + @Id |
| 111 | + private Long id; |
| 112 | + |
| 113 | + private String name; |
| 114 | + } |
| 115 | + |
| 116 | + static class WithSequence { |
| 117 | + |
| 118 | + @Id |
| 119 | + @Sequence(sequence = "seq_name") |
| 120 | + private Long id; |
| 121 | + |
| 122 | + private String name; |
| 123 | + } |
| 124 | +} |
0 commit comments