source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/CommentsDaoTest.java @ 3451

Last change on this file since 3451 was 3451, checked in by George.Georgovassilis@mpi.nl, 11 years ago

#360 Fixes for shha lux16 environment and renaming of DAO interfaces and implementations

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