1 package org.apache.maven.archiva.web.action.admin.repositories;
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
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.Action;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.database.ArchivaAuditLogsDao;
28 import org.apache.maven.archiva.model.ArchivaAuditLogs;
29 import org.apache.maven.archiva.security.ArchivaRoleConstants;
30 import org.codehaus.plexus.redback.role.RoleManager;
31 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
32 import org.codehaus.redback.integration.interceptor.SecureActionException;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34 import org.easymock.MockControl;
37 import java.util.Collections;
40 * AddManagedRepositoryActionTest
44 public class AddManagedRepositoryActionTest
45 extends PlexusInSpringTestCase
47 private AddManagedRepositoryAction action;
49 private RoleManager roleManager;
51 private MockControl roleManagerControl;
53 private MockControl archivaConfigurationControl;
55 private ArchivaConfiguration archivaConfiguration;
57 private ArchivaAuditLogsDao auditLogsDao;
59 private MockControl auditLogsDaoControl;
61 private static final String REPO_ID = "repo-ident";
63 private File location;
66 protected String getPlexusConfigLocation()
68 return AbstractManagedRepositoriesAction.class.getName().replace( '.', '/' ) + "Test.xml";
72 protected void setUp()
77 action = (AddManagedRepositoryAction) lookup( Action.class.getName(), "addManagedRepositoryAction" );
79 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
80 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
81 action.setArchivaConfiguration( archivaConfiguration );
83 auditLogsDaoControl = MockControl.createControl( ArchivaAuditLogsDao.class );
84 auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
85 auditLogsDao = (ArchivaAuditLogsDao) auditLogsDaoControl.getMock();
86 action.setAuditLogsDao( auditLogsDao );
88 roleManagerControl = MockControl.createControl( RoleManager.class );
89 roleManager = (RoleManager) roleManagerControl.getMock();
90 action.setRoleManager( roleManager );
91 location = getTestFile( "target/test/location" );
94 public void testSecureActionBundle()
95 throws SecureActionException
97 archivaConfiguration.getConfiguration();
98 archivaConfigurationControl.setReturnValue( new Configuration() );
99 archivaConfigurationControl.replay();
102 SecureActionBundle bundle = action.getSecureActionBundle();
103 assertTrue( bundle.requiresAuthentication() );
104 assertEquals( 1, bundle.getAuthorizationTuples().size() );
107 public void testAddRepositoryInitialPage()
110 archivaConfiguration.getConfiguration();
111 archivaConfigurationControl.setReturnValue( new Configuration() );
112 archivaConfigurationControl.replay();
115 ManagedRepositoryConfiguration configuration = action.getRepository();
116 assertNotNull( configuration );
117 assertNull( configuration.getId() );
118 // check all booleans are false
119 assertFalse( configuration.isDeleteReleasedSnapshots() );
120 assertFalse( configuration.isScanned() );
121 assertFalse( configuration.isReleases() );
122 assertFalse( configuration.isSnapshots() );
124 String status = action.input();
125 assertEquals( Action.INPUT, status );
128 assertFalse( configuration.isDeleteReleasedSnapshots() );
129 assertTrue( configuration.isScanned() );
130 assertTrue( configuration.isReleases() );
131 assertFalse( configuration.isSnapshots() );
134 public void testAddRepository()
137 FileUtils.deleteDirectory( location );
139 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
140 roleManagerControl.setReturnValue( false );
141 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
142 roleManagerControl.setVoidCallable();
143 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
144 roleManagerControl.setReturnValue( false );
145 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
146 roleManagerControl.setVoidCallable();
148 roleManagerControl.replay();
150 Configuration configuration = new Configuration();
151 archivaConfiguration.getConfiguration();
152 archivaConfigurationControl.setReturnValue( configuration );
154 archivaConfiguration.save( configuration );
156 archivaConfigurationControl.replay();
159 ManagedRepositoryConfiguration repository = action.getRepository();
160 populateRepository( repository );
162 auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
163 auditLogsDaoControl.replay();
165 assertFalse( location.exists() );
166 String status = action.commit();
167 assertEquals( Action.SUCCESS, status );
168 assertTrue( location.exists() );
169 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
171 roleManagerControl.verify();
172 archivaConfigurationControl.verify();
173 auditLogsDaoControl.verify();
177 public void testAddRepositoryExistingLocation()
180 if( !location.exists() )
186 ManagedRepositoryConfiguration repository = action.getRepository();
187 populateRepository( repository );
189 assertTrue( location.exists() );
190 String status = action.commit();
191 assertEquals( AddManagedRepositoryAction.CONFIRM, status );
194 private void populateRepository( ManagedRepositoryConfiguration repository )
196 repository.setId( REPO_ID );
197 repository.setName( "repo name" );
198 repository.setLocation( location.getAbsolutePath() );
199 repository.setLayout( "default" );
200 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
201 repository.setDaysOlder( 31 );
202 repository.setRetentionCount( 20 );
203 repository.setReleases( true );
204 repository.setSnapshots( true );
205 repository.setScanned( false );
206 repository.setDeleteReleasedSnapshots( true );
209 // TODO: test errors during add, other actions