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;
44 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
50 * Test the repositories action returns the correct data.
52 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
53 public class RepositoriesActionTest
54 extends StrutsSpringTestCase
56 private Logger log = LoggerFactory.getLogger( getClass() );
58 private RepositoriesAction action;
60 ArchivaConfiguration originalArchivaConfiguration;
70 action = (RepositoriesAction) getActionProxy( "/admin/index.action" ).getAction();
71 originalArchivaConfiguration =
72 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).getArchivaConfiguration();
73 // some other test are modifying archivaConfiguration with a mocked instance : this test need the real one
74 // so use the real one from spring, backup the mock and restore it at the end (tearDown)
75 ArchivaConfiguration real = applicationContext.getBean( ArchivaConfiguration.class );
76 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration( real );
77 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration( real );
78 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration( real );
84 public void tearDown()
88 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration(
89 originalArchivaConfiguration );
90 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
91 originalArchivaConfiguration );
92 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
93 originalArchivaConfiguration );
97 protected String[] getContextLocations()
99 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
103 public void testGetRepositories()
108 MockControl control = MockControl.createControl( MetadataRepository.class );
109 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
110 control.expectAndReturn( metadataRepository.getMetadataFacets( "internal", RepositoryStatistics.FACET_ID ),
111 Arrays.asList( "20091125.123456.678" ) );
112 control.expectAndReturn(
113 metadataRepository.getMetadataFacet( "internal", RepositoryStatistics.FACET_ID, "20091125.123456.678" ),
114 new RepositoryStatistics() );
115 control.expectAndReturn( metadataRepository.getMetadataFacets( "snapshots", RepositoryStatistics.FACET_ID ),
116 Arrays.asList( "20091112.012345.012" ) );
117 control.expectAndReturn( metadataRepository.getMetadataFacet( "snapshots", RepositoryStatistics.FACET_ID,
118 "20091112.012345.012" ),
119 new RepositoryStatistics() );
122 RepositorySession session = mock( RepositorySession.class );
123 when( session.getRepository() ).thenReturn( metadataRepository );
124 TestRepositorySessionFactory factory =
125 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
126 factory.setRepositorySession( session );
128 ServletRunner sr = new ServletRunner();
129 ServletUnitClient sc = sr.newClient();
131 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositories.action" ).getRequest() );
134 String result = action.execute();
135 assertEquals( Action.SUCCESS, result );
137 // TODO: for some reason servletunit is not populating the port of the servlet request
138 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
140 assertNotNull( action.getManagedRepositories() );
141 assertNotNull( action.getRemoteRepositories() );
142 assertNotNull( action.getRepositoryStatistics() );
144 assertEquals( 2, action.getManagedRepositories().size() );
145 assertEquals( 1, action.getRemoteRepositories().size() );
146 assertEquals( 2, action.getRepositoryStatistics().size() );
150 catch ( Exception e )
152 log.error( e.getMessage(), e );
158 public void testSecureActionBundle()
159 throws SecureActionException
161 SecureActionBundle bundle = action.getSecureActionBundle();
162 assertTrue( bundle.requiresAuthentication() );
163 assertEquals( 1, bundle.getAuthorizationTuples().size() );