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.sun.syndication.feed.synd.SyndEntry;
23 import com.sun.syndication.feed.synd.SyndFeed;
24 import org.apache.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.model.MetadataFacet;
26 import org.apache.archiva.metadata.model.ProjectMetadata;
27 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
28 import org.apache.archiva.metadata.model.ProjectVersionReference;
29 import org.apache.archiva.metadata.repository.MetadataRepository;
30 import org.apache.archiva.metadata.repository.MetadataResolutionException;
31 import org.apache.archiva.rss.RssFeedGenerator;
32 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34 import java.util.ArrayList;
35 import java.util.Calendar;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.TimeZone;
43 public class NewArtifactsRssFeedProcessorTest
44 extends PlexusInSpringTestCase
46 private static final String TEST_REPO = "test-repo";
48 private NewArtifactsRssFeedProcessor newArtifactsProcessor;
50 private MetadataRepositoryMock metadataRepository;
58 newArtifactsProcessor = new NewArtifactsRssFeedProcessor();
59 newArtifactsProcessor.setGenerator( new RssFeedGenerator() );
61 metadataRepository = new MetadataRepositoryMock();
64 @SuppressWarnings( "unchecked" )
65 public void testProcess()
68 List<ArtifactMetadata> newArtifacts = new ArrayList<ArtifactMetadata>();
69 Date whenGathered = Calendar.getInstance().getTime();
71 newArtifacts.add( createArtifact( "artifact-one", "1.0", whenGathered ) );
72 newArtifacts.add( createArtifact( "artifact-one", "1.1", whenGathered ) );
73 newArtifacts.add( createArtifact( "artifact-one", "2.0", whenGathered ) );
74 newArtifacts.add( createArtifact( "artifact-two", "1.0.1", whenGathered ) );
75 newArtifacts.add( createArtifact( "artifact-two", "1.0.2", whenGathered ) );
76 newArtifacts.add( createArtifact( "artifact-two", "1.0.3-SNAPSHOT", whenGathered ) );
77 newArtifacts.add( createArtifact( "artifact-three", "2.0-SNAPSHOT", whenGathered ) );
78 newArtifacts.add( createArtifact( "artifact-four", "1.1-beta-2", whenGathered ) );
80 metadataRepository.setArtifactsByDateRange( newArtifacts );
82 Map<String, String> reqParams = new HashMap<String, String>();
83 reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
85 SyndFeed feed = newArtifactsProcessor.process( reqParams, metadataRepository );
87 // check that the date used in the call is close to the one passed (5 seconds difference at most)
88 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
89 cal.add( Calendar.DATE, -30 );
90 assertTrue( ( metadataRepository.getFrom().getTime() - cal.getTimeInMillis() ) < 1000 * 5 );
91 assertEquals( null, metadataRepository.getTo() );
92 assertEquals( TEST_REPO, metadataRepository.getRepoId() );
94 assertTrue( feed.getTitle().equals( "New Artifacts in Repository 'test-repo'" ) );
95 assertTrue( feed.getDescription().equals(
96 "New artifacts found in repository 'test-repo' during repository scan." ) );
97 assertTrue( feed.getLanguage().equals( "en-us" ) );
98 assertTrue( feed.getPublishedDate().equals( whenGathered ) );
100 List<SyndEntry> entries = feed.getEntries();
101 assertEquals( entries.size(), 1 );
102 assertTrue( entries.get( 0 ).getTitle().equals(
103 "New Artifacts in Repository 'test-repo' as of " + whenGathered ) );
104 assertTrue( entries.get( 0 ).getPublishedDate().equals( whenGathered ) );
107 private ArtifactMetadata createArtifact( String artifactId, String version, Date whenGathered )
109 ArtifactMetadata artifact = new ArtifactMetadata();
110 artifact.setNamespace( "org.apache.archiva" );
111 artifact.setId( artifactId + "-" + version + ".jar" );
112 artifact.setRepositoryId( TEST_REPO );
113 artifact.setWhenGathered( whenGathered );
114 artifact.setProject( artifactId );
115 artifact.setProjectVersion( version );
116 artifact.setVersion( version );
120 // TODO: replace with mockito
121 private class MetadataRepositoryMock
122 implements MetadataRepository
124 private Date from, to;
126 private String repoId;
128 private List<ArtifactMetadata> artifactsByDateRange;
130 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date from, Date to )
135 return artifactsByDateRange;
138 public void addMetadataFacet( String arg0, MetadataFacet arg1 )
140 throw new UnsupportedOperationException();
143 public void removeArtifact( String arg0, String arg1, String arg2, String arg3, String arg4 )
145 throw new UnsupportedOperationException();
148 public void removeRepository( String arg0 )
150 throw new UnsupportedOperationException();
153 public List<ArtifactMetadata> getArtifactsByChecksum( String arg0, String arg1 )
155 throw new UnsupportedOperationException();
158 public MetadataFacet getMetadataFacet( String arg0, String arg1, String arg2 )
160 throw new UnsupportedOperationException();
163 public List<String> getMetadataFacets( String arg0, String arg1 )
165 throw new UnsupportedOperationException();
168 public Collection<String> getRepositories()
170 throw new UnsupportedOperationException();
173 public void removeMetadataFacet( String arg0, String arg1, String arg2 )
175 throw new UnsupportedOperationException();
178 public void removeMetadataFacets( String arg0, String arg1 )
180 throw new UnsupportedOperationException();
183 public void updateArtifact( String arg0, String arg1, String arg2, String arg3, ArtifactMetadata arg4 )
185 throw new UnsupportedOperationException();
188 public void updateNamespace( String arg0, String arg1 )
190 throw new UnsupportedOperationException();
193 public void updateProject( String arg0, ProjectMetadata arg1 )
195 throw new UnsupportedOperationException();
198 public void updateProjectReference( String arg0, String arg1, String arg2, String arg3,
199 ProjectVersionReference arg4 )
201 throw new UnsupportedOperationException();
204 public void updateProjectVersion( String arg0, String arg1, String arg2, ProjectVersionMetadata arg3 )
206 throw new UnsupportedOperationException();
209 public Collection<String> getArtifactVersions( String arg0, String arg1, String arg2, String arg3 )
211 throw new UnsupportedOperationException();
214 public Collection<ArtifactMetadata> getArtifacts( String arg0, String arg1, String arg2, String arg3 )
216 throw new UnsupportedOperationException();
221 throw new UnsupportedOperationException();
226 throw new UnsupportedOperationException();
231 throw new UnsupportedOperationException();
234 public boolean canObtainAccess( Class<?> aClass )
239 public Object obtainAccess( Class<?> aClass )
241 throw new UnsupportedOperationException();
244 public Collection<String> getNamespaces( String arg0, String arg1 )
246 throw new UnsupportedOperationException();
249 public ProjectMetadata getProject( String arg0, String arg1, String arg2 )
251 throw new UnsupportedOperationException();
254 public Collection<ProjectVersionReference> getProjectReferences( String arg0, String arg1, String arg2,
257 throw new UnsupportedOperationException();
260 public ProjectVersionMetadata getProjectVersion( String arg0, String arg1, String arg2, String arg3 )
261 throws MetadataResolutionException
263 throw new UnsupportedOperationException();
266 public Collection<String> getProjectVersions( String arg0, String arg1, String arg2 )
268 throw new UnsupportedOperationException();
271 public Collection<String> getProjects( String arg0, String arg1 )
273 throw new UnsupportedOperationException();
276 public Collection<String> getRootNamespaces( String arg0 )
278 throw new UnsupportedOperationException();
281 public void setFrom( Date from )
286 public Date getFrom()
291 public void setTo( Date to )
301 public void setRepoId( String repoId )
303 this.repoId = repoId;
306 public String getRepoId()
311 public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
313 this.artifactsByDateRange = artifactsByDateRange;
316 public List<ArtifactMetadata> getArtifacts( String repositoryId )
318 return artifactsByDateRange;