1 package org.apache.maven.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.servlet.http.HttpServletResponse;
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;
34 import com.meterware.httpunit.GetMethodWebRequest;
35 import com.meterware.httpunit.PutMethodWebRequest;
36 import com.meterware.httpunit.WebRequest;
37 import com.meterware.httpunit.WebResponse;
41 * RepositoryServletRepositoryGroupTest
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)
58 public class RepositoryServletRepositoryGroupTest
59 extends AbstractRepositoryServletTestCase
61 protected File repoRootFirst;
63 protected File repoRootLast;
65 protected File repoRootInvalid;
67 protected static final String MANAGED_REPO_FIRST = "first";
69 protected static final String MANAGED_REPO_LAST = "last";
71 protected static final String MANAGED_REPO_INVALID = "invalid";
73 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
75 protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
78 protected void setUp()
83 String appserverBase = System.getProperty( "appserver.base" );
85 Configuration configuration = archivaConfiguration.getConfiguration();
87 repoRootFirst = new File( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
88 repoRootLast = new File( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
90 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
91 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
93 List<String> managedRepoIds = new ArrayList<String>();
94 managedRepoIds.add( MANAGED_REPO_FIRST );
95 managedRepoIds.add( MANAGED_REPO_LAST );
97 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
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 );
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 ) );
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 );
112 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
114 configuration.removeManagedRepository( managedRepositoryConfiguration );
115 FileUtils.deleteDirectory( repoRootInvalid );
117 saveConfiguration( archivaConfiguration );
120 protected void tearDown()
123 setupCleanRepo( repoRootFirst );
124 setupCleanRepo( repoRootLast );
132 public void testGetFromFirstManagedRepositoryReturnOk()
135 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
137 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
138 dummyInternalResourceFile.getParentFile().mkdirs();
139 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
141 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
142 WebResponse response = sc.getResponse( request );
144 assertResponseOK( response );
145 assertEquals( "Expected file contents", "first", response.getText() );
151 public void testGetFromLastManagedRepositoryReturnOk()
154 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
156 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
157 dummyReleasesResourceFile.getParentFile().mkdirs();
158 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
160 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
161 WebResponse response = sc.getResponse( request );
163 assertResponseOK( response );
164 assertEquals( "Expected file contents", "last", response.getText() );
170 public void testGetFromValidRepositoryGroupReturnNotFound()
173 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
175 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
176 WebResponse response = sc.getResponse( request );
178 assertResponseNotFound( response );
184 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
187 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
189 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
190 WebResponse response = sc.getResponse( request );
192 assertResponseNotFound( response );
198 public void testPutValidRepositoryGroupReturnBadRequest()
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" );
205 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
206 WebResponse response = sc.getResponse( request );
208 assertResponseMethodNotAllowed( response );
211 public void testBrowseRepositoryGroup()
214 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS );
215 WebResponse response = sc.getResponse( request );
217 assertNotNull( "Should have received a response", response );
218 assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
222 public void testGetMergedMetadata()
225 // first metadata file
226 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
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 );
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 );
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 );
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>";
256 //assertResponseOK( response );
257 //assertEquals( "Expected file contents", expectedString, response.getText() );
260 protected void assertResponseMethodNotAllowed( WebResponse response )
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() );
266 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
268 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
269 repoGroupConfiguration.setId( id );
270 repoGroupConfiguration.setRepositories( repositories );
271 return repoGroupConfiguration;