From 767b90911b5c86ea72a1a8ae2b164e2616bbcb86 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 15 May 2023 11:48:14 +0200 Subject: [PATCH] Polishing. Fix formatting. See #664 --- .../AirlineRepositoryIntegrationTests.java | 108 ++++++------- ...tiveAirlineRepositoryIntegrationTests.java | 142 ++++++++-------- ...vaCouchbaseOperationsIntegrationTests.java | 90 +++++------ .../java/com/example/demo/AirlineGates.java | 57 ++++--- .../example/demo/AirlineGatesRepository.java | 2 +- .../com/example/demo/AirlineGatesService.java | 132 +++++++-------- .../main/java/com/example/demo/CmdRunner.java | 151 +++++++++--------- .../main/java/com/example/demo/Config.java | 40 ++--- .../com/example/demo/DemoApplication.java | 6 +- .../example/demo/DemoApplicationTests.java | 9 +- 10 files changed, 361 insertions(+), 376 deletions(-) diff --git a/couchbase/example/src/test/java/example/springdata/couchbase/repository/AirlineRepositoryIntegrationTests.java b/couchbase/example/src/test/java/example/springdata/couchbase/repository/AirlineRepositoryIntegrationTests.java index 6cdc5cc4..e5c630cb 100644 --- a/couchbase/example/src/test/java/example/springdata/couchbase/repository/AirlineRepositoryIntegrationTests.java +++ b/couchbase/example/src/test/java/example/springdata/couchbase/repository/AirlineRepositoryIntegrationTests.java @@ -17,6 +17,9 @@ package example.springdata.couchbase.repository; import static org.assertj.core.api.Assertions.*; +import example.springdata.couchbase.model.Airline; +import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; + import java.util.List; import org.junit.jupiter.api.BeforeEach; @@ -26,9 +29,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.couchbase.core.CouchbaseOperations; -import example.springdata.couchbase.model.Airline; -import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; - /** * Integration tests showing basic CRUD operations through {@link AirlineRepository}. * @@ -38,70 +38,68 @@ import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; @EnabledOnCouchbaseAvailable public class AirlineRepositoryIntegrationTests { - @Autowired - AirlineRepository airlineRepository; + @Autowired AirlineRepository airlineRepository; - @Autowired - CouchbaseOperations couchbaseOperations; + @Autowired CouchbaseOperations couchbaseOperations; - @BeforeEach - public void before() { - if (couchbaseOperations.existsById().one("LH")) { - couchbaseOperations.removeById().one("LH"); - } - } + @BeforeEach + public void before() { + if (couchbaseOperations.existsById().one("LH")) { + couchbaseOperations.removeById().one("LH"); + } + } - /** - * The derived query executes a N1QL query emitting a single element. - */ - @Test - public void shouldFindAirlineN1ql() { + /** + * The derived query executes a N1QL query emitting a single element. + */ + @Test + public void shouldFindAirlineN1ql() { - List airlines = airlineRepository.findByIata("TQ"); - assertThat(airlines.get(0).getCallsign()).isEqualTo("TXW"); - } + List airlines = airlineRepository.findByIata("TQ"); + assertThat(airlines.get(0).getCallsign()).isEqualTo("TXW"); + } - /** - * The derived query executes a N1QL query and the emitted element is used to invoke - * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(Object)} for an Id-based lookup. - * Queries without a result do not emit a value. - */ - @Test - public void shouldFindById() { + /** + * The derived query executes a N1QL query and the emitted element is used to invoke + * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(Object)} for an Id-based + * lookup. Queries without a result do not emit a value. + */ + @Test + public void shouldFindById() { - Airline airline = airlineRepository.findByIata("TQ").get(0); - assertThat(airlineRepository.findById(airline.getId()).isPresent()); - } + Airline airline = airlineRepository.findByIata("TQ").get(0); + assertThat(airlineRepository.findById(airline.getId()).isPresent()); + } - /** - * Find all {@link Airline}s applying the {@code airlines/all} view. - */ - @Test - public void shouldFindByView() { + /** + * Find all {@link Airline}s applying the {@code airlines/all} view. + */ + @Test + public void shouldFindByView() { - List airlines = airlineRepository.findAllBy(); + List airlines = airlineRepository.findAllBy(); - assertThat(airlines).hasSizeGreaterThan(100); - } + assertThat(airlines).hasSizeGreaterThan(100); + } - /** - * Created elements are emitted by the - * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#save(Object)} method. - */ - @Test - public void shouldCreateAirline() { + /** + * Created elements are emitted by the + * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#save(Object)} method. + */ + @Test + public void shouldCreateAirline() { - Airline airline = new Airline(); + Airline airline = new Airline(); - airline.setId("LH"); - airline.setIata("LH"); - airline.setIcao("DLH"); - airline.setCallsign("Lufthansa"); - airline.setName("Lufthansa"); - airline.setCountry("Germany"); + airline.setId("LH"); + airline.setIata("LH"); + airline.setIcao("DLH"); + airline.setCallsign("Lufthansa"); + airline.setName("Lufthansa"); + airline.setCountry("Germany"); - airlineRepository.save(airline); + airlineRepository.save(airline); - assertThat(airlineRepository.findById("LH")).contains(airline); - } + assertThat(airlineRepository.findById("LH")).contains(airline); + } } diff --git a/couchbase/reactive/src/test/java/example/springdata/couchbase/repository/ReactiveAirlineRepositoryIntegrationTests.java b/couchbase/reactive/src/test/java/example/springdata/couchbase/repository/ReactiveAirlineRepositoryIntegrationTests.java index 5a405818..203ee6a5 100644 --- a/couchbase/reactive/src/test/java/example/springdata/couchbase/repository/ReactiveAirlineRepositoryIntegrationTests.java +++ b/couchbase/reactive/src/test/java/example/springdata/couchbase/repository/ReactiveAirlineRepositoryIntegrationTests.java @@ -17,6 +17,11 @@ package example.springdata.couchbase.repository; import static org.assertj.core.api.Assertions.*; +import example.springdata.couchbase.model.Airline; +import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -24,11 +29,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.couchbase.core.CouchbaseOperations; -import example.springdata.couchbase.model.Airline; -import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - /** * Integration tests showing basic CRUD operations through {@link ReactiveAirlineRepository} * @@ -38,87 +38,85 @@ import reactor.test.StepVerifier; @EnabledOnCouchbaseAvailable public class ReactiveAirlineRepositoryIntegrationTests { - @Autowired - ReactiveAirlineRepository airlineRepository; + @Autowired ReactiveAirlineRepository airlineRepository; - @Autowired - CouchbaseOperations couchbaseOperations; + @Autowired CouchbaseOperations couchbaseOperations; - @BeforeEach - public void before() { - if (couchbaseOperations.existsById().one("LH")) { - couchbaseOperations.removeById().one("LH"); - } - } + @BeforeEach + public void before() { + if (couchbaseOperations.existsById().one("LH")) { + couchbaseOperations.removeById().one("LH"); + } + } - /** - * The derived query executes a N1QL query emitting a single element. - */ - @Test - public void shouldFindAirlineN1ql() { + /** + * The derived query executes a N1QL query emitting a single element. + */ + @Test + public void shouldFindAirlineN1ql() { - airlineRepository.findByIata("TQ") // - .as(StepVerifier::create) // - .assertNext(it -> { - assertThat(it.getCallsign()).isEqualTo("TXW"); - }).verifyComplete(); - } + airlineRepository.findByIata("TQ") // + .as(StepVerifier::create) // + .assertNext(it -> { + assertThat(it.getCallsign()).isEqualTo("TXW"); + }).verifyComplete(); + } - /** - * The derived query executes a N1QL query and the emitted element is used to invoke - * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(Object)} for an Id-based lookup. - * Queries without a result do not emit a value. - */ - @Test - public void shouldFindById() { + /** + * The derived query executes a N1QL query and the emitted element is used to invoke + * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(Object)} for an Id-based + * lookup. Queries without a result do not emit a value. + */ + @Test + public void shouldFindById() { - Mono airline = airlineRepository.findByIata("TQ") // - .map(Airline::getId) // - .flatMap(airlineRepository::findById); + Mono airline = airlineRepository.findByIata("TQ") // + .map(Airline::getId) // + .flatMap(airlineRepository::findById); - airline.as(StepVerifier::create) // - .assertNext(it -> { + airline.as(StepVerifier::create) // + .assertNext(it -> { - assertThat(it.getCallsign()).isEqualTo("TXW"); - }).verifyComplete(); + assertThat(it.getCallsign()).isEqualTo("TXW"); + }).verifyComplete(); - } + } - /** - * Find all {@link Airline}s applying the {@code airlines/all} view. - */ - @Test - public void shouldFindAll() { - airlineRepository.findAllBy().count() // - .as(StepVerifier::create) // - .assertNext(count -> { + /** + * Find all {@link Airline}s applying the {@code airlines/all} view. + */ + @Test + public void shouldFindAll() { + airlineRepository.findAllBy().count() // + .as(StepVerifier::create) // + .assertNext(count -> { - assertThat(count).isGreaterThan(100); - }).verifyComplete(); - } + assertThat(count).isGreaterThan(100); + }).verifyComplete(); + } - /** - * Created elements are emitted by the - * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#save(Object)} method. - */ - @Test - public void shouldCreateAirline() { + /** + * Created elements are emitted by the + * {@link org.springframework.data.repository.reactive.ReactiveCrudRepository#save(Object)} method. + */ + @Test + public void shouldCreateAirline() { - Airline airline = new Airline(); + Airline airline = new Airline(); - airline.setId("LH"); - airline.setIata("LH"); - airline.setIcao("DLH"); - airline.setCallsign("Lufthansa"); - airline.setName("Lufthansa"); - airline.setCountry("Germany"); + airline.setId("LH"); + airline.setIata("LH"); + airline.setIcao("DLH"); + airline.setCallsign("Lufthansa"); + airline.setName("Lufthansa"); + airline.setCountry("Germany"); - Mono airlineMono = airlineRepository.save(airline) // - .map(Airline::getId) // - .flatMap(airlineRepository::findById); + Mono airlineMono = airlineRepository.save(airline) // + .map(Airline::getId) // + .flatMap(airlineRepository::findById); - airlineMono.as(StepVerifier::create) // - .expectNext(airline) // - .verifyComplete(); - } + airlineMono.as(StepVerifier::create) // + .expectNext(airline) // + .verifyComplete(); + } } diff --git a/couchbase/reactive/src/test/java/example/springdata/couchbase/template/ReactiveJavaCouchbaseOperationsIntegrationTests.java b/couchbase/reactive/src/test/java/example/springdata/couchbase/template/ReactiveJavaCouchbaseOperationsIntegrationTests.java index c26cd3d1..38accec4 100644 --- a/couchbase/reactive/src/test/java/example/springdata/couchbase/template/ReactiveJavaCouchbaseOperationsIntegrationTests.java +++ b/couchbase/reactive/src/test/java/example/springdata/couchbase/template/ReactiveJavaCouchbaseOperationsIntegrationTests.java @@ -17,6 +17,11 @@ package example.springdata.couchbase.template; import static org.assertj.core.api.Assertions.*; +import example.springdata.couchbase.model.Airline; +import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -25,11 +30,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.couchbase.core.CouchbaseOperations; import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations; -import example.springdata.couchbase.model.Airline; -import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - /** * Integration tests showing basic CRUD operations through * {@link org.springframework.data.couchbase.core.ReactiveCouchbaseOperations}. @@ -41,53 +41,51 @@ import reactor.test.StepVerifier; @EnabledOnCouchbaseAvailable public class ReactiveJavaCouchbaseOperationsIntegrationTests { - @Autowired - ReactiveCouchbaseOperations operations; + @Autowired ReactiveCouchbaseOperations operations; - @Autowired - CouchbaseOperations couchbaseOperations; + @Autowired CouchbaseOperations couchbaseOperations; - @BeforeEach - public void before() { - if (couchbaseOperations.existsById().one("LH")) { - couchbaseOperations.removeById().one("LH"); - } - } + @BeforeEach + public void before() { + if (couchbaseOperations.existsById().one("LH")) { + couchbaseOperations.removeById().one("LH"); + } + } - /** - * Find all {@link Airline}s applying the _class filter . - */ - @Test - public void shouldFindByAll() { - operations.findByQuery(Airline.class).all() // - .count() // - .as(StepVerifier::create) // - .assertNext(count -> { + /** + * Find all {@link Airline}s applying the _class filter . + */ + @Test + public void shouldFindByAll() { + operations.findByQuery(Airline.class).all() // + .count() // + .as(StepVerifier::create) // + .assertNext(count -> { - assertThat(count).isGreaterThan(100); - }) // - .verifyComplete(); - } + assertThat(count).isGreaterThan(100); + }) // + .verifyComplete(); + } - /** - * Created elements are emitted by {@link ReactiveCouchbaseOperations#upsertById(Class)} )}. - */ - @Test - public void shouldCreateAirline() { - Airline airline = new Airline(); + /** + * Created elements are emitted by {@link ReactiveCouchbaseOperations#upsertById(Class)} )}. + */ + @Test + public void shouldCreateAirline() { + Airline airline = new Airline(); - airline.setId("LH"); - airline.setIata("LH"); - airline.setIcao("DLH"); - airline.setCallsign("Lufthansa"); - airline.setName("Lufthansa"); - airline.setCountry("Germany"); + airline.setId("LH"); + airline.setIata("LH"); + airline.setIcao("DLH"); + airline.setCallsign("Lufthansa"); + airline.setName("Lufthansa"); + airline.setCountry("Germany"); - Mono airlineMono = operations.upsertById(Airline.class).one(airline) // - .map(Airline::getId) // - .flatMap(id -> operations.findById(Airline.class).one(id)); + Mono airlineMono = operations.upsertById(Airline.class).one(airline) // + .map(Airline::getId) // + .flatMap(id -> operations.findById(Airline.class).one(id)); - airlineMono.as(StepVerifier::create) // - .expectNext(airline).verifyComplete(); - } + airlineMono.as(StepVerifier::create) // + .expectNext(airline).verifyComplete(); + } } diff --git a/couchbase/transactions/src/main/java/com/example/demo/AirlineGates.java b/couchbase/transactions/src/main/java/com/example/demo/AirlineGates.java index 5df4d5e3..b91b2d88 100644 --- a/couchbase/transactions/src/main/java/com/example/demo/AirlineGates.java +++ b/couchbase/transactions/src/main/java/com/example/demo/AirlineGates.java @@ -25,40 +25,37 @@ import org.springframework.data.couchbase.core.mapping.Document; */ @Document public class AirlineGates { - @Id - String id; - @Version - Long version; - @QueryIndexed - String name; - String iata; - Long gates; + @Id String id; + @Version Long version; + @QueryIndexed String name; + String iata; + Long gates; - public AirlineGates(String id, String name, String iata, Long gates) { - this.id = id; - this.name = name; - this.iata = iata; - this.gates = gates; - } + public AirlineGates(String id, String name, String iata, Long gates) { + this.id = id; + this.name = name; + this.iata = iata; + this.gates = gates; + } - public String getId() { - return id; - } + public String getId() { + return id; + } - public Long getGates() { - return gates; - } + public Long getGates() { + return gates; + } - public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append("{"); - sb.append("\"id\":" + id); - sb.append(", \"name\":" + name); - sb.append(", \"iata\":" + iata); - sb.append(", \"gates\":" + gates); - sb.append("}"); + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("{"); + sb.append("\"id\":" + id); + sb.append(", \"name\":" + name); + sb.append(", \"iata\":" + iata); + sb.append(", \"gates\":" + gates); + sb.append("}"); - return sb.toString(); - } + return sb.toString(); + } } diff --git a/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesRepository.java b/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesRepository.java index e5a0cba5..a1602704 100644 --- a/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesRepository.java +++ b/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesRepository.java @@ -22,6 +22,6 @@ import org.springframework.data.couchbase.repository.DynamicProxyable; * @author Michael Reiche */ public interface AirlineGatesRepository - extends CouchbaseRepository, DynamicProxyable { + extends CouchbaseRepository, DynamicProxyable { } diff --git a/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesService.java b/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesService.java index 7b10db7b..f1196a91 100644 --- a/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesService.java +++ b/couchbase/transactions/src/main/java/com/example/demo/AirlineGatesService.java @@ -15,88 +15,88 @@ */ package com.example.demo; +import reactor.core.publisher.Mono; + import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import reactor.core.publisher.Mono; - /** * @author Michael Reiche */ @Service public class AirlineGatesService { - CouchbaseTemplate template; - ReactiveCouchbaseTemplate reactiveTemplate; - AirlineGatesRepository airlineGatesRepository; + CouchbaseTemplate template; + ReactiveCouchbaseTemplate reactiveTemplate; + AirlineGatesRepository airlineGatesRepository; - public AirlineGatesService(CouchbaseTemplate template, AirlineGatesRepository airlineGatesRepository) { - this.template = template; - this.reactiveTemplate = template.reactive(); - this.airlineGatesRepository = airlineGatesRepository; - } + public AirlineGatesService(CouchbaseTemplate template, AirlineGatesRepository airlineGatesRepository) { + this.template = template; + this.reactiveTemplate = template.reactive(); + this.airlineGatesRepository = airlineGatesRepository; + } - @Transactional - public void transferGates(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) { - AirlineGates fromAirlineGates = template.findById(AirlineGates.class).one(fromId); - AirlineGates toAirlineGates = template.findById(AirlineGates.class).one(toId); - toAirlineGates.gates += gatesToTransfer; - fromAirlineGates.gates -= gatesToTransfer; - template.save(fromAirlineGates); - if (exceptionToThrow != null) { - throw exceptionToThrow; - } - template.save(toAirlineGates); - } + @Transactional + public void transferGates(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) { + AirlineGates fromAirlineGates = template.findById(AirlineGates.class).one(fromId); + AirlineGates toAirlineGates = template.findById(AirlineGates.class).one(toId); + toAirlineGates.gates += gatesToTransfer; + fromAirlineGates.gates -= gatesToTransfer; + template.save(fromAirlineGates); + if (exceptionToThrow != null) { + throw exceptionToThrow; + } + template.save(toAirlineGates); + } - @Transactional - public void transferGatesRepo(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) { - AirlineGates fromAirlineGates = airlineGatesRepository.findById(fromId).orElse(null); - AirlineGates toAirlineGates = airlineGatesRepository.findById(toId).orElse(null); - toAirlineGates.gates += gatesToTransfer; - fromAirlineGates.gates -= gatesToTransfer; - airlineGatesRepository.save(fromAirlineGates); - if (exceptionToThrow != null) { - throw exceptionToThrow; - } - airlineGatesRepository.save(toAirlineGates); - } + @Transactional + public void transferGatesRepo(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) { + AirlineGates fromAirlineGates = airlineGatesRepository.findById(fromId).orElse(null); + AirlineGates toAirlineGates = airlineGatesRepository.findById(toId).orElse(null); + toAirlineGates.gates += gatesToTransfer; + fromAirlineGates.gates -= gatesToTransfer; + airlineGatesRepository.save(fromAirlineGates); + if (exceptionToThrow != null) { + throw exceptionToThrow; + } + airlineGatesRepository.save(toAirlineGates); + } - // The @Transactional annotation results in the method of the proxy for the service executing this in a transaction - @Transactional - public Mono transferGatesReactive(String fromId, String toId, int gatesToTransfer, - RuntimeException exceptionToThrow) { - return Mono.deferContextual(ctx -> { - AirlineGates fromAirlineGates = template.findById(AirlineGates.class).one(fromId); - AirlineGates toAirlineGates = template.findById(AirlineGates.class).one(toId); - toAirlineGates.gates += gatesToTransfer; - fromAirlineGates.gates -= gatesToTransfer; - template.save(fromAirlineGates); - if (exceptionToThrow != null) { - throw exceptionToThrow; - } - return reactiveTemplate.save(toAirlineGates).then(); - }); - } + // The @Transactional annotation results in the method of the proxy for the service executing this in a transaction + @Transactional + public Mono transferGatesReactive(String fromId, String toId, int gatesToTransfer, + RuntimeException exceptionToThrow) { + return Mono.deferContextual(ctx -> { + AirlineGates fromAirlineGates = template.findById(AirlineGates.class).one(fromId); + AirlineGates toAirlineGates = template.findById(AirlineGates.class).one(toId); + toAirlineGates.gates += gatesToTransfer; + fromAirlineGates.gates -= gatesToTransfer; + template.save(fromAirlineGates); + if (exceptionToThrow != null) { + throw exceptionToThrow; + } + return reactiveTemplate.save(toAirlineGates).then(); + }); + } - // This does not have the @Transactional annotation therefore is not executed in a transaction - public AirlineGates save(AirlineGates airlineGates) { - return template.save(airlineGates); - } + // This does not have the @Transactional annotation therefore is not executed in a transaction + public AirlineGates save(AirlineGates airlineGates) { + return template.save(airlineGates); + } - // This does not have the @Transactional annotation therefore is not executed in a transaction - public AirlineGates findById(String id) { - return template.findById(AirlineGates.class).one(id); - } + // This does not have the @Transactional annotation therefore is not executed in a transaction + public AirlineGates findById(String id) { + return template.findById(AirlineGates.class).one(id); + } - // This does not have the @Transactional annotation therefore is not executed in a transaction - public AirlineGates saveRepo(AirlineGates airlineGates) { - return airlineGatesRepository.save(airlineGates); - } + // This does not have the @Transactional annotation therefore is not executed in a transaction + public AirlineGates saveRepo(AirlineGates airlineGates) { + return airlineGatesRepository.save(airlineGates); + } - // This does not have the @Transactional annotation therefore is not executed in a transaction - public AirlineGates findByIdRepo(String id) { - return airlineGatesRepository.findById(id).orElse(null); - } + // This does not have the @Transactional annotation therefore is not executed in a transaction + public AirlineGates findByIdRepo(String id) { + return airlineGatesRepository.findById(id).orElse(null); + } } diff --git a/couchbase/transactions/src/main/java/com/example/demo/CmdRunner.java b/couchbase/transactions/src/main/java/com/example/demo/CmdRunner.java index 43c9724e..63462ceb 100644 --- a/couchbase/transactions/src/main/java/com/example/demo/CmdRunner.java +++ b/couchbase/transactions/src/main/java/com/example/demo/CmdRunner.java @@ -23,98 +23,93 @@ import org.springframework.stereotype.Component; import org.springframework.util.Assert; /** - * Components of the type CommandLineRunner are called right after the application start up. So the method *run* is called as - * soon as the application starts. + * Components of the type CommandLineRunner are called right after the application start up. So the method *run* is + * called as soon as the application starts. * * @author Michael Reiche */ @Component public class CmdRunner implements CommandLineRunner { - @Autowired - CouchbaseTemplate template; - @Autowired - AirlineGatesService airlineGatesService; + @Autowired CouchbaseTemplate template; + @Autowired AirlineGatesService airlineGatesService; - // @Override - public void run(String... strings) { + // @Override + public void run(String... strings) { - try { // remove leftovers from previous run - template.removeById(AirlineGates.class).one("1"); - } catch (Exception e) { - } - try { - template.removeById(AirlineGates.class).one("2"); - } catch (Exception e) { - } + try { // remove leftovers from previous run + template.removeById(AirlineGates.class).one("1"); + } catch (Exception e) {} + try { + template.removeById(AirlineGates.class).one("2"); + } catch (Exception e) {} - AirlineGates airlineGates1 = new AirlineGates("1", "American Airlines", "JFK", Long.valueOf(200)); // 1 - AirlineGates airlineGates2 = new AirlineGates("2", "Lufthansa", "JFK", Long.valueOf(200)); - AirlineGates saved1 = airlineGatesService.save(airlineGates1); - AirlineGates saved2 = airlineGatesService.save(airlineGates2); - AirlineGates found_a_1 = airlineGatesService.findById(saved1.getId()); // 2 - AirlineGates found_a_2 = airlineGatesService.findById(saved2.getId()); - System.err.println("initialized airlines"); - System.err.println(" found before transferGates: " + found_a_1); - System.err.println(" found before transferGates: " + found_a_2); - // move 50 gates from airline1 to airline2 - int gatesToTransfer = 50; - System.err.println("==============================================================="); - System.err.println("this transferGates attempt will succeed. transferring " + gatesToTransfer); + AirlineGates airlineGates1 = new AirlineGates("1", "American Airlines", "JFK", Long.valueOf(200)); // 1 + AirlineGates airlineGates2 = new AirlineGates("2", "Lufthansa", "JFK", Long.valueOf(200)); + AirlineGates saved1 = airlineGatesService.save(airlineGates1); + AirlineGates saved2 = airlineGatesService.save(airlineGates2); + AirlineGates found_a_1 = airlineGatesService.findById(saved1.getId()); // 2 + AirlineGates found_a_2 = airlineGatesService.findById(saved2.getId()); + System.err.println("initialized airlines"); + System.err.println(" found before transferGates: " + found_a_1); + System.err.println(" found before transferGates: " + found_a_2); + // move 50 gates from airline1 to airline2 + int gatesToTransfer = 50; + System.err.println("==============================================================="); + System.err.println("this transferGates attempt will succeed. transferring " + gatesToTransfer); - airlineGatesService.transferGates(airlineGates1.getId(), airlineGates2.getId(), gatesToTransfer, null); // 3 + airlineGatesService.transferGates(airlineGates1.getId(), airlineGates2.getId(), gatesToTransfer, null); // 3 - AirlineGates found_b_1 = airlineGatesService.findById(airlineGates1.getId()); - AirlineGates found_b_2 = airlineGatesService.findById(airlineGates2.getId()); - System.err.println(" found after transferGates: " + found_b_1); // 4 - System.err.println(" found after transferGates: " + found_b_2); - Assert.isTrue(found_b_1.getGates().equals(found_a_1.getGates() - gatesToTransfer), "should have transferred"); - Assert.isTrue(found_b_2.getGates().equals(found_a_1.getGates() + gatesToTransfer), "should have transferred"); - System.err.println("==============================================================="); - gatesToTransfer = 44; - System.err.println("this transferGates attempt will fail. transferring " + gatesToTransfer); - // attempt to move 44 gates from airline1 to airline2, but it fails. - try { + AirlineGates found_b_1 = airlineGatesService.findById(airlineGates1.getId()); + AirlineGates found_b_2 = airlineGatesService.findById(airlineGates2.getId()); + System.err.println(" found after transferGates: " + found_b_1); // 4 + System.err.println(" found after transferGates: " + found_b_2); + Assert.isTrue(found_b_1.getGates().equals(found_a_1.getGates() - gatesToTransfer), "should have transferred"); + Assert.isTrue(found_b_2.getGates().equals(found_a_1.getGates() + gatesToTransfer), "should have transferred"); + System.err.println("==============================================================="); + gatesToTransfer = 44; + System.err.println("this transferGates attempt will fail. transferring " + gatesToTransfer); + // attempt to move 44 gates from airline1 to airline2, but it fails. + try { - airlineGatesService.transferGates(airlineGates1.getId(), airlineGates2.getId(), 44, new SimulateErrorException()); + airlineGatesService.transferGates(airlineGates1.getId(), airlineGates2.getId(), 44, new SimulateErrorException()); - } catch (RuntimeException rte) { - if (!(rte instanceof TransactionSystemUnambiguousException) && rte != null - && rte.getCause() instanceof SimulateErrorException) { - throw rte; - } - System.err.println(" got exception " + rte); - } - AirlineGates found_c_1 = airlineGatesService.findById(airlineGates1.getId()); - AirlineGates found_c_2 = airlineGatesService.findById(airlineGates2.getId()); - System.err.println(" found after transferGates: " + found_c_1); - System.err.println(" found after transferGates: " + found_c_2); - Assert.isTrue(found_c_1.getGates().equals(found_b_1.getGates()), "should be same as previous"); - Assert.isTrue(found_c_2.getGates().equals(found_b_2.getGates()), "should be same as previous"); - System.err.println("==============================================================="); - gatesToTransfer = 44; - System.err.println("this transferGates attempt will succeed. transferring " + gatesToTransfer); - try { + } catch (RuntimeException rte) { + if (!(rte instanceof TransactionSystemUnambiguousException) && rte != null + && rte.getCause() instanceof SimulateErrorException) { + throw rte; + } + System.err.println(" got exception " + rte); + } + AirlineGates found_c_1 = airlineGatesService.findById(airlineGates1.getId()); + AirlineGates found_c_2 = airlineGatesService.findById(airlineGates2.getId()); + System.err.println(" found after transferGates: " + found_c_1); + System.err.println(" found after transferGates: " + found_c_2); + Assert.isTrue(found_c_1.getGates().equals(found_b_1.getGates()), "should be same as previous"); + Assert.isTrue(found_c_2.getGates().equals(found_b_2.getGates()), "should be same as previous"); + System.err.println("==============================================================="); + gatesToTransfer = 44; + System.err.println("this transferGates attempt will succeed. transferring " + gatesToTransfer); + try { - airlineGatesService.transferGatesReactive(airlineGates1.getId(), airlineGates2.getId(), gatesToTransfer, null) - .block(); + airlineGatesService.transferGatesReactive(airlineGates1.getId(), airlineGates2.getId(), gatesToTransfer, null) + .block(); - } catch (RuntimeException rte) { - if (!(rte instanceof TransactionSystemUnambiguousException) && rte != null - && rte.getCause() instanceof SimulateErrorException) { - throw rte; - } - System.err.println(" got exception " + rte); - } - AirlineGates found_d_1 = airlineGatesService.findById(airlineGates1.getId()); - AirlineGates found_d_2 = airlineGatesService.findById(airlineGates2.getId()); - System.err.println(" found after transferGates: " + found_d_1); - System.err.println(" found after transferGates: " + found_d_2); - Assert.isTrue(found_d_1.getGates().equals(found_c_1.getGates() - gatesToTransfer), "should have transferred"); - Assert.isTrue(found_d_2.getGates().equals(found_c_2.getGates() + gatesToTransfer), "should have transferred"); - System.err.println("==============================================================="); - } + } catch (RuntimeException rte) { + if (!(rte instanceof TransactionSystemUnambiguousException) && rte != null + && rte.getCause() instanceof SimulateErrorException) { + throw rte; + } + System.err.println(" got exception " + rte); + } + AirlineGates found_d_1 = airlineGatesService.findById(airlineGates1.getId()); + AirlineGates found_d_2 = airlineGatesService.findById(airlineGates2.getId()); + System.err.println(" found after transferGates: " + found_d_1); + System.err.println(" found after transferGates: " + found_d_2); + Assert.isTrue(found_d_1.getGates().equals(found_c_1.getGates() - gatesToTransfer), "should have transferred"); + Assert.isTrue(found_d_2.getGates().equals(found_c_2.getGates() + gatesToTransfer), "should have transferred"); + System.err.println("==============================================================="); + } - static class SimulateErrorException extends RuntimeException { - } + static class SimulateErrorException extends RuntimeException {} } diff --git a/couchbase/transactions/src/main/java/com/example/demo/Config.java b/couchbase/transactions/src/main/java/com/example/demo/Config.java index bcce2bca..8f8685a7 100644 --- a/couchbase/transactions/src/main/java/com/example/demo/Config.java +++ b/couchbase/transactions/src/main/java/com/example/demo/Config.java @@ -31,29 +31,29 @@ import com.couchbase.client.java.transactions.config.TransactionsConfig; @EnableCouchbaseRepositories({ "com.example.demo" }) @EnableTransactionManagement public class Config extends AbstractCouchbaseConfiguration { - @Override - public String getConnectionString() { - return "127.0.0.1"; - } + @Override + public String getConnectionString() { + return "127.0.0.1"; + } - @Override - public String getUserName() { - return "Administrator"; - } + @Override + public String getUserName() { + return "Administrator"; + } - @Override - public String getPassword() { - return "password"; - } + @Override + public String getPassword() { + return "password"; + } - @Override - public String getBucketName() { - return "travel-sample"; - } + @Override + public String getBucketName() { + return "travel-sample"; + } - @Override - public void configureEnvironment(ClusterEnvironment.Builder builder) { - builder.transactionsConfig(TransactionsConfig.durabilityLevel(DurabilityLevel.NONE)); - } + @Override + public void configureEnvironment(ClusterEnvironment.Builder builder) { + builder.transactionsConfig(TransactionsConfig.durabilityLevel(DurabilityLevel.NONE)); + } } diff --git a/couchbase/transactions/src/main/java/com/example/demo/DemoApplication.java b/couchbase/transactions/src/main/java/com/example/demo/DemoApplication.java index 6d6df255..95e8ce5d 100644 --- a/couchbase/transactions/src/main/java/com/example/demo/DemoApplication.java +++ b/couchbase/transactions/src/main/java/com/example/demo/DemoApplication.java @@ -24,8 +24,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { - public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } } diff --git a/couchbase/transactions/src/test/java/com/example/demo/DemoApplicationTests.java b/couchbase/transactions/src/test/java/com/example/demo/DemoApplicationTests.java index 9059b7f2..3cd9e2ac 100644 --- a/couchbase/transactions/src/test/java/com/example/demo/DemoApplicationTests.java +++ b/couchbase/transactions/src/test/java/com/example/demo/DemoApplicationTests.java @@ -15,12 +15,12 @@ */ package com.example.demo; +import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; + import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; - /** * @author Michael Reiche * @author Christoph Strobl @@ -29,7 +29,6 @@ import example.springdata.couchbase.util.EnabledOnCouchbaseAvailable; @EnabledOnCouchbaseAvailable class DemoApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() {} }