1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.rest; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.TestBackendConstants; |
---|
21 | import eu.dasish.annotation.backend.dao.NotebookDao; |
---|
22 | import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; |
---|
23 | import eu.dasish.annotation.backend.identifiers.NotebookIdentifier; |
---|
24 | import eu.dasish.annotation.backend.identifiers.UserIdentifier; |
---|
25 | import eu.dasish.annotation.schema.Notebook; |
---|
26 | import eu.dasish.annotation.schema.NotebookInfo; |
---|
27 | import eu.dasish.annotation.schema.NotebookInfos; |
---|
28 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
29 | import java.util.ArrayList; |
---|
30 | import java.util.List; |
---|
31 | import java.util.UUID; |
---|
32 | import javax.xml.bind.JAXBElement; |
---|
33 | import org.jmock.Expectations; |
---|
34 | import org.jmock.Mockery; |
---|
35 | import org.junit.Test; |
---|
36 | import org.junit.runner.RunWith; |
---|
37 | import org.springframework.beans.factory.annotation.Autowired; |
---|
38 | import org.springframework.mock.web.MockHttpServletRequest; |
---|
39 | import org.springframework.test.context.ContextConfiguration; |
---|
40 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
41 | import static org.junit.Assert.*; |
---|
42 | |
---|
43 | /** |
---|
44 | * |
---|
45 | * @author Peter Withers <peter.withers@mpi.nl> |
---|
46 | */ |
---|
47 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
48 | @ContextConfiguration(locations = {"/spring-test-config/dataSource.xml", "/spring-test-config/mockAnnotationDao.xml", "/spring-test-config/mockUserDao.xml", |
---|
49 | "/spring-test-config/mockSourceDao.xml", "/spring-test-config/mockNotebookDao.xml", "/spring-test-config/mockery.xml"}) |
---|
50 | public class NotebookResourceTest { |
---|
51 | |
---|
52 | @Autowired |
---|
53 | private Mockery mockery; |
---|
54 | @Autowired |
---|
55 | private NotebookDao notebookDao; |
---|
56 | @Autowired |
---|
57 | private NotebookResource notebookResource; |
---|
58 | |
---|
59 | public NotebookResourceTest() { |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Test of getNotebookInfo method, of class NotebookResource. |
---|
64 | */ |
---|
65 | @Test |
---|
66 | public void testGetNotebookInfo() { |
---|
67 | System.out.println("getNotebookInfo"); |
---|
68 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
69 | httpServletRequest.setRemoteUser(TestBackendConstants._TEST_UID_1_); |
---|
70 | |
---|
71 | mockery.checking(new Expectations() { |
---|
72 | { |
---|
73 | oneOf(notebookDao).getNotebookInfos(new UserIdentifier(httpServletRequest.getRemoteUser())); |
---|
74 | will(returnValue(new ArrayList<NotebookInfo>())); |
---|
75 | } |
---|
76 | }); |
---|
77 | JAXBElement<NotebookInfos> result = notebookResource.getNotebookInfo(httpServletRequest); |
---|
78 | assertEquals(0, result.getValue().getNotebook().size()); // todo: shoudnt this return 3 infos? |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Test of getUsersNotebooks method, of class NotebookResource. |
---|
83 | */ |
---|
84 | @Test |
---|
85 | public void testGetUsersNotebooks() { |
---|
86 | System.out.println("getUsersNotebooks"); |
---|
87 | mockery.checking(new Expectations() { |
---|
88 | { |
---|
89 | oneOf(notebookDao).getUsersNotebooks(new UserIdentifier(TestBackendConstants._TEST_UID_2_)); |
---|
90 | will(returnValue(new ArrayList<Notebook>())); |
---|
91 | } |
---|
92 | }); |
---|
93 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
94 | httpServletRequest.setRemoteUser(TestBackendConstants._TEST_UID_2_); |
---|
95 | List result = notebookResource.getUsersNotebooks(httpServletRequest); |
---|
96 | assertEquals(0, result.size()); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Test of createNotebook method, of class NotebookResource. |
---|
101 | */ |
---|
102 | @Test |
---|
103 | public void testCreateNotebook() throws Exception { |
---|
104 | System.out.println("createNotebook"); |
---|
105 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
106 | httpServletRequest.setRemoteUser(TestBackendConstants._TEST_UID_2_); |
---|
107 | mockery.checking(new Expectations() { |
---|
108 | { |
---|
109 | oneOf(notebookDao).addNotebook(new UserIdentifier(httpServletRequest.getRemoteUser()), null); |
---|
110 | will(returnValue(new NotebookIdentifier(new UUID(0, 1)))); |
---|
111 | } |
---|
112 | }); |
---|
113 | String expResult = "/api/notebooks/00000000-0000-0000-0000-000000000001"; |
---|
114 | String result = notebookResource.createNotebook(httpServletRequest); |
---|
115 | assertEquals(expResult, result.substring(result.length() - expResult.length())); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Test of deleteNotebook method, of class NotebookResource. |
---|
120 | */ |
---|
121 | @Test |
---|
122 | public void testDeleteNotebook() { |
---|
123 | System.out.println("deleteNotebook"); |
---|
124 | final NotebookIdentifier notebookIdentifier = new NotebookIdentifier(new UUID(0, 1)); |
---|
125 | mockery.checking(new Expectations() { |
---|
126 | { |
---|
127 | oneOf(notebookDao).deleteNotebook(notebookIdentifier); |
---|
128 | will(returnValue(1)); |
---|
129 | } |
---|
130 | }); |
---|
131 | String expResult = "1"; |
---|
132 | String result = notebookResource.deleteNotebook(notebookIdentifier); |
---|
133 | assertEquals(expResult, result); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Test of getMetadata method, of class NotebookResource. Get all metadata |
---|
138 | * about a specified notebook <nid>, including the information if it is |
---|
139 | * private or not. GET api/notebooks/<nid>/metadata |
---|
140 | */ |
---|
141 | @Test |
---|
142 | public void testGetMetadata() { |
---|
143 | System.out.println("test GetMetadata"); |
---|
144 | |
---|
145 | final String notebookIdentifier= TestBackendConstants._TEST_NOTEBOOK_3_EXT; |
---|
146 | final int notebookID = 3; |
---|
147 | final NotebookInfo testInfo = new ObjectFactory().createNotebookInfo(); |
---|
148 | |
---|
149 | mockery.checking(new Expectations() { |
---|
150 | { |
---|
151 | oneOf(notebookDao).getNotebookID(new NotebookIdentifier(notebookIdentifier)); |
---|
152 | will(returnValue(notebookID)); |
---|
153 | |
---|
154 | oneOf(notebookDao).getNotebookInfo(notebookID); |
---|
155 | will(returnValue(testInfo)); |
---|
156 | } |
---|
157 | }); |
---|
158 | |
---|
159 | JAXBElement<NotebookInfo> result = notebookResource.getMetadata(notebookIdentifier); |
---|
160 | NotebookInfo entity = result.getValue(); |
---|
161 | assertEquals(testInfo.getRef(), entity.getRef()); |
---|
162 | assertEquals(testInfo.getTitle(), entity.getTitle()); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | @Test |
---|
167 | public void testGetAllAnnotations() { |
---|
168 | System.out.println("test getAllAnnotations"); |
---|
169 | final String notebookIdentifier= TestBackendConstants._TEST_NOTEBOOK_3_EXT; |
---|
170 | final AnnotationIdentifier aIdOne= new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_2_EXT); |
---|
171 | final AnnotationIdentifier aIdTwo= new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_3_EXT); |
---|
172 | final List<AnnotationIdentifier> annotationIds = new ArrayList<AnnotationIdentifier>(); |
---|
173 | annotationIds.add(aIdOne); |
---|
174 | annotationIds.add(aIdTwo); |
---|
175 | |
---|
176 | mockery.checking(new Expectations() { |
---|
177 | { |
---|
178 | oneOf(notebookDao).getAnnotationExternalIDs(new NotebookIdentifier(notebookIdentifier)); |
---|
179 | will(returnValue(annotationIds)); |
---|
180 | |
---|
181 | } |
---|
182 | }); |
---|
183 | |
---|
184 | List<AnnotationIdentifier> result= notebookResource.getAllAnnotations(notebookIdentifier, 0, 0, null, 0); |
---|
185 | assertFalse(null==result); |
---|
186 | assertEquals(aIdOne, result.get(0)); |
---|
187 | assertEquals(aIdTwo, result.get(1)); |
---|
188 | |
---|
189 | |
---|
190 | } |
---|
191 | } |
---|