|
| 1 | +/* |
| 2 | + * Copyright 2022 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.couchbase.transactions.sdk; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | +import static org.junit.jupiter.api.Assertions.fail; |
| 21 | +import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction; |
| 22 | + |
| 23 | +import org.springframework.data.couchbase.domain.PersonWithoutVersion; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | +import reactor.core.scheduler.Schedulers; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.DisplayName; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.data.couchbase.CouchbaseClientFactory; |
| 33 | +import org.springframework.data.couchbase.core.CouchbaseTemplate; |
| 34 | +import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; |
| 35 | +import org.springframework.data.couchbase.domain.Person; |
| 36 | +import org.springframework.data.couchbase.transactions.TransactionsConfig; |
| 37 | +import org.springframework.data.couchbase.util.Capabilities; |
| 38 | +import org.springframework.data.couchbase.util.ClusterType; |
| 39 | +import org.springframework.data.couchbase.util.IgnoreWhen; |
| 40 | +import org.springframework.data.couchbase.util.JavaIntegrationTests; |
| 41 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 42 | + |
| 43 | +/** |
| 44 | + * Added for issue 1527: Tests the .save() command. |
| 45 | + * |
| 46 | + * @author Graham Pople |
| 47 | + */ |
| 48 | +@IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED) |
| 49 | +@SpringJUnitConfig(TransactionsConfig.class) |
| 50 | +public class SDKTransactionsSaveIntegrationTests extends JavaIntegrationTests { |
| 51 | + @Autowired private CouchbaseClientFactory couchbaseClientFactory; |
| 52 | + @Autowired private CouchbaseTemplate ops; |
| 53 | + @Autowired private ReactiveCouchbaseTemplate reactiveOps; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void beforeEachTest() { |
| 57 | + assertNotInTransaction(); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterEach |
| 61 | + public void afterEachTest() { |
| 62 | + assertNotInTransaction(); |
| 63 | + } |
| 64 | + |
| 65 | + @DisplayName("ReactiveCouchbaseTemplate.save() called inside a reactive SDK transaction should work") |
| 66 | + @Test |
| 67 | + public void reactiveSaveInReactiveTransaction() { |
| 68 | + couchbaseClientFactory.getCluster().reactive().transactions().run(ctx -> { |
| 69 | + PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White"); |
| 70 | + return reactiveOps.save(p); |
| 71 | + }).block(); |
| 72 | + } |
| 73 | + |
| 74 | + @DisplayName("ReactiveCouchbaseTemplate.save().block() called inside a non-reactive SDK transaction should work") |
| 75 | + @Test |
| 76 | + public void reactiveSaveInBlockingTransaction() { |
| 77 | + couchbaseClientFactory.getCluster().transactions().run(ctx -> { |
| 78 | + PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White"); |
| 79 | + reactiveOps.save(p).block(); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @DisplayName("ReactiveCouchbaseTemplate.save() called inside a reactive SDK transaction should work") |
| 84 | + @Test |
| 85 | + public void blockingSaveInReactiveTransaction() { |
| 86 | + couchbaseClientFactory.getCluster().reactive().transactions().run(ctx -> { |
| 87 | + PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White"); |
| 88 | + ops.save(p); |
| 89 | + return Mono.empty(); |
| 90 | + }).block(); |
| 91 | + } |
| 92 | + |
| 93 | + @DisplayName("ReactiveCouchbaseTemplate.save().block() called inside a non-reactive SDK transaction should work") |
| 94 | + @Test |
| 95 | + public void blockingSaveInBlockingTransaction() { |
| 96 | + couchbaseClientFactory.getCluster().transactions().run(ctx -> { |
| 97 | + PersonWithoutVersion p = new PersonWithoutVersion("Walter", "White"); |
| 98 | + ops.save(p); |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
0 commit comments