View Javadoc
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      @Override
62      public void setUp()
63          throws Exception
64      {
65          super.setUp();
66  
67          newVersionsProcessor = new NewVersionsOfArtifactRssFeedProcessor();
68          newVersionsProcessor.setGenerator( new RssFeedGenerator() );
69  
70          metadataRepositoryControl = createControl();
71          metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class );
72      }
73  
74      @SuppressWarnings("unchecked")
75      @Test
76      public void testProcess()
77          throws Exception
78      {
79          Date whenGathered = new Date( 123456789 );
80  
81          ArtifactMetadata artifact1 = createArtifact( whenGathered, "1.0.1" );
82          ArtifactMetadata artifact2 = createArtifact( whenGathered, "1.0.2" );
83  
84          Date whenGatheredNext = new Date( 345678912 );
85  
86          ArtifactMetadata artifact3 = createArtifact( whenGatheredNext, "1.0.3-SNAPSHOT" );
87  
88          Map<String, String> reqParams = new HashMap<>();
89          reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID );
90          reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID );
91  
92          expect( metadataRepository.getRepositories() ).andReturn( Collections.singletonList( TEST_REPO ) );
93          expect( metadataRepository.getProjectVersions( TEST_REPO, GROUP_ID, ARTIFACT_ID ) ).andReturn(
94              Arrays.asList( "1.0.1", "1.0.2", "1.0.3-SNAPSHOT" ) );
95          expect( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1" ) ).andReturn(
96              Collections.singletonList( artifact1 ) );
97          expect( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2" ) ).andReturn(
98              Collections.singletonList( artifact2 ) );
99          expect( metadataRepository.getArtifacts( TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT" ) ).andReturn(
100             Collections.singletonList( artifact3 ) );
101         metadataRepositoryControl.replay();
102 
103         SyndFeed feed = newVersionsProcessor.process( reqParams, metadataRepository );
104 
105         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle() );
106         assertEquals( "New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.",
107                       feed.getDescription() );
108         assertEquals( "en-us", feed.getLanguage() );
109         assertEquals( whenGatheredNext, feed.getPublishedDate() );
110 
111         List<SyndEntry> entries = feed.getEntries();
112 
113         assertEquals( 2, entries.size() );
114 
115         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGathered,
116                       entries.get( 0 ).getTitle() );
117         assertEquals( whenGathered, entries.get( 0 ).getPublishedDate() );
118 
119         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGatheredNext,
120                       entries.get( 1 ).getTitle() );
121         assertEquals( whenGatheredNext, entries.get( 1 ).getPublishedDate() );
122 
123         metadataRepositoryControl.verify();
124     }
125 
126     private ArtifactMetadata createArtifact( Date whenGathered, String version )
127     {
128         ArtifactMetadata artifact = new ArtifactMetadata();
129         artifact.setNamespace( GROUP_ID );
130         artifact.setProject( ARTIFACT_ID );
131         artifact.setProjectVersion( version );
132         artifact.setVersion( version );
133         artifact.setRepositoryId( TEST_REPO );
134         artifact.setId( ARTIFACT_ID + "-" + version + ".jar" );
135         artifact.setWhenGathered( whenGathered );
136         return artifact;
137     }
138 }