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.meterware.servletunit.ServletRunner;
23 import com.meterware.servletunit.ServletUnitClient;
24 import com.opensymphony.xwork2.Action;
25 import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
26 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
27 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
28 import org.apache.archiva.metadata.repository.MetadataRepository;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
31 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
32 import org.apache.archiva.configuration.ArchivaConfiguration;
33 import org.apache.struts2.StrutsSpringTestCase;
34 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
36 import org.easymock.MockControl;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import java.util.Arrays;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
46 * Test the repositories action returns the correct data.
48 public class RepositoriesActionTest
49 extends StrutsSpringTestCase
51 private Logger log = LoggerFactory.getLogger( getClass() );
53 private RepositoriesAction action;
55 ArchivaConfiguration originalArchivaConfiguration;
57 protected void setUp()
63 action = (RepositoriesAction) getActionProxy( "/admin/index.action" ).getAction();
64 originalArchivaConfiguration =
65 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).getArchivaConfiguration();
66 // some other test are modifying archivaConfiguration with a mocked instance : this test need the real one
67 // so use the real one from spring, backup the mock and restore it at the end (tearDown)
68 ArchivaConfiguration real = applicationContext.getBean( ArchivaConfiguration.class );
69 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration( real );
70 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration( real );
71 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration( real );
76 protected void tearDown()
80 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration(
81 originalArchivaConfiguration );
82 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
83 originalArchivaConfiguration );
84 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
85 originalArchivaConfiguration );
89 protected String[] getContextLocations()
91 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
94 public void testGetRepositories()
99 MockControl control = MockControl.createControl( MetadataRepository.class );
100 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
101 control.expectAndReturn( metadataRepository.getMetadataFacets( "internal", RepositoryStatistics.FACET_ID ),
102 Arrays.asList( "20091125.123456.678" ) );
103 control.expectAndReturn(
104 metadataRepository.getMetadataFacet( "internal", RepositoryStatistics.FACET_ID, "20091125.123456.678" ),
105 new RepositoryStatistics() );
106 control.expectAndReturn( metadataRepository.getMetadataFacets( "snapshots", RepositoryStatistics.FACET_ID ),
107 Arrays.asList( "20091112.012345.012" ) );
108 control.expectAndReturn( metadataRepository.getMetadataFacet( "snapshots", RepositoryStatistics.FACET_ID,
109 "20091112.012345.012" ),
110 new RepositoryStatistics() );
113 RepositorySession session = mock( RepositorySession.class );
114 when( session.getRepository() ).thenReturn( metadataRepository );
115 TestRepositorySessionFactory factory =
116 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
117 factory.setRepositorySession( session );
119 ServletRunner sr = new ServletRunner();
120 ServletUnitClient sc = sr.newClient();
122 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositories.action" ).getRequest() );
125 String result = action.execute();
126 assertEquals( Action.SUCCESS, result );
128 // TODO: for some reason servletunit is not populating the port of the servlet request
129 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
131 assertNotNull( action.getManagedRepositories() );
132 assertNotNull( action.getRemoteRepositories() );
133 assertNotNull( action.getRepositoryStatistics() );
135 assertEquals( 2, action.getManagedRepositories().size() );
136 assertEquals( 1, action.getRemoteRepositories().size() );
137 assertEquals( 2, action.getRepositoryStatistics().size() );
141 catch ( Exception e )
143 log.error( e.getMessage(), e );
148 public void testSecureActionBundle()
149 throws SecureActionException
151 SecureActionBundle bundle = action.getSecureActionBundle();
152 assertTrue( bundle.requiresAuthentication() );
153 assertEquals( 1, bundle.getAuthorizationTuples().size() );