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();
62 newArtifactsProcessor.setMetadataRepository( metadataRepository );
65 @SuppressWarnings( "unchecked" )
66 public void testProcess()
69 List<ArtifactMetadata> newArtifacts = new ArrayList<ArtifactMetadata>();
70 Date whenGathered = Calendar.getInstance().getTime();
72 newArtifacts.add( createArtifact( "artifact-one", "1.0", whenGathered ) );
73 newArtifacts.add( createArtifact( "artifact-one", "1.1", whenGathered ) );
74 newArtifacts.add( createArtifact( "artifact-one", "2.0", whenGathered ) );
75 newArtifacts.add( createArtifact( "artifact-two", "1.0.1", whenGathered ) );
76 newArtifacts.add( createArtifact( "artifact-two", "1.0.2", whenGathered ) );
77 newArtifacts.add( createArtifact( "artifact-two", "1.0.3-SNAPSHOT", whenGathered ) );
78 newArtifacts.add( createArtifact( "artifact-three", "2.0-SNAPSHOT", whenGathered ) );
79 newArtifacts.add( createArtifact( "artifact-four", "1.1-beta-2", whenGathered ) );
81 metadataRepository.setArtifactsByDateRange( newArtifacts );
83 Map<String, String> reqParams = new HashMap<String, String>();
84 reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
86 SyndFeed feed = newArtifactsProcessor.process( reqParams );
88 // check that the date used in the call is close to the one passed (5 seconds difference at most)
89 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
90 cal.add( Calendar.DATE, -30 );
91 assertTrue( ( metadataRepository.getFrom().getTime() - cal.getTimeInMillis() ) < 1000 * 5 );
92 assertEquals( null, metadataRepository.getTo() );
93 assertEquals( TEST_REPO, metadataRepository.getRepoId() );
95 assertTrue( feed.getTitle().equals( "New Artifacts in Repository 'test-repo'" ) );
96 assertTrue( feed.getDescription().equals(
97 "New artifacts found in repository 'test-repo' during repository scan." ) );
98 assertTrue( feed.getLanguage().equals( "en-us" ) );
99 assertTrue( feed.getPublishedDate().equals( whenGathered ) );
101 List<SyndEntry> entries = feed.getEntries();
102 assertEquals( entries.size(), 1 );
103 assertTrue( entries.get( 0 ).getTitle().equals(
104 "New Artifacts in Repository 'test-repo' as of " + whenGathered ) );
105 assertTrue( entries.get( 0 ).getPublishedDate().equals( whenGathered ) );
108 private ArtifactMetadata createArtifact( String artifactId, String version, Date whenGathered )
110 ArtifactMetadata artifact = new ArtifactMetadata();
111 artifact.setNamespace( "org.apache.archiva" );
112 artifact.setId( artifactId + "-" + version + ".jar" );
113 artifact.setRepositoryId( TEST_REPO );
114 artifact.setWhenGathered( whenGathered );
115 artifact.setProject( artifactId );
116 artifact.setProjectVersion( version );
117 artifact.setVersion( version );
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();
219 public Collection<String> getNamespaces( String arg0, String arg1 )
221 throw new UnsupportedOperationException();
224 public ProjectMetadata getProject( String arg0, String arg1, String arg2 )
226 throw new UnsupportedOperationException();
229 public Collection<ProjectVersionReference> getProjectReferences( String arg0, String arg1, String arg2,
232 throw new UnsupportedOperationException();
235 public ProjectVersionMetadata getProjectVersion( String arg0, String arg1, String arg2, String arg3 )
236 throws MetadataResolutionException
238 throw new UnsupportedOperationException();
241 public Collection<String> getProjectVersions( String arg0, String arg1, String arg2 )
243 throw new UnsupportedOperationException();
246 public Collection<String> getProjects( String arg0, String arg1 )
248 throw new UnsupportedOperationException();
251 public Collection<String> getRootNamespaces( String arg0 )
253 throw new UnsupportedOperationException();
256 public void setFrom( Date from )
261 public Date getFrom()
266 public void setTo( Date to )
276 public void setRepoId( String repoId )
278 this.repoId = repoId;
281 public String getRepoId()
286 public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
288 this.artifactsByDateRange = artifactsByDateRange;
291 public List<ArtifactMetadata> getArtifacts( String repositoryId )
293 return artifactsByDateRange;