diff --git a/couchbase/example/README.md b/couchbase/example/README.md index ab0c8320..2533a2b2 100644 --- a/couchbase/example/README.md +++ b/couchbase/example/README.md @@ -4,7 +4,7 @@ This project contains samples of data access features with Spring Data (Couchbas ## Prerequisites -The examples require a running [Couchbase Server](https://www.couchbase.com/downloads) server with the travel sample bucket imported. We assume you're running Couchbase 6.5 and we have updated the `CouchbaseConfig` class accordingly to adapt RBAC authentication. +The examples require a running [Couchbase Server](https://www.couchbase.com/downloads) server with the travel sample bucket imported. We assume you're running Couchbase 6.5 and we have updated the `application.properties` class accordingly to adapt RBAC authentication. For more information, see the [official documentation](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#reference). diff --git a/couchbase/example/src/main/java/example/springdata/couchbase/model/Airline.java b/couchbase/example/src/main/java/example/springdata/couchbase/model/Airline.java index 1d660b74..6f127f7d 100644 --- a/couchbase/example/src/main/java/example/springdata/couchbase/model/Airline.java +++ b/couchbase/example/src/main/java/example/springdata/couchbase/model/Airline.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,14 +15,13 @@ */ package example.springdata.couchbase.model; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.Version; import org.springframework.data.couchbase.core.mapping.Document; import org.springframework.data.couchbase.core.mapping.Field; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; /** * A domain object representing an Airline @@ -34,21 +33,15 @@ import org.springframework.data.couchbase.core.mapping.Field; @JsonIgnoreProperties(ignoreUnknown = true) public class Airline { - @Id - private String id; + private @Id String id; - @Field private String name; - @Field private String iata; - @Field private String icao; - @Field private String callsign; - @Field private String country; } diff --git a/couchbase/example/src/main/java/example/springdata/couchbase/repository/AirlineRepository.java b/couchbase/example/src/main/java/example/springdata/couchbase/repository/AirlineRepository.java index b76bf748..8a6c16e1 100644 --- a/couchbase/example/src/main/java/example/springdata/couchbase/repository/AirlineRepository.java +++ b/couchbase/example/src/main/java/example/springdata/couchbase/repository/AirlineRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package example.springdata.couchbase.repository; import example.springdata.couchbase.model.Airline; import java.util.List; + import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @@ -28,7 +29,6 @@ import org.springframework.stereotype.Repository; * @author Mark Paluch * @author Denis Rosa */ -@Repository public interface AirlineRepository extends CrudRepository { /** diff --git a/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseConfig.java b/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseConfig.java deleted file mode 100644 index 3ce62537..00000000 --- a/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseConfig.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.couchbase.repository; - -import org.springframework.context.annotation.Configuration; -import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; - -/** - * @author Denis Rosa - * Configuration class to connnect with couchbase - */ -@Configuration -public class CouchbaseConfig extends AbstractCouchbaseConfiguration { - - - @Override - public String getConnectionString() { - return "couchbase://127.0.0.1"; - } - - @Override - public String getUserName() { - return "Administrator"; - } - - @Override - public String getPassword() { - return "password"; - } - - @Override - public String getBucketName() { - return "travel-sample"; - } - - - - @Override - protected boolean autoIndexCreation() { - return true; - } -} diff --git a/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseMain.java b/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseMain.java index 1629210a..fdf27422 100644 --- a/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseMain.java +++ b/couchbase/example/src/main/java/example/springdata/couchbase/repository/CouchbaseMain.java @@ -15,16 +15,17 @@ */ package example.springdata.couchbase.repository; -import com.couchbase.client.java.Cluster; -import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions; import example.springdata.couchbase.model.Airline; import lombok.RequiredArgsConstructor; + import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.couchbase.core.CouchbaseTemplate; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions; /** * Main Class of this module @@ -33,14 +34,11 @@ import org.springframework.data.couchbase.core.CouchbaseTemplate; */ @SpringBootApplication @RequiredArgsConstructor - public class CouchbaseMain { - @Autowired - private final CouchbaseTemplate couchbaseTemplate; + @Autowired private final CouchbaseTemplate couchbaseTemplate; - @Autowired - private Cluster cluster; + @Autowired private Cluster cluster; /** * Add the _class field to all Airline documents @@ -51,7 +49,6 @@ public class CouchbaseMain { CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions().ignoreIfExists(true)); // Need to post-process travel data to add _class attribute - cluster.query("update `travel-sample` set _class='"+Airline.class.getName()+"' where type = 'airline'"); - + cluster.query("update `travel-sample` set _class='" + Airline.class.getName() + "' where type = 'airline'"); } } diff --git a/couchbase/example/src/main/resources/application.properties b/couchbase/example/src/main/resources/application.properties index e900579f..f655413b 100644 --- a/couchbase/example/src/main/resources/application.properties +++ b/couchbase/example/src/main/resources/application.properties @@ -1,4 +1,8 @@ - # Increased timeout to fit slower environments like TravisCI spring.couchbase.env.timeouts.view=15000 spring.couchbase.env.timeouts.query=15000 +spring.couchbase.connection-string=couchbase://127.0.0.1 +spring.couchbase.username=Administrator +spring.couchbase.password=password +spring.data.couchbase.bucket-name=travel-sample +spring.data.couchbase.auto-index=true 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 9753851b..afc5e624 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 @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,21 +43,17 @@ public class AirlineRepositoryIntegrationTests { @ClassRule // public static CouchbaseAvailableRule COUCHBASE = CouchbaseAvailableRule.onLocalhost(); - @Autowired - AirlineRepository airlineRepository; + @Autowired AirlineRepository airlineRepository; @Autowired CouchbaseOperations couchbaseOperations; - - @Before public void before() { - if( couchbaseOperations.existsById().one("LH")) { + if (couchbaseOperations.existsById().one("LH")) { couchbaseOperations.removeById().one("LH"); } } - /** * The derived query executes a N1QL query emitting a single element. */ @@ -88,7 +84,7 @@ public class AirlineRepositoryIntegrationTests { List airlines = airlineRepository.findAllBy(); - assertThat(airlines).hasSize(374); + assertThat(airlines).hasSizeGreaterThan(100); } /** diff --git a/couchbase/pom.xml b/couchbase/pom.xml index 458189c0..d817a9a5 100644 --- a/couchbase/pom.xml +++ b/couchbase/pom.xml @@ -21,11 +21,13 @@ util - - + + org.springframework.data spring-data-couchbase + + diff --git a/couchbase/reactive/README.md b/couchbase/reactive/README.md index e873acd4..d064773d 100644 --- a/couchbase/reactive/README.md +++ b/couchbase/reactive/README.md @@ -8,7 +8,6 @@ This project contains samples of reactive data access features with Spring Data Spring Data Couchbase provides reactive repository support with Project Reactor: ```java -@Repository public interface ReactiveAirlineRepository extends ReactiveCrudRepository { Mono findByIata(String code); diff --git a/couchbase/reactive/src/main/java/example/springdata/couchbase/CouchbaseMain.java b/couchbase/reactive/src/main/java/example/springdata/couchbase/CouchbaseMain.java index 6c356dac..810d975d 100644 --- a/couchbase/reactive/src/main/java/example/springdata/couchbase/CouchbaseMain.java +++ b/couchbase/reactive/src/main/java/example/springdata/couchbase/CouchbaseMain.java @@ -15,19 +15,18 @@ */ package example.springdata.couchbase; -import com.couchbase.client.java.Cluster; -import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions; import example.springdata.couchbase.model.Airline; import lombok.RequiredArgsConstructor; -import java.util.List; - import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.couchbase.core.CouchbaseTemplate; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions; + /** * Main Class of the module * @@ -37,12 +36,9 @@ import org.springframework.data.couchbase.core.CouchbaseTemplate; @RequiredArgsConstructor public class CouchbaseMain { - @Autowired - private final CouchbaseTemplate couchbaseTemplate; - - @Autowired - private Cluster cluster; + @Autowired CouchbaseTemplate couchbaseTemplate; + @Autowired Cluster cluster; @PostConstruct private void postConstruct() { @@ -51,7 +47,7 @@ public class CouchbaseMain { CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions().ignoreIfExists(true)); // Need to post-process travel data to add _class attribute - cluster.query("update `travel-sample` set _class='"+Airline.class.getName()+"' where type = 'airline'"); + cluster.query("update `travel-sample` set _class='" + Airline.class.getName() + "' where type = 'airline'"); } } diff --git a/couchbase/reactive/src/main/java/example/springdata/couchbase/model/Airline.java b/couchbase/reactive/src/main/java/example/springdata/couchbase/model/Airline.java index ff010a4f..76af4a11 100644 --- a/couchbase/reactive/src/main/java/example/springdata/couchbase/model/Airline.java +++ b/couchbase/reactive/src/main/java/example/springdata/couchbase/model/Airline.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,6 @@ import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.couchbase.core.mapping.Document; -import org.springframework.data.couchbase.core.mapping.Field; - /** * A domain object representing an Airline @@ -35,15 +33,15 @@ public class Airline { @Id private String id; - @Field private String type; + private String type; - @Field private String name; + private String name; - @Field private String iata; + private String iata; - @Field private String icao; + private String icao; - @Field private String callsign; + private String callsign; - @Field private String country; + private String country; } diff --git a/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/CouchbaseConfig.java b/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/CouchbaseConfig.java deleted file mode 100644 index 3ce62537..00000000 --- a/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/CouchbaseConfig.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.couchbase.repository; - -import org.springframework.context.annotation.Configuration; -import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; - -/** - * @author Denis Rosa - * Configuration class to connnect with couchbase - */ -@Configuration -public class CouchbaseConfig extends AbstractCouchbaseConfiguration { - - - @Override - public String getConnectionString() { - return "couchbase://127.0.0.1"; - } - - @Override - public String getUserName() { - return "Administrator"; - } - - @Override - public String getPassword() { - return "password"; - } - - @Override - public String getBucketName() { - return "travel-sample"; - } - - - - @Override - protected boolean autoIndexCreation() { - return true; - } -} diff --git a/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/ReactiveAirlineRepository.java b/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/ReactiveAirlineRepository.java index 85638e52..cce90068 100644 --- a/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/ReactiveAirlineRepository.java +++ b/couchbase/reactive/src/main/java/example/springdata/couchbase/repository/ReactiveAirlineRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/couchbase/reactive/src/main/resources/application.properties b/couchbase/reactive/src/main/resources/application.properties index 134a5b33..f655413b 100644 --- a/couchbase/reactive/src/main/resources/application.properties +++ b/couchbase/reactive/src/main/resources/application.properties @@ -1,3 +1,8 @@ # Increased timeout to fit slower environments like TravisCI spring.couchbase.env.timeouts.view=15000 spring.couchbase.env.timeouts.query=15000 +spring.couchbase.connection-string=couchbase://127.0.0.1 +spring.couchbase.username=Administrator +spring.couchbase.password=password +spring.data.couchbase.bucket-name=travel-sample +spring.data.couchbase.auto-index=true 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 4a04a2cf..27e428bf 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 @@ -60,9 +60,11 @@ public class ReactiveAirlineRepositoryIntegrationTests { @Test public void shouldFindAirlineN1ql() { - StepVerifier.create(airlineRepository.findByIata("TQ")).assertNext(it -> { - assertThat(it.getCallsign()).isEqualTo("TXW"); - }).verifyComplete(); + airlineRepository.findByIata("TQ") // + .as(StepVerifier::create) // + .assertNext(it -> { + assertThat(it.getCallsign()).isEqualTo("TXW"); + }).verifyComplete(); } /** @@ -77,10 +79,11 @@ public class ReactiveAirlineRepositoryIntegrationTests { .map(Airline::getId) // .flatMap(airlineRepository::findById); - StepVerifier.create(airline).assertNext(it -> { + airline.as(StepVerifier::create) // + .assertNext(it -> { - assertThat(it.getCallsign()).isEqualTo("TXW"); - }).verifyComplete(); + assertThat(it.getCallsign()).isEqualTo("TXW"); + }).verifyComplete(); } @@ -89,7 +92,12 @@ public class ReactiveAirlineRepositoryIntegrationTests { */ @Test public void shouldFindAll() { - StepVerifier.create(airlineRepository.findAllBy()).expectNextCount(374).verifyComplete(); + airlineRepository.findAllBy().count() // + .as(StepVerifier::create) // + .assertNext(count -> { + + assertThat(count).isGreaterThan(100); + }).verifyComplete(); } /** @@ -112,6 +120,8 @@ public class ReactiveAirlineRepositoryIntegrationTests { .map(Airline::getId) // .flatMap(airlineRepository::findById); - StepVerifier.create(airlineMono).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 4b3cce9f..783f49c4 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 @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,20 +19,18 @@ import static org.assertj.core.api.Assertions.*; import example.springdata.couchbase.model.Airline; import example.springdata.couchbase.util.CouchbaseAvailableRule; -import org.springframework.data.couchbase.core.CouchbaseOperations; -import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations; -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; -import rx.Observable; -import rx.observers.AssertableSubscriber; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.couchbase.core.CouchbaseOperations; +import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations; import org.springframework.test.context.junit4.SpringRunner; /** @@ -67,7 +65,14 @@ public class ReactiveJavaCouchbaseOperationsIntegrationTests { */ @Test public void shouldFindByAll() { - StepVerifier.create( operations.findByQuery( Airline.class).all()).expectNextCount(374).verifyComplete(); + operations.findByQuery(Airline.class).all() // + .count() // + .as(StepVerifier::create) // + .assertNext(count -> { + + assertThat(count).isGreaterThan(100); + }) // + .verifyComplete(); } /** @@ -89,6 +94,7 @@ public class ReactiveJavaCouchbaseOperationsIntegrationTests { .map(Airline::getId) // .flatMap(id -> operations.findById(Airline.class).one(id)); - StepVerifier.create(airlineMono).expectNext(airline).verifyComplete(); + airlineMono.as(StepVerifier::create) // + .expectNext(airline).verifyComplete(); } } diff --git a/pom.xml b/pom.xml index c71bf7cd..1981f6b8 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ bom - couchbase + couchbase elasticsearch jdbc jpa