You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RssFeedServletTest.java 17KB

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