]> source.dussan.org Git - archiva.git/blob
0a35b18528ed5b90392d1504a6ae4d5bfbdbdf1a
[archiva.git] /
1 package org.apache.archiva.rss.processor;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
42
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;
52 import java.util.Map;
53
54 import static org.easymock.EasyMock.createControl;
55 import static org.easymock.EasyMock.expect;
56
57 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
58 public class NewVersionsOfArtifactRssFeedProcessorTest
59     extends TestCase
60 {
61     private NewVersionsOfArtifactRssFeedProcessor newVersionsProcessor;
62
63     private static final String TEST_REPO = "test-repo";
64
65     private static final String GROUP_ID = "org.apache.archiva";
66
67     private static final String ARTIFACT_ID = "artifact-two";
68
69     private IMocksControl metadataRepositoryControl;
70
71     private MetadataRepository metadataRepository;
72
73     private IMocksControl sessionFactoryControl;
74     private RepositorySessionFactory sessionFactory;
75
76     private IMocksControl sessionControl;
77     private RepositorySession session;
78
79     private IMocksControl repositoryRegistryControl;
80     private RepositoryRegistry repositoryRegistry;
81
82
83     @Before
84     @Override
85     public void setUp()
86         throws Exception
87     {
88         super.setUp();
89
90         newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor();
91         newVersionsProcessor.setGenerator( new RssFeedGenerator() );
92
93         metadataRepositoryControl = createControl();
94         metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class );
95
96         sessionFactoryControl = EasyMock.createControl();
97         sessionControl = EasyMock.createControl();
98         sessionControl.resetToNice();
99
100         sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
101         session = sessionControl.createMock( RepositorySession.class );
102
103         EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session );
104         EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository );
105         sessionFactoryControl.replay();
106         sessionControl.replay();
107
108         repositoryRegistryControl = EasyMock.createControl();
109         repositoryRegistry = repositoryRegistryControl.createMock( ArchivaRepositoryRegistry.class );
110
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();
115
116         newVersionsProcessor.setRepositorySessionFactory( sessionFactory );
117         newVersionsProcessor.setRepositoryRegistry( repositoryRegistry );
118     }
119
120     @SuppressWarnings("unchecked")
121     @Test
122     public void testProcess()
123         throws Exception
124     {
125         Date whenGatheredDate = new Date( 123456789 );
126         ZonedDateTime whenGathered = ZonedDateTime.ofInstant(whenGatheredDate.toInstant(), ZoneId.systemDefault());
127
128         ArtifactMetadata artifact1 = createArtifact( whenGathered, "1.0.1" );
129         ArtifactMetadata artifact2 = createArtifact( whenGathered, "1.0.2" );
130
131         Date whenGatheredNextDate = new Date( 345678912 );
132         ZonedDateTime whenGatheredNext = ZonedDateTime.ofInstant(whenGatheredNextDate.toInstant(), ZoneId.systemDefault());
133
134         ArtifactMetadata artifact3 = createArtifact( whenGatheredNext, "1.0.3-SNAPSHOT" );
135
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 );
139
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();
149
150         SyndFeed feed = newVersionsProcessor.process( reqParams );
151
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() );
157
158         List<SyndEntry> entries = feed.getEntries();
159
160         assertEquals( 2, entries.size() );
161
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() );
164
165         assertTrue(entries.get(1).getTitle().contains("New Versions of Artifact 'org.apache.archiva:artifact-two' as of "));
166
167         assertEquals( whenGatheredNext.toInstant(), entries.get( 1 ).getPublishedDate().toInstant() );
168
169         metadataRepositoryControl.verify();
170     }
171
172     private ArtifactMetadata createArtifact(ZonedDateTime whenGathered, String version )
173     {
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 );
182         return artifact;
183     }
184 }