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.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.security.ArchivaRoleConstants;
27 import org.codehaus.plexus.redback.role.RoleManager;
28 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
29 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.easymock.MockControl;
34 import java.io.IOException;
35 import java.util.Collections;
38 * EditManagedRepositoryActionTest
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class EditManagedRepositoryActionTest
44 extends PlexusInSpringTestCase
46 private EditManagedRepositoryAction 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 = (EditManagedRepositoryAction) lookup( Action.class.getName(), "editManagedRepositoryAction" );
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 testEditRepositoryInitialPage()
99 Configuration configuration = createConfigurationForEditing( createRepository() );
101 archivaConfiguration.getConfiguration();
102 archivaConfigurationControl.setReturnValue( configuration );
103 archivaConfigurationControl.replay();
105 action.setRepoid( REPO_ID );
108 assertEquals( REPO_ID, action.getRepoid() );
109 ManagedRepositoryConfiguration repository = action.getRepository();
110 assertNotNull( repository );
111 assertRepositoryEquals( repository, createRepository() );
113 String status = action.input();
114 assertEquals( Action.INPUT, status );
115 repository = action.getRepository();
116 assertRepositoryEquals( repository, createRepository() );
119 public void testEditRepository()
122 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
123 roleManagerControl.setReturnValue( false );
124 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
125 roleManagerControl.setVoidCallable();
126 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
127 roleManagerControl.setReturnValue( false );
128 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
129 roleManagerControl.setVoidCallable();
131 roleManagerControl.replay();
133 Configuration configuration = createConfigurationForEditing( createRepository() );
134 archivaConfiguration.getConfiguration();
135 archivaConfigurationControl.setReturnValue( configuration );
136 archivaConfigurationControl.setReturnValue( configuration );
138 archivaConfiguration.save( configuration );
140 archivaConfigurationControl.replay();
142 action.setRepoid( REPO_ID );
144 assertEquals( REPO_ID, action.getRepoid() );
145 ManagedRepositoryConfiguration repository = action.getRepository();
146 populateRepository( repository );
147 repository.setName( "new repo name" );
149 String status = action.commit();
150 assertEquals( Action.SUCCESS, status );
152 ManagedRepositoryConfiguration newRepository = createRepository();
153 newRepository.setName( "new repo name" );
154 assertRepositoryEquals( repository, newRepository );
155 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
157 roleManagerControl.verify();
158 archivaConfigurationControl.verify();
161 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
162 ManagedRepositoryConfiguration actualRepository )
164 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
165 assertEquals( expectedRepository.getId(), actualRepository.getId() );
166 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
167 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
168 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
169 assertEquals( expectedRepository.getName(), actualRepository.getName() );
170 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
171 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
172 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
173 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
174 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
175 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
178 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
180 Configuration configuration = new Configuration();
181 configuration.addManagedRepository( repositoryConfiguration );
182 return configuration;
185 private ManagedRepositoryConfiguration createRepository()
188 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
190 populateRepository( r );
194 private void populateRepository( ManagedRepositoryConfiguration repository )
197 repository.setId( REPO_ID );
198 repository.setName( "repo name" );
199 repository.setLocation( location.getCanonicalPath() );
200 repository.setLayout( "default" );
201 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
202 repository.setDaysOlder( 31 );
203 repository.setRetentionCount( 20 );
204 repository.setReleases( true );
205 repository.setSnapshots( true );
206 repository.setScanned( false );
207 repository.setDeleteReleasedSnapshots( true );