]> source.dussan.org Git - archiva.git/blob
ed6e2868344d8501119c2bb8b8420607e4edf80b
[archiva.git] /
1 package org.apache.archiva.admin.repository.group;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.admin.model.beans.RepositoryGroup;
24 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
26 import org.apache.archiva.metadata.model.facets.AuditEvent;
27 import org.apache.archiva.repository.Repository;
28 import org.apache.archiva.repository.RepositoryRegistry;
29 import org.junit.Test;
30
31 import javax.inject.Inject;
32 import java.nio.file.Paths;
33 import java.util.Arrays;
34
35 /**
36  * @author Olivier Lamy
37  */
38 public class RepositoryGroupAdminTest
39     extends AbstractRepositoryAdminTest
40 {
41     @Inject
42     RepositoryGroupAdmin repositoryGroupAdmin;
43
44     @Inject
45     RepositoryRegistry repositoryRegistry;
46
47     @Test
48     public void addAndDeleteGroup()
49         throws Exception
50     {
51         try
52         {
53             Repository repo = repositoryRegistry.getRepository("test-new-one");
54             if (repo!=null) {
55                 repositoryRegistry.removeRepository(repo);
56             }
57             repo = repositoryRegistry.getRepository("test-new-two");
58             if (repo!=null) {
59                 repositoryRegistry.removeRepository(repo);
60             }
61             ManagedRepository managedRepositoryOne =
62                 getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH,"test-new-one" ).toString());
63
64             ManagedRepository managedRepositoryTwo =
65                 getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH, "test-new-two" ).toString());
66
67             managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
68
69             managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
70
71
72             RepositoryGroup repositoryGroup =
73                 new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one", "test-new-two" ) );
74             // repositoryGroupAdmin.deleteRepositoryGroup("repo-group-one", null);
75
76             mockAuditListener.clearEvents();
77
78             repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
79
80             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
81             assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
82             assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
83             assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
84                           repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
85
86             // verify if default values were saved
87             assertEquals(30, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getMergedIndexTtl() );
88             assertEquals(".indexer", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getMergedIndexPath() );
89
90             repositoryGroupAdmin.deleteRepositoryGroup( "repo-group-one", getFakeAuditInformation() );
91
92             assertEquals( 0, repositoryGroupAdmin.getRepositoriesGroups().size() );
93
94             assertEquals( 2, mockAuditListener.getAuditEvents().size() );
95
96             assertEquals( AuditEvent.ADD_REPO_GROUP, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
97             assertEquals( AuditEvent.DELETE_REPO_GROUP, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
98         }
99         finally
100         {
101             mockAuditListener.clearEvents();
102             repositoryRegistry.removeRepository(repositoryRegistry.getManagedRepository("test-new-one"));
103             repositoryRegistry.removeRepository(repositoryRegistry.getManagedRepository("test-new-two"));
104         }
105     }
106
107     @Test
108     public void addAndUpdateAndDeleteGroup()
109         throws Exception
110     {
111         try
112         {
113             ManagedRepository managedRepositoryOne =
114                 getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH,"test-new-one" ).toString());
115
116             ManagedRepository managedRepositoryTwo =
117                 getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH, "test-new-two" ).toString());
118
119             managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
120
121             managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
122
123             RepositoryGroup repositoryGroup = new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one" ) )
124                     .mergedIndexTtl( 20 ).mergedIndexPath( "/.nonDefaultPath" );
125
126             mockAuditListener.clearEvents();
127
128             repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
129
130             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
131             assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
132             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
133             assertEquals( Arrays.asList( "test-new-one" ),
134                           repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
135             assertEquals( 20, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getMergedIndexTtl() );
136             assertEquals( "/.nonDefaultPath", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getMergedIndexPath() );
137
138             repositoryGroup = repositoryGroupAdmin.getRepositoryGroup( "repo-group-one" );
139             assertNotNull( repositoryGroup );
140
141             repositoryGroup.addRepository( managedRepositoryTwo.getId() );
142
143             repositoryGroupAdmin.updateRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
144
145             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
146             assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
147             assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
148             assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
149                           repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
150
151             repositoryGroupAdmin.deleteRepositoryGroup( "repo-group-one", getFakeAuditInformation() );
152
153             assertEquals( 0, repositoryGroupAdmin.getRepositoriesGroups().size() );
154
155             assertEquals( 3, mockAuditListener.getAuditEvents().size() );
156
157             assertEquals( AuditEvent.ADD_REPO_GROUP, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
158             assertEquals( AuditEvent.MODIFY_REPO_GROUP, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
159             assertEquals( AuditEvent.DELETE_REPO_GROUP, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
160         }
161         finally
162         {
163             mockAuditListener.clearEvents();
164             managedRepositoryAdmin.deleteManagedRepository( "test-new-one", getFakeAuditInformation(), true );
165             managedRepositoryAdmin.deleteManagedRepository( "test-new-two", getFakeAuditInformation(), true );
166         }
167     }
168
169     @Test
170     public void addAndDeleteGroupWithRemovedManagedRepo()
171         throws Exception
172     {
173         try
174         {
175             ManagedRepository managedRepositoryOne =
176                 getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH , "test-new-one" ).toString());
177
178             ManagedRepository managedRepositoryTwo =
179                 getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH ,"test-new-two" ).toString());
180
181             managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
182
183             managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
184
185             RepositoryGroup repositoryGroup =
186                 new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one", "test-new-two" ) );
187
188             mockAuditListener.clearEvents();
189
190             repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
191
192             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
193             assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
194             assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
195             assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
196                           repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
197
198             // deleting a managed repo to validate repogroup correctly updated !
199             managedRepositoryAdmin.deleteManagedRepository( "test-new-one", getFakeAuditInformation(), true );
200
201             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
202             assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
203             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
204             assertEquals( Arrays.asList( "test-new-two" ),
205                           repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
206
207             repositoryGroupAdmin.deleteRepositoryGroup( "repo-group-one", getFakeAuditInformation() );
208
209             assertEquals( 0, repositoryGroupAdmin.getRepositoriesGroups().size() );
210
211             assertEquals( 3, mockAuditListener.getAuditEvents().size() );
212
213             assertEquals( AuditEvent.ADD_REPO_GROUP, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
214             assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
215             assertEquals( AuditEvent.DELETE_REPO_GROUP, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
216         }
217         finally
218         {
219             mockAuditListener.clearEvents();
220             repositoryRegistry.removeRepository(repositoryRegistry.getRepository("test-new-two"));
221         }
222     }
223
224     @Test( expected = RepositoryAdminException.class )
225     public void testAddGroupWithInvalidMergedIndexTtl() throws Exception {
226         try {
227             ManagedRepository managedRepositoryOne =
228                     getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH , "test-new-one" ).toString());
229
230             ManagedRepository managedRepositoryTwo =
231                     getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH , "test-new-two" ).toString());
232
233             managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
234
235             managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
236
237             RepositoryGroup repositoryGroup =
238                     new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one", "test-new-two" ) )
239                     .mergedIndexTtl( -1 );
240
241             mockAuditListener.clearEvents();
242
243             repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
244         }
245         finally
246         {
247             mockAuditListener.clearEvents();
248             repositoryRegistry.removeRepository(repositoryRegistry.getRepository("test-new-one"));
249             repositoryRegistry.removeRepository(repositoryRegistry.getRepository("test-new-two"));
250         }
251     }
252
253     @Test( expected = RepositoryAdminException.class )
254     public void testAddAndUpdateGroupWithInvalidMergedIndexTtl() throws Exception {
255         try {
256             ManagedRepository managedRepositoryOne =
257                     getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH , "test-new-one" ).toString());
258
259             ManagedRepository managedRepositoryTwo =
260                     getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH , "test-new-two" ).toString());
261
262             managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
263
264             managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
265
266             RepositoryGroup repositoryGroup =
267                     new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one", "test-new-two" ) );
268
269             mockAuditListener.clearEvents();
270
271             repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
272
273             assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
274             assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
275             assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
276             assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
277                     repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
278
279             // verify if default values were saved
280             assertEquals(30, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getMergedIndexTtl() );
281             assertEquals(".indexer", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getMergedIndexPath() );
282
283             repositoryGroup = repositoryGroupAdmin.getRepositoryGroup( "repo-group-one" );
284             assertNotNull( repositoryGroup );
285
286             repositoryGroup.mergedIndexTtl( -1 );
287
288             repositoryGroupAdmin.updateRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
289         }
290         finally
291         {
292             mockAuditListener.clearEvents();
293             managedRepositoryAdmin.deleteManagedRepository( "test-new-one", getFakeAuditInformation(), true );
294             managedRepositoryAdmin.deleteManagedRepository( "test-new-two", getFakeAuditInformation(), true );
295         }
296     }
297 }