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 com.sun.jersey.api.client.GenericType; |
---|
21 | import eu.dasish.annotation.backend.TestBackendConstants; |
---|
22 | import eu.dasish.annotation.backend.TestInstances; |
---|
23 | import eu.dasish.annotation.backend.dao.AnnotationDao; |
---|
24 | import eu.dasish.annotation.backend.dao.NotebookDao; |
---|
25 | import eu.dasish.annotation.backend.dao.SourceDao; |
---|
26 | import eu.dasish.annotation.backend.dao.UserDao; |
---|
27 | import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; |
---|
28 | import eu.dasish.annotation.backend.identifiers.UserIdentifier; |
---|
29 | import eu.dasish.annotation.schema.Annotation; |
---|
30 | import eu.dasish.annotation.schema.ResourceREF; |
---|
31 | import java.sql.SQLException; |
---|
32 | import javax.xml.bind.JAXBElement; |
---|
33 | import org.jmock.Expectations; |
---|
34 | import org.jmock.Mockery; |
---|
35 | import org.junit.Test; |
---|
36 | import static org.junit.Assert.*; |
---|
37 | import org.junit.runner.RunWith; |
---|
38 | import org.springframework.beans.factory.annotation.Autowired; |
---|
39 | import org.springframework.test.context.ContextConfiguration; |
---|
40 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
41 | import java.lang.InstantiationException; |
---|
42 | import javax.servlet.ServletException; |
---|
43 | import org.junit.Ignore; |
---|
44 | import org.springframework.mock.web.MockHttpServletRequest; |
---|
45 | /** |
---|
46 | * |
---|
47 | * @author olhsha |
---|
48 | */ |
---|
49 | |
---|
50 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
51 | @ContextConfiguration(locations = {"/spring-test-config/dataSource.xml", "/spring-test-config/mockAnnotationDao.xml", |
---|
52 | "/spring-test-config/mockSourceDao.xml", |
---|
53 | "/spring-test-config/mockUserDao.xml", "/spring-test-config/mockNotebookDao.xml", "/spring-test-config/mockery.xml"}) |
---|
54 | public class AnnotationResourceTest { |
---|
55 | |
---|
56 | @Autowired |
---|
57 | private Mockery mockery; |
---|
58 | @Autowired |
---|
59 | private AnnotationDao annotationDao; |
---|
60 | @Autowired |
---|
61 | private UserDao userDao; |
---|
62 | @Autowired |
---|
63 | private NotebookDao notebookDao; |
---|
64 | @Autowired |
---|
65 | private SourceDao sourceDao; |
---|
66 | |
---|
67 | @Autowired |
---|
68 | private AnnotationResource annotationResource; |
---|
69 | |
---|
70 | public AnnotationResourceTest() { |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /** |
---|
75 | * Test of getAnnotation method, of class AnnotationResource. |
---|
76 | */ |
---|
77 | @Test |
---|
78 | @Ignore |
---|
79 | public void testGetAnnotation() throws SQLException { |
---|
80 | System.out.println("getAnnotation"); |
---|
81 | final String annotationIdentifier= TestBackendConstants._TEST_ANNOT_2_EXT; |
---|
82 | final int annotationID = 2; |
---|
83 | final Annotation expectedAnnotation = (new TestInstances()).getAnnotationOne(); |
---|
84 | // the result of the mocking chain is the same as the expected annotation. |
---|
85 | // mockery.checking(new Expectations() { |
---|
86 | // { |
---|
87 | // oneOf(annotationDao).getInternalID(new AnnotationIdentifier(annotationIdentifier)); |
---|
88 | // will(returnValue(annotationID)); |
---|
89 | // |
---|
90 | // oneOf(annotationDao).getAnnotation(annotationID); |
---|
91 | // will(returnValue(expectedAnnotation)); |
---|
92 | // } |
---|
93 | // }); |
---|
94 | |
---|
95 | JAXBElement<Annotation> result = annotationResource.getAnnotation(annotationIdentifier); |
---|
96 | assertEquals(expectedAnnotation, result.getValue()); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Test of deleteAnnotation method, of class AnnotationResource. |
---|
101 | */ |
---|
102 | @Test |
---|
103 | public void testDeleteAnnotation() throws SQLException { |
---|
104 | System.out.println("deleteAnnotation"); |
---|
105 | |
---|
106 | |
---|
107 | mockery.checking(new Expectations() { |
---|
108 | { |
---|
109 | oneOf(annotationDao).getInternalID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_5_EXT)); |
---|
110 | will(returnValue(5)); |
---|
111 | |
---|
112 | oneOf(annotationDao).deleteAnnotation(5); |
---|
113 | will(returnValue(1)); |
---|
114 | } |
---|
115 | }); |
---|
116 | |
---|
117 | String result = annotationResource.deleteAnnotation(TestBackendConstants._TEST_ANNOT_5_EXT); |
---|
118 | assertEquals("1", result); |
---|
119 | |
---|
120 | // now, try to delete the same annotation one more time |
---|
121 | // if it has been already deleted then the method under testing should return 0 |
---|
122 | mockery.checking(new Expectations() { |
---|
123 | { |
---|
124 | oneOf(annotationDao).getInternalID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_5_EXT)); |
---|
125 | will(returnValue(5)); |
---|
126 | |
---|
127 | oneOf(annotationDao).deleteAnnotation(5); |
---|
128 | will(returnValue(0)); |
---|
129 | } |
---|
130 | }); |
---|
131 | |
---|
132 | result = annotationResource.deleteAnnotation(TestBackendConstants._TEST_ANNOT_5_EXT); |
---|
133 | assertEquals("0", result); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Test of createAnnotation method, of class AnnotationResource. |
---|
138 | */ |
---|
139 | @Test |
---|
140 | @Ignore |
---|
141 | public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException, ServletException { |
---|
142 | System.out.println("test createAnnotation"); |
---|
143 | final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance(); |
---|
144 | |
---|
145 | final Annotation addedAnnotation = annotationToAdd; |
---|
146 | final AnnotationIdentifier annotationIdentifier = new GenericType<AnnotationIdentifier>(){}.getRawClass().newInstance(); |
---|
147 | addedAnnotation.setURI(annotationIdentifier.toString()); |
---|
148 | ResourceREF ownerRef = new ResourceREF(); |
---|
149 | ownerRef.setRef(String.valueOf(5)); |
---|
150 | addedAnnotation.setOwner(ownerRef); |
---|
151 | |
---|
152 | final UserIdentifier owner = new UserIdentifier(TestBackendConstants._TEST_USER_5_EXT_ID); |
---|
153 | |
---|
154 | // mockery.checking(new Expectations() { |
---|
155 | // { |
---|
156 | // oneOf(userDao).getInternalID(owner); |
---|
157 | // will(returnValue(5)); |
---|
158 | // |
---|
159 | // oneOf(annotationDao).addAnnotation(annotationToAdd, 5); |
---|
160 | // will(returnValue(addedAnnotation)); |
---|
161 | // |
---|
162 | // oneOf(permissionsDao).addAnnotationPrincipalPermission(annotationIdentifier, owner, Permission.OWNER); |
---|
163 | // will(returnValue(1)); |
---|
164 | // } |
---|
165 | // }); |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
170 | httpServletRequest.setRemoteUser(TestBackendConstants._TEST_USER_5_EXT_ID); |
---|
171 | annotationResource.setHttpRequest(httpServletRequest); |
---|
172 | |
---|
173 | JAXBElement<Annotation> result = annotationResource.createAnnotation(annotationToAdd); |
---|
174 | assertEquals(String.valueOf(5), result.getValue().getOwner().getRef()); |
---|
175 | assertEquals(annotationIdentifier.toString(), result.getValue().getURI()); |
---|
176 | |
---|
177 | |
---|
178 | } |
---|
179 | } |
---|