]> source.dussan.org Git - archiva.git/blob
2fe4f671e524336cdbc50851500f4ebe7d8c1c11
[archiva.git] /
1 package org.apache.archiva.repository.base.managed;
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  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.common.filelock.DefaultFileLockManager;
22 import org.apache.archiva.common.filelock.FileLockManager;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.repository.EditableManagedRepository;
25 import org.apache.archiva.repository.ManagedRepository;
26 import org.apache.archiva.repository.RepositoryException;
27 import org.apache.archiva.repository.RepositoryRegistry;
28 import org.apache.archiva.repository.base.ConfigurationHandler;
29 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
30 import org.apache.archiva.repository.mock.ManagedRepositoryContentMock;
31 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
32 import org.apache.archiva.repository.validation.ValidationResponse;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.junit.jupiter.SpringExtension;
38
39 import javax.inject.Inject;
40 import java.io.IOException;
41 import java.net.URISyntaxException;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44
45 import static org.junit.jupiter.api.Assertions.*;
46
47 /**
48  * @author Martin Stockhammer <martin_s@apache.org>
49  */
50 @ExtendWith( SpringExtension.class)
51 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
52 class BasicManagedRepositoryValidatorTest
53 {
54
55     @Inject
56     ConfigurationHandler configurationHandler;
57
58     @Inject
59     RepositoryRegistry repositoryRegistry;
60
61     @SuppressWarnings( "unused" )
62     @Inject
63     RepositoryGroupHandler repositoryGroupHandler;
64
65     Path repoBaseDir;
66
67     @AfterEach
68     void cleanup() {
69         try
70         {
71             repositoryRegistry.removeRepository( "test" );
72         }
73         catch ( RepositoryException e )
74         {
75             // Ignore this
76         }
77
78     }
79
80     protected EditableManagedRepository createRepository( String id, String name, Path location ) throws IOException
81     {
82         FileLockManager lockManager = new DefaultFileLockManager();
83         FilesystemStorage storage = new FilesystemStorage(location.toAbsolutePath(), lockManager);
84         BasicManagedRepository repo = new BasicManagedRepository(id, name, storage);
85         repo.setLocation( location.toAbsolutePath().toUri());
86         repo.setContent(new ManagedRepositoryContentMock());
87         return repo;
88     }
89
90     private Path getRepoBaseDir() {
91         if (repoBaseDir==null) {
92             try
93             {
94                 repoBaseDir = Paths.get(Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories" ).toURI());
95             }
96             catch ( URISyntaxException e )
97             {
98                 throw new RuntimeException( "Could not retrieve repository base directory" );
99             }
100         }
101         return repoBaseDir;
102     }
103
104
105     @Test
106     void apply( ) throws IOException
107     {
108         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
109         validator.setRepositoryRegistry( repositoryRegistry );
110         Path repoDir = getRepoBaseDir().resolve("test" );
111         EditableManagedRepository repo = createRepository( "test", "test", repoDir );
112         ValidationResponse<ManagedRepository> result = validator.apply( repo );
113         assertTrue( result.isValid( ) );
114     }
115
116     @Test
117     void applyWithExistingRepo( ) throws IOException, RepositoryException
118     {
119         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
120         validator.setRepositoryRegistry( repositoryRegistry );
121         Path repoDir = getRepoBaseDir().resolve("test" );
122         EditableManagedRepository repo = createRepository( "test", "test", repoDir );
123         Path repoDir2 = getRepoBaseDir().resolve("test2" );
124         EditableManagedRepository repo2 = createRepository( "test", "test", repoDir2 );
125         repositoryRegistry.putRepository( repo );
126         ValidationResponse<ManagedRepository> result = validator.apply( repo );
127         assertFalse( result.isValid( ) );
128         assertEquals( 1, result.getResult( ).size( ) );
129         assertTrue( result.getResult( ).containsKey( "id" ) );
130         assertEquals( "managed_repository", result.getResult( ).get( "id" ).get( 0 ).getCategory( ) );
131         assertEquals( "managed_repo_exists", result.getResult( ).get( "id" ).get( 0 ).getType( ) );
132         assertEquals( "id", result.getResult( ).get( "id" ).get( 0 ).getAttribute() );
133     }
134
135     @Test
136     void applyUpdateWithExistingRepo( ) throws IOException, RepositoryException
137     {
138         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
139         validator.setRepositoryRegistry( repositoryRegistry );
140         Path repoDir = getRepoBaseDir().resolve("test" );
141         EditableManagedRepository repo = createRepository( "test", "test", repoDir );
142         Path repoDir2 = getRepoBaseDir().resolve("test2" );
143         EditableManagedRepository repo2 = createRepository( "test", "test", repoDir2 );
144         repositoryRegistry.putRepository( repo );
145         ValidationResponse<ManagedRepository> result = validator.applyForUpdate( repo );
146         assertTrue( result.isValid( ) );
147         assertEquals( 0, result.getResult( ).size( ) );
148     }
149
150     @Test
151     void applyWithNullObject( ) throws IOException
152     {
153         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
154         validator.setRepositoryRegistry( repositoryRegistry );
155         ValidationResponse<ManagedRepository> result = validator.apply( null );
156         assertFalse( result.isValid( ) );
157         assertEquals( 1, result.getResult( ).size( ) );
158         assertTrue( result.getResult( ).containsKey( "object" ) );
159         assertEquals( "managed_repository", result.getResult( ).get( "object" ).get( 0 ).getCategory( ) );
160         assertEquals( "isnull", result.getResult( ).get( "object" ).get( 0 ).getType( ) );
161         assertEquals( "object", result.getResult( ).get( "object" ).get( 0 ).getAttribute() );
162     }
163
164     @Test
165     void applyWithEmptyId( ) throws IOException
166     {
167         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
168         validator.setRepositoryRegistry( repositoryRegistry );
169         Path repoDir = getRepoBaseDir().resolve("test" );
170         EditableManagedRepository repo = createRepository( "", "test", repoDir );
171         ValidationResponse<ManagedRepository> result = validator.apply( repo );
172         assertFalse( result.isValid( ) );
173         assertEquals( 1, result.getResult( ).size( ) );
174         assertTrue( result.getResult( ).containsKey( "id" ) );
175         assertEquals( "managed_repository", result.getResult( ).get( "id" ).get( 0 ).getCategory( ) );
176         assertEquals( "empty", result.getResult( ).get( "id" ).get( 0 ).getType( ) );
177         assertEquals( "id", result.getResult( ).get( "id" ).get( 0 ).getAttribute() );
178     }
179
180     @Test
181     void applyWithBadName( ) throws IOException
182     {
183         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
184         validator.setRepositoryRegistry( repositoryRegistry );
185         Path repoDir = getRepoBaseDir().resolve("test" );
186         EditableManagedRepository repo = createRepository( "test", "badtest\\name", repoDir );
187         ValidationResponse<ManagedRepository> result = validator.apply( repo );
188         assertFalse( result.isValid( ) );
189         assertEquals( 1, result.getResult( ).size( ) );
190         assertEquals( "invalid_chars", result.getResult( ).get( "name" ).get( 0 ).getType( ) );
191     }
192
193     @Test
194     void applyWithBadSchedulingExpression( ) throws IOException
195     {
196         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
197         validator.setRepositoryRegistry( repositoryRegistry );
198         Path repoDir = getRepoBaseDir().resolve("test" );
199         EditableManagedRepository repo = createRepository( "test", "test", repoDir );
200         repo.setSchedulingDefinition( "xxxxx" );
201         ValidationResponse<ManagedRepository> result = validator.apply( repo );
202         assertFalse( result.isValid( ) );
203         assertEquals( 1, result.getResult( ).size( ) );
204         assertEquals( "invalid_scheduling_exp", result.getResult( ).get( "scheduling_definition" ).get( 0 ).getType( ) );
205     }
206
207     @Test
208     void applyForUpdate( )
209     {
210     }
211
212     @Test
213     void getFlavour( )
214     {
215         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
216         validator.setRepositoryRegistry( repositoryRegistry );
217         assertEquals( ManagedRepository.class, validator.getFlavour( ) );
218     }
219
220     @Test
221     void isFlavour( )
222     {
223         BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
224         validator.setRepositoryRegistry( repositoryRegistry );
225         assertTrue( validator.isFlavour( ManagedRepository.class ) );
226         assertTrue( validator.isFlavour( BasicManagedRepository.class ) );
227     }
228 }