1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.model.Comment; |
---|
4 | import java.util.List; |
---|
5 | import org.junit.runner.RunWith; |
---|
6 | import org.junit.Test; |
---|
7 | import static org.junit.Assert.assertNotNull; |
---|
8 | import static org.junit.Assert.assertNull; |
---|
9 | import static org.junit.Assert.assertEquals; |
---|
10 | import org.junit.Before; |
---|
11 | |
---|
12 | import org.springframework.beans.factory.annotation.Autowired; |
---|
13 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
14 | import org.springframework.test.context.ContextConfiguration; |
---|
15 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
16 | |
---|
17 | /** |
---|
18 | * |
---|
19 | * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> |
---|
20 | */ |
---|
21 | @RunWith(SpringJUnit4ClassRunner.class) |
---|
22 | @ContextConfiguration(locations = {"/applicationContext.xml"}) |
---|
23 | public class CommentsDaoTest { |
---|
24 | |
---|
25 | @Autowired |
---|
26 | private JdbcTemplate jdbcTemplate; |
---|
27 | @Autowired |
---|
28 | private CommentsDao commentsDao; |
---|
29 | |
---|
30 | @Before |
---|
31 | public void init() { |
---|
32 | ComponentRegistryTestDatabase.resetDatabase(jdbcTemplate); |
---|
33 | ComponentRegistryTestDatabase.createTableComments(jdbcTemplate); |
---|
34 | } |
---|
35 | |
---|
36 | @Test |
---|
37 | public void testInjection() { |
---|
38 | assertNotNull(jdbcTemplate); |
---|
39 | assertNotNull(commentsDao); |
---|
40 | } |
---|
41 | |
---|
42 | @Test |
---|
43 | public void testInsertProfileComment() { |
---|
44 | Comment comment = createTestComment(); |
---|
45 | comment.setProfileDescriptionId(TEST_COMMENT_PROFILE_ID); |
---|
46 | |
---|
47 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
48 | Number newId = commentsDao.insertComment(comment, TEST_COMMENT_USER_ID); |
---|
49 | assertNotNull(newId); |
---|
50 | |
---|
51 | List<Comment> comments = commentsDao.getAllComments(); |
---|
52 | assertNotNull(comments); |
---|
53 | assertEquals(1, comments.size()); |
---|
54 | |
---|
55 | assertEquals(TEST_COMMENT_NAME, comments.get(0).getComment()); |
---|
56 | assertEquals(TEST_COMMENT_PROFILE_ID, comments.get(0).getProfileDescriptionId()); |
---|
57 | assertEquals(TEST_COMMENT_DATE, comments.get(0).getCommentDate()); |
---|
58 | assertEquals(TEST_COMMENT_USER_NAME, comments.get(0).getUserName()); |
---|
59 | assertEquals(Integer.toString(TEST_COMMENT_USER_ID), comments.get(0).getUserId()); |
---|
60 | } |
---|
61 | |
---|
62 | @Test |
---|
63 | public void testInsertComponentComment() { |
---|
64 | Comment comment = createTestComment(); |
---|
65 | comment.setComponentDescriptionId(TEST_COMMENT_COMPONENT_ID); |
---|
66 | |
---|
67 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
68 | Number newId = commentsDao.insertComment(comment, TEST_COMMENT_USER_ID); |
---|
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).getComponentDescriptionId()); |
---|
77 | assertEquals(TEST_COMMENT_DATE, comments.get(0).getCommentDate()); |
---|
78 | assertEquals(TEST_COMMENT_USER_NAME, comments.get(0).getUserName()); |
---|
79 | assertEquals(Integer.toString(TEST_COMMENT_USER_ID), comments.get(0).getUserId()); |
---|
80 | } |
---|
81 | |
---|
82 | @Test |
---|
83 | public void testGetCommentsFromProfile() { |
---|
84 | List<Comment> descriptions = commentsDao.getCommentsFromProfile(TEST_COMMENT_PROFILE_ID); |
---|
85 | assertNotNull(descriptions); |
---|
86 | |
---|
87 | int size = descriptions.size(); |
---|
88 | Comment comment1 = createTestComment(); |
---|
89 | comment1.setProfileDescriptionId(TEST_COMMENT_PROFILE_ID); |
---|
90 | commentsDao.insertComment(comment1, TEST_COMMENT_USER_ID); |
---|
91 | |
---|
92 | descriptions = commentsDao.getCommentsFromProfile(TEST_COMMENT_PROFILE_ID); |
---|
93 | assertEquals(size + 1, descriptions.size()); |
---|
94 | |
---|
95 | Comment comment2 = createTestComment(); |
---|
96 | comment2.setProfileDescriptionId(TEST_COMMENT_PROFILE_ID); |
---|
97 | commentsDao.insertComment(comment2, TEST_COMMENT_USER_ID); |
---|
98 | |
---|
99 | descriptions = commentsDao.getCommentsFromProfile(TEST_COMMENT_PROFILE_ID); |
---|
100 | assertEquals(size + 2, descriptions.size()); |
---|
101 | } |
---|
102 | |
---|
103 | @Test |
---|
104 | public void testGetCommentsFromComponent() { |
---|
105 | List<Comment> descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
106 | assertNotNull(descriptions); |
---|
107 | |
---|
108 | int size = descriptions.size(); |
---|
109 | Comment comment1 = createTestComment(); |
---|
110 | comment1.setComponentDescriptionId(TEST_COMMENT_COMPONENT_ID); |
---|
111 | commentsDao.insertComment(comment1, TEST_COMMENT_USER_ID); |
---|
112 | |
---|
113 | descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
114 | assertEquals(size + 1, descriptions.size()); |
---|
115 | |
---|
116 | Comment comment2 = createTestComment(); |
---|
117 | comment2.setComponentDescriptionId(TEST_COMMENT_COMPONENT_ID); |
---|
118 | commentsDao.insertComment(comment2, TEST_COMMENT_USER_ID); |
---|
119 | |
---|
120 | descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
121 | assertEquals(size + 2, descriptions.size()); |
---|
122 | } |
---|
123 | |
---|
124 | @Test |
---|
125 | public void testGetAllComments() { |
---|
126 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
127 | } |
---|
128 | |
---|
129 | @Test |
---|
130 | public void testGetComment() { |
---|
131 | Comment comment = createTestComment(); |
---|
132 | commentsDao.insertComment(comment, 8); |
---|
133 | |
---|
134 | assertNotNull(commentsDao.getByComment(TEST_COMMENT_NAME)); |
---|
135 | assertNull(commentsDao.getByComment("NON_EXISTENT_COMMENT_NAME")); |
---|
136 | } |
---|
137 | |
---|
138 | @Test |
---|
139 | public void testSetDelete() { |
---|
140 | Comment comment = createTestComment(); |
---|
141 | int count = commentsDao.getAllComments().size(); |
---|
142 | |
---|
143 | Number id = commentsDao.insertComment(comment, 8); |
---|
144 | assertNotNull(id); |
---|
145 | assertEquals(count + 1, commentsDao.getAllComments().size()); |
---|
146 | assertNotNull(commentsDao.getByComment(comment.getComment())); |
---|
147 | |
---|
148 | comment.setId(id.toString()); |
---|
149 | commentsDao.deleteComment(comment); |
---|
150 | assertEquals(count, commentsDao.getAllComments().size()); |
---|
151 | |
---|
152 | assertNull(commentsDao.getByComment(comment.getComment())); |
---|
153 | } |
---|
154 | |
---|
155 | public static Comment createTestComment() { |
---|
156 | Comment testComment = new Comment(); |
---|
157 | testComment.setComment(TEST_COMMENT_NAME); |
---|
158 | testComment.setCommentDate(TEST_COMMENT_DATE); |
---|
159 | testComment.setUserId(Integer.toString(TEST_COMMENT_USER_ID)); |
---|
160 | testComment.setUserName(TEST_COMMENT_USER_NAME); |
---|
161 | return testComment; |
---|
162 | } |
---|
163 | public final static String TEST_COMMENT_ID = "1"; |
---|
164 | public final static String TEST_COMMENT_PROFILE_ID = "clarin.eu:cr1:p_1297242111880"; |
---|
165 | public final static String TEST_COMMENT_COMPONENT_ID = "clarin.eu:cr1:c_1290431694600"; |
---|
166 | public final static String TEST_COMMENT_NAME = "test"; |
---|
167 | public final static int TEST_COMMENT_USER_ID = 8; |
---|
168 | public final static String TEST_COMMENT_USER_NAME = "J. Unit"; |
---|
169 | public final static String TEST_COMMENT_DATE = Comment.createNewDate(); |
---|
170 | } |
---|