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 18KB

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