1 package org.apache.archiva.repository.base.group;
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.repository.EditableManagedRepository;
22 import org.apache.archiva.repository.EditableRepositoryGroup;
23 import org.apache.archiva.repository.ManagedRepository;
24 import org.apache.archiva.repository.RepositoryException;
25 import org.apache.archiva.repository.RepositoryGroup;
26 import org.apache.archiva.repository.RepositoryRegistry;
27 import org.apache.archiva.repository.base.ConfigurationHandler;
28 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
29 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
30 import org.apache.archiva.repository.mock.ManagedRepositoryContentMock;
31 import org.apache.archiva.repository.validation.ValidationResponse;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit.jupiter.SpringExtension;
38 import javax.inject.Inject;
39 import java.io.IOException;
40 import java.net.URISyntaxException;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
44 import static org.junit.jupiter.api.Assertions.*;
47 * @author Martin Stockhammer <martin_s@apache.org>
49 @ExtendWith( SpringExtension.class)
50 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
51 class BasicRepositoryGroupValidatorTest
55 ConfigurationHandler configurationHandler;
58 RepositoryRegistry repositoryRegistry;
60 @SuppressWarnings( "unused" )
62 RepositoryGroupHandler repositoryGroupHandler;
65 ManagedRepositoryHandler managedRepositoryHandler;
70 void cleanup() throws RepositoryException
72 repositoryRegistry.removeRepository("test");
75 protected EditableManagedRepository createRepository( String id, String name, Path location ) throws IOException
77 BasicManagedRepository repo = BasicManagedRepository.newFilesystemInstance(id, name, location);
78 repo.setLocation( location.toAbsolutePath().toUri());
79 repo.setContent(new ManagedRepositoryContentMock());
83 protected EditableRepositoryGroup createGroup(String id, String name) throws IOException
85 return BasicRepositoryGroup.newFilesystemInstance( id, name, getRepoBaseDir( ).resolve( id ) );
88 private Path getRepoBaseDir() {
89 if (repoBaseDir==null) {
92 repoBaseDir = Paths.get(Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories" ).toURI());
94 catch ( URISyntaxException e )
96 throw new RuntimeException( "Could not retrieve repository base directory" );
103 void apply( ) throws IOException
105 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
106 EditableRepositoryGroup group = createGroup( "test", "test" );
107 group.setMergedIndexTTL( 360 );
108 ValidationResponse<RepositoryGroup> result = validator.apply( group );
109 assertNotNull( result );
110 assertTrue( result.isValid( ) );
114 void applyWithExisting( ) throws IOException, RepositoryException
116 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
117 validator.setRepositoryRegistry( repositoryRegistry );
118 EditableRepositoryGroup group = createGroup( "test", "test" );
119 group.setMergedIndexTTL( 360 );
120 repositoryRegistry.putRepositoryGroup( group );
121 EditableRepositoryGroup group2 = createGroup( "test", "test2" );
122 group2.setMergedIndexTTL( 360 );
123 ValidationResponse<RepositoryGroup> result = validator.apply( group2 );
124 assertNotNull( result );
125 assertFalse( result.isValid( ) );
126 assertEquals( "group_exists", result.getResult( ).get( "id" ).get( 0 ).getType( ) );
130 void applyWithBadTTL( ) throws IOException
132 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
133 EditableRepositoryGroup group = createGroup( "test", "test" );
134 group.setMergedIndexTTL( 0 );
135 ValidationResponse<RepositoryGroup> result = validator.apply( group );
136 assertNotNull( result );
137 assertFalse( result.isValid( ) );
138 assertTrue( result.getResult( ).containsKey( "merged_index_ttl" ) );
139 assertEquals( "repository_group", result.getResult( ).get( "merged_index_ttl" ).get( 0 ).getCategory( ) );
140 assertEquals( "min", result.getResult( ).get( "merged_index_ttl" ).get( 0 ).getType( ) );
141 assertEquals( "merged_index_ttl", result.getResult( ).get( "merged_index_ttl" ).get( 0 ).getAttribute() );
145 void applyWithNullObject( ) throws IOException
147 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
148 EditableRepositoryGroup group = createGroup( "", "test" );
149 group.setMergedIndexTTL( 0 );
150 ValidationResponse<RepositoryGroup> result = validator.apply( null );
151 assertNotNull( result );
152 assertFalse( result.isValid( ) );
153 assertTrue( result.getResult( ).containsKey( "object" ) );
154 assertEquals( "repository_group", result.getResult( ).get( "object" ).get( 0 ).getCategory( ) );
155 assertEquals( "isnull", result.getResult( ).get( "object" ).get( 0 ).getType( ) );
156 assertEquals( "object", result.getResult( ).get( "object" ).get( 0 ).getAttribute() );
160 void applyWithEmptyId( ) throws IOException
162 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
163 EditableRepositoryGroup group = createGroup( "", "test" );
164 group.setMergedIndexTTL( 0 );
165 ValidationResponse<RepositoryGroup> result = validator.apply( group );
166 assertNotNull( result );
167 assertFalse( result.isValid( ) );
168 assertTrue( result.getResult( ).containsKey( "id" ) );
169 assertEquals( "repository_group", result.getResult( ).get( "id" ).get( 0 ).getCategory( ) );
170 assertEquals( "empty", result.getResult( ).get( "id" ).get( 0 ).getType( ) );
171 assertEquals( "id", result.getResult( ).get( "id" ).get( 0 ).getAttribute() );
175 void applyWithBadName( ) throws IOException
177 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
178 validator.setRepositoryRegistry( repositoryRegistry );
179 EditableRepositoryGroup group = createGroup( "test", "badtest\\name" );
180 group.setMergedIndexTTL( 360);
181 ValidationResponse<RepositoryGroup> result = validator.apply( group );
182 assertFalse( result.isValid( ) );
183 assertEquals( 1, result.getResult( ).size( ) );
184 assertEquals( "invalid_chars", result.getResult( ).get( "name" ).get( 0 ).getType( ) );
190 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
191 assertEquals( RepositoryGroup.class, validator.getFlavour( ) );
196 BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
197 assertTrue( validator.isFlavour( RepositoryGroup.class ) );
198 assertFalse( validator.isFlavour( ManagedRepository.class ) );
199 assertTrue( validator.isFlavour( BasicRepositoryGroup.class ) );