#265 - Add reactive examples.

Original pull request: #275.
This commit is contained in:
Mark Paluch
2017-11-22 17:20:20 +01:00
parent b61875f97c
commit bd7b5e553b
11 changed files with 710 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2017 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 static org.assertj.core.api.Assertions.*;
import example.springdata.couchbase.model.Airline;
import example.springdata.couchbase.util.CouchbaseAvailableRule;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
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.test.context.junit4.SpringRunner;
/**
* Integration tests showing basic CRUD operations through {@link ReactiveAirlineRepository}
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReactiveAirlineRepositoryIntegrationTests {
@ClassRule //
public static CouchbaseAvailableRule COUCHBASE = CouchbaseAvailableRule.onLocalhost();
@Autowired ReactiveAirlineRepository airlineRepository;
@Autowired CouchbaseOperations couchbaseOperations;
@Before
public void before() {
Airline toDelete = couchbaseOperations.findById("LH", Airline.class);
if (toDelete != null) {
couchbaseOperations.remove(toDelete);
}
}
/**
* The derived query executes a N1QL query emitting a single element.
*/
@Test
public void shouldFindAirlineN1ql() {
StepVerifier.create(airlineRepository.findAirlineByIataCode("TQ")).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() {
Mono<Airline> airline = airlineRepository.findAirlineByIataCode("TQ") //
.map(Airline::getId) //
.flatMap(airlineRepository::findById);
StepVerifier.create(airline).assertNext(it -> {
assertThat(it.getCallsign()).isEqualTo("TXW");
}).verifyComplete();
StepVerifier.create(airlineRepository.findById("unknown")).verifyComplete();
}
/**
* Find all {@link Airline}s applying the {@code airlines/all} view.
*/
@Test
public void shouldFindByView() {
StepVerifier.create(airlineRepository.findAllBy()).expectNextCount(187).verifyComplete();
}
/**
* 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.setId("LH");
airline.setIataCode("LH");
airline.setIcao("DLH");
airline.setCallsign("Lufthansa");
airline.setName("Lufthansa");
airline.setCountry("Germany");
Mono<Airline> airlineMono = airlineRepository.save(airline) //
.map(Airline::getId) //
.flatMap(airlineRepository::findById);
StepVerifier.create(airlineMono).expectNext(airline).verifyComplete();
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright 2017 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 static org.assertj.core.api.Assertions.*;
import example.springdata.couchbase.model.Airline;
import example.springdata.couchbase.util.CouchbaseAvailableRule;
import rx.Single;
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.test.context.junit4.SpringRunner;
/**
* Integration tests showing basic CRUD operations through {@link RxJava1AirlineRepository}
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RxJava1AirlineRepositoryIntegrationTests {
@ClassRule //
public static CouchbaseAvailableRule COUCHBASE = CouchbaseAvailableRule.onLocalhost();
@Autowired RxJava1AirlineRepository airlineRepository;
@Autowired CouchbaseOperations couchbaseOperations;
@Before
public void before() {
Airline toDelete = couchbaseOperations.findById("LH", Airline.class);
if (toDelete != null) {
couchbaseOperations.remove(toDelete);
}
}
/**
* The derived query executes a N1QL query emitting a single element.
*/
@Test
public void shouldFindAirlineN1ql() {
AssertableSubscriber<Airline> subscriber = airlineRepository.findAirlineByIataCode("TQ") //
.test() //
.awaitTerminalEvent() //
.assertCompleted();
assertThat(subscriber.getValueCount()).isEqualTo(1);
assertThat(subscriber.getOnNextEvents().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() {
Single<Airline> airline = airlineRepository.findAirlineByIataCode("TQ") //
.map(Airline::getId) //
.flatMap(airlineRepository::findById);
AssertableSubscriber<Airline> subscriber = airline.test().awaitTerminalEvent().assertCompleted();
assertThat(subscriber.getValueCount()).isEqualTo(1);
assertThat(subscriber.getOnNextEvents().get(0).getCallsign()).isEqualTo("TXW");
airlineRepository.findById("unknown").test().awaitTerminalEvent().assertNoValues();
}
/**
* Find all {@link Airline}s applying the {@code airlines/all} view.
*/
@Test
public void shouldFindByView() {
airlineRepository.findAllBy().test().awaitTerminalEvent().assertValueCount(187);
}
/**
* 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.setId("LH");
airline.setIataCode("LH");
airline.setIcao("DLH");
airline.setCallsign("Lufthansa");
airline.setName("Lufthansa");
airline.setCountry("Germany");
Single<Airline> single = airlineRepository.save(airline) //
.map(Airline::getId) //
.flatMap(airlineRepository::findById);
single.test().awaitTerminalEvent().assertResult(airline);
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2017 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.template;
import static org.assertj.core.api.Assertions.*;
import example.springdata.couchbase.model.Airline;
import example.springdata.couchbase.util.CouchbaseAvailableRule;
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.RxJavaCouchbaseOperations;
import org.springframework.test.context.junit4.SpringRunner;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.view.ViewQuery;
/**
* Integration tests showing basic CRUD operations through
* {@link org.springframework.data.couchbase.core.RxJavaCouchbaseOperations}.
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RxJavaCouchbaseOperationsIntegrationTests {
@ClassRule //
public static CouchbaseAvailableRule COUCHBASE = CouchbaseAvailableRule.onLocalhost();
@Autowired RxJavaCouchbaseOperations operations;
@Before
public void before() {
operations.findById("LH", Airline.class).flatMap(operations::remove).test().awaitTerminalEvent();
}
/**
* The derived query executes a N1QL query emitting a single element.
*/
@Test
public void shouldFindAirlineN1ql() {
String n1ql = "SELECT META(`travel-sample`).id AS _ID, META(`travel-sample`).cas AS _CAS, `travel-sample`.* " + //
"FROM `travel-sample` " + //
"WHERE (`iata` = \"TQ\") AND `_class` = \"example.springdata.couchbase.model.Airline\"";
AssertableSubscriber<Airline> subscriber = operations.findByN1QL(N1qlQuery.simple(n1ql), Airline.class) //
.test() //
.awaitTerminalEvent() //
.assertCompleted();
assertThat(subscriber.getOnNextEvents()).hasSize(1);
assertThat(subscriber.getOnNextEvents().get(0).getCallsign()).isEqualTo("TXW");
}
/**
* Find all {@link Airline}s applying the {@code airlines/all} view.
*/
@Test
public void shouldFindByView() {
Observable<Airline> airlines = operations.findByView(ViewQuery.from("airlines", "all"), Airline.class);
airlines.test().awaitTerminalEvent().assertValueCount(187);
}
/**
* Created elements are emitted by {@link RxJavaCouchbaseOperations#save(Object)}.
*/
@Test
public void shouldCreateAirline() {
Airline airline = new Airline();
airline.setId("LH");
airline.setIataCode("LH");
airline.setIcao("DLH");
airline.setCallsign("Lufthansa");
airline.setName("Lufthansa");
airline.setCountry("Germany");
Observable<Airline> single = operations.save(airline) //
.map(Airline::getId) //
.flatMap(id -> operations.findById(id, Airline.class));
single.test().awaitTerminalEvent().assertResult(airline);
}
}