1 package org.apache.archiva.web.servlet;
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;
33 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
34 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
36 import com.meterware.httpunit.GetMethodWebRequest;
37 import com.meterware.httpunit.PutMethodWebRequest;
38 import com.meterware.httpunit.WebRequest;
39 import com.meterware.httpunit.WebResponse;
43 * RepositoryServletRepositoryGroupTest
45 * Test Case 1. Accessing a valid repository group root url (e.g. http://machine.com/repository/repository-group/) returns a Bad Request (HTTP 400)
46 * Test Case 2. Accessing an invalid repository group root url is forwarded to managed repository checking (this is not covered here)
47 * Test Case 3. Accessing an artifact in a valid repository group will iterate over the managed repositories in the repository group
48 * Test Case 3.a. If an invalid managed repository is encountered (managed repository doesn't exist),
49 * a Not Found (HTTP 404) is returned and the iteration is broken
50 * Test Case 3.b. If an artifact is not found in a valid managed repository (after proxying, etc.),
51 * a Not Found (HTTP 404) is set but not returned yet, the iteration continues to the next managed repository.
52 * The Not Found (HTTP 404) is returned after exhausting all valid managed repositories
53 * Test Case 3.c. If an artifact is found in a valid managed repository,
54 * the artifact is returned, the iteration is broken and any Not Found (HTTP 404) is disregarded
55 * Test Case 4. Accessing a valid repository group with any http write method returns a Bad Request (HTTP 400)
59 public class RepositoryServletRepositoryGroupTest
60 extends AbstractRepositoryServletTestCase
62 protected File repoRootFirst;
64 protected File repoRootLast;
66 protected File repoRootInvalid;
68 protected static final String MANAGED_REPO_FIRST = "first";
70 protected static final String MANAGED_REPO_LAST = "last";
72 protected static final String MANAGED_REPO_INVALID = "invalid";
74 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
76 protected static final String REPO_GROUP_WITH_INVALID_REPOS = "group-with-invalid-repos";
80 protected void setUp()
85 String appserverBase = System.getProperty( "appserver.base" );
87 Configuration configuration = archivaConfiguration.getConfiguration();
89 repoRootFirst = new File( appserverBase, "data/repositories/" + MANAGED_REPO_FIRST );
90 repoRootLast = new File( appserverBase, "data/repositories/" + MANAGED_REPO_LAST );
92 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
93 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
95 List<String> managedRepoIds = new ArrayList<String>();
96 managedRepoIds.add( MANAGED_REPO_FIRST );
97 managedRepoIds.add( MANAGED_REPO_LAST );
99 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_VALID_REPOS, managedRepoIds ) );
101 // Create the repository group with an invalid managed repository
102 repoRootInvalid = new File( appserverBase, "data/repositories/" + MANAGED_REPO_INVALID );
103 ManagedRepositoryConfiguration managedRepositoryConfiguration = createManagedRepository( MANAGED_REPO_INVALID, "Invalid Test Repo", repoRootInvalid );
105 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_FIRST, "First Test Repo", repoRootFirst ) );
106 configuration.addManagedRepository( managedRepositoryConfiguration );
107 configuration.addManagedRepository( createManagedRepository( MANAGED_REPO_LAST, "Last Test Repo", repoRootLast ) );
109 List<String> invalidManagedRepoIds = new ArrayList<String>();
110 invalidManagedRepoIds.add( MANAGED_REPO_FIRST );
111 invalidManagedRepoIds.add( MANAGED_REPO_INVALID );
112 invalidManagedRepoIds.add( MANAGED_REPO_LAST );
114 configuration.addRepositoryGroup( createRepositoryGroup( REPO_GROUP_WITH_INVALID_REPOS, invalidManagedRepoIds ) );
116 configuration.removeManagedRepository( managedRepositoryConfiguration );
117 FileUtils.deleteDirectory( repoRootInvalid );
119 saveConfiguration( archivaConfiguration );
123 protected void tearDown()
126 setupCleanRepo( repoRootFirst );
127 setupCleanRepo( repoRootLast );
135 public void testGetFromFirstManagedRepositoryReturnOk()
138 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
140 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
141 dummyInternalResourceFile.getParentFile().mkdirs();
142 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
144 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
145 WebResponse response = sc.getResponse( request );
147 assertResponseOK( response );
148 assertEquals( "Expected file contents", "first", response.getText() );
154 public void testGetFromLastManagedRepositoryReturnOk()
157 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
159 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
160 dummyReleasesResourceFile.getParentFile().mkdirs();
161 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
163 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
164 WebResponse response = sc.getResponse( request );
166 assertResponseOK( response );
167 assertEquals( "Expected file contents", "last", response.getText() );
173 public void testGetFromValidRepositoryGroupReturnNotFound()
176 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
178 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
179 WebResponse response = sc.getResponse( request );
181 assertResponseNotFound( response );
187 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
190 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
192 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
193 WebResponse response = sc.getResponse( request );
195 assertResponseNotFound( response );
201 public void testPutValidRepositoryGroupReturnBadRequest()
204 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
205 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
206 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
208 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
209 WebResponse response = sc.getResponse( request );
211 assertResponseMethodNotAllowed( response );
214 public void testBrowseRepositoryGroup()
217 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS );
218 WebResponse response = sc.getResponse( request );
220 assertNotNull( "Should have received a response", response );
221 assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
225 public void testGetMergedMetadata()
228 // first metadata file
229 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
231 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
232 dummyInternalResourceFile.getParentFile().mkdirs();
233 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
234 "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n" +
235 "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n" +
236 "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>", null );
238 //second metadata file
239 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
240 dummyInternalResourceFile = new File( repoRootLast, resourceName );
241 dummyInternalResourceFile.getParentFile().mkdirs();
242 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
243 "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
244 "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>" +
245 "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>" +
246 "</versioning></metadata>", null );
248 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
249 "dummy-merged-metadata-resource/maven-metadata.xml" );
250 WebResponse response = sc.getResource( request );
252 File returnedMetadata = new File( getBasedir(), "/target/test-classes/retrievedMetadataFile.xml");
253 FileUtils.writeStringToFile( returnedMetadata, response.getText() );
254 ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( returnedMetadata );
256 assertResponseOK( response );
257 assertEquals( "Versions list size", 4, metadata.getAvailableVersions().size() );
258 assertTrue( "Versions list contains version 1.0", metadata.getAvailableVersions().contains( "1.0" ) );
259 assertTrue( "Versions list contains version 1.5", metadata.getAvailableVersions().contains( "1.5" ) );
260 assertTrue( "Versions list contains version 2.0", metadata.getAvailableVersions().contains( "2.0" ) );
261 assertTrue( "Versions list contains version 2.5", metadata.getAvailableVersions().contains( "2.5" ) );
263 // request the sha1 checksum of the metadata
265 new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
266 "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
267 response = sc.getResource( request );
269 assertResponseOK( response );
270 assertEquals( "add113b0d7f8c6adb92a5015a7a3701081edf998", response.getText() );
272 // request the md5 checksum of the metadata
274 new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
275 "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
276 response = sc.getResource( request );
278 assertResponseOK( response );
279 assertEquals( "5b85ea4aa5f52bb76760041a52f98de8", response.getText().trim() );
282 protected void assertResponseMethodNotAllowed( WebResponse response )
284 assertNotNull( "Should have recieved a response", response );
285 assertEquals( "Should have been an 405/Method Not Allowed response code.", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getResponseCode() );
288 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
290 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
291 repoGroupConfiguration.setId( id );
292 repoGroupConfiguration.setRepositories( repositories );
293 return repoGroupConfiguration;