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.identifiers.AnnotationIdentifier; |
---|
24 | import eu.dasish.annotation.backend.identifiers.UserIdentifier; |
---|
25 | import eu.dasish.annotation.schema.Annotation; |
---|
26 | import java.sql.SQLException; |
---|
27 | import javax.xml.bind.JAXBElement; |
---|
28 | import org.jmock.Expectations; |
---|
29 | import org.jmock.Mockery; |
---|
30 | import org.junit.Test; |
---|
31 | import static org.junit.Assert.*; |
---|
32 | import org.junit.runner.RunWith; |
---|
33 | import org.springframework.beans.factory.annotation.Autowired; |
---|
34 | import org.springframework.test.context.ContextConfiguration; |
---|
35 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
36 | import java.lang.InstantiationException; |
---|
37 | import javax.servlet.ServletException; |
---|
38 | import org.springframework.mock.web.MockHttpServletRequest; |
---|
39 | /** |
---|
40 | * |
---|
41 | * @author olhsha |
---|
42 | */ |
---|
43 | |
---|
44 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
45 | @ContextConfiguration(locations = {"/spring-test-config/dataSource.xml", "/spring-test-config/mockDaoDispatcher.xml", |
---|
46 | "/spring-test-config/mockery.xml", }) |
---|
47 | public class AnnotationResourceTest { |
---|
48 | |
---|
49 | @Autowired |
---|
50 | private Mockery mockery; |
---|
51 | @Autowired |
---|
52 | private DaoDispatcher daoDispatcher; |
---|
53 | @Autowired |
---|
54 | private AnnotationResource annotationResource; |
---|
55 | |
---|
56 | public AnnotationResourceTest() { |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * Test of getAnnotation method, of class AnnotationResource. |
---|
62 | */ |
---|
63 | @Test |
---|
64 | public void testGetAnnotation() throws SQLException { |
---|
65 | System.out.println("getAnnotation"); |
---|
66 | final String annotationIdentifier= TestBackendConstants._TEST_ANNOT_2_EXT; |
---|
67 | final int annotationID = 2; |
---|
68 | final Annotation expectedAnnotation = (new TestInstances()).getAnnotationOne(); |
---|
69 | |
---|
70 | //final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier)); |
---|
71 | //final Annotation annotation = daoDispatcher.getAnnotation(annotationID); |
---|
72 | mockery.checking(new Expectations() { |
---|
73 | { |
---|
74 | oneOf(daoDispatcher).getAnnotationInternalIdentifier(with(aNonNull(AnnotationIdentifier.class))); |
---|
75 | will(returnValue(annotationID)); |
---|
76 | |
---|
77 | oneOf(daoDispatcher).getAnnotation(annotationID); |
---|
78 | will(returnValue(expectedAnnotation)); |
---|
79 | } |
---|
80 | }); |
---|
81 | |
---|
82 | JAXBElement<Annotation> result = annotationResource.getAnnotation(annotationIdentifier); |
---|
83 | assertEquals(expectedAnnotation, result.getValue()); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Test of deleteAnnotation method, of class AnnotationResource. |
---|
88 | */ |
---|
89 | @Test |
---|
90 | public void testDeleteAnnotation() throws SQLException { |
---|
91 | System.out.println("deleteAnnotation"); |
---|
92 | //final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier)); |
---|
93 | //int[] resultDelete = daoDispatcher.deleteAnnotation(annotationID); |
---|
94 | |
---|
95 | mockery.checking(new Expectations() { |
---|
96 | { |
---|
97 | oneOf(daoDispatcher).getAnnotationInternalIdentifier(with(aNonNull(AnnotationIdentifier.class))); |
---|
98 | will(returnValue(5)); |
---|
99 | |
---|
100 | oneOf(daoDispatcher).deleteAnnotation(5); |
---|
101 | will(returnValue(1)); |
---|
102 | } |
---|
103 | }); |
---|
104 | |
---|
105 | String result = annotationResource.deleteAnnotation(TestBackendConstants._TEST_ANNOT_5_EXT); |
---|
106 | assertEquals("1", result); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Test of createAnnotation method, of class AnnotationResource. |
---|
111 | */ |
---|
112 | @Test |
---|
113 | public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException, ServletException { |
---|
114 | System.out.println("test createAnnotation"); |
---|
115 | final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance(); |
---|
116 | |
---|
117 | // Number userID = null; |
---|
118 | // if (remoteUser != null) { |
---|
119 | // userID = daoDispatcher.getUserInternalIdentifier(new UserIdentifier(remoteUser)); |
---|
120 | // } |
---|
121 | // Number newAnnotationID = daoDispatcher.addUsersAnnotation(annotation, userID); |
---|
122 | // Annotation newAnnotation = daoDispatcher.getAnnotation(newAnnotationID); |
---|
123 | |
---|
124 | mockery.checking(new Expectations() { |
---|
125 | { |
---|
126 | oneOf(daoDispatcher).getUserInternalIdentifier(with(aNonNull(UserIdentifier.class))); |
---|
127 | will(returnValue(5)); |
---|
128 | |
---|
129 | oneOf(daoDispatcher).addUsersAnnotation(annotationToAdd, 5); |
---|
130 | will(returnValue(1)); |
---|
131 | } |
---|
132 | }); |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); |
---|
137 | httpServletRequest.setRemoteUser(TestBackendConstants._TEST_USER_5_EXT_ID); |
---|
138 | annotationResource.setHttpRequest(httpServletRequest); |
---|
139 | |
---|
140 | JAXBElement<Annotation> result = annotationResource.createAnnotation(annotationToAdd); |
---|
141 | assertEquals(String.valueOf(5), result.getValue().getOwner().getRef()); |
---|
142 | assertFalse(null == result.getValue().getURI()); |
---|
143 | |
---|
144 | } |
---|
145 | } |
---|