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.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.PlexusTestCase;
29 import org.codehaus.plexus.redback.role.RoleManager;
30 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
31 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
32 import org.easymock.MockControl;
35 import java.util.Collections;
38 * EditManagedRepositoryActionTest
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class EditManagedRepositoryActionTest
44 extends PlexusTestCase
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;
60 protected void setUp()
65 action = (EditManagedRepositoryAction) lookup( Action.class.getName(), "editManagedRepositoryAction" );
67 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
68 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
69 action.setArchivaConfiguration( archivaConfiguration );
71 roleManagerControl = MockControl.createControl( RoleManager.class );
72 roleManager = (RoleManager) roleManagerControl.getMock();
73 action.setRoleManager( roleManager );
74 location = getTestFile( "target/test/location" );
77 public void testSecureActionBundle()
78 throws SecureActionException
80 archivaConfiguration.getConfiguration();
81 archivaConfigurationControl.setReturnValue( new Configuration() );
82 archivaConfigurationControl.replay();
85 SecureActionBundle bundle = action.getSecureActionBundle();
86 assertTrue( bundle.requiresAuthentication() );
87 assertEquals( 1, bundle.getAuthorizationTuples().size() );
90 public void testEditRepositoryInitialPage()
93 Configuration configuration = createConfigurationForEditing( createRepository() );
95 archivaConfiguration.getConfiguration();
96 archivaConfigurationControl.setReturnValue( configuration );
97 archivaConfigurationControl.replay();
99 action.setRepoid( REPO_ID );
102 assertEquals( REPO_ID, action.getRepoid() );
103 ManagedRepositoryConfiguration repository = action.getRepository();
104 assertNotNull( repository );
105 assertRepositoryEquals( repository, createRepository() );
107 String status = action.input();
108 assertEquals( Action.INPUT, status );
109 repository = action.getRepository();
110 assertRepositoryEquals( repository, createRepository() );
113 public void testEditRepository()
116 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
117 roleManagerControl.setReturnValue( false );
118 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
119 roleManagerControl.setVoidCallable();
120 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
121 roleManagerControl.setReturnValue( false );
122 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
123 roleManagerControl.setVoidCallable();
125 roleManagerControl.replay();
127 Configuration configuration = createConfigurationForEditing( createRepository() );
128 archivaConfiguration.getConfiguration();
129 archivaConfigurationControl.setReturnValue( configuration );
130 archivaConfigurationControl.setReturnValue( configuration );
132 archivaConfiguration.save( configuration );
134 archivaConfigurationControl.replay();
136 action.setRepoid( REPO_ID );
138 assertEquals( REPO_ID, action.getRepoid() );
139 ManagedRepositoryConfiguration repository = action.getRepository();
140 populateRepository( repository );
141 repository.setName( "new repo name" );
143 String status = action.commit();
144 assertEquals( Action.SUCCESS, status );
146 ManagedRepositoryConfiguration newRepository = createRepository();
147 newRepository.setName( "new repo name" );
148 assertRepositoryEquals( repository, newRepository );
149 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
151 roleManagerControl.verify();
152 archivaConfigurationControl.verify();
155 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
156 ManagedRepositoryConfiguration actualRepository )
158 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
159 assertEquals( expectedRepository.getId(), actualRepository.getId() );
160 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
161 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
162 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
163 assertEquals( expectedRepository.getName(), actualRepository.getName() );
164 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
165 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
166 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
167 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
168 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
169 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
172 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
174 Configuration configuration = new Configuration();
175 configuration.addManagedRepository( repositoryConfiguration );
176 return configuration;
179 private ManagedRepositoryConfiguration createRepository()
181 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
183 populateRepository( r );
187 private void populateRepository( ManagedRepositoryConfiguration repository )
189 repository.setId( REPO_ID );
190 repository.setName( "repo name" );
191 repository.setLocation( location.getAbsolutePath() );
192 repository.setLayout( "default" );
193 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
194 repository.setDaysOlder( 31 );
195 repository.setRetentionCount( 20 );
196 repository.setReleases( true );
197 repository.setSnapshots( true );
198 repository.setScanned( false );
199 repository.setDeleteReleasedSnapshots( true );