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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.junit.Before;
  36. import org.junit.Test;
  37. import org.junit.runner.RunWith;
  38. import java.nio.file.Paths;
  39. import java.time.ZoneId;
  40. import java.time.ZonedDateTime;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43. import java.util.Collections;
  44. import java.util.Date;
  45. import java.util.HashMap;
  46. import java.util.List;
  47. import java.util.Map;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.when;
  50. @RunWith(ArchivaBlockJUnit4ClassRunner.class)
  51. public class NewVersionsOfArtifactRssFeedProcessorTest
  52. extends TestCase
  53. {
  54. private NewVersionsOfArtifactRssFeedProcessor newVersionsProcessor;
  55. private static final String TEST_REPO = "test-repo";
  56. private static final String GROUP_ID = "org.apache.archiva";
  57. private static final String ARTIFACT_ID = "artifact-two";
  58. private MetadataRepository metadataRepository;
  59. private RepositorySessionFactory sessionFactory;
  60. private RepositorySession session;
  61. private RepositoryRegistry repositoryRegistry;
  62. @Before
  63. @Override
  64. public void setUp()
  65. throws Exception
  66. {
  67. super.setUp();
  68. newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor();
  69. newVersionsProcessor.setGenerator( new RssFeedGenerator() );
  70. metadataRepository = mock( MetadataRepository.class );
  71. sessionFactory = mock( RepositorySessionFactory.class );
  72. session = mock( RepositorySession.class );
  73. when( sessionFactory.createSession() ).thenReturn( session );
  74. when( session.getRepository( ) ).thenReturn( metadataRepository );
  75. repositoryRegistry = mock( ArchivaRepositoryRegistry.class );
  76. List<Repository> reg = new ArrayList<>( );
  77. reg.add( new BasicManagedRepository( TEST_REPO, TEST_REPO, new FilesystemStorage( Paths.get("target/test-storage"), new DefaultFileLockManager() ) ) );
  78. when( repositoryRegistry.getRepositories() ).thenReturn( reg );
  79. newVersionsProcessor.setRepositorySessionFactory( sessionFactory );
  80. newVersionsProcessor.setRepositoryRegistry( repositoryRegistry );
  81. }
  82. @SuppressWarnings("unchecked")
  83. @Test
  84. public void testProcess()
  85. throws Exception
  86. {
  87. Date whenGatheredDate = new Date( 123456789 );
  88. ZonedDateTime whenGathered = ZonedDateTime.ofInstant(whenGatheredDate.toInstant(), ZoneId.systemDefault());
  89. ArtifactMetadata artifact1 = createArtifact( whenGathered, "1.0.1" );
  90. ArtifactMetadata artifact2 = createArtifact( whenGathered, "1.0.2" );
  91. Date whenGatheredNextDate = new Date( 345678912 );
  92. ZonedDateTime whenGatheredNext = ZonedDateTime.ofInstant(whenGatheredNextDate.toInstant(), ZoneId.systemDefault());
  93. ArtifactMetadata artifact3 = createArtifact( whenGatheredNext, "1.0.3-SNAPSHOT" );
  94. Map<String, String> reqParams = new HashMap<>();
  95. reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID );
  96. reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID );
  97. when(metadataRepository.getProjectVersions(session, TEST_REPO, GROUP_ID, ARTIFACT_ID)).thenReturn(
  98. Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT"));
  99. when(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).thenReturn(
  100. Collections.singletonList(artifact1));
  101. when(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).thenReturn(
  102. Collections.singletonList(artifact2));
  103. when(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).thenReturn(
  104. Collections.singletonList(artifact3));
  105. SyndFeed feed = newVersionsProcessor.process( reqParams );
  106. assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle() );
  107. assertEquals( "New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.",
  108. feed.getDescription() );
  109. assertEquals( "en-us", feed.getLanguage() );
  110. assertEquals( whenGatheredNext.toInstant(), ZonedDateTime.ofInstant(feed.getPublishedDate().toInstant(), ZoneId.systemDefault()).toInstant() );
  111. List<SyndEntry> entries = feed.getEntries();
  112. assertEquals( 2, entries.size() );
  113. assertTrue( entries.get(0).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
  114. assertEquals( whenGathered.toInstant(), entries.get( 0 ).getPublishedDate().toInstant() );
  115. assertTrue(entries.get(1).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
  116. assertEquals( whenGatheredNext.toInstant(), entries.get( 1 ).getPublishedDate().toInstant() );
  117. }
  118. private ArtifactMetadata createArtifact(ZonedDateTime whenGathered, String version )
  119. {
  120. ArtifactMetadata artifact = new ArtifactMetadata();
  121. artifact.setNamespace( GROUP_ID );
  122. artifact.setProject( ARTIFACT_ID );
  123. artifact.setProjectVersion( version );
  124. artifact.setVersion( version );
  125. artifact.setRepositoryId( TEST_REPO );
  126. artifact.setId( ARTIFACT_ID + "-" + version + ".jar" );
  127. artifact.setWhenGathered( whenGathered );
  128. return artifact;
  129. }
  130. }