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;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.security.ArchivaRoleConstants;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.redback.role.RoleManager;
31 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
32 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
33 import org.easymock.MockControl;
36 import java.util.Collections;
39 * AddManagedRepositoryActionTest
41 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
44 public class AddManagedRepositoryActionTest
45 extends PlexusTestCase
47 private AddManagedRepositoryAction action;
49 private RoleManager roleManager;
51 private MockControl roleManagerControl;
53 private MockControl archivaConfigurationControl;
55 private ArchivaConfiguration archivaConfiguration;
57 private static final String REPO_ID = "repo-ident";
59 private File location;
61 protected void setUp()
66 action = (AddManagedRepositoryAction) lookup( Action.class.getName(), "addManagedRepositoryAction" );
68 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
69 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
70 action.setArchivaConfiguration( archivaConfiguration );
72 roleManagerControl = MockControl.createControl( RoleManager.class );
73 roleManager = (RoleManager) roleManagerControl.getMock();
74 action.setRoleManager( roleManager );
75 location = getTestFile( "target/test/location" );
78 public void testSecureActionBundle()
79 throws SecureActionException
81 archivaConfiguration.getConfiguration();
82 archivaConfigurationControl.setReturnValue( new Configuration() );
83 archivaConfigurationControl.replay();
86 SecureActionBundle bundle = action.getSecureActionBundle();
87 assertTrue( bundle.requiresAuthentication() );
88 assertEquals( 1, bundle.getAuthorizationTuples().size() );
91 public void testAddRepositoryInitialPage()
94 archivaConfiguration.getConfiguration();
95 archivaConfigurationControl.setReturnValue( new Configuration() );
96 archivaConfigurationControl.replay();
99 ManagedRepositoryConfiguration configuration = action.getRepository();
100 assertNotNull( configuration );
101 assertNull( configuration.getId() );
102 // check all booleans are false
103 assertFalse( configuration.isDeleteReleasedSnapshots() );
104 assertFalse( configuration.isScanned() );
105 assertFalse( configuration.isReleases() );
106 assertFalse( configuration.isSnapshots() );
108 String status = action.input();
109 assertEquals( Action.INPUT, status );
112 assertFalse( configuration.isDeleteReleasedSnapshots() );
113 assertTrue( configuration.isScanned() );
114 assertTrue( configuration.isReleases() );
115 assertFalse( configuration.isSnapshots() );
118 public void testAddRepository()
121 FileUtils.deleteDirectory( location );
123 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
124 roleManagerControl.setReturnValue( false );
125 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
126 roleManagerControl.setVoidCallable();
127 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
128 roleManagerControl.setReturnValue( false );
129 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
130 roleManagerControl.setVoidCallable();
132 roleManagerControl.replay();
134 Configuration configuration = new Configuration();
135 archivaConfiguration.getConfiguration();
136 archivaConfigurationControl.setReturnValue( configuration );
138 archivaConfiguration.save( configuration );
140 archivaConfigurationControl.replay();
143 ManagedRepositoryConfiguration repository = action.getRepository();
144 populateRepository( repository );
146 assertFalse( location.exists() );
147 String status = action.commit();
148 assertEquals( Action.SUCCESS, status );
149 assertTrue( location.exists() );
151 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
153 roleManagerControl.verify();
154 archivaConfigurationControl.verify();
157 private void populateRepository( ManagedRepositoryConfiguration repository )
159 repository.setId( REPO_ID );
160 repository.setName( "repo name" );
161 repository.setLocation( location.getAbsolutePath() );
162 repository.setLayout( "default" );
163 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
164 repository.setDaysOlder( 31 );
165 repository.setRetentionCount( 20 );
166 repository.setReleases( true );
167 repository.setSnapshots( true );
168 repository.setScanned( false );
169 repository.setDeleteReleasedSnapshots( true );
172 // TODO: test errors during add, other actions