#562 - Upgrading to Spring Data Couchbase 4.
Original pull request: #564.
This commit is contained in:
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
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.couchbase.client.java.repository.annotation.Field;
|
||||
import com.couchbase.client.java.repository.annotation.Id;
|
||||
|
||||
/**
|
||||
* A domain object representing an Airline
|
||||
@@ -29,19 +31,17 @@ import com.couchbase.client.java.repository.annotation.Id;
|
||||
*/
|
||||
@Data
|
||||
@Document
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Airline {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field
|
||||
private String type;
|
||||
|
||||
@Field
|
||||
private String name;
|
||||
|
||||
@Field("iata")
|
||||
private String iataCode;
|
||||
@Field
|
||||
private String iata;
|
||||
|
||||
@Field
|
||||
private String icao;
|
||||
|
||||
@@ -18,20 +18,17 @@ package example.springdata.couchbase.repository;
|
||||
import example.springdata.couchbase.model.Airline;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
|
||||
import org.springframework.data.couchbase.core.query.View;
|
||||
import org.springframework.data.couchbase.core.query.ViewIndexed;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* Repository interface to manage {@link Airline} instances.
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
* @author Mark Paluch
|
||||
* @author Denis Rosa
|
||||
*/
|
||||
@N1qlPrimaryIndexed
|
||||
@ViewIndexed(designDoc = "airlines")
|
||||
@Repository
|
||||
public interface AirlineRepository extends CrudRepository<Airline, String> {
|
||||
|
||||
/**
|
||||
@@ -40,13 +37,12 @@ public interface AirlineRepository extends CrudRepository<Airline, String> {
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
Airline findAirlineByIataCode(String code);
|
||||
List<Airline> findByIata(String code);
|
||||
|
||||
/**
|
||||
* Query method using {@code airlines/all} view.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@View(designDocument = "airlines", viewName = "all")
|
||||
List<Airline> findAllBy();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017-2018 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
|
||||
*
|
||||
* https://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 example.springdata.couchbase.model.Airline;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.couchbase.config.BeanNames;
|
||||
import org.springframework.data.couchbase.core.CouchbaseOperations;
|
||||
import org.springframework.data.couchbase.repository.support.IndexManager;
|
||||
|
||||
import com.couchbase.client.java.query.N1qlQuery;
|
||||
|
||||
/**
|
||||
* Simple configuration class.
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@RequiredArgsConstructor
|
||||
public class CouchbaseConfiguration {
|
||||
|
||||
private final CouchbaseOperations couchbaseOperations;
|
||||
|
||||
/**
|
||||
* Create an {@link IndexManager} that allows index creation.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = BeanNames.COUCHBASE_INDEX_MANAGER)
|
||||
public IndexManager indexManager() {
|
||||
return new IndexManager(true, true, false);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void postConstruct() {
|
||||
|
||||
// Need to post-process travel data to add _class attribute
|
||||
List<Airline> airlinesWithoutClassAttribute = couchbaseOperations.findByN1QL(N1qlQuery.simple( //
|
||||
"SELECT META(`travel-sample`).id AS _ID, META(`travel-sample`).cas AS _CAS, `travel-sample`.* " + //
|
||||
"FROM `travel-sample` " + //
|
||||
"WHERE type = \"airline\" AND _class IS MISSING;"),
|
||||
Airline.class);
|
||||
|
||||
airlinesWithoutClassAttribute.forEach(couchbaseOperations::save);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 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;
|
||||
|
||||
|
||||
/**
|
||||
* Main Class of this module
|
||||
*
|
||||
* @author Denis Rosa
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@RequiredArgsConstructor
|
||||
|
||||
public class CouchbaseMain {
|
||||
|
||||
@Autowired
|
||||
private final CouchbaseTemplate couchbaseTemplate;
|
||||
|
||||
@Autowired
|
||||
private Cluster cluster;
|
||||
|
||||
/**
|
||||
* Add the _class field to all Airline documents
|
||||
*/
|
||||
@PostConstruct
|
||||
private void postConstruct() {
|
||||
cluster.queryIndexes().createPrimaryIndex(couchbaseTemplate.getBucketName(),
|
||||
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'");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,3 @@
|
||||
spring.couchbase.bucket.name=travel-sample
|
||||
spring.couchbase.bootstrap-hosts=localhost
|
||||
|
||||
# Required for Couchbase 5
|
||||
spring.couchbase.bucket.password=password
|
||||
|
||||
# Increased timeout to fit slower environments like TravisCI
|
||||
spring.couchbase.env.timeouts.view=15000
|
||||
|
||||
@@ -34,8 +34,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
/**
|
||||
* Integration tests showing basic CRUD operations through {@link AirlineRepository}.
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
* @author Mark Paluch
|
||||
* @author Denis Rosa
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@@ -49,20 +48,24 @@ public class AirlineRepositoryIntegrationTests {
|
||||
|
||||
@Autowired CouchbaseOperations couchbaseOperations;
|
||||
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
airlineRepository.findById("LH").ifPresent(couchbaseOperations::remove);
|
||||
if( couchbaseOperations.existsById().one("LH")) {
|
||||
couchbaseOperations.removeById().one("LH");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The derived query executes a N1QL query emitting a single element.
|
||||
*/
|
||||
@Test
|
||||
public void shouldFindAirlineN1ql() {
|
||||
|
||||
Airline airline = airlineRepository.findAirlineByIataCode("TQ");
|
||||
|
||||
assertThat(airline.getCallsign()).isEqualTo("TXW");
|
||||
List<Airline> airlines = airlineRepository.findByIata("TQ");
|
||||
assertThat(airlines.get(0).getCallsign()).isEqualTo("TXW");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,10 +76,8 @@ public class AirlineRepositoryIntegrationTests {
|
||||
@Test
|
||||
public void shouldFindById() {
|
||||
|
||||
Airline airline = airlineRepository.findAirlineByIataCode("TQ");
|
||||
|
||||
assertThat(airlineRepository.findById(airline.getId())).contains(airline);
|
||||
assertThat(airlineRepository.findById("unknown")).isEmpty();
|
||||
Airline airline = airlineRepository.findByIata("TQ").get(0);
|
||||
assertThat(airlineRepository.findById(airline.getId()).isPresent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,7 +88,7 @@ public class AirlineRepositoryIntegrationTests {
|
||||
|
||||
List<Airline> airlines = airlineRepository.findAllBy();
|
||||
|
||||
assertThat(airlines).hasSize(187);
|
||||
assertThat(airlines).hasSize(374);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,7 +101,7 @@ public class AirlineRepositoryIntegrationTests {
|
||||
Airline airline = new Airline();
|
||||
|
||||
airline.setId("LH");
|
||||
airline.setIataCode("LH");
|
||||
airline.setIata("LH");
|
||||
airline.setIcao("DLH");
|
||||
airline.setCallsign("Lufthansa");
|
||||
airline.setName("Lufthansa");
|
||||
|
||||
Reference in New Issue
Block a user