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.xwork.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.security.ArchivaRoleConstants;
28 import org.codehaus.plexus.redback.role.RoleManager;
29 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
30 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
31 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
32 import org.easymock.MockControl;
35 import java.util.Collections;
38 * AddManagedRepositoryActionTest
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class AddManagedRepositoryActionTest
44 extends PlexusInSpringTestCase
46 private AddManagedRepositoryAction action;
48 private RoleManager roleManager;
50 private MockControl roleManagerControl;
52 private MockControl archivaConfigurationControl;
54 private ArchivaConfiguration archivaConfiguration;
56 private static final String REPO_ID = "repo-ident";
58 private File location;
61 protected String getPlexusConfigLocation()
63 return AbstractManagedRepositoriesAction.class.getName().replace( '.', '/' ) + "Test.xml";
66 protected void setUp()
71 action = (AddManagedRepositoryAction) lookup( Action.class.getName(), "addManagedRepositoryAction" );
73 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
74 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
75 action.setArchivaConfiguration( archivaConfiguration );
77 roleManagerControl = MockControl.createControl( RoleManager.class );
78 roleManager = (RoleManager) roleManagerControl.getMock();
79 action.setRoleManager( roleManager );
80 location = getTestFile( "target/test/location" );
83 public void testSecureActionBundle()
84 throws SecureActionException
86 archivaConfiguration.getConfiguration();
87 archivaConfigurationControl.setReturnValue( new Configuration() );
88 archivaConfigurationControl.replay();
91 SecureActionBundle bundle = action.getSecureActionBundle();
92 assertTrue( bundle.requiresAuthentication() );
93 assertEquals( 1, bundle.getAuthorizationTuples().size() );
96 public void testAddRepositoryInitialPage()
99 archivaConfiguration.getConfiguration();
100 archivaConfigurationControl.setReturnValue( new Configuration() );
101 archivaConfigurationControl.replay();
104 ManagedRepositoryConfiguration configuration = action.getRepository();
105 assertNotNull( configuration );
106 assertNull( configuration.getId() );
107 // check all booleans are false
108 assertFalse( configuration.isDeleteReleasedSnapshots() );
109 assertFalse( configuration.isScanned() );
110 assertFalse( configuration.isReleases() );
111 assertFalse( configuration.isSnapshots() );
113 String status = action.input();
114 assertEquals( Action.INPUT, status );
117 assertFalse( configuration.isDeleteReleasedSnapshots() );
118 assertTrue( configuration.isScanned() );
119 assertTrue( configuration.isReleases() );
120 assertFalse( configuration.isSnapshots() );
123 public void testAddRepository()
126 FileUtils.deleteDirectory( location );
128 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
129 roleManagerControl.setReturnValue( false );
130 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
131 roleManagerControl.setVoidCallable();
132 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
133 roleManagerControl.setReturnValue( false );
134 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
135 roleManagerControl.setVoidCallable();
137 roleManagerControl.replay();
139 Configuration configuration = new Configuration();
140 archivaConfiguration.getConfiguration();
141 archivaConfigurationControl.setReturnValue( configuration );
143 archivaConfiguration.save( configuration );
145 archivaConfigurationControl.replay();
148 ManagedRepositoryConfiguration repository = action.getRepository();
149 populateRepository( repository );
151 assertFalse( location.exists() );
152 String status = action.commit();
153 assertEquals( Action.SUCCESS, status );
154 assertTrue( location.exists() );
156 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
158 roleManagerControl.verify();
159 archivaConfigurationControl.verify();
162 private void populateRepository( ManagedRepositoryConfiguration repository )
164 repository.setId( REPO_ID );
165 repository.setName( "repo name" );
166 repository.setLocation( location.getAbsolutePath() );
167 repository.setLayout( "default" );
168 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
169 repository.setDaysOlder( 31 );
170 repository.setRetentionCount( 20 );
171 repository.setReleases( true );
172 repository.setSnapshots( true );
173 repository.setScanned( false );
174 repository.setDeleteReleasedSnapshots( true );
177 // TODO: test errors during add, other actions