]> source.dussan.org Git - archiva.git/blob
f06c73cd18c8cbe9befa0f2e86618682b8cd41f2
[archiva.git] /
1 package org.apache.maven.archiva.webdav;
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 java.io.File;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
33
34 import com.meterware.httpunit.GetMethodWebRequest;
35 import com.meterware.httpunit.PutMethodWebRequest;
36 import com.meterware.httpunit.WebRequest;
37 import com.meterware.httpunit.WebResponse;
38
39
40 /**
41  * RepositoryServletRepositoryGroupTest
42  * 
43  * Test Case 1.  Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
44  * Test Case 2.  Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
45  * Test Case 3.  Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
46  *     Test Case 3.a.  If an invalid managed repository is encountered (managed repository doesn't exist),
47  *                     a Not Found (HTTP 404) is returned and the iteration is broken
48  *     Test Case 3.b.  If an artifact is not found in a valid managed repository (after proxying, etc.),
49  *                     a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
50  *                     The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
51  *     Test Case 3.c.  If an artifact is found in a valid managed repository,
52  *                     the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
53  * Test Case 4.  Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
54  *                     
55  * @author 
56  *
57  */
58 public class RepositoryServletRepositoryGroupTest
59     extends AbstractRepositoryServletTestCase
60 {
61     protected File repoRootFirst;
62     
63     protected File repoRootLast;
64     
65     protected File repoRootInvalid;
66     
67     protected static final String MANAGED_REPO_FIRST = "first";
68     
69     protected static final String MANAGED_REPO_LAST = "last";
70     
71     protected static final String MANAGED_REPO_INVALID = "invalid";
72     
73     protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
74
75     protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
76     
77     
78     protected void setUp()
79         throws Exception
80     {
81         super.setUp();
82         
83         String appserverBase = System.getProperty( "appserver.base" );
84         
85         Configuration configuration = archivaConfiguration.getConfiguration();
86         
87         repoRootFirst = new File( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
88         repoRootLast = new File( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
89         
90         configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
91         configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
92         
93         List<String> managedRepoIds = new ArrayList<String>();
94         managedRepoIds.add( MANAGED_REPO_FIRST );
95         managedRepoIds.add( MANAGED_REPO_LAST );
96         
97         configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
98         
99         // Create the repository group with an invalid managed repository
100         repoRootInvalid = new File( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
101         ManagedRepositoryConfiguration managedRepositoryConfiguration = createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid );
102         
103         configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
104         configuration.addManagedRepository( managedRepositoryConfiguration );
105         configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
106         
107         List<String> invalidManagedRepoIds = new ArrayList<String>();
108         invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
109         invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
110         invalidManagedRepoIds.add( MANAGED_REPO_LAST );
111         
112         configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
113         
114         configuration.removeManagedRepository( managedRepositoryConfiguration );
115         FileUtils.deleteDirectory( repoRootInvalid );
116         
117         saveConfiguration( archivaConfiguration );
118     }
119     
120     protected void tearDown()
121         throws Exception
122     {
123         setupCleanRepo( repoRootFirst );
124         setupCleanRepo( repoRootLast );
125         
126         super.tearDown();
127     }
128         
129     /*
130      * Test Case 3.c
131      */
132     public void testGetFromFirstManagedRepositoryReturnOk()
133         throws Exception
134     {
135         String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
136         
137         File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
138         dummyInternalResourceFile.getParentFile().mkdirs();
139         FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
140         
141         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
142         WebResponse response = sc.getResponse( request );
143         
144         assertResponseOK( response );
145         assertEquals( "Expected file contents", "first", response.getText() );
146     }
147     
148     /*
149      * Test Case 3.c
150      */
151     public void testGetFromLastManagedRepositoryReturnOk()
152         throws Exception
153     {        
154         String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
155         
156         File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
157         dummyReleasesResourceFile.getParentFile().mkdirs();
158         FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
159     
160         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
161         WebResponse response = sc.getResponse( request );
162         
163         assertResponseOK( response );
164         assertEquals( "Expected file contents", "last", response.getText() );
165     }
166     
167     /*
168      * Test Case 3.b
169      */
170     public void testGetFromValidRepositoryGroupReturnNotFound()
171         throws Exception
172     {
173         String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
174         
175         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
176         WebResponse response = sc.getResponse( request );
177         
178         assertResponseNotFound( response );
179     }
180     
181     /*
182      * Test Case 3.a
183      */
184     public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
185         throws Exception
186     {
187         String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
188         
189         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
190         WebResponse response = sc.getResponse( request );
191         
192         assertResponseNotFound( response );
193     }
194     
195     /*
196      * Test Case 4
197      */
198     public void testPutValidRepositoryGroupReturnBadRequest()
199         throws Exception
200     {
201         String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
202         String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
203         InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
204         
205         WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
206         WebResponse response = sc.getResponse( request );
207          
208         assertResponseMethodNotAllowed( response );
209     }
210     
211     public void testBrowseRepositoryGroup()
212         throws Exception
213     {
214         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS );
215         WebResponse response = sc.getResponse( request );
216                 
217         assertNotNull( "Should have received a response", response );
218         assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
219     }
220     
221     // MRM-872
222     public void testGetMergedMetadata()
223         throws Exception
224     {
225         // first metadata file
226         String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
227         
228         File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
229         dummyInternalResourceFile.getParentFile().mkdirs();
230         FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
231                         "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
232                         "<versioning><latest>1.0</latest><release>1.0</release><versions><version>1.0</version>" +
233                         "<version>2.5</version><lastUpdated>20080708095554</lastUpdated></versioning></metadata>", null );
234         
235         //second metadata file
236         resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";        
237         dummyInternalResourceFile = new File( repoRootLast, resourceName );
238         dummyInternalResourceFile.getParentFile().mkdirs();
239         FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
240                 "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
241                 "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>" +
242                 "<version>1.5</version><version>2.0</version><lastUpdated>20080709095554</lastUpdated>" +
243                 "</versioning></metadata>", null );
244         
245         WebRequest request =
246             new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
247                 "dummy-merged-metadata-resource/maven-metadata.xml" );
248         WebResponse response = sc.getResource( request );
249         
250         String expectedString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
251             "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
252             "<versioning><latest>2.5</latest><versions><version>1.0</version>" +
253             "<version>1.5</version><version>2.0</version><version>2.5</version><lastUpdated>20080709095554</lastUpdated>" +
254             "</versioning></metadata>"; 
255                 
256         //assertResponseOK( response );
257         //assertEquals( "Expected file contents", expectedString, response.getText() );
258     }
259         
260     protected void assertResponseMethodNotAllowed( WebResponse response )
261     {
262         assertNotNull( "Should have recieved a response", response );
263         assertEquals( "Should have been an 405/Method Not Allowed response code.", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getResponseCode() );
264     }
265
266     protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
267     {
268         RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
269         repoGroupConfiguration.setId( id );
270         repoGroupConfiguration.setRepositories( repositories );
271         return repoGroupConfiguration;
272     }
273 }