1 package org.apache.maven.archiva.web.repository;
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 );
133 public void testGetValidRepositoryGroupRootPathReturnBadRequest()
136 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS );
137 WebResponse response = sc.getResponse( request );
139 assertResponseBadRequest( response );
145 public void testGetFromFirstManagedRepositoryReturnOk()
148 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
150 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
151 dummyInternalResourceFile.getParentFile().mkdirs();
152 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
154 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
155 WebResponse response = sc.getResponse( request );
157 assertResponseOK( response );
158 assertEquals( "Expected file contents", "first", response.getText() );
164 public void testGetFromLastManagedRepositoryReturnOk()
167 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
169 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
170 dummyReleasesResourceFile.getParentFile().mkdirs();
171 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
173 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
174 WebResponse response = sc.getResponse( request );
176 assertResponseOK( response );
177 assertEquals( "Expected file contents", "last", response.getText() );
183 public void testGetFromValidRepositoryGroupReturnNotFound()
186 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
188 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
189 WebResponse response = sc.getResponse( request );
191 assertResponseNotFound( response );
197 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
200 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
202 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
203 WebResponse response = sc.getResponse( request );
205 assertResponseNotFound( response );
211 public void testPutValidRepositoryGroupReturnBadRequest()
214 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
215 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
216 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
218 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
219 WebResponse response = sc.getResponse( request );
221 assertResponseBadRequest( response );
225 protected void assertResponseBadRequest( WebResponse response )
227 assertNotNull( "Should have recieved a response", response );
228 assertEquals( "Should have been an 400/Bad Request response code.", HttpServletResponse.SC_BAD_REQUEST, response.getResponseCode() );
231 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
233 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
234 repoGroupConfiguration.setId( id );
235 repoGroupConfiguration.setRepositories( repositories );
236 return repoGroupConfiguration;