]> source.dussan.org Git - archiva.git/blob
9470302db40f6350631eaa5e6b026dc03e216975
[archiva.git] /
1 package org.apache.archiva.repository.base.group;
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.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;
37
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;
43
44 import static org.junit.jupiter.api.Assertions.*;
45
46 /**
47  * @author Martin Stockhammer <martin_s@apache.org>
48  */
49 @ExtendWith( SpringExtension.class)
50 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
51 class BasicRepositoryGroupValidatorTest
52 {
53
54     @Inject
55     ConfigurationHandler configurationHandler;
56
57     @Inject
58     RepositoryRegistry repositoryRegistry;
59
60     @SuppressWarnings( "unused" )
61     @Inject
62     RepositoryGroupHandler repositoryGroupHandler;
63
64     @Inject
65     ManagedRepositoryHandler managedRepositoryHandler;
66
67     Path repoBaseDir;
68
69     @AfterEach
70     void cleanup() throws RepositoryException
71     {
72         repositoryRegistry.removeRepository("test");
73     }
74
75     protected EditableManagedRepository createRepository( String id, String name, Path location ) throws IOException
76     {
77         BasicManagedRepository repo = BasicManagedRepository.newFilesystemInstance(id, name, location);
78         repo.setLocation( location.toAbsolutePath().toUri());
79         repo.setContent(new ManagedRepositoryContentMock());
80         return repo;
81     }
82
83     protected EditableRepositoryGroup createGroup(String id, String name) throws IOException
84     {
85         return BasicRepositoryGroup.newFilesystemInstance( id, name, getRepoBaseDir( ).resolve( id ) );
86     }
87
88     private Path getRepoBaseDir() {
89         if (repoBaseDir==null) {
90             try
91             {
92                 repoBaseDir = Paths.get(Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories" ).toURI());
93             }
94             catch ( URISyntaxException e )
95             {
96                 throw new RuntimeException( "Could not retrieve repository base directory" );
97             }
98         }
99         return repoBaseDir;
100     }
101
102     @Test
103     void apply( ) throws IOException
104     {
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( ) );
111     }
112
113     @Test
114     void applyWithExisting( ) throws IOException, RepositoryException
115     {
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( ) );
127     }
128
129     @Test
130     void applyWithBadTTL( ) throws IOException
131     {
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() );
142     }
143
144     @Test
145     void applyWithNullObject( ) throws IOException
146     {
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() );
157     }
158
159     @Test
160     void applyWithEmptyId( ) throws IOException
161     {
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() );
172     }
173
174     @Test
175     void applyWithBadName( ) throws IOException
176     {
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( ) );
185     }
186
187     @Test
188     void getFlavour( )
189     {
190         BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
191         assertEquals( RepositoryGroup.class, validator.getFlavour( ) );
192     }
193
194     @Test
195     void isFlavour() {
196         BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator( configurationHandler );
197         assertTrue( validator.isFlavour( RepositoryGroup.class ) );
198         assertFalse( validator.isFlavour( ManagedRepository.class ) );
199         assertTrue( validator.isFlavour( BasicRepositoryGroup.class ) );
200     }
201 }