View Javadoc
1   package org.apache.archiva.remotedownload;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.admin.model.beans.ManagedRepository;
22  import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
23  import org.apache.archiva.rest.api.services.RepositoriesService;
24  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
25  import org.apache.commons.io.FileUtils;
26  import org.assertj.core.api.Assertions;
27  import org.junit.After;
28  import org.junit.AfterClass;
29  import org.junit.Assert;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  
34  import javax.ws.rs.RedirectionException;
35  import javax.ws.rs.core.Response;
36  import java.io.File;
37  import java.io.IOException;
38  import java.net.URI;
39  import java.nio.file.Files;
40  import java.nio.file.Path;
41  import java.nio.file.Paths;
42  
43  /**
44   * @author Olivier Lamy
45   */
46  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
47  public class DownloadArtifactFromQueryTest
48      extends AbstractDownloadTest
49  {
50  
51      @BeforeClass
52      public static void setAppServerBase()
53          throws IOException
54      {
55          previousAppServerBase = System.getProperty( "appserver.base" );
56          System.setProperty( "appserver.base",
57                              new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/"
58                                  + DownloadArtifactFromQueryTest.class.getName() );
59      }
60  
61      @AfterClass
62      public static void resetAppServerBase()
63      {
64          System.setProperty( "appserver.base", previousAppServerBase );
65      }
66  
67      @Override
68      protected String getSpringConfigLocation()
69      {
70          return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
71      }
72  
73      @After
74      public void cleanup()
75          throws Exception
76      {
77          super.tearDown();
78          Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
79          if ( Files.exists( tmpIndexDir ) )
80          {
81              FileUtils.deleteDirectory( tmpIndexDir.toFile() );
82          }
83      }
84  
85  
86      protected String createAndScanRepo()
87          throws Exception
88      {
89  
90          Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
91          if ( Files.exists( tmpIndexDir ) )
92          {
93              FileUtils.deleteDirectory( tmpIndexDir.toFile() );
94          }
95          String id = Long.toString( System.currentTimeMillis() );
96          ManagedRepository managedRepository = new ManagedRepository();
97          managedRepository.setId( id );
98          managedRepository.setName( "name of " + id );
99          managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
100         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
101 
102         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
103 
104         if ( managedRepositoriesService.getManagedRepository( id ) != null )
105         {
106             managedRepositoriesService.deleteManagedRepository( id, false );
107         }
108 
109         getManagedRepositoriesService().addManagedRepository( managedRepository );
110 
111         RepositoriesService repositoriesService = getRepositoriesService();
112 
113         repositoriesService.scanRepositoryNow( id, true );
114 
115         // wait a bit to ensure index is finished
116         int timeout = 20000;
117         while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
118         {
119             Thread.sleep( 500 );
120             timeout -= 500;
121         }
122 
123         return id;
124 
125     }
126 
127     @Test( expected = RedirectionException.class )
128     public void downloadFixedVersion()
129         throws Exception
130     {
131 
132         String id = createAndScanRepo();
133 
134         try
135         {
136             Response response =
137                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
138                                                            null );
139 
140         }
141         catch ( RedirectionException e )
142         {
143             Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
144                                                                            + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo(
145                 0 );
146             throw e;
147         }
148         finally
149         {
150             getManagedRepositoriesService().deleteManagedRepository( id, false );
151         }
152 
153     }
154 
155 
156     @Test( expected = RedirectionException.class )
157     public void downloadLatestVersion()
158         throws Exception
159     {
160         String id = createAndScanRepo();
161 
162         try
163         {
164             Response response =
165                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
166                                                            null );
167 
168         }
169         catch ( RedirectionException e )
170         {
171             Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
172                                                                            + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo(
173                 0 );
174             throw e;
175         }
176         finally
177         {
178             getManagedRepositoriesService().deleteManagedRepository( id, false );
179         }
180 
181     }
182 
183     @Test
184     public void download_no_content()
185         throws Exception
186     {
187         String id = createAndScanRepo();
188 
189         try
190         {
191             Response response =
192                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST",
193                                                            null, null );
194 
195             Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
196 
197 
198         }
199         finally
200         {
201             getManagedRepositoriesService().deleteManagedRepository( id, false );
202         }
203 
204     }
205 }