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