1 package org.apache.archiva.rss.processor;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.rometools.rome.feed.synd.SyndEntry;
23 import com.rometools.rome.feed.synd.SyndFeed;
24 import junit.framework.TestCase;
25 import org.apache.archiva.common.filelock.DefaultFileLockManager;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
30 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
31 import org.apache.archiva.repository.base.BasicManagedRepository;
32 import org.apache.archiva.repository.Repository;
33 import org.apache.archiva.repository.RepositoryRegistry;
34 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
35 import org.apache.archiva.rss.RssFeedGenerator;
36 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
37 import org.easymock.EasyMock;
38 import org.easymock.IMocksControl;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
43 import java.nio.file.Paths;
44 import java.time.ZoneId;
45 import java.time.ZonedDateTime;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.HashMap;
51 import java.util.List;
54 import static org.easymock.EasyMock.createControl;
55 import static org.easymock.EasyMock.expect;
57 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
58 public class NewVersionsOfArtifactRssFeedProcessorTest
61 private NewVersionsOfArtifactRssFeedProcessor newVersionsProcessor;
63 private static final String TEST_REPO = "test-repo";
65 private static final String GROUP_ID = "org.apache.archiva";
67 private static final String ARTIFACT_ID = "artifact-two";
69 private IMocksControl metadataRepositoryControl;
71 private MetadataRepository metadataRepository;
73 private IMocksControl sessionFactoryControl;
74 private RepositorySessionFactory sessionFactory;
76 private IMocksControl sessionControl;
77 private RepositorySession session;
79 private IMocksControl repositoryRegistryControl;
80 private RepositoryRegistry repositoryRegistry;
90 newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor();
91 newVersionsProcessor.setGenerator( new RssFeedGenerator() );
93 metadataRepositoryControl = createControl();
94 metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class );
96 sessionFactoryControl = EasyMock.createControl();
97 sessionControl = EasyMock.createControl();
98 sessionControl.resetToNice();
100 sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
101 session = sessionControl.createMock( RepositorySession.class );
103 EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session );
104 EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository );
105 sessionFactoryControl.replay();
106 sessionControl.replay();
108 repositoryRegistryControl = EasyMock.createControl();
109 repositoryRegistry = repositoryRegistryControl.createMock( ArchivaRepositoryRegistry.class );
111 List<Repository> reg = new ArrayList<>( );
112 reg.add( new BasicManagedRepository( TEST_REPO, TEST_REPO, new FilesystemStorage( Paths.get("target/test-storage"), new DefaultFileLockManager() ) ) );
113 EasyMock.expect( repositoryRegistry.getRepositories() ).andStubReturn( reg );
114 repositoryRegistryControl.replay();
116 newVersionsProcessor.setRepositorySessionFactory( sessionFactory );
117 newVersionsProcessor.setRepositoryRegistry( repositoryRegistry );
120 @SuppressWarnings("unchecked")
122 public void testProcess()
125 Date whenGatheredDate = new Date( 123456789 );
126 ZonedDateTime whenGathered = ZonedDateTime.ofInstant(whenGatheredDate.toInstant(), ZoneId.systemDefault());
128 ArtifactMetadata artifact1 = createArtifact( whenGathered, "1.0.1" );
129 ArtifactMetadata artifact2 = createArtifact( whenGathered, "1.0.2" );
131 Date whenGatheredNextDate = new Date( 345678912 );
132 ZonedDateTime whenGatheredNext = ZonedDateTime.ofInstant(whenGatheredNextDate.toInstant(), ZoneId.systemDefault());
134 ArtifactMetadata artifact3 = createArtifact( whenGatheredNext, "1.0.3-SNAPSHOT" );
136 Map<String, String> reqParams = new HashMap<>();
137 reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID );
138 reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID );
140 expect(metadataRepository.getProjectVersions(session, TEST_REPO, GROUP_ID, ARTIFACT_ID)).andReturn(
141 Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT"));
142 expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).andReturn(
143 Collections.singletonList(artifact1));
144 expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).andReturn(
145 Collections.singletonList(artifact2));
146 expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).andReturn(
147 Collections.singletonList(artifact3));
148 metadataRepositoryControl.replay();
150 SyndFeed feed = newVersionsProcessor.process( reqParams );
152 assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle() );
153 assertEquals( "New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.",
154 feed.getDescription() );
155 assertEquals( "en-us", feed.getLanguage() );
156 assertEquals( whenGatheredNext.toInstant(), ZonedDateTime.ofInstant(feed.getPublishedDate().toInstant(), ZoneId.systemDefault()).toInstant() );
158 List<SyndEntry> entries = feed.getEntries();
160 assertEquals( 2, entries.size() );
162 assertTrue( entries.get(0).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
163 assertEquals( whenGathered.toInstant(), entries.get( 0 ).getPublishedDate().toInstant() );
165 assertTrue(entries.get(1).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
167 assertEquals( whenGatheredNext.toInstant(), entries.get( 1 ).getPublishedDate().toInstant() );
169 metadataRepositoryControl.verify();
172 private ArtifactMetadata createArtifact(ZonedDateTime whenGathered, String version )
174 ArtifactMetadata artifact = new ArtifactMetadata();
175 artifact.setNamespace( GROUP_ID );
176 artifact.setProject( ARTIFACT_ID );
177 artifact.setProjectVersion( version );
178 artifact.setVersion( version );
179 artifact.setRepositoryId( TEST_REPO );
180 artifact.setId( ARTIFACT_ID + "-" + version + ".jar" );
181 artifact.setWhenGathered( whenGathered );