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 junit.framework.TestCase;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.model.MetadataFacet;
27 import org.apache.archiva.metadata.model.ProjectMetadata;
28 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
29 import org.apache.archiva.metadata.model.ProjectVersionReference;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
32 import org.apache.archiva.metadata.repository.MetadataResolutionException;
33 import org.apache.archiva.rss.RssFeedGenerator;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
39 import java.util.ArrayList;
40 import java.util.Calendar;
41 import java.util.Collection;
42 import java.util.Date;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.TimeZone;
48 @RunWith( JUnit4.class )
49 public class NewArtifactsRssFeedProcessorTest
52 private static final String TEST_REPO = "test-repo";
54 private NewArtifactsRssFeedProcessor newArtifactsProcessor;
56 private MetadataRepositoryMock metadataRepository;
64 newArtifactsProcessor = new NewArtifactsRssFeedProcessor();
65 newArtifactsProcessor.setGenerator( new RssFeedGenerator() );
67 metadataRepository = new MetadataRepositoryMock();
70 @SuppressWarnings( "unchecked" )
72 public void testProcess()
75 List<ArtifactMetadata> newArtifacts = new ArrayList<ArtifactMetadata>();
76 Date whenGathered = Calendar.getInstance().getTime();
78 newArtifacts.add( createArtifact( "artifact-one", "1.0", whenGathered ) );
79 newArtifacts.add( createArtifact( "artifact-one", "1.1", whenGathered ) );
80 newArtifacts.add( createArtifact( "artifact-one", "2.0", whenGathered ) );
81 newArtifacts.add( createArtifact( "artifact-two", "1.0.1", whenGathered ) );
82 newArtifacts.add( createArtifact( "artifact-two", "1.0.2", whenGathered ) );
83 newArtifacts.add( createArtifact( "artifact-two", "1.0.3-SNAPSHOT", whenGathered ) );
84 newArtifacts.add( createArtifact( "artifact-three", "2.0-SNAPSHOT", whenGathered ) );
85 newArtifacts.add( createArtifact( "artifact-four", "1.1-beta-2", whenGathered ) );
87 metadataRepository.setArtifactsByDateRange( newArtifacts );
89 Map<String, String> reqParams = new HashMap<String, String>();
90 reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
92 SyndFeed feed = newArtifactsProcessor.process( reqParams, metadataRepository );
94 // check that the date used in the call is close to the one passed (5 seconds difference at most)
95 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
96 cal.add( Calendar.DATE, -30 );
97 assertTrue( ( metadataRepository.getFrom().getTime() - cal.getTimeInMillis() ) < 1000 * 5 );
98 assertEquals( null, metadataRepository.getTo() );
99 assertEquals( TEST_REPO, metadataRepository.getRepoId() );
101 assertTrue( feed.getTitle().equals( "New Artifacts in Repository 'test-repo'" ) );
102 assertTrue( feed.getDescription().equals(
103 "New artifacts found in repository 'test-repo' during repository scan." ) );
104 assertTrue( feed.getLanguage().equals( "en-us" ) );
105 assertTrue( feed.getPublishedDate().equals( whenGathered ) );
107 List<SyndEntry> entries = feed.getEntries();
108 assertEquals( entries.size(), 1 );
109 assertTrue( entries.get( 0 ).getTitle().equals(
110 "New Artifacts in Repository 'test-repo' as of " + whenGathered ) );
111 assertTrue( entries.get( 0 ).getPublishedDate().equals( whenGathered ) );
114 private ArtifactMetadata createArtifact( String artifactId, String version, Date whenGathered )
116 ArtifactMetadata artifact = new ArtifactMetadata();
117 artifact.setNamespace( "org.apache.archiva" );
118 artifact.setId( artifactId + "-" + version + ".jar" );
119 artifact.setRepositoryId( TEST_REPO );
120 artifact.setWhenGathered( whenGathered );
121 artifact.setProject( artifactId );
122 artifact.setProjectVersion( version );
123 artifact.setVersion( version );
127 // TODO: replace with mockito
128 private class MetadataRepositoryMock
129 implements MetadataRepository
131 private Date from, to;
133 private String repoId;
135 private List<ArtifactMetadata> artifactsByDateRange;
137 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date from, Date to )
142 return artifactsByDateRange;
145 public void removeArtifact( String repositoryId, String namespace, String project, String version,
146 MetadataFacet metadataFacet )
147 throws MetadataRepositoryException
149 throw new UnsupportedOperationException();
152 public void addMetadataFacet( String arg0, MetadataFacet arg1 )
154 throw new UnsupportedOperationException();
157 public void removeArtifact( String arg0, String arg1, String arg2, String arg3, String arg4 )
159 throw new UnsupportedOperationException();
162 public void removeRepository( String arg0 )
164 throw new UnsupportedOperationException();
167 public List<ArtifactMetadata> getArtifactsByChecksum( String arg0, String arg1 )
169 throw new UnsupportedOperationException();
172 public MetadataFacet getMetadataFacet( String arg0, String arg1, String arg2 )
174 throw new UnsupportedOperationException();
177 public List<String> getMetadataFacets( String arg0, String arg1 )
179 throw new UnsupportedOperationException();
182 public Collection<String> getRepositories()
184 throw new UnsupportedOperationException();
187 public void removeMetadataFacet( String arg0, String arg1, String arg2 )
189 throw new UnsupportedOperationException();
192 public void removeMetadataFacets( String arg0, String arg1 )
194 throw new UnsupportedOperationException();
197 public void updateArtifact( String arg0, String arg1, String arg2, String arg3, ArtifactMetadata arg4 )
199 throw new UnsupportedOperationException();
202 public void updateNamespace( String arg0, String arg1 )
204 throw new UnsupportedOperationException();
207 public void updateProject( String arg0, ProjectMetadata arg1 )
209 throw new UnsupportedOperationException();
212 public void updateProjectVersion( String arg0, String arg1, String arg2, ProjectVersionMetadata arg3 )
214 throw new UnsupportedOperationException();
217 public Collection<String> getArtifactVersions( String arg0, String arg1, String arg2, String arg3 )
219 throw new UnsupportedOperationException();
222 public Collection<ArtifactMetadata> getArtifacts( String arg0, String arg1, String arg2, String arg3 )
224 throw new UnsupportedOperationException();
229 throw new UnsupportedOperationException();
234 throw new UnsupportedOperationException();
239 throw new UnsupportedOperationException();
242 public boolean canObtainAccess( Class<?> aClass )
247 public Object obtainAccess( Class<?> aClass )
249 throw new UnsupportedOperationException();
252 public Collection<String> getNamespaces( String arg0, String arg1 )
254 throw new UnsupportedOperationException();
257 public ProjectMetadata getProject( String arg0, String arg1, String arg2 )
259 throw new UnsupportedOperationException();
262 public Collection<ProjectVersionReference> getProjectReferences( String arg0, String arg1, String arg2,
265 throw new UnsupportedOperationException();
268 public ProjectVersionMetadata getProjectVersion( String arg0, String arg1, String arg2, String arg3 )
269 throws MetadataResolutionException
271 throw new UnsupportedOperationException();
274 public Collection<String> getProjectVersions( String arg0, String arg1, String arg2 )
276 throw new UnsupportedOperationException();
279 public Collection<String> getProjects( String arg0, String arg1 )
281 throw new UnsupportedOperationException();
284 public Collection<String> getRootNamespaces( String arg0 )
286 throw new UnsupportedOperationException();
289 public void setFrom( Date from )
294 public Date getFrom()
299 public void setTo( Date to )
309 public void setRepoId( String repoId )
311 this.repoId = repoId;
314 public String getRepoId()
319 public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
321 this.artifactsByDateRange = artifactsByDateRange;
324 public List<ArtifactMetadata> getArtifacts( String repositoryId )
326 return artifactsByDateRange;