]> source.dussan.org Git - archiva.git/blob
341d8388e8ff82c1768c4549dd41cea3dd2c6bc0
[archiva.git] /
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             Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() );
141
142             //String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) );
143
144             //Assert.assertEquals( "http://localhost:" + port + "/repository/" + id
145             //                         + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar", location );
146         }
147         catch ( RedirectionException e )
148         {
149             Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
150                                                                            + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo(
151                 0 );
152             throw e;
153         }
154         finally
155         {
156             getManagedRepositoriesService().deleteManagedRepository( id, false );
157         }
158
159     }
160
161
162     @Test( expected = RedirectionException.class )
163     public void downloadLatestVersion()
164         throws Exception
165     {
166         String id = createAndScanRepo();
167
168         try
169         {
170             Response response =
171                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
172                                                            null );
173
174             Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() );
175
176             String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) );
177
178             /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar
179
180             //Assert.assertEquals( "http://localhost:" + port + "/repository/" + id
181             //                         + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar", location );
182
183         }
184         catch ( RedirectionException e )
185         {
186             Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
187                                                                            + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo(
188                 0 );
189             throw e;
190         }
191         finally
192         {
193             getManagedRepositoriesService().deleteManagedRepository( id, false );
194         }
195
196     }
197
198     @Test
199     public void download_no_content()
200         throws Exception
201     {
202         String id = createAndScanRepo();
203
204         try
205         {
206             Response response =
207                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST",
208                                                            null, null );
209
210             Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
211
212
213         }
214         finally
215         {
216             getManagedRepositoriesService().deleteManagedRepository( id, false );
217         }
218
219     }
220 }