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

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

renaming DaoDispatcher?

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.schema.Notebook;
23import eu.dasish.annotation.schema.NotebookInfo;
24import eu.dasish.annotation.schema.NotebookInfos;
25import eu.dasish.annotation.schema.ObjectFactory;
26import java.util.ArrayList;
27import java.util.List;
28import java.util.UUID;
29import javax.xml.bind.JAXBElement;
30import org.jmock.Expectations;
31import org.jmock.Mockery;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.springframework.beans.factory.annotation.Autowired;
35import org.springframework.mock.web.MockHttpServletRequest;
36import org.springframework.test.context.ContextConfiguration;
37import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38import static org.junit.Assert.*;
39
40/**
41 *
42 * @author Peter Withers <peter.withers@mpi.nl>
43 */
44@RunWith(value = SpringJUnit4ClassRunner.class)
45@ContextConfiguration(locations = {"/spring-test-config/dataSource.xml", "/spring-test-config/mockAnnotationDao.xml", "/spring-test-config/mockUserDao.xml", 
46    "/spring-test-config/mockSourceDao.xml", "/spring-test-config/mockNotebookDao.xml", "/spring-test-config/mockDBIntegrityService.xml", "/spring-test-config/mockery.xml"})
47public class NotebookResourceTest {
48
49    @Autowired
50    private Mockery mockery;
51    @Autowired
52    private NotebookDao notebookDao;
53    @Autowired
54    private NotebookResource notebookResource;
55
56    public NotebookResourceTest() {
57    }
58
59    /**
60     * Test of getNotebookInfo method, of class NotebookResource.
61     */
62    @Test
63    public void testGetNotebookInfo() {
64        System.out.println("getNotebookInfo");
65        final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
66        httpServletRequest.setRemoteUser(TestBackendConstants._TEST_UID_1_);       
67        httpServletRequest.setServletPath(TestBackendConstants._TEST_SERVLET_URI);
68       
69       
70        mockery.checking(new Expectations() {
71            {
72                oneOf(notebookDao).getNotebookInfos(UUID.fromString(httpServletRequest.getRemoteUser()));               
73                will(returnValue(new ArrayList<NotebookInfo>()));
74            }
75        });
76        JAXBElement<NotebookInfos> result = notebookResource.getNotebookInfo(httpServletRequest);
77        assertEquals(0, result.getValue().getNotebook().size()); // todo: shoudnt this return 3 infos?
78    }
79
80    /**
81     * Test of getUsersNotebooks method, of class NotebookResource.
82     */
83    @Test
84    public void testGetUsersNotebooks() {
85        System.out.println("getUsersNotebooks");
86        mockery.checking(new Expectations() {
87            {
88                oneOf(notebookDao).getUsersNotebooks(UUID.fromString(TestBackendConstants._TEST_UID_2_));
89                will(returnValue(new ArrayList<Notebook>()));
90            }
91        });
92        final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
93        httpServletRequest.setRemoteUser(TestBackendConstants._TEST_UID_2_);               
94        httpServletRequest.setServletPath(TestBackendConstants._TEST_SERVLET_URI);
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        httpServletRequest.setServletPath(TestBackendConstants._TEST_SERVLET_URI);
108       
109        mockery.checking(new Expectations() {
110            {
111                oneOf(notebookDao).addNotebook(UUID.fromString(httpServletRequest.getRemoteUser()), null);
112                will(returnValue(UUID.fromString(TestBackendConstants._TEST_NOTEBOOK_1_EXT_ID)));
113            }
114        });
115        String expResult = "/api/notebooks/00000000-0000-0000-0000-000000000001";
116        String result = notebookResource.createNotebook(httpServletRequest); 
117        assertEquals(expResult, result.substring(result.length() - expResult.length()));
118    }
119
120    /**
121     * Test of deleteNotebook method, of class NotebookResource.
122     */
123    @Test
124    public void testDeleteNotebook() {
125        System.out.println("deleteNotebook");
126        final UUID externalID = UUID.fromString(TestBackendConstants._TEST_NOTEBOOK_1_EXT_ID);
127        mockery.checking(new Expectations() {
128            {
129                oneOf(notebookDao).deleteNotebook(externalID);
130                will(returnValue(1));
131            }
132        });
133        String expResult = "1";
134        String result = notebookResource.deleteNotebook(externalID);
135        assertEquals(expResult, result);
136    }
137   
138    /**
139     * Test of getMetadata method, of class NotebookResource. Get all metadata
140     * about a specified notebook <nid>, including the information if it is
141     * private or not. GET api/notebooks/<nid>/metadata
142     */
143    @Test
144    public void testGetMetadata() {
145        System.out.println("test GetMetadata");
146       
147        final String externalID= TestBackendConstants._TEST_NOTEBOOK_3_EXT;
148        final int notebookID = 3;
149        final NotebookInfo testInfo = new ObjectFactory().createNotebookInfo();
150       
151        mockery.checking(new Expectations() {
152            {
153                oneOf(notebookDao).getInternalID(UUID.fromString(externalID));               
154                will(returnValue(notebookID));
155               
156                oneOf(notebookDao).getNotebookInfo(notebookID); 
157               will(returnValue(testInfo)); 
158            }
159        });
160       
161        JAXBElement<NotebookInfo> result = notebookResource.getMetadata(externalID);
162        NotebookInfo entity = result.getValue();
163        assertEquals(testInfo.getRef(), entity.getRef());
164        assertEquals(testInfo.getTitle(), entity.getTitle());
165    }
166
167   
168    @Test
169    public void testGetAllAnnotations() {
170        System.out.println("test getAllAnnotations");       
171        final String externalID= TestBackendConstants._TEST_NOTEBOOK_3_EXT; 
172        final UUID aIdOne= UUID.fromString(TestBackendConstants._TEST_ANNOT_2_EXT);
173        final UUID aIdTwo= UUID.fromString(TestBackendConstants._TEST_ANNOT_3_EXT);
174        final List<UUID> annotationIds = new ArrayList<UUID>();
175        annotationIds.add(aIdOne);
176        annotationIds.add(aIdTwo);
177       
178        mockery.checking(new Expectations() {
179            {
180                oneOf(notebookDao).getAnnotationExternalIDs(UUID.fromString(externalID));               
181                will(returnValue(annotationIds));
182               
183            }
184        });
185       
186        List<JAXBElement<UUID>> result= notebookResource.getAllAnnotations(externalID, 0, 0, null, 0);
187        assertFalse(null==result);
188        assertEquals(aIdOne, result.get(0).getValue());
189        assertEquals(aIdTwo, result.get(1).getValue());
190       
191       
192    }
193}
Note: See TracBrowser for help on using the repository browser.