|
| 1 | +package com.baeldung.apachecayenne; |
| 2 | + |
| 3 | +import com.baeldung.apachecayenne.persistent.Article; |
| 4 | +import com.baeldung.apachecayenne.persistent.Author; |
| 5 | +import org.apache.cayenne.ObjectContext; |
| 6 | +import org.apache.cayenne.configuration.server.ServerRuntime; |
| 7 | +import org.apache.cayenne.query.ObjectSelect; |
| 8 | +import org.apache.cayenne.query.SQLTemplate; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.BeforeClass; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static junit.framework.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertNull; |
| 17 | + |
| 18 | + |
| 19 | +public class CayenneOperationTests { |
| 20 | + private static ObjectContext context = null; |
| 21 | + |
| 22 | + @BeforeClass |
| 23 | + public static void setupTheCayenneContext(){ |
| 24 | + ServerRuntime cayenneRuntime = ServerRuntime.builder() |
| 25 | + .addConfig("cayenne-project.xml") |
| 26 | + .build(); |
| 27 | + context = cayenneRuntime.newContext(); |
| 28 | + } |
| 29 | + |
| 30 | + @After |
| 31 | + public void deleteAllRecords(){ |
| 32 | + SQLTemplate deleteArticles = new SQLTemplate(Article.class, "delete from article"); |
| 33 | + SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author"); |
| 34 | + |
| 35 | + context.performGenericQuery(deleteArticles); |
| 36 | + context.performGenericQuery(deleteAuthors); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenAuthor_whenInsert_thenWeGetOneRecordInTheDatabase(){ |
| 41 | + Author author = context.newObject(Author.class); |
| 42 | + author.setFirstname("Paul"); |
| 43 | + author.setLastname("Smith"); |
| 44 | + |
| 45 | + context.commitChanges(); |
| 46 | + |
| 47 | + long records = ObjectSelect.dataRowQuery(Author.class).selectCount(context); |
| 48 | + assertEquals(1, records); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenAuthor_whenInsert_andQueryByFirstName_thenWeGetTheAuthor(){ |
| 53 | + Author author = context.newObject(Author.class); |
| 54 | + author.setFirstname("Paul"); |
| 55 | + author.setLastname("Smith"); |
| 56 | + |
| 57 | + context.commitChanges(); |
| 58 | + |
| 59 | + Author expectedAuthor = ObjectSelect.query(Author.class) |
| 60 | + .where(Author.FIRSTNAME.eq("Paul")) |
| 61 | + .selectOne(context); |
| 62 | + |
| 63 | + assertEquals("Paul", expectedAuthor.getFirstname()); |
| 64 | + assertEquals("Smith", expectedAuthor.getLastname()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenTwoAuthor_whenInsert_andQueryAll_thenWeGetTwoAuthors(){ |
| 69 | + Author firstAuthor = context.newObject(Author.class); |
| 70 | + firstAuthor.setFirstname("Paul"); |
| 71 | + firstAuthor.setLastname("Smith"); |
| 72 | + |
| 73 | + Author secondAuthor = context.newObject(Author.class); |
| 74 | + secondAuthor.setFirstname("Ludovic"); |
| 75 | + secondAuthor.setLastname("Garcia"); |
| 76 | + |
| 77 | + context.commitChanges(); |
| 78 | + |
| 79 | + List<Author> authors = ObjectSelect.query(Author.class).select(context); |
| 80 | + assertEquals(2, authors.size()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenAuthor_whenUpdating_thenWeGetAnUpatedeAuthor(){ |
| 85 | + Author author = context.newObject(Author.class); |
| 86 | + author.setFirstname("Paul"); |
| 87 | + author.setLastname("Smith"); |
| 88 | + context.commitChanges(); |
| 89 | + |
| 90 | + Author expectedAuthor = ObjectSelect.query(Author.class) |
| 91 | + .where(Author.FIRSTNAME.eq("Paul")) |
| 92 | + .selectOne(context); |
| 93 | + expectedAuthor.setLastname("Smith 2"); |
| 94 | + context.commitChanges(); |
| 95 | + |
| 96 | + assertEquals(author.getFirstname(), expectedAuthor.getFirstname()); |
| 97 | + assertEquals(author.getLastname(), expectedAuthor.getLastname()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void givenAuthor_whenDeleting_thenWeLostHisDetails(){ |
| 102 | + Author author = context.newObject(Author.class); |
| 103 | + author.setFirstname("Paul"); |
| 104 | + author.setLastname("Smith"); |
| 105 | + context.commitChanges(); |
| 106 | + |
| 107 | + Author savedAuthor = ObjectSelect.query(Author.class) |
| 108 | + .where(Author.FIRSTNAME.eq("Paul")).selectOne(context); |
| 109 | + if(savedAuthor != null) { |
| 110 | + context.deleteObjects(author); |
| 111 | + context.commitChanges(); |
| 112 | + } |
| 113 | + |
| 114 | + Author expectedAuthor = ObjectSelect.query(Author.class) |
| 115 | + .where(Author.FIRSTNAME.eq("Paul")).selectOne(context); |
| 116 | + assertNull(expectedAuthor); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void givenAuthor_whenAttachingToArticle_thenTheRelationIsMade(){ |
| 121 | + Author author = context.newObject(Author.class); |
| 122 | + author.setFirstname("Paul"); |
| 123 | + author.setLastname("Smith"); |
| 124 | + |
| 125 | + Article article = context.newObject(Article.class); |
| 126 | + article.setTitle("My post title"); |
| 127 | + article.setContent("The content"); |
| 128 | + article.setAuthor(author); |
| 129 | + |
| 130 | + context.commitChanges(); |
| 131 | + |
| 132 | + Author expectedAuthor = ObjectSelect.query(Author.class) |
| 133 | + .where(Author.LASTNAME.eq("Smith")) |
| 134 | + .selectOne(context); |
| 135 | + |
| 136 | + Article expectedArticle = (expectedAuthor.getArticles()).get(0); |
| 137 | + assertEquals(article.getTitle(), expectedArticle.getTitle()); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments