#265 - Add example for Spring Data Couchbase.
Original pull request: #275.
This commit is contained in:
committed by
Mark Paluch
parent
4b648b1d50
commit
5ba65308a7
28
couchbase/example/pom.xml
Normal file
28
couchbase/example/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-couchbase-example</artifactId>
|
||||
<name>Basic sample for Spring Data Couchbase</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>spring-data-couchbase-examples</artifactId>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,35 @@
|
||||
package example.springdata.couchbase;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Couchbase Configuration to connect to Couchbase data store
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@Configuration
|
||||
@EnableCouchbaseRepositories
|
||||
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
|
||||
|
||||
@Override
|
||||
protected List<String> getBootstrapHosts() {
|
||||
return Collections.singletonList("192.168.99.100");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return "travel-sample";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package example.springdata.couchbase.model;
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Field;
|
||||
import com.couchbase.client.java.repository.annotation.Id;
|
||||
import lombok.*;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
|
||||
/**
|
||||
* A domain object representing an Airline
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Document
|
||||
public class Airline {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
@Field
|
||||
private String type;
|
||||
|
||||
@Field
|
||||
private String name;
|
||||
|
||||
@Field("iata")
|
||||
private String iataCode;
|
||||
|
||||
@Field
|
||||
private String icao;
|
||||
|
||||
@Field
|
||||
private String callsign;
|
||||
|
||||
@Field
|
||||
private String country;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package example.springdata.couchbase.repository;
|
||||
|
||||
import example.springdata.couchbase.model.Airline;
|
||||
import org.springframework.data.couchbase.core.query.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Repository interface to manage {@link Airline} instances.
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
*/
|
||||
public interface AirlineRepository extends CrudRepository<Airline, Integer>{
|
||||
|
||||
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND name = $1")
|
||||
List<Airline> findAirlineByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package example.springdata.couchbase.repository;
|
||||
|
||||
import example.springdata.couchbase.CouchbaseConfiguration;
|
||||
import example.springdata.couchbase.model.Airline;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for basic CRUD operations
|
||||
*
|
||||
* @author Chandana Kithalagama
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = CouchbaseConfiguration.class)
|
||||
public class AirlineRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
AirlineRepository airlineRepository;
|
||||
Airline a, b, c;
|
||||
List<Airline> airlineList;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
a = new Airline(20000,"airline", "CK 1", "LK", "CMB", "CLK","Sri Lanka");
|
||||
b = new Airline(20001,"airline", "CK 2", "LK", "CMB", "CLK","Sri Lanka");
|
||||
c = new Airline(20002,"airline", "CK 3", "LK", "CMB", "CLK","Sri Lanka");
|
||||
|
||||
airlineList = Arrays.asList(a, b, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAirline() {
|
||||
Iterable<Airline> itr = airlineRepository.save(Arrays.asList(a));
|
||||
assertThat(itr).isNotNull();
|
||||
Airline airline = airlineRepository.findOne(a.getId());
|
||||
assertThat(airline).isNotNull();
|
||||
assertThat(airline).isEqualTo(a);
|
||||
assertThat(airline).isNotEqualTo(b);
|
||||
airlineRepository.delete(a.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllAirlines() {
|
||||
Iterable<Airline> itr = airlineRepository.save(airlineList);
|
||||
assertThat(itr).isNotNull();
|
||||
Iterable<Airline> itr2 = airlineRepository.findAll(Arrays.asList(a.getId(), b.getId(), c.getId()));
|
||||
assertThat(itr2).isNotNull();
|
||||
airlineRepository.delete(airlineList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteAirlines() {
|
||||
Iterable<Airline> itr = airlineRepository.save(Arrays.asList(a));
|
||||
assertThat(itr).isNotNull();
|
||||
airlineRepository.delete(a.getId());
|
||||
Airline airline = airlineRepository.findOne(a.getId());
|
||||
assertThat(airline).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByName() {
|
||||
Iterable<Airline> itr = airlineRepository.save(Arrays.asList(a));
|
||||
assertThat(itr).isNotNull();
|
||||
List<Airline> airlines = airlineRepository.findAirlineByName(a.getName());
|
||||
assertThat(airlines.size()).isGreaterThanOrEqualTo(1);
|
||||
assertThat(airlines.get(0)).isEqualTo(a);
|
||||
airlineRepository.delete(a.getId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
airlineRepository.delete(airlineList);
|
||||
}
|
||||
}
|
||||
30
couchbase/pom.xml
Normal file
30
couchbase/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-couchbase-examples</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>spring-data-examples</artifactId>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data Couchbase - Examples</name>
|
||||
<description>Sample projects for Spring Data Couchbase</description>
|
||||
<url>https://github.com/spring-projects/spring-data-couchbase</url>
|
||||
|
||||
<modules>
|
||||
<module>example</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-couchbase</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
Reference in New Issue
Block a user