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.

NewVersionsOfArtifactRssFeedProcessorTest.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.rometools.rome.feed.synd.SyndEntry;
  21. import com.rometools.rome.feed.synd.SyndFeed;
  22. import junit.framework.TestCase;
  23. import org.apache.archiva.common.filelock.DefaultFileLockManager;
  24. import org.apache.archiva.metadata.model.ArtifactMetadata;
  25. import org.apache.archiva.metadata.repository.MetadataRepository;
  26. import org.apache.archiva.metadata.repository.RepositorySession;
  27. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  28. import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
  29. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  30. import org.apache.archiva.repository.Repository;
  31. import org.apache.archiva.repository.RepositoryRegistry;
  32. import org.apache.archiva.repository.storage.fs.FilesystemStorage;
  33. import org.apache.archiva.rss.RssFeedGenerator;
  34. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  35. import org.easymock.EasyMock;
  36. import org.easymock.IMocksControl;
  37. import org.junit.Before;
  38. import org.junit.Test;
  39. import org.junit.runner.RunWith;
  40. import java.nio.file.Paths;
  41. import java.time.ZoneId;
  42. import java.time.ZonedDateTime;
  43. import java.util.ArrayList;
  44. import java.util.Arrays;
  45. import java.util.Collections;
  46. import java.util.Date;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Map;
  50. import static org.easymock.EasyMock.createControl;
  51. import static org.easymock.EasyMock.expect;
  52. @RunWith(ArchivaBlockJUnit4ClassRunner.class)
  53. public class NewVersionsOfArtifactRssFeedProcessorTest
  54. extends TestCase
  55. {
  56. private NewVersionsOfArtifactRssFeedProcessor newVersionsProcessor;
  57. private static final String TEST_REPO = "test-repo";
  58. private static final String GROUP_ID = "org.apache.archiva";
  59. private static final String ARTIFACT_ID = "artifact-two";
  60. private IMocksControl metadataRepositoryControl;
  61. private MetadataRepository metadataRepository;
  62. private IMocksControl sessionFactoryControl;
  63. private RepositorySessionFactory sessionFactory;
  64. private IMocksControl sessionControl;
  65. private RepositorySession session;
  66. private IMocksControl repositoryRegistryControl;
  67. private RepositoryRegistry repositoryRegistry;
  68. @Before
  69. @Override
  70. public void setUp()
  71. throws Exception
  72. {
  73. super.setUp();
  74. newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor();
  75. newVersionsProcessor.setGenerator( new RssFeedGenerator() );
  76. metadataRepositoryControl = createControl();
  77. metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class );
  78. sessionFactoryControl = EasyMock.createControl();
  79. sessionControl = EasyMock.createControl();
  80. sessionControl.resetToNice();
  81. sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
  82. session = sessionControl.createMock( RepositorySession.class );
  83. EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session );
  84. EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository );
  85. sessionFactoryControl.replay();
  86. sessionControl.replay();
  87. repositoryRegistryControl = EasyMock.createControl();
  88. repositoryRegistry = repositoryRegistryControl.createMock( ArchivaRepositoryRegistry.class );
  89. List<Repository> reg = new ArrayList<>( );
  90. reg.add( new BasicManagedRepository( TEST_REPO, TEST_REPO, new FilesystemStorage( Paths.get("target/test-storage"), new DefaultFileLockManager() ) ) );
  91. EasyMock.expect( repositoryRegistry.getRepositories() ).andStubReturn( reg );
  92. repositoryRegistryControl.replay();
  93. newVersionsProcessor.setRepositorySessionFactory( sessionFactory );
  94. newVersionsProcessor.setRepositoryRegistry( repositoryRegistry );
  95. }
  96. @SuppressWarnings("unchecked")
  97. @Test
  98. public void testProcess()
  99. throws Exception
  100. {
  101. Date whenGatheredDate = new Date( 123456789 );
  102. ZonedDateTime whenGathered = ZonedDateTime.ofInstant(whenGatheredDate.toInstant(), ZoneId.systemDefault());
  103. ArtifactMetadata artifact1 = createArtifact( whenGathered, "1.0.1" );
  104. ArtifactMetadata artifact2 = createArtifact( whenGathered, "1.0.2" );
  105. Date whenGatheredNextDate = new Date( 345678912 );
  106. ZonedDateTime whenGatheredNext = ZonedDateTime.ofInstant(whenGatheredNextDate.toInstant(), ZoneId.systemDefault());
  107. ArtifactMetadata artifact3 = createArtifact( whenGatheredNext, "1.0.3-SNAPSHOT" );
  108. Map<String, String> reqParams = new HashMap<>();
  109. reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID );
  110. reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID );
  111. expect(metadataRepository.getProjectVersions(session, TEST_REPO, GROUP_ID, ARTIFACT_ID)).andReturn(
  112. Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT"));
  113. expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).andReturn(
  114. Collections.singletonList(artifact1));
  115. expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).andReturn(
  116. Collections.singletonList(artifact2));
  117. expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).andReturn(
  118. Collections.singletonList(artifact3));
  119. metadataRepositoryControl.replay();
  120. SyndFeed feed = newVersionsProcessor.process( reqParams );
  121. assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle() );
  122. assertEquals( "New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.",
  123. feed.getDescription() );
  124. assertEquals( "en-us", feed.getLanguage() );
  125. assertEquals( whenGatheredNext.toInstant(), ZonedDateTime.ofInstant(feed.getPublishedDate().toInstant(), ZoneId.systemDefault()).toInstant() );
  126. List<SyndEntry> entries = feed.getEntries();
  127. assertEquals( 2, entries.size() );
  128. assertTrue( entries.get(0).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
  129. assertEquals( whenGathered.toInstant(), entries.get( 0 ).getPublishedDate().toInstant() );
  130. assertTrue(entries.get(1).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
  131. assertEquals( whenGatheredNext.toInstant(), entries.get( 1 ).getPublishedDate().toInstant() );
  132. metadataRepositoryControl.verify();
  133. }
  134. private ArtifactMetadata createArtifact(ZonedDateTime whenGathered, String version )
  135. {
  136. ArtifactMetadata artifact = new ArtifactMetadata();
  137. artifact.setNamespace( GROUP_ID );
  138. artifact.setProject( ARTIFACT_ID );
  139. artifact.setProjectVersion( version );
  140. artifact.setVersion( version );
  141. artifact.setRepositoryId( TEST_REPO );
  142. artifact.setId( ARTIFACT_ID + "-" + version + ".jar" );
  143. artifact.setWhenGathered( whenGathered );
  144. return artifact;
  145. }
  146. }