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;
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)
60 public class RepositoryServletRepositoryGroupTest
61 extends AbstractRepositoryServletTestCase
63 protected File repoRootFirst;
65 protected File repoRootLast;
67 protected File repoRootInvalid;
69 protected static final String MANAGED_REPO_FIRST = "first";
71 protected static final String MANAGED_REPO_LAST = "last";
73 protected static final String MANAGED_REPO_INVALID = "invalid";
75 protected static final String REPO_GROUP_WITH_VALID_REPOS = "group-with-valid-repos";
77 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 );
122 protected void tearDown()
125 setupCleanRepo( repoRootFirst );
126 setupCleanRepo( repoRootLast );
134 public void testGetFromFirstManagedRepositoryReturnOk()
137 String resourceName = "dummy/dummy-first-resource/1.0/dummy-first-resource-1.0.txt";
139 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
140 dummyInternalResourceFile.getParentFile().mkdirs();
141 FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null );
143 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
144 WebResponse response = sc.getResponse( request );
146 assertResponseOK( response );
147 assertEquals( "Expected file contents", "first", response.getText() );
153 public void testGetFromLastManagedRepositoryReturnOk()
156 String resourceName = "dummy/dummy-last-resource/1.0/dummy-last-resource-1.0.txt";
158 File dummyReleasesResourceFile = new File( repoRootLast, resourceName );
159 dummyReleasesResourceFile.getParentFile().mkdirs();
160 FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null );
162 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
163 WebResponse response = sc.getResponse( request );
165 assertResponseOK( response );
166 assertEquals( "Expected file contents", "last", response.getText() );
172 public void testGetFromValidRepositoryGroupReturnNotFound()
175 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
177 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName );
178 WebResponse response = sc.getResponse( request );
180 assertResponseNotFound( response );
186 public void testGetInvalidManagedRepositoryInGroupReturnNotFound()
189 String resourceName = "dummy/dummy-no-resource/1.0/dummy-no-resource-1.0.txt";
191 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_INVALID_REPOS + "/" + resourceName );
192 WebResponse response = sc.getResponse( request );
194 assertResponseNotFound( response );
200 public void testPutValidRepositoryGroupReturnBadRequest()
203 String resourceName = "dummy/dummy-put-resource/1.0/dummy-put-resource-1.0.txt";
204 String putUrl = "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/" + resourceName;
205 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
207 WebRequest request = new PutMethodWebRequest( putUrl, is, "text/plain" );
208 WebResponse response = sc.getResponse( request );
210 assertResponseMethodNotAllowed( response );
213 public void testBrowseRepositoryGroup()
216 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS );
217 WebResponse response = sc.getResponse( request );
219 assertNotNull( "Should have received a response", response );
220 assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
224 public void testGetMergedMetadata()
227 // first metadata file
228 String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
230 File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
231 dummyInternalResourceFile.getParentFile().mkdirs();
232 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
233 "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n" +
234 "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n" +
235 "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>", null );
237 //second metadata file
238 resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
239 dummyInternalResourceFile = new File( repoRootLast, resourceName );
240 dummyInternalResourceFile.getParentFile().mkdirs();
241 FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
242 "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
243 "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>" +
244 "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>" +
245 "</versioning></metadata>", null );
248 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 //check if the checksum files were generated
264 File checksumFileSha1 = new File( repoRootFirst, resourceName + ".sha1" );
265 checksumFileSha1.getParentFile().mkdirs();
266 FileUtils.writeStringToFile( checksumFileSha1, "3290853214d3687134", null );
268 File checksumFileMd5 = new File( repoRootFirst, resourceName + ".md5" );
269 checksumFileMd5.getParentFile().mkdirs();
270 FileUtils.writeStringToFile( checksumFileMd5, "98745897234eda12836423", null );
272 // request the sha1 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.sha1" );
276 response = sc.getResource( request );
278 assertResponseOK( response );
279 assertEquals( "d2321a573e0488bca571b624f891104009408dd8 merged-maven-metadata.xml", response.getText() );
281 // request the md5 checksum of the metadata
283 new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
284 "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
285 response = sc.getResource( request );
287 assertResponseOK( response );
288 assertEquals( "79d271fbe8bd1d17b23273937750d407 merged-maven-metadata.xml", response.getText().trim() );
291 protected void assertResponseMethodNotAllowed( WebResponse response )
293 assertNotNull( "Should have recieved a response", response );
294 assertEquals( "Should have been an 405/Method Not Allowed response code.", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getResponseCode() );
297 protected RepositoryGroupConfiguration createRepositoryGroup( String id, List<String> repositories )
299 RepositoryGroupConfiguration repoGroupConfiguration = new RepositoryGroupConfiguration();
300 repoGroupConfiguration.setId( id );
301 repoGroupConfiguration.setRepositories( repositories );
302 return repoGroupConfiguration;