1 package org.apache.archiva.web.rss;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import junit.framework.TestCase;
24 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.apache.commons.codec.Encoder;
26 import org.apache.commons.codec.binary.Base64;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.springframework.beans.BeansException;
35 import org.springframework.beans.factory.BeanFactory;
36 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
37 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.ApplicationEvent;
40 import org.springframework.context.MessageSourceResolvable;
41 import org.springframework.context.NoSuchMessageException;
42 import org.springframework.core.ResolvableType;
43 import org.springframework.core.env.Environment;
44 import org.springframework.core.io.Resource;
45 import org.springframework.mock.web.MockHttpServletRequest;
46 import org.springframework.mock.web.MockHttpServletResponse;
47 import org.springframework.mock.web.MockServletConfig;
48 import org.springframework.mock.web.MockServletContext;
49 import org.springframework.test.context.ContextConfiguration;
50 import org.springframework.web.context.WebApplicationContext;
51
52 import javax.inject.Inject;
53 import javax.servlet.ServletContext;
54 import javax.servlet.http.HttpServletResponse;
55 import java.io.IOException;
56 import java.lang.annotation.Annotation;
57 import java.util.Locale;
58 import java.util.Map;
59
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration(
62 locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-test-common.xml",
63 "classpath*:/spring-context-rss-servlet.xml" } )
64 public class RssFeedServletTest
65 extends TestCase
66 {
67 private RssFeedServlet rssFeedServlet = new RssFeedServlet();
68
69 static String PREVIOUS_ARCHIVA_PATH;
70
71 @Inject
72 protected ApplicationContext applicationContext;
73
74 @BeforeClass
75 public static void initConfigurationPath()
76 throws Exception
77 {
78 PREVIOUS_ARCHIVA_PATH = System.getProperty( "archiva.user.configFileName" );
79 System.setProperty( "archiva.user.configFileName",
80 System.getProperty( "test.resources.path/" ) + "empty-archiva.xml" );
81 }
82
83
84 @AfterClass
85 public static void restoreConfigurationPath()
86 throws Exception
87 {
88 System.setProperty( "archiva.user.configFileName", PREVIOUS_ARCHIVA_PATH );
89 }
90
91 @Before
92 @Override
93 public void setUp()
94 throws Exception
95 {
96 final MockServletContext mockServletContext = new MockServletContext();
97
98 WebApplicationContext webApplicationContext =
99 new TestWebapplicationContext( applicationContext, mockServletContext );
100
101 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
102 webApplicationContext );
103
104 MockServletConfig mockServletConfig = new MockServletConfig()
105 {
106 @Override
107 public ServletContext getServletContext()
108 {
109 return mockServletContext;
110 }
111 };
112
113 rssFeedServlet.init( mockServletConfig );
114 }
115
116 @After
117 @Override
118 public void tearDown()
119 throws Exception
120 {
121 super.tearDown();
122 }
123
124 public static class TestWebapplicationContext
125 implements WebApplicationContext
126 {
127 private ApplicationContext applicationContext;
128
129 private ServletContext servletContext;
130
131 TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext )
132 {
133 this.applicationContext = applicationContext;
134 }
135
136 @Override
137 public ServletContext getServletContext()
138 {
139 return servletContext;
140 }
141
142 @Override
143 public String getId()
144 {
145 return applicationContext.getId();
146 }
147
148 @Override
149 public String getApplicationName()
150 {
151 return applicationContext.getApplicationName();
152 }
153
154 @Override
155 public String getDisplayName()
156 {
157 return applicationContext.getDisplayName();
158 }
159
160 @Override
161 public long getStartupDate()
162 {
163 return applicationContext.getStartupDate();
164 }
165
166 @Override
167 public ApplicationContext getParent()
168 {
169 return applicationContext.getParent();
170 }
171
172 @Override
173 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
174 throws IllegalStateException
175 {
176 return applicationContext.getAutowireCapableBeanFactory();
177 }
178
179 @Override
180 public void publishEvent( ApplicationEvent applicationEvent )
181 {
182 applicationContext.publishEvent( applicationEvent );
183 }
184
185 @Override
186 public Environment getEnvironment()
187 {
188 return applicationContext.getEnvironment();
189 }
190
191 @Override
192 public BeanFactory getParentBeanFactory()
193 {
194 return applicationContext.getParentBeanFactory();
195 }
196
197 @Override
198 public boolean containsLocalBean( String s )
199 {
200 return applicationContext.containsLocalBean( s );
201 }
202
203 @Override
204 public boolean containsBeanDefinition( String s )
205 {
206 return applicationContext.containsBeanDefinition( s );
207 }
208
209 @Override
210 public int getBeanDefinitionCount()
211 {
212 return applicationContext.getBeanDefinitionCount();
213 }
214
215 @Override
216 public String[] getBeanDefinitionNames()
217 {
218 return applicationContext.getBeanDefinitionNames();
219 }
220
221 @Override
222 public String[] getBeanNamesForType( Class<?> aClass )
223 {
224 return applicationContext.getBeanNamesForType( aClass );
225 }
226
227 @Override
228 public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
229 {
230 return applicationContext.getBeanNamesForType( aClass, b, b2 );
231 }
232
233 @Override
234 public <T> Map<String, T> getBeansOfType( Class<T> tClass )
235 throws BeansException
236 {
237 return applicationContext.getBeansOfType( tClass );
238 }
239
240 @Override
241 public <T> T getBean( Class<T> aClass, Object... objects )
242 throws BeansException
243 {
244 return applicationContext.getBean( aClass, objects );
245 }
246
247 @Override
248 public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
249 throws BeansException
250 {
251 return applicationContext.getBeansOfType( tClass, b, b2 );
252 }
253
254 @Override
255 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
256 {
257 return applicationContext.getBeanNamesForAnnotation( aClass );
258 }
259
260 @Override
261 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
262 throws BeansException
263 {
264 return applicationContext.getBeansWithAnnotation( aClass );
265 }
266
267 @Override
268 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
269 throws NoSuchBeanDefinitionException
270 {
271 return applicationContext.findAnnotationOnBean( s, aClass );
272 }
273
274 @Override
275 public Object getBean( String s )
276 throws BeansException
277 {
278 return applicationContext.getBean( s );
279 }
280
281 @Override
282 public <T> T getBean( String s, Class<T> tClass )
283 throws BeansException
284 {
285 return applicationContext.getBean( s, tClass );
286 }
287
288 @Override
289 public <T> T getBean( Class<T> tClass )
290 throws BeansException
291 {
292 return applicationContext.getBean( tClass );
293 }
294
295 @Override
296 public Object getBean( String s, Object... objects )
297 throws BeansException
298 {
299 return applicationContext.getBean( s, objects );
300 }
301
302 @Override
303 public boolean containsBean( String s )
304 {
305 return applicationContext.containsBean( s );
306 }
307
308 @Override
309 public boolean isSingleton( String s )
310 throws NoSuchBeanDefinitionException
311 {
312 return applicationContext.isSingleton( s );
313 }
314
315 @Override
316 public boolean isPrototype( String s )
317 throws NoSuchBeanDefinitionException
318 {
319 return applicationContext.isPrototype( s );
320 }
321
322 @Override
323 public boolean isTypeMatch( String s, Class<?> aClass )
324 throws NoSuchBeanDefinitionException
325 {
326 return applicationContext.isTypeMatch( s, aClass );
327 }
328
329 @Override
330 public Class<?> getType( String s )
331 throws NoSuchBeanDefinitionException
332 {
333 return applicationContext.getType( s );
334 }
335
336 @Override
337 public String[] getAliases( String s )
338 {
339 return applicationContext.getAliases( s );
340 }
341
342 @Override
343 public String getMessage( String s, Object[] objects, String s2, Locale locale )
344 {
345 return applicationContext.getMessage( s, objects, s2, locale );
346 }
347
348 @Override
349 public String getMessage( String s, Object[] objects, Locale locale )
350 throws NoSuchMessageException
351 {
352 return applicationContext.getMessage( s, objects, locale );
353 }
354
355 @Override
356 public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
357 throws NoSuchMessageException
358 {
359 return applicationContext.getMessage( messageSourceResolvable, locale );
360 }
361
362 @Override
363 public Resource[] getResources( String s )
364 throws IOException
365 {
366 return applicationContext.getResources( s );
367 }
368
369 @Override
370 public Resource getResource( String s )
371 {
372 return applicationContext.getResource( s );
373 }
374
375 @Override
376 public ClassLoader getClassLoader()
377 {
378 return applicationContext.getClassLoader();
379 }
380
381 @Override
382 public void publishEvent( Object o )
383 {
384
385 }
386
387 @Override
388 public String[] getBeanNamesForType( ResolvableType resolvableType )
389 {
390 return new String[0];
391 }
392
393 @Override
394 public boolean isTypeMatch( String s, ResolvableType resolvableType )
395 throws NoSuchBeanDefinitionException
396 {
397 return false;
398 }
399 }
400
401
402 @Test
403 public void testRequestNewArtifactsInRepo()
404 throws Exception
405 {
406 MockHttpServletRequest request = new MockHttpServletRequest();
407 request.setRequestURI( "/feeds/test-repo" );
408 request.addHeader( "User-Agent", "Apache Archiva unit test" );
409 request.setMethod( "GET" );
410
411 Base64 encoder = new Base64( 0, new byte[0] );
412 String userPass = "user1:password1";
413 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
414 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
415
416 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
417
418 rssFeedServlet.doGet( request, mockHttpServletResponse );
419
420 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
421 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
422 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
423 mockHttpServletResponse.getStatus() );
424
425 }
426
427 @Test
428 public void testRequestNewVersionsOfArtifact()
429 throws Exception
430 {
431 MockHttpServletRequest request = new MockHttpServletRequest();
432 request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
433 request.addHeader( "User-Agent", "Apache Archiva unit test" );
434 request.setMethod( "GET" );
435
436
437
438 Base64 encoder = new Base64( 0, new byte[0] );
439 String userPass = "user1:password1";
440 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
441 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
442
443 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
444
445 rssFeedServlet.doGet( request, mockHttpServletResponse );
446
447 assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
448 assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
449 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
450 mockHttpServletResponse.getStatus() );
451 }
452
453 @Test
454 public void testInvalidRequest()
455 throws Exception
456 {
457 MockHttpServletRequest request = new MockHttpServletRequest();
458 request.setRequestURI( "/feeds?invalid_param=xxx" );
459 request.addHeader( "User-Agent", "Apache Archiva unit test" );
460 request.setMethod( "GET" );
461
462 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
463
464 rssFeedServlet.doGet( request, mockHttpServletResponse );
465
466 assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
467
468 }
469
470 @Test
471 public void testInvalidAuthenticationRequest()
472 throws Exception
473 {
474
475 MockHttpServletRequest request = new MockHttpServletRequest();
476 request.setRequestURI( "/feeds/unauthorized-repo" );
477 request.addHeader( "User-Agent", "Apache Archiva unit test" );
478 request.setMethod( "GET" );
479
480 Encoder encoder = new Base64();
481 String userPass = "unauthUser:unauthPass";
482 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
483 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
484
485 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
486 rssFeedServlet.doGet( request, mockHttpServletResponse );
487
488 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
489
490 }
491
492 @Test
493 public void testUnauthorizedRequest()
494 throws Exception
495 {
496
497 MockHttpServletRequest request = new MockHttpServletRequest();
498 request.setRequestURI( "/feeds/unauthorized-repo" );
499 request.addHeader( "User-Agent", "Apache Archiva unit test" );
500 request.setMethod( "GET" );
501
502 Base64 encoder = new Base64( 0, new byte[0] );
503 String userPass = "user1:password1";
504 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
505 request.addHeader( "Authorization", "BASIC " + encodedUserPass );
506
507 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
508 rssFeedServlet.doGet( request, mockHttpServletResponse );
509
510 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
511
512 }
513
514
515 }