View Javadoc
1   package org.apache.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 com.gargoylesoftware.htmlunit.WebRequest;
23  import com.gargoylesoftware.htmlunit.WebResponse;
24  import org.apache.archiva.admin.model.beans.ManagedRepository;
25  import org.apache.archiva.configuration.ArchivaConfiguration;
26  import org.apache.archiva.configuration.Configuration;
27  import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import java.io.File;
32  
33  import static org.assertj.core.api.Assertions.assertThat;
34  
35  /**
36   * RepositoryServletTest
37   */
38  public class RepositoryServletTest
39      extends AbstractRepositoryServletTestCase
40  {
41      private static final String REQUEST_PATH = "http://machine.com/repository/internal/";
42  
43      private static final String NEW_REPOSITORY_ID = "new-id";
44  
45      private static final String NEW_REPOSITORY_NAME = "New Repository";
46  
47      @Before
48      @Override
49      public void setUp()
50          throws Exception
51      {
52          super.setUp();
53          startRepository();
54      }
55  
56      @Test
57      public void testGetRepository()
58          throws Exception
59      {
60  
61          RepositoryServlet servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
62          assertNotNull( servlet );
63  
64          assertRepositoryValid( servlet, REPOID_INTERNAL );
65      }
66  
67  
68      @Test
69      public void testGetRepositoryAfterDelete()
70          throws Exception
71      {
72          RepositoryServlet servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
73  
74          assertNotNull( servlet );
75  
76          ArchivaConfiguration archivaConfiguration = servlet.getConfiguration();
77          Configuration c = archivaConfiguration.getConfiguration();
78          c.removeManagedRepository( c.findManagedRepositoryById( REPOID_INTERNAL ) );
79          saveConfiguration( archivaConfiguration );
80  
81          ManagedRepository repository = servlet.getRepository( REPOID_INTERNAL );
82          assertNull( repository );
83      }
84  
85      @Test
86      public void testGetRepositoryAfterAdd()
87          throws Exception
88      {
89          RepositoryServlet servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
90          assertNotNull( servlet );
91  
92          ArchivaConfiguration archivaConfiguration = servlet.getConfiguration();
93          Configuration c = archivaConfiguration.getConfiguration();
94          ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
95          repo.setId( NEW_REPOSITORY_ID );
96          repo.setName( NEW_REPOSITORY_NAME );
97          File repoRoot = new File( "target/test-repository-root" );
98          if ( !repoRoot.exists() )
99          {
100             repoRoot.mkdirs();
101         }
102         repo.setLocation( repoRoot.getAbsolutePath() );
103         c.addManagedRepository( repo );
104         saveConfiguration( archivaConfiguration );
105 
106         ManagedRepository repository = servlet.getRepository( NEW_REPOSITORY_ID );
107         assertNotNull( repository );
108         assertEquals( NEW_REPOSITORY_NAME, repository.getName() );
109 
110         // check other is still intact
111         assertRepositoryValid( servlet, REPOID_INTERNAL );
112     }
113 
114     @Test
115     public void testGetRepositoryInvalidPathPassthroughPresent()
116         throws Exception
117     {
118         String path = REQUEST_PATH + ".index/filecontent/segments.gen";
119 
120         populateRepo( repoRootInternal, ".index/filecontent/segments.gen", "index file" );
121 
122         WebRequest request = new GetMethodWebRequest( path );
123         WebResponse response = getServletUnitClient().getResponse( request );
124         assertResponseOK( response );
125         assertEquals( "index file", response.getContentAsString() );
126     }
127 
128     @Test
129     public void testGetRepositoryInvalidPathPassthroughMissing()
130         throws Exception
131     {
132         String path = REQUEST_PATH + ".index/filecontent/foo.bar";
133 
134         WebRequest request = new GetMethodWebRequest( path );
135         WebResponse response = getServletUnitClient().getResponse( request );
136         assertResponseNotFound( response );
137         assertThat( response.getContentAsString() ) //
138             .contains( "Legacy Maven1 repository not supported anymore." );
139     }
140 }