Adding source code for tutorial tracked by BAEL-2971 (#7250)

* Adding source code for tutorial tracked by BAEL-2971

* Renaming Integration Test as par standard
This commit is contained in:
Kumar Chandrakant 2019-07-10 20:22:01 +05:30 committed by Grzegorz Piwowarek
parent 62d9845b91
commit 7ebf52f409
8 changed files with 408 additions and 0 deletions

3
morphia/README.md Normal file
View File

@ -0,0 +1,3 @@
### Relevant Articles:
- [Intro to Morphia](http://www.baeldung.com/intro-to-morphia)

43
morphia/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<groupId>com.baeldung.morphia</groupId>
<artifactId>morphia</artifactId>
<name>morphia</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>core</artifactId>
<version>${morphia.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<spring-boot-maven-plugin.version>1.4.2.RELEASE</spring-boot-maven-plugin.version>
<morphia.version>1.5.3</morphia.version>
</properties>
</project>

View File

@ -0,0 +1,31 @@
package com.baeldung.morphia.domain;
import java.util.List;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
@Entity
public class Author {
@Id
private String name;
private List<String> books;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
}

View File

@ -0,0 +1,116 @@
package com.baeldung.morphia.domain;
import java.util.HashSet;
import java.util.Set;
import dev.morphia.annotations.Embedded;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Field;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Index;
import dev.morphia.annotations.IndexOptions;
import dev.morphia.annotations.Indexes;
import dev.morphia.annotations.Property;
import dev.morphia.annotations.Reference;
import dev.morphia.annotations.Validation;
@Entity("Books")
@Indexes({ @Index(fields = @Field("title"), options = @IndexOptions(name = "book_title")) })
@Validation("{ price : { $gt : 0 } }")
public class Book {
@Id
private String isbn;
@Property
private String title;
private String author;
@Embedded
private Publisher publisher;
@Property("price")
private double cost;
@Reference
private Set<Book> companionBooks;
public Book() {
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getCost() {
return cost;
}
public void addCompanionBooks(Book book) {
if (companionBooks != null)
this.companionBooks.add(book);
}
public Book(String isbn, String title, String author, double cost, Publisher publisher) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.cost = cost;
this.publisher = publisher;
this.companionBooks = new HashSet<>();
}
@Override
public String toString() {
return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", cost=" + cost + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
long temp;
temp = Double.doubleToLongBits(cost);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
result = prime * result + ((publisher == null) ? 0 : publisher.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (Double.doubleToLongBits(cost) != Double.doubleToLongBits(other.cost))
return false;
if (isbn == null) {
if (other.isbn != null)
return false;
} else if (!isbn.equals(other.isbn))
return false;
if (publisher == null) {
if (other.publisher != null)
return false;
} else if (!publisher.equals(other.publisher))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.morphia.domain;
import org.bson.types.ObjectId;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
@Entity
public class Publisher {
@Id
private ObjectId id;
private String name;
public Publisher() {
}
public Publisher(ObjectId id, String name) {
this.id = id;
this.name = name;
}
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Catalog [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Publisher other = (Publisher) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,130 @@
package com.baeldung.morphia;
import static dev.morphia.aggregation.Group.grouping;
import static dev.morphia.aggregation.Group.push;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Iterator;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import com.baeldung.morphia.domain.Author;
import com.baeldung.morphia.domain.Book;
import com.baeldung.morphia.domain.Publisher;
import com.mongodb.MongoClient;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import dev.morphia.query.Query;
import dev.morphia.query.UpdateOperations;
@Ignore
public class MorphiaIntegrationTest {
private static Datastore datastore;
private static ObjectId id = new ObjectId();
@BeforeClass
public static void setUp() {
Morphia morphia = new Morphia();
morphia.mapPackage("com.baeldung.morphia");
datastore = morphia.createDatastore(new MongoClient(), "library");
datastore.ensureIndexes();
}
@Test
public void givenDataSource_whenCreateEntity_thenEntityCreated() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
Book companionBook = new Book("9789332575103", "Java Performance Companion", "Tom Kirkman", 1.95, publisher);
book.addCompanionBooks(companionBook);
datastore.save(companionBook);
datastore.save(book);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.find()
.toList();
assertEquals(books.size(), 1);
assertEquals(books.get(0), book);
}
@Test
public void givenDocument_whenUpdated_thenUpdateReflected() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
datastore.save(book);
Query<Book> query = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java");
UpdateOperations<Book> updates = datastore.createUpdateOperations(Book.class)
.inc("price", 1);
datastore.update(query, updates);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.find()
.toList();
assertEquals(books.get(0)
.getCost(), 4.95);
}
@Test
public void givenDocument_whenDeleted_thenDeleteReflected() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
datastore.save(book);
Query<Book> query = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java");
datastore.delete(query);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.find()
.toList();
assertEquals(books.size(), 0);
}
@Test
public void givenDocument_whenAggregated_thenResultsCollected() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
datastore.save(new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher));
datastore.save(new Book("9781449313142", "Learning Perl", "Mark Pence", 2.95, publisher));
datastore.save(new Book("9787564100476", "Learning Python", "Mark Pence", 5.95, publisher));
datastore.save(new Book("9781449368814", "Learning Scala", "Mark Pence", 6.95, publisher));
datastore.save(new Book("9781784392338", "Learning Go", "Jonathan Sawyer", 8.95, publisher));
Iterator<Author> authors = datastore.createAggregation(Book.class)
.group("author", grouping("books", push("title")))
.out(Author.class);
assertTrue(authors.hasNext());
}
@Test
public void givenDocument_whenProjected_thenOnlyProjectionReceived() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
datastore.save(book);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.project("title", true)
.find()
.toList();
assertEquals(books.size(), 1);
assertEquals("Learning Java", books.get(0)
.getTitle());
assertEquals(null, books.get(0)
.getAuthor());
}
}

View File

@ -558,6 +558,7 @@
<module>tensorflow-java</module>
<module>spring-boot-flowable</module>
<module>spring-security-kerberos</module>
<module>morphia</module>
</modules>
@ -791,6 +792,7 @@
<module>tensorflow-java</module>
<module>spring-boot-flowable</module>
<module>spring-security-kerberos</module>
<module>morphia</module>
</modules>