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 testInsertComment() { |
---|
44 | Comment comment = createTestComment(); |
---|
45 | |
---|
46 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
47 | Number newId = commentsDao.insertComment(comment, Integer.parseInt(TEST_COMMENT_USER_ID)); |
---|
48 | assertNotNull(newId); |
---|
49 | |
---|
50 | List<Comment> comments = commentsDao.getAllComments(); |
---|
51 | assertNotNull(comments); |
---|
52 | assertEquals(1, comments.size()); |
---|
53 | |
---|
54 | assertEquals(TEST_COMMENT_NAME, comments.get(0).getComment()); |
---|
55 | // assertEquals("A component1", comments.get(0).getComponentDescriptionId()); |
---|
56 | assertEquals(TEST_COMMENT_PROFILE_ID, comments.get(0).getProfileDescriptionId()); |
---|
57 | assertEquals(TEST_COMMENT_DATE, comments.get(0).getCommentDate()); |
---|
58 | assertEquals(TEST_COMMENT_USER_ID, comments.get(0).getUserId()); |
---|
59 | //assertEquals(TEST_COMMENT_ID, comments.get(0).getId()); |
---|
60 | } |
---|
61 | |
---|
62 | @Test |
---|
63 | public void testGetCommentsFromProfile() { |
---|
64 | List<Comment> descriptions = commentsDao.getCommentsFromProfile(TEST_COMMENT_PROFILE_ID); |
---|
65 | assertNotNull(descriptions); |
---|
66 | } |
---|
67 | |
---|
68 | @Test |
---|
69 | public void testGetCommentFromComponent(){ |
---|
70 | List<Comment> descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); |
---|
71 | assertNotNull(descriptions); |
---|
72 | } |
---|
73 | |
---|
74 | @Test |
---|
75 | public void testGetAllComments() { |
---|
76 | assertEquals(0, commentsDao.getAllComments().size()); |
---|
77 | } |
---|
78 | |
---|
79 | @Test |
---|
80 | public void testGetComment() { |
---|
81 | Comment comment = createTestComment(); |
---|
82 | commentsDao.insertComment(comment, 8); |
---|
83 | |
---|
84 | assertNotNull(commentsDao.getByComment(TEST_COMMENT_NAME)); |
---|
85 | assertNull(commentsDao.getByComment("NOT_EXITING_COMMENT_NAME")); |
---|
86 | } |
---|
87 | |
---|
88 | public static Comment createTestComment() { |
---|
89 | Comment testComment = new Comment(); |
---|
90 | testComment.setComment(TEST_COMMENT_NAME); |
---|
91 | testComment.setCommentDate(TEST_COMMENT_DATE); |
---|
92 | testComment.setId(TEST_COMMENT_ID); |
---|
93 | //testComment.setComponentDescriptionId(TEST_COMMENT_COMPONENT_ID); |
---|
94 | testComment.setProfileDescriptionId(TEST_COMMENT_PROFILE_ID); |
---|
95 | testComment.setUserId(TEST_COMMENT_USER_ID); |
---|
96 | return testComment; |
---|
97 | } |
---|
98 | |
---|
99 | public void testSetDelete(){ |
---|
100 | Comment comment = createTestComment(); |
---|
101 | int count = commentsDao.getAllComments().size(); |
---|
102 | |
---|
103 | commentsDao.insertComment(comment, 8); |
---|
104 | assertEquals(count + 1, commentsDao.getAllComments().size()); |
---|
105 | |
---|
106 | commentsDao.deleteComment(comment); |
---|
107 | assertEquals(count, commentsDao.getAllComments().size()); |
---|
108 | |
---|
109 | assertNull(commentsDao.getByComment("NOT_EXISTING_COMMENT_NAME")); |
---|
110 | } |
---|
111 | |
---|
112 | public final static String TEST_COMMENT_ID = "1"; |
---|
113 | public final static String TEST_COMMENT_PROFILE_ID = "clarin.eu:cr1:p_1297242111880"; |
---|
114 | public final static String TEST_COMMENT_COMPONENT_ID = "clarin.eu:cr1:c_1290431694600"; |
---|
115 | public final static String TEST_COMMENT_NAME = "test"; |
---|
116 | public final static String TEST_COMMENT_USER_ID = "8"; |
---|
117 | public final static String TEST_COMMENT_DATE = Comment.createNewDate(); |
---|
118 | } |
---|