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.

NewArtifactsRssFeedProcessorTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package org.apache.archiva.rss.processor;
  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 com.sun.syndication.feed.synd.SyndEntry;
  21. import com.sun.syndication.feed.synd.SyndFeed;
  22. import junit.framework.TestCase;
  23. import org.apache.archiva.metadata.model.ArtifactMetadata;
  24. import org.apache.archiva.metadata.model.MetadataFacet;
  25. import org.apache.archiva.metadata.model.ProjectMetadata;
  26. import org.apache.archiva.metadata.model.ProjectVersionMetadata;
  27. import org.apache.archiva.metadata.model.ProjectVersionReference;
  28. import org.apache.archiva.metadata.repository.MetadataRepository;
  29. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  30. import org.apache.archiva.metadata.repository.MetadataResolutionException;
  31. import org.apache.archiva.rss.RssFeedGenerator;
  32. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import java.util.ArrayList;
  37. import java.util.Calendar;
  38. import java.util.Collection;
  39. import java.util.Date;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.TimeZone;
  44. @RunWith (ArchivaBlockJUnit4ClassRunner.class)
  45. public class NewArtifactsRssFeedProcessorTest
  46. extends TestCase
  47. {
  48. private static final String TEST_REPO = "test-repo";
  49. private NewArtifactsRssFeedProcessor newArtifactsProcessor;
  50. private MetadataRepositoryMock metadataRepository;
  51. @Before
  52. public void setUp()
  53. throws Exception
  54. {
  55. super.setUp();
  56. newArtifactsProcessor = new NewArtifactsRssFeedProcessor();
  57. newArtifactsProcessor.setGenerator( new RssFeedGenerator() );
  58. metadataRepository = new MetadataRepositoryMock();
  59. }
  60. @SuppressWarnings ("unchecked")
  61. @Test
  62. public void testProcess()
  63. throws Exception
  64. {
  65. List<ArtifactMetadata> newArtifacts = new ArrayList<>();
  66. Date whenGathered = Calendar.getInstance().getTime();
  67. newArtifacts.add( createArtifact( "artifact-one", "1.0", whenGathered ) );
  68. newArtifacts.add( createArtifact( "artifact-one", "1.1", whenGathered ) );
  69. newArtifacts.add( createArtifact( "artifact-one", "2.0", whenGathered ) );
  70. newArtifacts.add( createArtifact( "artifact-two", "1.0.1", whenGathered ) );
  71. newArtifacts.add( createArtifact( "artifact-two", "1.0.2", whenGathered ) );
  72. newArtifacts.add( createArtifact( "artifact-two", "1.0.3-SNAPSHOT", whenGathered ) );
  73. newArtifacts.add( createArtifact( "artifact-three", "2.0-SNAPSHOT", whenGathered ) );
  74. newArtifacts.add( createArtifact( "artifact-four", "1.1-beta-2", whenGathered ) );
  75. metadataRepository.setArtifactsByDateRange( newArtifacts );
  76. Map<String, String> reqParams = new HashMap<String, String>();
  77. reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
  78. SyndFeed feed = newArtifactsProcessor.process( reqParams, metadataRepository );
  79. // check that the date used in the call is close to the one passed (5 seconds difference at most)
  80. Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
  81. cal.add( Calendar.DATE, -30 );
  82. assertTrue( ( metadataRepository.getFrom().getTime() - cal.getTimeInMillis() ) < 1000 * 5 );
  83. assertEquals( null, metadataRepository.getTo() );
  84. assertEquals( TEST_REPO, metadataRepository.getRepoId() );
  85. assertTrue( feed.getTitle().equals( "New Artifacts in Repository 'test-repo'" ) );
  86. assertTrue(
  87. feed.getDescription().equals( "New artifacts found in repository 'test-repo' during repository scan." ) );
  88. assertTrue( feed.getLanguage().equals( "en-us" ) );
  89. assertTrue( feed.getPublishedDate().equals( whenGathered ) );
  90. List<SyndEntry> entries = feed.getEntries();
  91. assertEquals( entries.size(), 1 );
  92. assertTrue(
  93. entries.get( 0 ).getTitle().equals( "New Artifacts in Repository 'test-repo' as of " + whenGathered ) );
  94. assertTrue( entries.get( 0 ).getPublishedDate().equals( whenGathered ) );
  95. }
  96. private ArtifactMetadata createArtifact( String artifactId, String version, Date whenGathered )
  97. {
  98. ArtifactMetadata artifact = new ArtifactMetadata();
  99. artifact.setNamespace( "org.apache.archiva" );
  100. artifact.setId( artifactId + "-" + version + ".jar" );
  101. artifact.setRepositoryId( TEST_REPO );
  102. artifact.setWhenGathered( whenGathered );
  103. artifact.setProject( artifactId );
  104. artifact.setProjectVersion( version );
  105. artifact.setVersion( version );
  106. return artifact;
  107. }
  108. // TODO: replace with mockito
  109. private class MetadataRepositoryMock
  110. implements MetadataRepository
  111. {
  112. private Date from, to;
  113. private String repoId;
  114. private List<ArtifactMetadata> artifactsByDateRange;
  115. public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date from, Date to )
  116. {
  117. setRepoId( repoId );
  118. setFrom( from );
  119. setTo( to );
  120. return artifactsByDateRange;
  121. }
  122. public void removeArtifact( String repositoryId, String namespace, String project, String version,
  123. MetadataFacet metadataFacet )
  124. throws MetadataRepositoryException
  125. {
  126. throw new UnsupportedOperationException();
  127. }
  128. public void addMetadataFacet( String arg0, MetadataFacet arg1 )
  129. {
  130. throw new UnsupportedOperationException();
  131. }
  132. public void removeArtifact( String arg0, String arg1, String arg2, String arg3, String arg4 )
  133. {
  134. throw new UnsupportedOperationException();
  135. }
  136. public void removeRepository( String arg0 )
  137. {
  138. throw new UnsupportedOperationException();
  139. }
  140. public List<ArtifactMetadata> getArtifactsByChecksum( String arg0, String arg1 )
  141. {
  142. throw new UnsupportedOperationException();
  143. }
  144. public MetadataFacet getMetadataFacet( String arg0, String arg1, String arg2 )
  145. {
  146. throw new UnsupportedOperationException();
  147. }
  148. public List<String> getMetadataFacets( String arg0, String arg1 )
  149. {
  150. throw new UnsupportedOperationException();
  151. }
  152. public Collection<String> getRepositories()
  153. {
  154. throw new UnsupportedOperationException();
  155. }
  156. public void removeMetadataFacet( String arg0, String arg1, String arg2 )
  157. {
  158. throw new UnsupportedOperationException();
  159. }
  160. public void removeMetadataFacets( String arg0, String arg1 )
  161. {
  162. throw new UnsupportedOperationException();
  163. }
  164. public void updateArtifact( String arg0, String arg1, String arg2, String arg3, ArtifactMetadata arg4 )
  165. {
  166. throw new UnsupportedOperationException();
  167. }
  168. public void updateNamespace( String arg0, String arg1 )
  169. {
  170. throw new UnsupportedOperationException();
  171. }
  172. public void updateProject( String arg0, ProjectMetadata arg1 )
  173. {
  174. throw new UnsupportedOperationException();
  175. }
  176. public void updateProjectVersion( String arg0, String arg1, String arg2, ProjectVersionMetadata arg3 )
  177. {
  178. throw new UnsupportedOperationException();
  179. }
  180. public Collection<String> getArtifactVersions( String arg0, String arg1, String arg2, String arg3 )
  181. {
  182. throw new UnsupportedOperationException();
  183. }
  184. public Collection<ArtifactMetadata> getArtifacts( String arg0, String arg1, String arg2, String arg3 )
  185. {
  186. throw new UnsupportedOperationException();
  187. }
  188. public void save()
  189. {
  190. throw new UnsupportedOperationException();
  191. }
  192. public void close()
  193. {
  194. throw new UnsupportedOperationException();
  195. }
  196. public boolean hasMetadataFacet( String repositoryId, String facetId )
  197. throws MetadataRepositoryException
  198. {
  199. return false;
  200. }
  201. public void revert()
  202. {
  203. throw new UnsupportedOperationException();
  204. }
  205. public boolean canObtainAccess( Class<?> aClass )
  206. {
  207. return false;
  208. }
  209. public <T>T obtainAccess( Class<T> aClass )
  210. {
  211. throw new UnsupportedOperationException();
  212. }
  213. public Collection<String> getNamespaces( String arg0, String arg1 )
  214. {
  215. throw new UnsupportedOperationException();
  216. }
  217. public ProjectMetadata getProject( String arg0, String arg1, String arg2 )
  218. {
  219. throw new UnsupportedOperationException();
  220. }
  221. public Collection<ProjectVersionReference> getProjectReferences( String arg0, String arg1, String arg2,
  222. String arg3 )
  223. {
  224. throw new UnsupportedOperationException();
  225. }
  226. public ProjectVersionMetadata getProjectVersion( String arg0, String arg1, String arg2, String arg3 )
  227. throws MetadataResolutionException
  228. {
  229. throw new UnsupportedOperationException();
  230. }
  231. public Collection<String> getProjectVersions( String arg0, String arg1, String arg2 )
  232. {
  233. throw new UnsupportedOperationException();
  234. }
  235. public Collection<String> getProjects( String arg0, String arg1 )
  236. {
  237. throw new UnsupportedOperationException();
  238. }
  239. public Collection<String> getRootNamespaces( String arg0 )
  240. {
  241. throw new UnsupportedOperationException();
  242. }
  243. public void removeProject( String repositoryId, String namespace, String projectId )
  244. throws MetadataRepositoryException
  245. {
  246. throw new UnsupportedOperationException();
  247. }
  248. public void setFrom( Date from )
  249. {
  250. this.from = from;
  251. }
  252. public Date getFrom()
  253. {
  254. return from;
  255. }
  256. public void setTo( Date to )
  257. {
  258. this.to = to;
  259. }
  260. public Date getTo()
  261. {
  262. return to;
  263. }
  264. public void setRepoId( String repoId )
  265. {
  266. this.repoId = repoId;
  267. }
  268. public String getRepoId()
  269. {
  270. return repoId;
  271. }
  272. public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
  273. {
  274. this.artifactsByDateRange = artifactsByDateRange;
  275. }
  276. public List<ArtifactMetadata> getArtifacts( String repositoryId )
  277. {
  278. return artifactsByDateRange;
  279. }
  280. public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
  281. throws MetadataRepositoryException
  282. {
  283. }
  284. public void removeNamespace( String repositoryId, String namespace )
  285. throws MetadataRepositoryException
  286. {
  287. }
  288. public void removeProjectVersion( String repoId, String namespace, String projectId, String projectVersion )
  289. throws MetadataRepositoryException
  290. {
  291. }
  292. }
  293. }