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.dao.DBIntegrityService; |
---|
21 | import com.sun.jersey.api.client.GenericType; |
---|
22 | import eu.dasish.annotation.backend.Helpers; |
---|
23 | import eu.dasish.annotation.backend.TestBackendConstants; |
---|
24 | import eu.dasish.annotation.backend.TestInstances; |
---|
25 | import eu.dasish.annotation.schema.Annotation; |
---|
26 | import eu.dasish.annotation.schema.ResourceREF; |
---|
27 | import java.sql.SQLException; |
---|
28 | import javax.xml.bind.JAXBElement; |
---|
29 | import org.jmock.Expectations; |
---|
30 | import org.jmock.Mockery; |
---|
31 | import org.junit.Test; |
---|
32 | import static org.junit.Assert.*; |
---|
33 | import org.junit.runner.RunWith; |
---|
34 | import org.springframework.beans.factory.annotation.Autowired; |
---|
35 | import org.springframework.test.context.ContextConfiguration; |
---|
36 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
37 | import java.lang.InstantiationException; |
---|
38 | import java.sql.Timestamp; |
---|
39 | import java.util.UUID; |
---|
40 | import javax.servlet.ServletException; |
---|
41 | import javax.xml.datatype.DatatypeConfigurationException; |
---|
42 | import org.springframework.mock.web.MockHttpServletRequest; |
---|
43 | /** |
---|
44 | * |
---|
45 | * @author olhsha |
---|
46 | */ |
---|
47 | |
---|
48 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
49 | @ContextConfiguration(locations = { "/spring-test-config/mockery.xml", "/spring-test-config/mockDBIntegrityService.xml", |
---|
50 | "/spring-test-config/mockAnnotationDao.xml","/spring-test-config/mockUserDao.xml", "/spring-test-config/mockNotebookDao.xml", |
---|
51 | "/spring-test-config/mockSourceDao.xml", "/spring-test-config/mockVersionDao.xml", "/spring-test-config/mockCachedRepresentationDao.xml"}) |
---|
52 | public class AnnotationResourceTest { |
---|
53 | |
---|
54 | @Autowired |
---|
55 | private Mockery mockery; |
---|
56 | @Autowired |
---|
57 | private DBIntegrityService daoDispatcher; |
---|
58 | @Autowired |
---|
59 | private AnnotationResource annotationResource; |
---|
60 | |
---|
61 | public AnnotationResourceTest() { |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | * Test of getAnnotation method, of class AnnotationResource. |
---|
67 | */ |
---|
68 | @Test |
---|
69 | public void testGetAnnotation() throws SQLException { |
---|
70 | System.out.println("getAnnotation"); |
---|
71 | final String externalIDstring= TestBackendConstants._TEST_ANNOT_2_EXT; |
---|
72 | final int annotationID = 2; |
---|
73 | final Annotation expectedAnnotation = (new TestInstances()).getAnnotationOne(); |
---|
74 | |
---|
75 | mockery.checking(new Expectations() { |
---|
76 | { |
---|
77 | oneOf(daoDispatcher).setServiceURI(with(any(String.class))); |
---|
78 | will(doAll()); |
---|
79 | |
---|
80 | oneOf(daoDispatcher).getAnnotationInternalIdentifier(with(any(UUID.class))); |
---|
81 | will(returnValue(annotationID)); |
---|
82 | |
---|
83 | oneOf(daoDispatcher).getAnnotation(annotationID); |
---|
84 | will(returnValue(expectedAnnotation)); |
---|
85 | } |
---|
86 | }); |
---|
87 | |
---|
88 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
89 | httpServletRequest.setServletPath(TestBackendConstants._TEST_SERVLET_URI); |
---|
90 | annotationResource.setHttpRequest(httpServletRequest); |
---|
91 | |
---|
92 | JAXBElement<Annotation> result = annotationResource.getAnnotation(externalIDstring); |
---|
93 | assertEquals(expectedAnnotation, result.getValue()); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Test of deleteAnnotation method, of class AnnotationResource. |
---|
98 | */ |
---|
99 | @Test |
---|
100 | public void testDeleteAnnotation() throws SQLException { |
---|
101 | System.out.println("deleteAnnotation"); |
---|
102 | //final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(UUID.fromString(UUID)); |
---|
103 | //int[] resultDelete = daoDispatcher.deleteAnnotation(annotationID); |
---|
104 | |
---|
105 | final int[] mockDelete = new int[4]; |
---|
106 | mockDelete[0]=1; // # deleted annotations |
---|
107 | mockDelete[3]=1; // # deleted annotation_prinipal_permissions |
---|
108 | mockDelete[2]=2; // # deleted annotations_target_sources, (5,3), (5,4) |
---|
109 | mockDelete[3]=1; // # deletd sources, 4 |
---|
110 | mockery.checking(new Expectations() { |
---|
111 | { |
---|
112 | oneOf(daoDispatcher).getAnnotationInternalIdentifier(with(aNonNull(UUID.class))); |
---|
113 | will(returnValue(5)); |
---|
114 | |
---|
115 | oneOf(daoDispatcher).deleteAnnotation(5); |
---|
116 | will(returnValue(mockDelete)); |
---|
117 | } |
---|
118 | }); |
---|
119 | |
---|
120 | String result = annotationResource.deleteAnnotation(TestBackendConstants._TEST_ANNOT_5_EXT); |
---|
121 | assertEquals("1", result); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Test of createAnnotation method, of class AnnotationResource. |
---|
126 | */ |
---|
127 | @Test |
---|
128 | public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException, ServletException, DatatypeConfigurationException { |
---|
129 | System.out.println("test createAnnotation"); |
---|
130 | final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance(); |
---|
131 | |
---|
132 | // Number userID = null; |
---|
133 | // if (remoteUser != null) { |
---|
134 | // userID = daoDispatcher.getUserInternalIdentifier(UUID.fromString(remoteUser)); |
---|
135 | // } |
---|
136 | // Number newAnnotationID = daoDispatcher.addUsersAnnotation(annotation, userID); |
---|
137 | // Annotation newAnnotation = daoDispatcher.getAnnotation(newAnnotationID); |
---|
138 | final String ownerString = "5"; |
---|
139 | final Number ownerID = 5; |
---|
140 | final Number newAnnotationID = 6; |
---|
141 | final Annotation addedAnnotation = annotationToAdd; |
---|
142 | ResourceREF owner = new ResourceREF(); |
---|
143 | owner.setRef(ownerString); |
---|
144 | addedAnnotation.setOwner(owner); |
---|
145 | addedAnnotation.setURI((UUID.randomUUID()).toString()); |
---|
146 | addedAnnotation.setTimeStamp(Helpers.setXMLGregorianCalendar(Timestamp.valueOf("2013-08-12 11:25:00.383000"))); |
---|
147 | |
---|
148 | mockery.checking(new Expectations() { |
---|
149 | { |
---|
150 | oneOf(daoDispatcher).setServiceURI(with(any(String.class))); |
---|
151 | will(doAll()); |
---|
152 | |
---|
153 | oneOf(daoDispatcher).getUserInternalIdentifier(with(aNonNull(UUID.class))); |
---|
154 | will(returnValue(ownerID)); |
---|
155 | |
---|
156 | oneOf(daoDispatcher).addUsersAnnotation(annotationToAdd, ownerID); |
---|
157 | will(returnValue(newAnnotationID)); |
---|
158 | |
---|
159 | oneOf(daoDispatcher).getAnnotation(newAnnotationID); |
---|
160 | will(returnValue(addedAnnotation)); |
---|
161 | } |
---|
162 | }); |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
167 | httpServletRequest.setRemoteUser(TestBackendConstants._TEST_USER_5_EXT_ID); |
---|
168 | httpServletRequest.setServletPath(TestBackendConstants._TEST_SERVLET_URI); |
---|
169 | annotationResource.setHttpRequest(httpServletRequest); |
---|
170 | |
---|
171 | JAXBElement<Annotation> result = annotationResource.createAnnotation(annotationToAdd); |
---|
172 | assertEquals(addedAnnotation.getOwner().getRef(), result.getValue().getOwner().getRef()); |
---|
173 | assertEquals(addedAnnotation.getURI(), result.getValue().getURI()); |
---|
174 | assertEquals(addedAnnotation.getHeadline(), result.getValue().getHeadline()); |
---|
175 | assertEquals(addedAnnotation.getPermissions(), result.getValue().getPermissions()); |
---|
176 | assertEquals(addedAnnotation.getTargetSources(), result.getValue().getTargetSources()); |
---|
177 | assertEquals(addedAnnotation.getTimeStamp(), result.getValue().getTimeStamp()); |
---|
178 | assertEquals(addedAnnotation.getBody(), result.getValue().getBody()); |
---|
179 | |
---|
180 | } |
---|
181 | } |
---|