1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
4 | import clarin.cmdi.componentregistry.DatesHelper; |
---|
5 | import clarin.cmdi.componentregistry.model.Comment; |
---|
6 | import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; |
---|
7 | |
---|
8 | import java.util.Date; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | import org.junit.Test; |
---|
12 | |
---|
13 | import static org.junit.Assert.assertNotNull; |
---|
14 | import static org.junit.Assert.assertNull; |
---|
15 | import static org.junit.Assert.assertEquals; |
---|
16 | |
---|
17 | import org.junit.Before; |
---|
18 | import org.springframework.beans.factory.annotation.Autowired; |
---|
19 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
20 | |
---|
21 | /** |
---|
22 | * |
---|
23 | * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> |
---|
24 | * @author George.Georgovassilis@mpi.nl |
---|
25 | */ |
---|
26 | public class CommentsDaoTest extends BaseUnitTest{ |
---|
27 | |
---|
28 | @Autowired |
---|
29 | private JdbcTemplate jdbcTemplate; |
---|
30 | @Autowired |
---|
31 | private CommentsDao commentsDao; |
---|
32 | |
---|
33 | @Test |
---|
34 | public void testInjection() { |
---|
35 | assertNotNull(jdbcTemplate); |
---|
36 | assertNotNull(commentsDao); |
---|
37 | } |
---|
38 | |
---|
39 | @Test |
---|
40 | public void testInsertProfileComment() { |
---|
41 | Comment comment = createTestComment(); |
---|
42 | comment.setComponentId(TEST_COMMENT_PROFILE_ID); |
---|
43 | |
---|
44 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
45 | comment.setUserId(TEST_COMMENT_USER_ID); |
---|
46 | Number newId = Long.parseLong(commentsDao.saveAndFlush(comment).getId()); |
---|
47 | assertNotNull(newId); |
---|
48 | |
---|
49 | List<Comment> comments = commentsDao.getAllComments(); |
---|
50 | assertNotNull(comments); |
---|
51 | assertEquals(1, comments.size()); |
---|
52 | |
---|
53 | assertEquals(TEST_COMMENT_NAME, comments.get(0).getComment()); |
---|
54 | assertEquals(TEST_COMMENT_PROFILE_ID, comments.get(0).getComponentId()); |
---|
55 | assertEquals(TEST_COMMENT_DATE, comments.get(0).getCommentDate()); |
---|
56 | assertEquals(TEST_COMMENT_USER_NAME, comments.get(0).getUserName()); |
---|
57 | assertEquals(TEST_COMMENT_USER_ID, comments.get(0).getUserId()); |
---|
58 | } |
---|
59 | |
---|
60 | @Test |
---|
61 | public void testInsertComponentComment() { |
---|
62 | Comment comment = createTestComment(); |
---|
63 | comment.setComponentId(TEST_COMMENT_COMPONENT_ID); |
---|
64 | |
---|
65 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
66 | |
---|
67 | comment.setUserId(TEST_COMMENT_USER_ID); |
---|
68 | Number newId = Long.parseLong(commentsDao.saveAndFlush(comment).getId()); |
---|
69 | assertNotNull(newId); |
---|
70 | |
---|
71 | List<Comment> comments = commentsDao.getAllComments(); |
---|
72 | assertNotNull(comments); |
---|
73 | assertEquals(1, comments.size()); |
---|
74 | |
---|
75 | assertEquals(TEST_COMMENT_NAME, comments.get(0).getComment()); |
---|
76 | assertEquals(TEST_COMMENT_COMPONENT_ID, comments.get(0).getComponentId()); |
---|
77 | assertEquals(TEST_COMMENT_DATE, comments.get(0).getCommentDate()); |
---|
78 | assertEquals(TEST_COMMENT_USER_NAME, comments.get(0).getUserName()); |
---|
79 | assertEquals(TEST_COMMENT_USER_ID, comments.get(0).getUserId()); |
---|
80 | } |
---|
81 | |
---|
82 | @Test |
---|
83 | public void testGetById() { |
---|
84 | Comment comment1 = createTestComment(); |
---|
85 | comment1.setComponentId(TEST_COMMENT_PROFILE_ID); |
---|
86 | comment1.setUserId(TEST_COMMENT_USER_ID); |
---|
87 | Number commentId = Long.parseLong(commentsDao.saveAndFlush(comment1).getId()); |
---|
88 | Comment commentById = commentsDao.findOne(commentId.longValue()); |
---|
89 | assertEquals(TEST_COMMENT_PROFILE_ID, commentById.getComponentId()); |
---|
90 | assertEquals(TEST_COMMENT_NAME, commentById.getComment()); |
---|
91 | } |
---|
92 | |
---|
93 | @Test |
---|
94 | public void testGetCommentsFromProfile() { |
---|
95 | List<Comment> descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_PROFILE_ID); |
---|
96 | assertNotNull(descriptions); |
---|
97 | |
---|
98 | int size = descriptions.size(); |
---|
99 | Comment comment1 = createTestComment(); |
---|
100 | comment1.setComponentId(TEST_COMMENT_PROFILE_ID); |
---|
101 | comment1.setUserId(TEST_COMMENT_USER_ID); |
---|
102 | commentsDao.save(comment1); |
---|
103 | |
---|
104 | descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_PROFILE_ID); |
---|
105 | assertEquals(size + 1, descriptions.size()); |
---|
106 | |
---|
107 | Comment comment2 = createTestComment(); |
---|
108 | comment2.setComponentId(TEST_COMMENT_PROFILE_ID); |
---|
109 | comment2.setUserId(TEST_COMMENT_USER_ID); |
---|
110 | commentsDao.save(comment2); |
---|
111 | |
---|
112 | descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_PROFILE_ID); |
---|
113 | assertEquals(size + 2, descriptions.size()); |
---|
114 | } |
---|
115 | |
---|
116 | @Test |
---|
117 | public void testGetCommentsFromComponent() { |
---|
118 | List<Comment> descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
119 | assertNotNull(descriptions); |
---|
120 | |
---|
121 | int size = descriptions.size(); |
---|
122 | Comment comment1 = createTestComment(); |
---|
123 | comment1.setComponentId(TEST_COMMENT_COMPONENT_ID); |
---|
124 | comment1.setUserId(TEST_COMMENT_USER_ID); |
---|
125 | commentsDao.saveAndFlush(comment1); |
---|
126 | |
---|
127 | descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
128 | assertEquals(size + 1, descriptions.size()); |
---|
129 | |
---|
130 | Comment comment2 = createTestComment(); |
---|
131 | comment2.setComponentId(TEST_COMMENT_COMPONENT_ID); |
---|
132 | comment2.setUserId(TEST_COMMENT_USER_ID); |
---|
133 | commentsDao.saveAndFlush(comment2); |
---|
134 | |
---|
135 | descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
136 | assertEquals(size + 2, descriptions.size()); |
---|
137 | } |
---|
138 | |
---|
139 | @Test |
---|
140 | public void testGetAllComments() { |
---|
141 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
142 | } |
---|
143 | |
---|
144 | @Test |
---|
145 | public void testGetComment() { |
---|
146 | Comment comment = createTestComment(); |
---|
147 | comment.setUserId(8); |
---|
148 | commentsDao.saveAndFlush(comment); |
---|
149 | |
---|
150 | assertNotNull(commentsDao.getByComment(TEST_COMMENT_NAME)); |
---|
151 | assertNull(commentsDao.getByComment("NON_EXISTENT_COMMENT_NAME")); |
---|
152 | } |
---|
153 | |
---|
154 | @Test |
---|
155 | public void testSetDelete() { |
---|
156 | Comment comment = createTestComment(); |
---|
157 | int count = commentsDao.getAllComments().size(); |
---|
158 | |
---|
159 | comment.setUserId(8); |
---|
160 | Number id = Long.parseLong(commentsDao.saveAndFlush(comment).getId()); |
---|
161 | assertNotNull(id); |
---|
162 | assertEquals(count + 1, commentsDao.getAllComments().size()); |
---|
163 | assertNotNull(commentsDao.getByComment(comment.getComment())); |
---|
164 | |
---|
165 | comment.setId(id.toString()); |
---|
166 | commentsDao.delete(comment); |
---|
167 | assertEquals(count, commentsDao.getAllComments().size()); |
---|
168 | |
---|
169 | assertNull(commentsDao.getByComment(comment.getComment())); |
---|
170 | } |
---|
171 | |
---|
172 | public static Comment createTestComment() { |
---|
173 | Comment testComment = new Comment(); |
---|
174 | testComment.setComment(TEST_COMMENT_NAME); |
---|
175 | testComment.setCommentDate(TEST_COMMENT_DATE); |
---|
176 | testComment.setUserId(TEST_COMMENT_USER_ID); |
---|
177 | testComment.setUserName(TEST_COMMENT_USER_NAME); |
---|
178 | return testComment; |
---|
179 | } |
---|
180 | public final static String TEST_COMMENT_ID = "1"; |
---|
181 | public final static String TEST_COMMENT_PROFILE_ID = "clarin.eu:cr1:p_1297242111880"; |
---|
182 | public final static String TEST_COMMENT_COMPONENT_ID = "clarin.eu:cr1:c_1290431694600"; |
---|
183 | public final static String TEST_COMMENT_NAME = "test"; |
---|
184 | public final static int TEST_COMMENT_USER_ID = 8; |
---|
185 | public final static String TEST_COMMENT_USER_NAME = "J. Unit"; |
---|
186 | public final static Date TEST_COMMENT_DATE = new Date(); |
---|
187 | } |
---|