1 package org.apache.archiva.repository.base.managed;
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
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
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;
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;
45 import static org.junit.jupiter.api.Assertions.*;
48 * @author Martin Stockhammer <martin_s@apache.org>
50 @ExtendWith( SpringExtension.class)
51 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
52 class BasicManagedRepositoryValidatorTest
56 ConfigurationHandler configurationHandler;
59 RepositoryRegistry repositoryRegistry;
61 @SuppressWarnings( "unused" )
63 RepositoryGroupHandler repositoryGroupHandler;
71 repositoryRegistry.removeRepository( "test" );
73 catch ( RepositoryException e )
80 protected EditableManagedRepository createRepository( String id, String name, Path location ) throws IOException
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());
90 private Path getRepoBaseDir() {
91 if (repoBaseDir==null) {
94 repoBaseDir = Paths.get(Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories" ).toURI());
96 catch ( URISyntaxException e )
98 throw new RuntimeException( "Could not retrieve repository base directory" );
106 void apply( ) throws IOException
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( ) );
117 void applyWithExistingRepo( ) throws IOException, RepositoryException
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() );
136 void applyUpdateWithExistingRepo( ) throws IOException, RepositoryException
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( ) );
151 void applyWithNullObject( ) throws IOException
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() );
165 void applyWithEmptyId( ) throws IOException
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() );
181 void applyWithBadName( ) throws IOException
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( ) );
194 void applyWithBadSchedulingExpression( ) throws IOException
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( ) );
208 void applyForUpdate( )
215 BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
216 validator.setRepositoryRegistry( repositoryRegistry );
217 assertEquals( ManagedRepository.class, validator.getFlavour( ) );
223 BasicManagedRepositoryValidator validator = new BasicManagedRepositoryValidator( configurationHandler );
224 validator.setRepositoryRegistry( repositoryRegistry );
225 assertTrue( validator.isFlavour( ManagedRepository.class ) );
226 assertTrue( validator.isFlavour( BasicManagedRepository.class ) );