BAEL-5009 Spring Data with ArangoDB (#11115)
* BAEL-5009 Spring Data with ArangoDB * BAEL-5009 Spring Data with ArangoDB * BAEL-5009 Spring Data with ArangoDB * BAEL-5009 Spring Data with ArangoDB * BAEL-5009 Spring Data with ArangoDB * BAEL-5009 Spring Data with ArangoDB Co-authored-by: majewsk6 <krzysztof.majewski.km1@contractors.roche.com>
This commit is contained in:
parent
1847bd8333
commit
439b5a4fde
|
@ -57,6 +57,7 @@
|
||||||
<module>spring-boot-persistence</module>
|
<module>spring-boot-persistence</module>
|
||||||
<module>spring-boot-persistence-h2</module>
|
<module>spring-boot-persistence-h2</module>
|
||||||
<module>spring-boot-persistence-mongodb</module>
|
<module>spring-boot-persistence-mongodb</module>
|
||||||
|
<module>spring-data-arangodb</module>
|
||||||
<module>spring-data-cassandra</module>
|
<module>spring-data-cassandra</module>
|
||||||
<module>spring-data-cassandra-test</module>
|
<module>spring-data-cassandra-test</module>
|
||||||
<module>spring-data-cassandra-reactive</module>
|
<module>spring-data-cassandra-reactive</module>
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
=========
|
||||||
|
|
||||||
|
## Spring Data ArangoDB
|
||||||
|
|
||||||
|
|
||||||
|
### Relevant Articles:
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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-arangodb</artifactId>
|
||||||
|
<name>spring-data-arangodb</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.arangodb</groupId>
|
||||||
|
<artifactId>arangodb-spring-data</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,7 @@
|
||||||
|
FROM arangodb:3.8.0
|
||||||
|
|
||||||
|
COPY init-session.js /docker-entrypoint-initdb.d/
|
||||||
|
|
||||||
|
EXPOSE 8529
|
||||||
|
|
||||||
|
ENV ARANGO_ROOT_PASSWORD=password
|
|
@ -0,0 +1 @@
|
||||||
|
rs.initiate();
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
docker image build -t spring-data-arangodb:live-test .
|
||||||
|
|
||||||
|
docker run -p 8529:8529 -e ARANGO_ROOT_PASSWORD=password --name spring-data-arangodb-live-test spring-data-arangodb:live-test
|
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
docker stop spring-data-arangodb-live-test
|
||||||
|
docker rm spring-data-arangodb-live-test
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
mvn clean compile test -P live-all -f ../../../pom.xml
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.arangodb;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ArangoDbSpringDataApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ArangoDbSpringDataApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.arangodb.configuration;
|
||||||
|
|
||||||
|
import com.arangodb.ArangoDB;
|
||||||
|
import com.arangodb.springframework.annotation.EnableArangoRepositories;
|
||||||
|
import com.arangodb.springframework.config.ArangoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableArangoRepositories(basePackages = {"com.baeldung"})
|
||||||
|
public class ArangoDbConfiguration implements ArangoConfiguration {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArangoDB.Builder arango() {
|
||||||
|
return new ArangoDB.Builder()
|
||||||
|
.host("127.0.0.1", 8529)
|
||||||
|
.user("root")
|
||||||
|
.password("password");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String database() {
|
||||||
|
return "baeldung-database";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package com.baeldung.arangodb.model;
|
||||||
|
|
||||||
|
import com.arangodb.springframework.annotation.ArangoId;
|
||||||
|
import com.arangodb.springframework.annotation.Document;
|
||||||
|
import com.arangodb.springframework.annotation.Relations;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@Document("articles")
|
||||||
|
public class Article {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@ArangoId
|
||||||
|
private String arangoId;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String author;
|
||||||
|
private ZonedDateTime publishDate;
|
||||||
|
private String htmlContent;
|
||||||
|
|
||||||
|
@Relations(edges = ArticleLink.class, lazy = true)
|
||||||
|
private Collection<Author> authors;
|
||||||
|
|
||||||
|
public Article() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Article(String name, String author, ZonedDateTime publishDate, String htmlContent) {
|
||||||
|
this.name = name;
|
||||||
|
this.author = author;
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
this.htmlContent = htmlContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getArangoId() {
|
||||||
|
return arangoId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArangoId(String arangoId) {
|
||||||
|
this.arangoId = arangoId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZonedDateTime getPublishDate() {
|
||||||
|
return publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublishDate(ZonedDateTime publishDate) {
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHtmlContent() {
|
||||||
|
return htmlContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHtmlContent(String htmlContent) {
|
||||||
|
this.htmlContent = htmlContent;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.arangodb.model;
|
||||||
|
|
||||||
|
import com.arangodb.springframework.annotation.Edge;
|
||||||
|
import com.arangodb.springframework.annotation.From;
|
||||||
|
import com.arangodb.springframework.annotation.To;
|
||||||
|
|
||||||
|
@Edge
|
||||||
|
public class ArticleLink {
|
||||||
|
|
||||||
|
@From
|
||||||
|
private Article article;
|
||||||
|
|
||||||
|
@To
|
||||||
|
private Author author;
|
||||||
|
|
||||||
|
public ArticleLink() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArticleLink(Article article, Author author) {
|
||||||
|
this.article = article;
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Article getArticle() {
|
||||||
|
return article;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArticle(Article article) {
|
||||||
|
this.article = article;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Author getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(Author author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.baeldung.arangodb.model;
|
||||||
|
|
||||||
|
import com.arangodb.springframework.annotation.ArangoId;
|
||||||
|
import com.arangodb.springframework.annotation.Document;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
@Document("articles")
|
||||||
|
public class Author {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@ArangoId
|
||||||
|
private String arangoId;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Author() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Author(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getArangoId() {
|
||||||
|
return arangoId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArangoId(String arangoId) {
|
||||||
|
this.arangoId = arangoId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.arangodb.repository;
|
||||||
|
|
||||||
|
import com.arangodb.springframework.annotation.Query;
|
||||||
|
import com.arangodb.springframework.repository.ArangoRepository;
|
||||||
|
import com.baeldung.arangodb.model.Article;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface ArticleRepository extends ArangoRepository<Article, String> {
|
||||||
|
|
||||||
|
Iterable<Article> findByAuthor(String author);
|
||||||
|
|
||||||
|
@Query("FOR a IN articles FILTER a.author == @author SORT a.publishDate ASC RETURN a")
|
||||||
|
Iterable<Article> getByAuthor(@Param("author") String author);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
arangodb.hosts=127.0.0.1:8529
|
||||||
|
arangodb.user=root
|
||||||
|
arangodb.password=password
|
|
@ -0,0 +1,113 @@
|
||||||
|
package com.baeldung.arangodb;
|
||||||
|
|
||||||
|
import com.baeldung.arangodb.model.Article;
|
||||||
|
import com.baeldung.arangodb.repository.ArticleRepository;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class ArticleRepositoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ArticleRepository articleRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNewArticle_whenSaveInArangoDb_thenDataIsCorrect() {
|
||||||
|
Article newArticle = new Article(
|
||||||
|
"ArangoDb with Spring Data",
|
||||||
|
"Baeldung Writer",
|
||||||
|
ZonedDateTime.now(),
|
||||||
|
"<html>Some HTML content</html>"
|
||||||
|
);
|
||||||
|
|
||||||
|
Article savedArticle = articleRepository.save(newArticle);
|
||||||
|
|
||||||
|
assertNotNull(savedArticle.getId());
|
||||||
|
assertNotNull(savedArticle.getArangoId());
|
||||||
|
|
||||||
|
assertEquals(savedArticle.getName(), newArticle.getName());
|
||||||
|
assertEquals(savedArticle.getAuthor(), newArticle.getAuthor());
|
||||||
|
assertEquals(savedArticle.getPublishDate(), newArticle.getPublishDate());
|
||||||
|
assertEquals(savedArticle.getHtmlContent(), newArticle.getHtmlContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArticleId_whenReadFromArangoDb_thenDataIsCorrect() {
|
||||||
|
Article newArticle = new Article(
|
||||||
|
"ArangoDb with Spring Data",
|
||||||
|
"Baeldung Writer",
|
||||||
|
ZonedDateTime.now(),
|
||||||
|
"<html>Some HTML content</html>"
|
||||||
|
);
|
||||||
|
|
||||||
|
Article savedArticle = articleRepository.save(newArticle);
|
||||||
|
|
||||||
|
String articleId = savedArticle.getId();
|
||||||
|
|
||||||
|
Optional<Article> article = articleRepository.findById(articleId);
|
||||||
|
assertTrue(article.isPresent());
|
||||||
|
|
||||||
|
Article foundArticle = article.get();
|
||||||
|
|
||||||
|
assertEquals(foundArticle.getId(), articleId);
|
||||||
|
assertEquals(foundArticle.getArangoId(), savedArticle.getArangoId());
|
||||||
|
assertEquals(foundArticle.getName(), savedArticle.getName());
|
||||||
|
assertEquals(foundArticle.getAuthor(), savedArticle.getAuthor());
|
||||||
|
assertEquals(foundArticle.getPublishDate(), savedArticle.getPublishDate());
|
||||||
|
assertEquals(foundArticle.getHtmlContent(), savedArticle.getHtmlContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArticleId_whenDeleteFromArangoDb_thenDataIsGone() {
|
||||||
|
Article newArticle = new Article(
|
||||||
|
"ArangoDb with Spring Data",
|
||||||
|
"Baeldung Writer",
|
||||||
|
ZonedDateTime.now(),
|
||||||
|
"<html>Some HTML content</html>"
|
||||||
|
);
|
||||||
|
|
||||||
|
Article savedArticle = articleRepository.save(newArticle);
|
||||||
|
|
||||||
|
String articleId = savedArticle.getId();
|
||||||
|
|
||||||
|
articleRepository.deleteById(articleId);
|
||||||
|
|
||||||
|
Optional<Article> article = articleRepository.findById(articleId);
|
||||||
|
assertFalse(article.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAuthorName_whenGetByAuthor_thenListOfArticles() {
|
||||||
|
Article newArticle = new Article(
|
||||||
|
"ArangoDb with Spring Data",
|
||||||
|
"Baeldung Writer",
|
||||||
|
ZonedDateTime.now(),
|
||||||
|
"<html>Some HTML content</html>"
|
||||||
|
);
|
||||||
|
articleRepository.save(newArticle);
|
||||||
|
|
||||||
|
Iterable<Article> articlesByAuthor = articleRepository.findByAuthor(newArticle.getAuthor());
|
||||||
|
List<Article> articlesByAuthorList = new ArrayList<>();
|
||||||
|
articlesByAuthor.forEach(articlesByAuthorList::add);
|
||||||
|
|
||||||
|
assertEquals(1, articlesByAuthorList.size());
|
||||||
|
|
||||||
|
Article foundArticle = articlesByAuthorList.get(0);
|
||||||
|
assertEquals(foundArticle.getName(), newArticle.getName());
|
||||||
|
assertEquals(foundArticle.getAuthor(), newArticle.getAuthor());
|
||||||
|
assertEquals(foundArticle.getPublishDate(), newArticle.getPublishDate());
|
||||||
|
assertEquals(foundArticle.getHtmlContent(), newArticle.getHtmlContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue