source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/NotebookResourceTest.java @ 3408

Last change on this file since 3408 was 3408, checked in by olhsha, 11 years ago

refactored DAO-s tests are added, but not all working yet. Also: add 8 more tests to dispatcher class

File size: 7.7 KB
Line 
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 */
18package eu.dasish.annotation.backend.rest;
19
20import eu.dasish.annotation.backend.TestBackendConstants;
21import eu.dasish.annotation.backend.dao.NotebookDao;
22import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
23import eu.dasish.annotation.backend.identifiers.NotebookIdentifier;
24import eu.dasish.annotation.backend.identifiers.UserIdentifier;
25import eu.dasish.annotation.schema.Notebook;
26import eu.dasish.annotation.schema.NotebookInfo;
27import eu.dasish.annotation.schema.NotebookInfos;
28import eu.dasish.annotation.schema.ObjectFactory;
29import java.util.ArrayList;
30import java.util.List;
31import java.util.UUID;
32import javax.xml.bind.JAXBElement;
33import org.jmock.Expectations;
34import org.jmock.Mockery;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.springframework.beans.factory.annotation.Autowired;
38import org.springframework.mock.web.MockHttpServletRequest;
39import org.springframework.test.context.ContextConfiguration;
40import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41import 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"})
50public 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}
Note: See TracBrowser for help on using the repository browser.