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