]> source.dussan.org Git - archiva.git/blob
daae4c78377ae75261918ede28ca8df48f31fb8d
[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.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 String[] getBeanNamesForType( Class<?> aClass )
235         {
236             return applicationContext.getBeanNamesForType( aClass );
237         }
238
239         @Override
240         public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
241         {
242             return applicationContext.getBeanNamesForType( aClass, b, b2 );
243         }
244
245         @Override
246         public <T> Map<String, T> getBeansOfType( Class<T> tClass )
247             throws BeansException
248         {
249             return applicationContext.getBeansOfType( tClass );
250         }
251
252         @Override
253         public <T> T getBean( Class<T> aClass, Object... objects )
254             throws BeansException
255         {
256             return applicationContext.getBean( aClass, objects );
257         }
258
259         @Override
260         public <T> ObjectProvider<T> getBeanProvider( Class<T> aClass )
261         {
262             return null;
263         }
264
265         @Override
266         public <T> ObjectProvider<T> getBeanProvider( ResolvableType resolvableType )
267         {
268             return null;
269         }
270
271         @Override
272         public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
273             throws BeansException
274         {
275             return applicationContext.getBeansOfType( tClass, b, b2 );
276         }
277
278         @Override
279         public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
280         {
281             return applicationContext.getBeanNamesForAnnotation( aClass );
282         }
283
284         @Override
285         public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
286             throws BeansException
287         {
288             return applicationContext.getBeansWithAnnotation( aClass );
289         }
290
291         @Override
292         public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
293             throws NoSuchBeanDefinitionException
294         {
295             return applicationContext.findAnnotationOnBean( s, aClass );
296         }
297
298         @Override
299         public Object getBean( String s )
300             throws BeansException
301         {
302             return applicationContext.getBean( s );
303         }
304
305         @Override
306         public <T> T getBean( String s, Class<T> tClass )
307             throws BeansException
308         {
309             return applicationContext.getBean( s, tClass );
310         }
311
312         @Override
313         public <T> T getBean( Class<T> tClass )
314             throws BeansException
315         {
316             return applicationContext.getBean( tClass );
317         }
318
319         @Override
320         public Object getBean( String s, Object... objects )
321             throws BeansException
322         {
323             return applicationContext.getBean( s, objects );
324         }
325
326         @Override
327         public boolean containsBean( String s )
328         {
329             return applicationContext.containsBean( s );
330         }
331
332         @Override
333         public boolean isSingleton( String s )
334             throws NoSuchBeanDefinitionException
335         {
336             return applicationContext.isSingleton( s );
337         }
338
339         @Override
340         public boolean isPrototype( String s )
341             throws NoSuchBeanDefinitionException
342         {
343             return applicationContext.isPrototype( s );
344         }
345
346         @Override
347         public boolean isTypeMatch( String s, Class<?> aClass )
348             throws NoSuchBeanDefinitionException
349         {
350             return applicationContext.isTypeMatch( s, aClass );
351         }
352
353         @Override
354         public Class<?> getType( String s )
355             throws NoSuchBeanDefinitionException
356         {
357             return applicationContext.getType( s );
358         }
359
360         @Override
361         public Class<?> getType( String s, boolean b ) throws NoSuchBeanDefinitionException
362         {
363             return null;
364         }
365
366         @Override
367         public String[] getAliases( String s )
368         {
369             return applicationContext.getAliases( s );
370         }
371
372         @Override
373         public String getMessage( String s, Object[] objects, String s2, Locale locale )
374         {
375             return applicationContext.getMessage( s, objects, s2, locale );
376         }
377
378         @Override
379         public String getMessage( String s, Object[] objects, Locale locale )
380             throws NoSuchMessageException
381         {
382             return applicationContext.getMessage( s, objects, locale );
383         }
384
385         @Override
386         public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
387             throws NoSuchMessageException
388         {
389             return applicationContext.getMessage( messageSourceResolvable, locale );
390         }
391
392         @Override
393         public Resource[] getResources( String s )
394             throws IOException
395         {
396             return applicationContext.getResources( s );
397         }
398
399         @Override
400         public Resource getResource( String s )
401         {
402             return applicationContext.getResource( s );
403         }
404
405         @Override
406         public ClassLoader getClassLoader()
407         {
408             return applicationContext.getClassLoader();
409         }
410
411         @Override
412         public void publishEvent( Object o )
413         {
414             // no op
415         }
416
417         @Override
418         public String[] getBeanNamesForType( ResolvableType resolvableType )
419         {
420             return new String[0];
421         }
422
423         @Override
424         public String[] getBeanNamesForType( ResolvableType resolvableType, boolean b, boolean b1 )
425         {
426             return new String[0];
427         }
428
429         @Override
430         public boolean isTypeMatch( String s, ResolvableType resolvableType )
431             throws NoSuchBeanDefinitionException
432         {
433             return false;
434         }
435     }
436
437
438     @Test
439     public void testRequestNewArtifactsInRepo()
440         throws Exception
441     {
442         MockHttpServletRequest request = new MockHttpServletRequest();
443         request.setRequestURI( "/feeds/test-repo" );
444         request.addHeader( "User-Agent", "Apache Archiva unit test" );
445         request.setMethod( "GET" );
446
447         Base64 encoder = new Base64( 0, new byte[0] );
448         String userPass = "user1:password1";
449         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
450         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
451
452         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
453
454         rssFeedServlet.doGet( request, mockHttpServletResponse );
455
456         assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
457         assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
458         assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
459                       mockHttpServletResponse.getStatus() );
460
461     }
462
463     @Test
464     public void testRequestNewVersionsOfArtifact()
465         throws Exception
466     {
467         MockHttpServletRequest request = new MockHttpServletRequest();
468         request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
469         request.addHeader( "User-Agent", "Apache Archiva unit test" );
470         request.setMethod( "GET" );
471
472         //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
473
474         Base64 encoder = new Base64( 0, new byte[0] );
475         String userPass = "user1:password1";
476         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
477         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
478
479         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
480
481         rssFeedServlet.doGet( request, mockHttpServletResponse );
482
483         assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
484         assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
485         assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
486                       mockHttpServletResponse.getStatus() );
487     }
488
489     @Test
490     public void testInvalidRequest()
491         throws Exception
492     {
493         MockHttpServletRequest request = new MockHttpServletRequest();
494         request.setRequestURI( "/feeds?invalid_param=xxx" );
495         request.addHeader( "User-Agent", "Apache Archiva unit test" );
496         request.setMethod( "GET" );
497
498         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
499
500         rssFeedServlet.doGet( request, mockHttpServletResponse );
501
502         assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
503
504     }
505
506     @Test
507     public void testInvalidAuthenticationRequest()
508         throws Exception
509     {
510
511         MockHttpServletRequest request = new MockHttpServletRequest();
512         request.setRequestURI( "/feeds/unauthorized-repo" );
513         request.addHeader( "User-Agent", "Apache Archiva unit test" );
514         request.setMethod( "GET" );
515
516         Encoder encoder = new Base64();
517         String userPass = "unauthUser:unauthPass";
518         String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
519         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
520
521         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
522         rssFeedServlet.doGet( request, mockHttpServletResponse );
523
524         assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
525
526     }
527
528     @Test
529     public void testUnauthorizedRequest()
530         throws Exception
531     {
532
533         MockHttpServletRequest request = new MockHttpServletRequest();
534         request.setRequestURI( "/feeds/unauthorized-repo" );
535         request.addHeader( "User-Agent", "Apache Archiva unit test" );
536         request.setMethod( "GET" );
537
538         Base64 encoder = new Base64( 0, new byte[0] );
539         String userPass = "user1:password1";
540         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
541         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
542
543         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
544         rssFeedServlet.doGet( request, mockHttpServletResponse );
545
546         assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
547
548     }
549
550
551 }