]> source.dussan.org Git - archiva.git/blob
bd1ee6770f4755939316ba725a87d55206d0197d
[archiva.git] /
1 package org.apache.archiva.rss.processor;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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.MetadataRepository;
27 import org.apache.archiva.rss.RssFeedGenerator;
28 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
29 import org.easymock.IMocksControl;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.Date;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 import static org.easymock.EasyMock.createControl;
42 import static org.easymock.EasyMock.expect;
43
44 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
45 public class NewVersionsOfArtifactRssFeedProcessorTest
46     extends TestCase
47 {
48     private NewVersionsOfArtifactRssFeedProcessor newVersionsProcessor;
49
50     private static final String TEST_REPO = "test-repo";
51
52     private static final String GROUP_ID = "org.apache.archiva";
53
54     private static final String ARTIFACT_ID = "artifact-two";
55
56     private IMocksControl metadataRepositoryControl;
57
58     private MetadataRepository metadataRepository;
59
60     @Before
61     public void setUp()
62         throws Exception
63     {
64         super.setUp();
65
66         newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor();
67         newVersionsProcessor.setGenerator( new RssFeedGenerator() );
68
69         metadataRepositoryControl = createControl();
70         metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class );
71     }
72
73     @SuppressWarnings("unchecked")
74     @Test
75     public void testProcess()
76         throws Exception
77     {
78         Date whenGathered = new Date( 123456789 );
79
80         ArtifactMetadata artifact1 = createArtifact( whenGathered, "1.0.1" );
81         ArtifactMetadata artifact2 = createArtifact( whenGathered, "1.0.2" );
82
83         Date whenGatheredNext = new Date( 345678912 );
84
85         ArtifactMetadata artifact3 = createArtifact( whenGatheredNext, "1.0.3-SNAPSHOT" );
86
87         Map<String, String> reqParams = new HashMap<String, String>();
88         reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID );
89         reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID );
90
91         //metadataRepositoryControl.expectAndReturn( metadataRepository.getRepositories(), Collections.singletonList(
92         //    TEST_REPO ) );
93         expect( metadataRepository.getRepositories() ).andReturn( Collections.singletonList( TEST_REPO ) );
94         //metadataRepositoryControl.expectAndReturn( metadataRepository.getProjectVersions( TEST_REPO, GROUP_ID,
95         //                                                                                  ARTIFACT_ID ), Arrays.asList(
96         //    "1.0.1", "1.0.2", "1.0.3-SNAPSHOT" ) );
97         expect( metadataRepository.getProjectVersions( TEST_REPO, GROUP_ID, ARTIFACT_ID ) ).andReturn(
98             Arrays.asList( "1.0.1", "1.0.2", "1.0.3-SNAPSHOT" ) );
99         //metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID,
100         //                                                                            "1.0.1" ),
101         //                                           Collections.singletonList( artifact1 ) );
102         expect( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1" ) ).andReturn(
103             Collections.singletonList( artifact1 ) );
104         //metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID,
105         //                                                                            "1.0.2" ),
106         //                                           Collections.singletonList( artifact2 ) );
107         expect( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2" ) ).andReturn(
108             Collections.singletonList( artifact2 ) );
109         //metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID,
110         //                                                                            "1.0.3-SNAPSHOT" ),
111         //                                           Collections.singletonList( artifact3 ) );
112         expect( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT" ) ).andReturn(
113             Collections.singletonList( artifact3 ) );
114         metadataRepositoryControl.replay();
115
116         SyndFeed feed = newVersionsProcessor.process( reqParams, metadataRepository );
117
118         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle() );
119         assertEquals( "New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.",
120                       feed.getDescription() );
121         assertEquals( "en-us", feed.getLanguage() );
122         assertEquals( whenGatheredNext, feed.getPublishedDate() );
123
124         List<SyndEntry> entries = feed.getEntries();
125
126         assertEquals( 2, entries.size() );
127
128         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGathered,
129                       entries.get( 0 ).getTitle() );
130         assertEquals( whenGathered, entries.get( 0 ).getPublishedDate() );
131
132         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGatheredNext,
133                       entries.get( 1 ).getTitle() );
134         assertEquals( whenGatheredNext, entries.get( 1 ).getPublishedDate() );
135
136         metadataRepositoryControl.verify();
137     }
138
139     private ArtifactMetadata createArtifact( Date whenGathered, String version )
140     {
141         ArtifactMetadata artifact = new ArtifactMetadata();
142         artifact.setNamespace( GROUP_ID );
143         artifact.setProject( ARTIFACT_ID );
144         artifact.setProjectVersion( version );
145         artifact.setVersion( version );
146         artifact.setRepositoryId( TEST_REPO );
147         artifact.setId( ARTIFACT_ID + "-" + version + ".jar" );
148         artifact.setWhenGathered( whenGathered );
149         return artifact;
150     }
151 }