1 package org.apache.archiva.rss.processor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.TimeZone;
29
30 import junit.framework.TestCase;
31
32 import org.apache.archiva.metadata.model.ArtifactMetadata;
33 import org.apache.archiva.metadata.repository.AbstractMetadataRepository;
34 import org.apache.archiva.rss.RssFeedGenerator;
35 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39
40 import com.sun.syndication.feed.synd.SyndEntry;
41 import com.sun.syndication.feed.synd.SyndFeed;
42
43 @RunWith (ArchivaBlockJUnit4ClassRunner.class)
44 public class NewArtifactsRssFeedProcessorTest
45 extends TestCase
46 {
47 private static final String TEST_REPO = "test-repo";
48
49 private NewArtifactsRssFeedProcessor newArtifactsProcessor;
50
51 private MetadataRepositoryMock metadataRepository;
52
53 @Before
54 @Override
55 public void setUp()
56 throws Exception
57 {
58 super.setUp();
59
60 newArtifactsProcessor = new NewArtifactsRssFeedProcessor();
61 newArtifactsProcessor.setGenerator( new RssFeedGenerator() );
62
63 metadataRepository = new MetadataRepositoryMock();
64 }
65
66 @SuppressWarnings ("unchecked")
67 @Test
68 public void testProcess()
69 throws Exception
70 {
71 List<ArtifactMetadata> newArtifacts = new ArrayList<>();
72 Date whenGathered = Calendar.getInstance().getTime();
73
74 newArtifacts.add( createArtifact( "artifact-one", "1.0", whenGathered ) );
75 newArtifacts.add( createArtifact( "artifact-one", "1.1", whenGathered ) );
76 newArtifacts.add( createArtifact( "artifact-one", "2.0", whenGathered ) );
77 newArtifacts.add( createArtifact( "artifact-two", "1.0.1", whenGathered ) );
78 newArtifacts.add( createArtifact( "artifact-two", "1.0.2", whenGathered ) );
79 newArtifacts.add( createArtifact( "artifact-two", "1.0.3-SNAPSHOT", whenGathered ) );
80 newArtifacts.add( createArtifact( "artifact-three", "2.0-SNAPSHOT", whenGathered ) );
81 newArtifacts.add( createArtifact( "artifact-four", "1.1-beta-2", whenGathered ) );
82
83 metadataRepository.setArtifactsByDateRange( newArtifacts );
84
85 Map<String, String> reqParams = new HashMap<>();
86 reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
87
88 SyndFeed feed = newArtifactsProcessor.process( reqParams, metadataRepository );
89
90
91 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
92 cal.add( Calendar.DATE, -30 );
93 assertTrue( ( metadataRepository.getFrom().getTime() - cal.getTimeInMillis() ) < 1000 * 5 );
94 assertEquals( null, metadataRepository.getTo() );
95 assertEquals( TEST_REPO, metadataRepository.getRepoId() );
96
97 assertTrue( feed.getTitle().equals( "New Artifacts in Repository 'test-repo'" ) );
98 assertTrue(
99 feed.getDescription().equals( "New artifacts found in repository 'test-repo' during repository scan." ) );
100 assertTrue( feed.getLanguage().equals( "en-us" ) );
101 assertTrue( feed.getPublishedDate().equals( whenGathered ) );
102
103 List<SyndEntry> entries = feed.getEntries();
104 assertEquals( entries.size(), 1 );
105 assertTrue(
106 entries.get( 0 ).getTitle().equals( "New Artifacts in Repository 'test-repo' as of " + whenGathered ) );
107 assertTrue( entries.get( 0 ).getPublishedDate().equals( whenGathered ) );
108 }
109
110 private ArtifactMetadata createArtifact( String artifactId, String version, Date whenGathered )
111 {
112 ArtifactMetadata artifact = new ArtifactMetadata();
113 artifact.setNamespace( "org.apache.archiva" );
114 artifact.setId( artifactId + "-" + version + ".jar" );
115 artifact.setRepositoryId( TEST_REPO );
116 artifact.setWhenGathered( whenGathered );
117 artifact.setProject( artifactId );
118 artifact.setProjectVersion( version );
119 artifact.setVersion( version );
120 return artifact;
121 }
122
123
124 private class MetadataRepositoryMock
125 extends AbstractMetadataRepository
126 {
127 private Date from, to;
128
129 private String repoId;
130
131 private List<ArtifactMetadata> artifactsByDateRange;
132
133 @Override
134 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date from, Date to )
135 {
136 setRepoId( repoId );
137 setFrom( from );
138 setTo( to );
139 return artifactsByDateRange;
140 }
141
142
143 public void setFrom( Date from )
144 {
145 this.from = from;
146 }
147
148 public Date getFrom()
149 {
150 return from;
151 }
152
153 public void setTo( Date to )
154 {
155 this.to = to;
156 }
157
158 public Date getTo()
159 {
160 return to;
161 }
162
163 public void setRepoId( String repoId )
164 {
165 this.repoId = repoId;
166 }
167
168 public String getRepoId()
169 {
170 return repoId;
171 }
172
173 public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
174 {
175 this.artifactsByDateRange = artifactsByDateRange;
176 }
177
178 @Override
179 public List<ArtifactMetadata> getArtifacts( String repositoryId )
180 {
181 return artifactsByDateRange;
182 }
183 }
184 }