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