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 );
137 archivaConfigurationControl.setReturnValue( configuration );
139 archivaConfiguration.save( configuration );
141 archivaConfigurationControl.replay();
143 action.setRepoid( REPO_ID );
145 assertEquals( REPO_ID, action.getRepoid() );
146 ManagedRepositoryConfiguration repository = action.getRepository();
147 populateRepository( repository );
148 repository.setName( "new repo name" );
150 String status = action.commit();
151 assertEquals( Action.SUCCESS, status );
153 ManagedRepositoryConfiguration newRepository = createRepository();
154 newRepository.setName( "new repo name" );
155 assertRepositoryEquals( repository, newRepository );
156 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
158 roleManagerControl.verify();
159 archivaConfigurationControl.verify();
162 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
163 ManagedRepositoryConfiguration actualRepository )
165 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
166 assertEquals( expectedRepository.getId(), actualRepository.getId() );
167 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
168 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
169 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
170 assertEquals( expectedRepository.getName(), actualRepository.getName() );
171 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
172 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
173 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
174 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
175 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
176 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
179 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
181 Configuration configuration = new Configuration();
182 configuration.addManagedRepository( repositoryConfiguration );
183 return configuration;
186 private ManagedRepositoryConfiguration createRepository()
189 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
191 populateRepository( r );
195 private void populateRepository( ManagedRepositoryConfiguration repository )
198 repository.setId( REPO_ID );
199 repository.setName( "repo name" );
200 repository.setLocation( location.getCanonicalPath() );
201 repository.setLayout( "default" );
202 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
203 repository.setDaysOlder( 31 );
204 repository.setRetentionCount( 20 );
205 repository.setReleases( true );
206 repository.setSnapshots( true );
207 repository.setScanned( false );
208 repository.setDeleteReleasedSnapshots( true );