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 private class MetadataRepositoryMock
121 implements MetadataRepository
123 private Date from, to;
125 private String repoId;
127 private List<ArtifactMetadata> artifactsByDateRange;
129 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date from, Date to )
134 return artifactsByDateRange;
137 public void addMetadataFacet( String arg0, MetadataFacet arg1 )
139 throw new UnsupportedOperationException();
142 public void removeArtifact( String arg0, String arg1, String arg2, String arg3, String arg4 )
144 throw new UnsupportedOperationException();
147 public void removeRepository( String arg0 )
149 throw new UnsupportedOperationException();
152 public List<ArtifactMetadata> getArtifactsByChecksum( String arg0, String arg1 )
154 throw new UnsupportedOperationException();
157 public MetadataFacet getMetadataFacet( String arg0, String arg1, String arg2 )
159 throw new UnsupportedOperationException();
162 public List<String> getMetadataFacets( String arg0, String arg1 )
164 throw new UnsupportedOperationException();
167 public Collection<String> getRepositories()
169 throw new UnsupportedOperationException();
172 public void removeMetadataFacet( String arg0, String arg1, String arg2 )
174 throw new UnsupportedOperationException();
177 public void removeMetadataFacets( String arg0, String arg1 )
179 throw new UnsupportedOperationException();
182 public void updateArtifact( String arg0, String arg1, String arg2, String arg3, ArtifactMetadata arg4 )
184 throw new UnsupportedOperationException();
187 public void updateNamespace( String arg0, String arg1 )
189 throw new UnsupportedOperationException();
192 public void updateProject( String arg0, ProjectMetadata arg1 )
194 throw new UnsupportedOperationException();
197 public void updateProjectReference( String arg0, String arg1, String arg2, String arg3,
198 ProjectVersionReference arg4 )
200 throw new UnsupportedOperationException();
203 public void updateProjectVersion( String arg0, String arg1, String arg2, ProjectVersionMetadata arg3 )
205 throw new UnsupportedOperationException();
208 public Collection<String> getArtifactVersions( String arg0, String arg1, String arg2, String arg3 )
210 throw new UnsupportedOperationException();
213 public Collection<ArtifactMetadata> getArtifacts( String arg0, String arg1, String arg2, String arg3 )
215 throw new UnsupportedOperationException();
218 public Collection<String> getNamespaces( String arg0, String arg1 )
220 throw new UnsupportedOperationException();
223 public ProjectMetadata getProject( String arg0, String arg1, String arg2 )
225 throw new UnsupportedOperationException();
228 public Collection<ProjectVersionReference> getProjectReferences( String arg0, String arg1, String arg2,
231 throw new UnsupportedOperationException();
234 public ProjectVersionMetadata getProjectVersion( String arg0, String arg1, String arg2, String arg3 )
235 throws MetadataResolutionException
237 throw new UnsupportedOperationException();
240 public Collection<String> getProjectVersions( String arg0, String arg1, String arg2 )
242 throw new UnsupportedOperationException();
245 public Collection<String> getProjects( String arg0, String arg1 )
247 throw new UnsupportedOperationException();
250 public Collection<String> getRootNamespaces( String arg0 )
252 throw new UnsupportedOperationException();
255 public void setFrom( Date from )
260 public Date getFrom()
265 public void setTo( Date to )
275 public void setRepoId( String repoId )
277 this.repoId = repoId;
280 public String getRepoId()
285 public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
287 this.artifactsByDateRange = artifactsByDateRange;
290 public List<ArtifactMetadata> getArtifacts( String repositoryId )
292 return artifactsByDateRange;