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.repository.AbstractMetadataRepository;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.rss.RssFeedGenerator;
30 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
31 import org.easymock.EasyMock;
32 import org.easymock.IMocksControl;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
37 import java.time.LocalDateTime;
38 import java.time.ZoneId;
39 import java.time.ZonedDateTime;
40 import java.time.temporal.ChronoUnit;
41 import java.util.ArrayList;
42 import java.util.Calendar;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.TimeZone;
49 @RunWith (ArchivaBlockJUnit4ClassRunner.class)
50 public class NewArtifactsRssFeedProcessorTest
53 private static final String TEST_REPO = "test-repo";
55 private NewArtifactsRssFeedProcessor newArtifactsProcessor;
57 private MetadataRepositoryMock metadataRepository;
59 private IMocksControl sessionFactoryControl;
60 private RepositorySessionFactory sessionFactory;
62 private IMocksControl sessionControl;
63 private RepositorySession session;
72 newArtifactsProcessor = new NewArtifactsRssFeedProcessor();
73 newArtifactsProcessor.setGenerator( new RssFeedGenerator() );
75 metadataRepository = new MetadataRepositoryMock();
77 sessionFactoryControl = EasyMock.createControl();
78 sessionControl = EasyMock.createControl();
79 sessionControl.resetToNice();
81 sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
82 session = sessionControl.createMock( RepositorySession.class );
84 EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session );
85 EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository );
87 sessionFactoryControl.replay();
88 sessionControl.replay();
90 newArtifactsProcessor.setRepositorySessionFactory( sessionFactory );
94 @SuppressWarnings ("unchecked")
96 public void testProcess()
99 List<ArtifactMetadata> newArtifacts = new ArrayList<>();
100 ZonedDateTime whenGathered = ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault());
102 newArtifacts.add( createArtifact( "artifact-one", "1.0", whenGathered ) );
103 newArtifacts.add( createArtifact( "artifact-one", "1.1", whenGathered ) );
104 newArtifacts.add( createArtifact( "artifact-one", "2.0", whenGathered ) );
105 newArtifacts.add( createArtifact( "artifact-two", "1.0.1", whenGathered ) );
106 newArtifacts.add( createArtifact( "artifact-two", "1.0.2", whenGathered ) );
107 newArtifacts.add( createArtifact( "artifact-two", "1.0.3-SNAPSHOT", whenGathered ) );
108 newArtifacts.add( createArtifact( "artifact-three", "2.0-SNAPSHOT", whenGathered ) );
109 newArtifacts.add( createArtifact( "artifact-four", "1.1-beta-2", whenGathered ) );
111 metadataRepository.setArtifactsByDateRange( newArtifacts );
114 Map<String, String> reqParams = new HashMap<>();
115 reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
117 SyndFeed feed = newArtifactsProcessor.process( reqParams );
119 // check that the date used in the call is close to the one passed (5 seconds difference at most)
120 ZonedDateTime cal = ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).minusDays(30);
121 assertTrue(ChronoUnit.SECONDS.between(cal.toInstant(), metadataRepository.getFrom().toInstant())<5);
122 assertEquals( null, metadataRepository.getTo() );
123 assertEquals( TEST_REPO, metadataRepository.getRepoId() );
125 assertTrue( feed.getTitle().equals( "New Artifacts in Repository 'test-repo'" ) );
127 feed.getDescription().equals( "New artifacts found in repository 'test-repo' during repository scan." ) );
128 assertTrue( feed.getLanguage().equals( "en-us" ) );
129 assertTrue( feed.getPublishedDate().toInstant().equals( whenGathered.toInstant() ) );
131 List<SyndEntry> entries = feed.getEntries();
132 assertEquals( entries.size(), 1 );
134 entries.get( 0 ).getTitle().contains( "New Artifacts in Repository 'test-repo' as of " ));
135 assertTrue( entries.get( 0 ).getPublishedDate().toInstant().equals( whenGathered.toInstant() ) );
138 private ArtifactMetadata createArtifact( String artifactId, String version, ZonedDateTime whenGathered )
140 ArtifactMetadata artifact = new ArtifactMetadata();
141 artifact.setNamespace( "org.apache.archiva" );
142 artifact.setId( artifactId + "-" + version + ".jar" );
143 artifact.setRepositoryId( TEST_REPO );
144 artifact.setWhenGathered( whenGathered );
145 artifact.setProject( artifactId );
146 artifact.setProjectVersion( version );
147 artifact.setVersion( version );
151 // TODO: replace with mockito
152 private class MetadataRepositoryMock
153 extends AbstractMetadataRepository
155 private ZonedDateTime from, to;
157 private String repoId;
159 private List<ArtifactMetadata> artifactsByDateRange;
164 public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repoId, ZonedDateTime from, ZonedDateTime to )
169 return artifactsByDateRange;
172 public void setFrom(ZonedDateTime from )
177 public ZonedDateTime getFrom()
182 public void setTo(ZonedDateTime to )
187 public ZonedDateTime getTo()
192 public void setRepoId( String repoId )
194 this.repoId = repoId;
197 public String getRepoId()
202 public void setArtifactsByDateRange( List<ArtifactMetadata> artifactsByDateRange )
204 this.artifactsByDateRange = artifactsByDateRange;
208 public List<ArtifactMetadata> getArtifacts( RepositorySession session, String repositoryId )
210 return artifactsByDateRange;