1 package org.apache.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.xwork2.Action;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.RemoteRepository;
25 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.struts2.StrutsSpringTestCase;
30 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
31 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
32 import org.easymock.MockControl;
34 import java.util.Collections;
36 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
41 * EditRemoteRepositoryActionTest
45 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
46 public class EditRemoteRepositoryActionTest
47 extends StrutsSpringTestCase
49 private static final String REPO_ID = "remote-repo-ident";
51 private EditRemoteRepositoryAction action;
53 private MockControl archivaConfigurationControl;
55 private ArchivaConfiguration archivaConfiguration;
58 protected String[] getContextLocations()
60 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
70 action = (EditRemoteRepositoryAction) getActionProxy( "/admin/editRemoteRepository.action" ).getAction();
72 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
73 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
75 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
76 archivaConfiguration );
80 public void testEditRemoteRepository()
83 Configuration configuration = createConfigurationForEditing( createRepository() );
84 archivaConfiguration.getConfiguration();
85 archivaConfigurationControl.setReturnValue( configuration, 4, 6 );
86 archivaConfiguration.save( configuration );
87 archivaConfigurationControl.replay();
89 action.setRepoid( REPO_ID );
92 assertEquals( REPO_ID, action.getRepoid() );
93 RemoteRepository repository = action.getRepository();
94 populateRepository( repository );
95 repository.setName( "new repo name" );
97 String status = action.commit();
98 assertEquals( Action.SUCCESS, status );
100 RemoteRepository newRepository = createRepository();
101 newRepository.setName( "new repo name" );
102 assertRepositoryEquals( repository, newRepository );
103 assertEquals( Collections.singletonList( repository ),
104 action.getRemoteRepositoryAdmin().getRemoteRepositories() );
106 archivaConfigurationControl.verify();
110 public void testEditRemoteRepositoryInitialPage()
113 Configuration configuration = createConfigurationForEditing( createRepository() );
115 archivaConfiguration.getConfiguration();
116 archivaConfigurationControl.setReturnValue( configuration, 2 );
117 archivaConfigurationControl.replay();
119 action.setRepoid( REPO_ID );
122 assertEquals( REPO_ID, action.getRepoid() );
123 RemoteRepository repository = action.getRepository();
124 assertNotNull( repository );
125 assertRepositoryEquals( repository, createRepository() );
127 String status = action.input();
128 assertEquals( Action.INPUT, status );
129 repository = action.getRepository();
130 assertRepositoryEquals( repository, createRepository() );
134 public void testSecureActionBundle()
135 throws SecureActionException, RepositoryAdminException
137 archivaConfiguration.getConfiguration();
138 archivaConfigurationControl.setReturnValue( new Configuration() );
139 archivaConfigurationControl.replay();
142 SecureActionBundle bundle = action.getSecureActionBundle();
143 assertTrue( bundle.requiresAuthentication() );
144 assertEquals( 1, bundle.getAuthorizationTuples().size() );
147 private void assertRepositoryEquals( RemoteRepository expectedRepository, RemoteRepository actualRepository )
149 assertEquals( expectedRepository.getId(), actualRepository.getId() );
150 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
151 assertEquals( expectedRepository.getUrl(), actualRepository.getUrl() );
152 assertEquals( expectedRepository.getName(), actualRepository.getName() );
155 private Configuration createConfigurationForEditing( RemoteRepository repositoryConfiguration )
157 Configuration configuration = new Configuration();
158 RemoteRepositoryConfiguration conf = new RemoteRepositoryConfiguration();
159 conf.setId( repositoryConfiguration.getId() );
160 conf.setLayout( repositoryConfiguration.getLayout() );
161 conf.setUrl( repositoryConfiguration.getUrl() );
162 conf.setName( repositoryConfiguration.getName() );
163 configuration.addRemoteRepository( conf );
164 return configuration;
167 private RemoteRepository createRepository()
169 RemoteRepository r = new RemoteRepository();
171 populateRepository( r );
175 private void populateRepository( RemoteRepository repository )
177 repository.setId( REPO_ID );
178 repository.setName( "repo name" );
179 repository.setUrl( "url" );
180 repository.setLayout( "default" );