2017-09-21 02:01:43 +01:00
|
|
|
package com.baeldung.apachecayenne;
|
|
|
|
|
|
|
|
import com.baeldung.apachecayenne.persistent.Article;
|
|
|
|
import com.baeldung.apachecayenne.persistent.Author;
|
|
|
|
import org.apache.cayenne.ObjectContext;
|
|
|
|
import org.apache.cayenne.configuration.server.ServerRuntime;
|
|
|
|
import org.apache.cayenne.query.ObjectSelect;
|
|
|
|
import org.apache.cayenne.query.SQLTemplate;
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static junit.framework.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
|
|
|
|
|
2018-06-05 17:05:55 +05:30
|
|
|
public class CayenneOperationLiveTest {
|
2017-09-21 02:01:43 +01:00
|
|
|
private static ObjectContext context = null;
|
|
|
|
|
|
|
|
@BeforeClass
|
2017-09-23 07:22:07 +01:00
|
|
|
public static void setupTheCayenneContext() {
|
2017-09-21 02:01:43 +01:00
|
|
|
ServerRuntime cayenneRuntime = ServerRuntime.builder()
|
|
|
|
.addConfig("cayenne-project.xml")
|
|
|
|
.build();
|
|
|
|
context = cayenneRuntime.newContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
2017-09-23 07:22:07 +01:00
|
|
|
public void deleteAllRecords() {
|
2017-09-21 02:01:43 +01:00
|
|
|
SQLTemplate deleteArticles = new SQLTemplate(Article.class, "delete from article");
|
|
|
|
SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author");
|
|
|
|
|
|
|
|
context.performGenericQuery(deleteArticles);
|
|
|
|
context.performGenericQuery(deleteAuthors);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-09-23 07:22:07 +01:00
|
|
|
public void givenAuthor_whenInsert_thenWeGetOneRecordInTheDatabase() {
|
2017-09-21 02:01:43 +01:00
|
|
|
Author author = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
author.setName("Paul");
|
2017-09-21 02:01:43 +01:00
|
|
|
|
|
|
|
context.commitChanges();
|
|
|
|
|
|
|
|
long records = ObjectSelect.dataRowQuery(Author.class).selectCount(context);
|
|
|
|
assertEquals(1, records);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-09-23 07:22:07 +01:00
|
|
|
public void givenAuthor_whenInsert_andQueryByFirstName_thenWeGetTheAuthor() {
|
2017-09-21 02:01:43 +01:00
|
|
|
Author author = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
author.setName("Paul");
|
2017-09-21 02:01:43 +01:00
|
|
|
|
|
|
|
context.commitChanges();
|
|
|
|
|
|
|
|
Author expectedAuthor = ObjectSelect.query(Author.class)
|
2017-09-24 18:32:50 +01:00
|
|
|
.where(Author.NAME.eq("Paul"))
|
2017-09-21 02:01:43 +01:00
|
|
|
.selectOne(context);
|
|
|
|
|
2017-09-24 18:32:50 +01:00
|
|
|
assertEquals("Paul", expectedAuthor.getName());
|
2017-09-21 02:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-09-23 07:22:07 +01:00
|
|
|
public void givenTwoAuthor_whenInsert_andQueryAll_thenWeGetTwoAuthors() {
|
2017-09-21 02:01:43 +01:00
|
|
|
Author firstAuthor = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
firstAuthor.setName("Paul");
|
2017-09-21 02:01:43 +01:00
|
|
|
|
|
|
|
Author secondAuthor = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
secondAuthor.setName("Ludovic");
|
2017-09-21 02:01:43 +01:00
|
|
|
|
|
|
|
context.commitChanges();
|
|
|
|
|
|
|
|
List<Author> authors = ObjectSelect.query(Author.class).select(context);
|
|
|
|
assertEquals(2, authors.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-09-23 07:22:07 +01:00
|
|
|
public void givenAuthor_whenUpdating_thenWeGetAnUpatedeAuthor() {
|
2017-09-21 02:01:43 +01:00
|
|
|
Author author = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
author.setName("Paul");
|
2017-09-21 02:01:43 +01:00
|
|
|
context.commitChanges();
|
|
|
|
|
|
|
|
Author expectedAuthor = ObjectSelect.query(Author.class)
|
2017-09-24 18:32:50 +01:00
|
|
|
.where(Author.NAME.eq("Paul"))
|
2017-09-21 02:01:43 +01:00
|
|
|
.selectOne(context);
|
2017-09-24 18:32:50 +01:00
|
|
|
expectedAuthor.setName("Garcia");
|
2017-09-21 02:01:43 +01:00
|
|
|
context.commitChanges();
|
|
|
|
|
2017-09-24 18:32:50 +01:00
|
|
|
assertEquals(author.getName(), expectedAuthor.getName());
|
2017-09-21 02:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-09-23 07:22:07 +01:00
|
|
|
public void givenAuthor_whenDeleting_thenWeLostHisDetails() {
|
2017-09-21 02:01:43 +01:00
|
|
|
Author author = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
author.setName("Paul");
|
2017-09-21 02:01:43 +01:00
|
|
|
context.commitChanges();
|
|
|
|
|
|
|
|
Author savedAuthor = ObjectSelect.query(Author.class)
|
2017-09-24 18:32:50 +01:00
|
|
|
.where(Author.NAME.eq("Paul")).selectOne(context);
|
2017-09-21 02:01:43 +01:00
|
|
|
if(savedAuthor != null) {
|
|
|
|
context.deleteObjects(author);
|
|
|
|
context.commitChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
Author expectedAuthor = ObjectSelect.query(Author.class)
|
2017-09-24 18:32:50 +01:00
|
|
|
.where(Author.NAME.eq("Paul")).selectOne(context);
|
2017-09-21 02:01:43 +01:00
|
|
|
assertNull(expectedAuthor);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-09-23 07:22:07 +01:00
|
|
|
public void givenAuthor_whenAttachingToArticle_thenTheRelationIsMade() {
|
2017-09-21 02:01:43 +01:00
|
|
|
Author author = context.newObject(Author.class);
|
2017-09-24 18:32:50 +01:00
|
|
|
author.setName("Paul");
|
2017-09-21 02:01:43 +01:00
|
|
|
|
|
|
|
Article article = context.newObject(Article.class);
|
|
|
|
article.setTitle("My post title");
|
|
|
|
article.setContent("The content");
|
|
|
|
article.setAuthor(author);
|
|
|
|
|
|
|
|
context.commitChanges();
|
|
|
|
|
|
|
|
Author expectedAuthor = ObjectSelect.query(Author.class)
|
2017-09-24 18:32:50 +01:00
|
|
|
.where(Author.NAME.eq("Paul"))
|
2017-09-21 02:01:43 +01:00
|
|
|
.selectOne(context);
|
|
|
|
|
|
|
|
Article expectedArticle = (expectedAuthor.getArticles()).get(0);
|
|
|
|
assertEquals(article.getTitle(), expectedArticle.getTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|