--- /dev/null
+package org.apache.archiva.admin.repository.group;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.AuditInformation;
+import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
+import org.apache.archiva.admin.repository.RepositoryAdminException;
+import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
+import org.apache.archiva.audit.AuditEvent;
+import org.apache.commons.lang.StringUtils;
+import org.apache.maven.archiva.configuration.Configuration;
+import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author Olivier Lamy
+ */
+@Service( "repositoryGroupAdmin#default" )
+public class DefaultRepositoryGroupAdmin
+ extends AbstractRepositoryAdmin
+ implements RepositoryGroupAdmin
+{
+
+ private Logger log = LoggerFactory.getLogger( getClass() );
+
+ private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
+
+ @Inject
+ private ManagedRepositoryAdmin managedRepositoryAdmin;
+
+ public List<RepositoryGroup> getRepositoriesGroups()
+ throws RepositoryAdminException
+ {
+ List<RepositoryGroup> repositoriesGroups = new ArrayList<RepositoryGroup>();
+
+ for ( RepositoryGroupConfiguration repositoryGroupConfiguration : getArchivaConfiguration().getConfiguration().getRepositoryGroups() )
+ {
+ repositoriesGroups.add( new RepositoryGroup( repositoryGroupConfiguration.getId(), new ArrayList<String>(
+ repositoryGroupConfiguration.getRepositories() ) ) );
+ }
+
+ return repositoriesGroups;
+ }
+
+ public RepositoryGroup getRepositoryGroup( String repositoryGroupId )
+ throws RepositoryAdminException
+ {
+ List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
+ for ( RepositoryGroup repositoryGroup : repositoriesGroups )
+ {
+ if ( StringUtils.equals( repositoryGroupId, repositoryGroup.getId() ) )
+ {
+ return repositoryGroup;
+ }
+ }
+ return null;
+ }
+
+ public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
+ throws RepositoryAdminException
+ {
+ validateRepositoryGroup( repositoryGroup, false );
+ validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
+ RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
+ repositoryGroupConfiguration.setId( repositoryGroup.getId() );
+ repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
+ Configuration configuration = getArchivaConfiguration().getConfiguration();
+ configuration.addRepositoryGroup( repositoryGroupConfiguration );
+ saveConfiguration( configuration );
+ triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
+ return Boolean.TRUE;
+ }
+
+ public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
+ throws RepositoryAdminException
+ {
+ Configuration configuration = getArchivaConfiguration().getConfiguration();
+ RepositoryGroupConfiguration repositoryGroupConfiguration =
+ configuration.getRepositoryGroupsAsMap().get( repositoryGroupId );
+ if ( repositoryGroupConfiguration == null )
+ {
+ throw new RepositoryAdminException(
+ "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot remove" );
+ }
+ configuration.removeRepositoryGroup( repositoryGroupConfiguration );
+ triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
+ return Boolean.TRUE;
+ }
+
+ public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
+ throws RepositoryAdminException
+ {
+ return updateRepositoryGroup( repositoryGroup, auditInformation, true );
+ }
+
+ private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
+ boolean triggerAuditEvent )
+ throws RepositoryAdminException
+ {
+ validateRepositoryGroup( repositoryGroup, true );
+ validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
+ Configuration configuration = getArchivaConfiguration().getConfiguration();
+
+ RepositoryGroupConfiguration repositoryGroupConfiguration =
+ configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
+
+ configuration.removeRepositoryGroup( repositoryGroupConfiguration );
+
+ repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
+ configuration.addRepositoryGroup( repositoryGroupConfiguration );
+
+ saveConfiguration( configuration );
+ if ( triggerAuditEvent )
+ {
+ triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
+ }
+ return Boolean.TRUE;
+ }
+
+
+ public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
+ AuditInformation auditInformation )
+ throws RepositoryAdminException
+ {
+ RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
+ if ( repositoryGroup == null )
+ {
+ throw new RepositoryAdminException(
+ "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
+ }
+
+ if ( repositoryGroup.getRepositories().contains( repositoryId ) )
+ {
+ log.info( "repositoryGroup {} already contains repository {} so skip adding it" );
+ return Boolean.TRUE;
+ }
+ validateManagedRepositoriesExists( Arrays.asList( repositoryId ) );
+
+ repositoryGroup.addRepository( repositoryId );
+ updateRepositoryGroup( repositoryGroup, auditInformation, false );
+ triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
+ return Boolean.TRUE;
+ }
+
+ public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
+ AuditInformation auditInformation )
+ throws RepositoryAdminException
+ {
+ RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
+ if ( repositoryGroup == null )
+ {
+ throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
+ + " doesn't not exists so cannot remove repository from it" );
+ }
+
+ if ( !repositoryGroup.getRepositories().contains( repositoryId ) )
+ {
+ log.info( "repositoryGroup {} already contains repository {} so skip removing it" );
+ return Boolean.TRUE;
+ }
+
+ repositoryGroup.removeRepository( repositoryId );
+ updateRepositoryGroup( repositoryGroup, auditInformation, false );
+ triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
+ return Boolean.TRUE;
+ }
+
+ public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
+ throws RepositoryAdminException
+ {
+ String repoGroupId = repositoryGroup.getId();
+ if ( StringUtils.isBlank( repoGroupId ) )
+ {
+ throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
+ }
+
+ if ( repoGroupId.length() > 100 )
+ {
+ throw new RepositoryAdminException(
+ "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
+
+ }
+
+ Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
+ if ( !matcher.matches() )
+ {
+ throw new RepositoryAdminException(
+ "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
+ }
+
+ Configuration configuration = getArchivaConfiguration().getConfiguration();
+
+ if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
+ {
+ if ( !updateMode )
+ {
+ throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
+ + "], that id already exists as a repository group." );
+ }
+ }
+ else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
+ {
+ throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
+ + "], that id already exists as a managed repository." );
+ }
+ else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
+ {
+ throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
+ + "], that id already exists as a remote repository." );
+ }
+
+ return Boolean.TRUE;
+ }
+
+ private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
+ throws RepositoryAdminException
+ {
+ for ( String id : managedRepositoriesIds )
+ {
+ if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
+ {
+ throw new RepositoryAdminException(
+ "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
+ }
+ }
+ }
+
+ public ManagedRepositoryAdmin getManagedRepositoryAdmin()
+ {
+ return managedRepositoryAdmin;
+ }
+
+ public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
+ {
+ this.managedRepositoryAdmin = managedRepositoryAdmin;
+ }
+}
--- /dev/null
+package org.apache.archiva.admin.repository.group;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+public class RepositoryGroup
+ implements Serializable
+{
+ /**
+ * repository group Id
+ */
+ private String id;
+
+ /**
+ * repositories ids
+ */
+ private List<String> repositories;
+
+ public RepositoryGroup()
+ {
+ // no op
+ }
+
+ public RepositoryGroup( String id, List<String> repositories )
+ {
+ this.id = id;
+ this.repositories = repositories;
+ }
+
+ /**
+ * Method addRepository.
+ *
+ * @param string
+ */
+ public void addRepository( String string )
+ {
+ getRepositories().add( string );
+ }
+
+ /**
+ * Get the id of the repository group.
+ *
+ * @return String
+ */
+ public String getId()
+ {
+ return this.id;
+ }
+
+ /**
+ * Method getRepositories.
+ *
+ * @return List
+ */
+ public java.util.List<String> getRepositories()
+ {
+ if ( this.repositories == null )
+ {
+ this.repositories = new ArrayList<String>();
+ }
+
+ return this.repositories;
+ }
+
+ /**
+ * Method removeRepository.
+ *
+ * @param string
+ */
+ public void removeRepository( String string )
+ {
+ getRepositories().remove( string );
+ }
+
+ /**
+ * Set the id of the repository group.
+ *
+ * @param id
+ */
+ public void setId( String id )
+ {
+ this.id = id;
+ }
+
+ /**
+ * Set the list of repository ids under the group.
+ *
+ * @param repositories
+ */
+ public void setRepositories( List<String> repositories )
+ {
+ this.repositories = repositories;
+ }
+}
--- /dev/null
+package org.apache.archiva.admin.repository.group;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.AuditInformation;
+import org.apache.archiva.admin.repository.RepositoryAdminException;
+
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+public interface RepositoryGroupAdmin
+{
+ List<RepositoryGroup> getRepositoriesGroups()
+ throws RepositoryAdminException;
+
+ RepositoryGroup getRepositoryGroup( String repositoryGroupId )
+ throws RepositoryAdminException;
+
+ Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
+ throws RepositoryAdminException;
+
+ Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
+ throws RepositoryAdminException;
+
+ Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
+ throws RepositoryAdminException;
+
+ Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId, AuditInformation auditInformation )
+ throws RepositoryAdminException;
+
+ Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
+ AuditInformation auditInformation )
+ throws RepositoryAdminException;
+}
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
+import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.role.RoleManagerException;
import org.codehaus.plexus.taskqueue.TaskQueueException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
return managedRepos;
}
+ public Map<String, ManagedRepository> getManagedRepositoriesAsMap()
+ throws RepositoryAdminException
+ {
+ List<ManagedRepository> managedRepositories = getManagedRepositories();
+ Map<String, ManagedRepository> repositoriesMap =
+ new HashMap<String, ManagedRepository>( managedRepositories.size() );
+ for ( ManagedRepository managedRepository : managedRepositories )
+ {
+ repositoriesMap.put( managedRepository.getId(), managedRepository );
+ }
+ return repositoriesMap;
+ }
+
public ManagedRepository getManagedRepository( String repositoryId )
throws RepositoryAdminException
{
}
+ // FIXME cleanup repositoryGroups when deleting a ManagedRepo
public Boolean deleteManagedRepository( String repositoryId, AuditInformation auditInformation,
boolean deleteContent )
throws RepositoryAdminException
List<String> repoGroups = repoToGroupMap.get( repository.getId() );
for ( String repoGroup : repoGroups )
{
- config.findRepositoryGroupById( repoGroup ).removeRepository( repository.getId() );
+ // copy to prevent UnsupportedOperationException
+ RepositoryGroupConfiguration repositoryGroupConfiguration = config.findRepositoryGroupById( repoGroup );
+ List<String> repos = new ArrayList<String>( repositoryGroupConfiguration.getRepositories() );
+ config.removeRepositoryGroup( repositoryGroupConfiguration );
+ repos.remove( repository.getId() );
+ repositoryGroupConfiguration.setRepositories( repos );
+ config.addRepositoryGroup( repositoryGroupConfiguration );
}
}
}
import org.apache.archiva.admin.repository.RepositoryAdminException;
import java.util.List;
+import java.util.Map;
/**
* @author Olivier Lamy
List<ManagedRepository> getManagedRepositories()
throws RepositoryAdminException;
+ Map<String, ManagedRepository> getManagedRepositoriesAsMap()
+ throws RepositoryAdminException;
+
ManagedRepository getManagedRepository( String repositoryId )
throws RepositoryAdminException;
import junit.framework.TestCase;
import org.apache.archiva.admin.AuditInformation;
import org.apache.archiva.admin.mock.MockAuditListener;
+import org.apache.archiva.admin.repository.managed.ManagedRepository;
+import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
+import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.users.User;
import org.codehaus.plexus.redback.users.memory.SimpleUser;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
+import java.io.File;
+import java.util.List;
/**
* @author Olivier Lamy
@Inject
protected MockAuditListener mockAuditListener;
+ @Inject
+ protected RoleManager roleManager;
+
+ @Inject
+ protected RemoteRepositoryAdmin remoteRepositoryAdmin;
+
+ @Inject
+ protected ManagedRepositoryAdmin managedRepositoryAdmin;
+
protected AuditInformation getFakeAuditInformation()
{
AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
user.setFullName( "The top user" );
return user;
}
+
+ protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
+ {
+ return new ManagedRepository( repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
+ repoLocation + "/.index", false, 1, 2, true );
+ }
+
+ protected File clearRepoLocation( String path )
+ throws Exception
+ {
+ File repoDir = new File( path );
+ if ( repoDir.exists() )
+ {
+ FileUtils.deleteDirectory( repoDir );
+ }
+ assertFalse( repoDir.exists() );
+ return repoDir;
+ }
+
+ protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
+ {
+ for ( ManagedRepository repo : repos )
+ {
+ if ( StringUtils.equals( id, repo.getId() ) )
+ {
+ return repo;
+ }
+ }
+ return null;
+ }
}
--- /dev/null
+package org.apache.archiva.admin.repository.group;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
+import org.apache.archiva.admin.repository.managed.ManagedRepository;
+import org.apache.archiva.audit.AuditEvent;
+import org.junit.Test;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.util.Arrays;
+
+/**
+ * @author Olivier Lamy
+ */
+public class RepositoryGroupAdminTest
+ extends AbstractRepositoryAdminTest
+{
+ @Inject
+ RepositoryGroupAdmin repositoryGroupAdmin;
+
+ @Test
+ public void addAndDeleteGroup()
+ throws Exception
+ {
+ try
+ {
+ ManagedRepository managedRepositoryOne =
+ getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+
+ ManagedRepository managedRepositoryTwo =
+ getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+
+ managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
+
+ managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
+
+ RepositoryGroup repositoryGroup =
+ new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one", "test-new-two" ) );
+
+ mockAuditListener.clearEvents();
+
+ repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
+
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
+ assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
+ assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
+ assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
+ repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
+
+ repositoryGroupAdmin.deleteRepositoryGroup( "repo-group-one", getFakeAuditInformation() );
+
+ assertEquals( 0, repositoryGroupAdmin.getRepositoriesGroups().size() );
+
+ assertEquals( 2, mockAuditListener.getAuditEvents().size() );
+
+ assertEquals( AuditEvent.ADD_REPO_GROUP, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
+ assertEquals( AuditEvent.DELETE_REPO_GROUP, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
+ }
+ finally
+ {
+ mockAuditListener.clearEvents();
+ managedRepositoryAdmin.deleteManagedRepository( "test-new-one", getFakeAuditInformation(), true );
+ managedRepositoryAdmin.deleteManagedRepository( "test-new-two", getFakeAuditInformation(), true );
+ }
+ }
+
+ @Test
+ public void addAndUpdateAndDeleteGroup()
+ throws Exception
+ {
+ try
+ {
+ ManagedRepository managedRepositoryOne =
+ getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+
+ ManagedRepository managedRepositoryTwo =
+ getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+
+ managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
+
+ managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
+
+ RepositoryGroup repositoryGroup = new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one" ) );
+
+ mockAuditListener.clearEvents();
+
+ repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
+
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
+ assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
+ assertEquals( Arrays.asList( "test-new-one" ),
+ repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
+
+ repositoryGroup = repositoryGroupAdmin.getRepositoryGroup( "repo-group-one" );
+ assertNotNull( repositoryGroup );
+
+ repositoryGroup.addRepository( managedRepositoryTwo.getId() );
+
+ repositoryGroupAdmin.updateRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
+
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
+ assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
+ assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
+ assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
+ repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
+
+ repositoryGroupAdmin.deleteRepositoryGroup( "repo-group-one", getFakeAuditInformation() );
+
+ assertEquals( 0, repositoryGroupAdmin.getRepositoriesGroups().size() );
+
+ assertEquals( 3, mockAuditListener.getAuditEvents().size() );
+
+ assertEquals( AuditEvent.ADD_REPO_GROUP, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
+ assertEquals( AuditEvent.MODIFY_REPO_GROUP, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
+ assertEquals( AuditEvent.DELETE_REPO_GROUP, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
+ }
+ finally
+ {
+ mockAuditListener.clearEvents();
+ managedRepositoryAdmin.deleteManagedRepository( "test-new-one", getFakeAuditInformation(), true );
+ managedRepositoryAdmin.deleteManagedRepository( "test-new-two", getFakeAuditInformation(), true );
+ }
+ }
+
+
+ @Test
+ public void addAndDeleteGroupWithRemowingManagedRepo()
+ throws Exception
+ {
+ try
+ {
+ ManagedRepository managedRepositoryOne =
+ getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+
+ ManagedRepository managedRepositoryTwo =
+ getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+
+ managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
+
+ managedRepositoryAdmin.addManagedRepository( managedRepositoryTwo, false, getFakeAuditInformation() );
+
+ RepositoryGroup repositoryGroup =
+ new RepositoryGroup( "repo-group-one", Arrays.asList( "test-new-one", "test-new-two" ) );
+
+ mockAuditListener.clearEvents();
+
+ repositoryGroupAdmin.addRepositoryGroup( repositoryGroup, getFakeAuditInformation() );
+
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
+ assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
+ assertEquals( 2, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
+ assertEquals( Arrays.asList( "test-new-one", "test-new-two" ),
+ repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
+
+ // deleting a managed repo to validate repogroup correctly updated !
+ managedRepositoryAdmin.deleteManagedRepository( "test-new-one", getFakeAuditInformation(), true );
+
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().size() );
+ assertEquals( "repo-group-one", repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getId() );
+ assertEquals( 1, repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories().size() );
+ assertEquals( Arrays.asList( "test-new-two" ),
+ repositoryGroupAdmin.getRepositoriesGroups().get( 0 ).getRepositories() );
+
+ repositoryGroupAdmin.deleteRepositoryGroup( "repo-group-one", getFakeAuditInformation() );
+
+ assertEquals( 0, repositoryGroupAdmin.getRepositoriesGroups().size() );
+
+ assertEquals( 3, mockAuditListener.getAuditEvents().size() );
+
+ assertEquals( AuditEvent.ADD_REPO_GROUP, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
+ assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
+ assertEquals( AuditEvent.DELETE_REPO_GROUP, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
+ }
+ finally
+ {
+ mockAuditListener.clearEvents();
+
+ managedRepositoryAdmin.deleteManagedRepository( "test-new-two", getFakeAuditInformation(), true );
+ }
+ }
+}
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
-import org.codehaus.plexus.redback.role.RoleManager;
import org.junit.Test;
-import javax.inject.Inject;
import java.io.File;
import java.util.List;
public class ManagedRepositoryAdminTest
extends AbstractRepositoryAdminTest
{
-
- @Inject
- private ManagedRepositoryAdmin managedRepositoryAdmin;
-
-
-
- @Inject
- protected RoleManager roleManager;
-
public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
@Test
ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
-
-
managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
assertTemplateRoleExists( repoId );
}
- private File clearRepoLocation( String path )
- throws Exception
- {
- File repoDir = new File( path );
- if ( repoDir.exists() )
- {
- FileUtils.deleteDirectory( repoDir );
- }
- assertFalse( repoDir.exists() );
- return repoDir;
- }
-
- private ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
- {
- for ( ManagedRepository repo : repos )
- {
- if ( StringUtils.equals( id, repo.getId() ) )
- {
- return repo;
- }
- }
- return null;
- }
-
-
- ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
- {
- return new ManagedRepository( repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
- repoLocation + "/.index", false, 1, 2, true );
- }
}
import org.apache.archiva.audit.AuditEvent;
import org.junit.Test;
-import javax.inject.Inject;
import java.util.List;
/**
extends AbstractRepositoryAdminTest
{
- @Inject
- private RemoteRepositoryAdmin remoteRepositoryAdmin;
-
@Test
public void getAll()
throws Exception
repo.setPassword( "titi" );
repo.setUrl( "http://foo.com/maven-really-rocks" );
-
remoteRepositoryAdmin.updateRemoteRepository( repo, getFakeAuditInformation() );
-
repo = remoteRepositoryAdmin.getRemoteRepository( "foo" );
assertEquals( "foo-name-changed", repo.getUserName() );
assertEquals( "titi", repo.getPassword() );
assertEquals( "http://foo.com/maven-really-rocks", repo.getUrl() );
-
-
remoteRepositoryAdmin.deleteRemoteRepository( "foo", getFakeAuditInformation() );
assertEquals( initialSize, remoteRepositoryAdmin.getRemoteRepositories().size() );
assertEquals( AuditEvent.MODIFY_REMOTE_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
assertEquals( "root", mockAuditListener.getAuditEvents().get( 1 ).getUserId() );
- assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get(1 ).getRemoteIP() );
+ assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 1 ).getRemoteIP() );
assertEquals( AuditEvent.DELETE_REMOTE_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
assertEquals( "root", mockAuditListener.getAuditEvents().get( 2 ).getUserId() );
*/
import com.opensymphony.xwork2.Preparable;
+import org.apache.archiva.admin.repository.RepositoryAdminException;
+import org.apache.archiva.admin.repository.managed.ManagedRepository;
import org.apache.archiva.audit.AuditEvent;
+import org.apache.archiva.web.util.ContextUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
-import org.apache.archiva.web.util.ContextUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
/**
* RepositoryGroupsAction
- *
*/
@Controller( "repositoryGroupsAction" )
@Scope( "prototype" )
private Map<String, RepositoryGroupConfiguration> repositoryGroups;
- private Map<String, ManagedRepositoryConfiguration> managedRepositories;
+ private Map<String, ManagedRepository> managedRepositories;
private Map<String, List<String>> groupToRepositoryMap;
}
public void prepare()
+ throws RepositoryAdminException
{
Configuration config = archivaConfiguration.getConfiguration();
repositoryGroup = new RepositoryGroupConfiguration();
repositoryGroups = config.getRepositoryGroupsAsMap();
- managedRepositories = config.getManagedRepositoriesAsMap();
+ managedRepositories = getManagedRepositoryAdmin().getManagedRepositoriesAsMap();
groupToRepositoryMap = config.getGroupToRepositoryMap();
}
this.repositoryGroups = repositoryGroups;
}
- public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
+ public Map<String, ManagedRepository> getManagedRepositories()
{
return managedRepositories;
}
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import com.opensymphony.xwork2.Action;
+import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
/**
* RepositoryGroupsActionTest
- *
- * @version
*/
-public class RepositoryGroupsActionTest
+public class RepositoryGroupsActionTest
extends AbstractActionTestCase
{
private static final String REPO_GROUP_ID = "repo-group-ident";
-
+
private static final String REPO1_ID = "managed-repo-ident-1";
-
+
private static final String REPO2_ID = "managed-repo-ident-2";
-
+
private RepositoryGroupsAction action;
-
+
private MockControl archivaConfigurationControl;
private ArchivaConfiguration archivaConfiguration;
throws Exception
{
super.setUp();
-
- //action = (RepositoryGroupsAction) lookup( Action.class.getName(), "repositoryGroupsAction" );
+
action = (RepositoryGroupsAction) getActionProxy( "/admin/repositoryGroups.action" ).getAction();
-
+
archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
action.setArchivaConfiguration( archivaConfiguration );
}
public void testSecureActionBundle()
- throws SecureActionException
+ throws SecureActionException, RepositoryAdminException
{
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( new Configuration() );
archivaConfigurationControl.replay();
-
+
action.prepare();
SecureActionBundle bundle = action.getSecureActionBundle();
assertTrue( bundle.requiresAuthentication() );
Configuration configuration = new Configuration();
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 2 );
-
+
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
+
action.prepare();
RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
repositoryGroup.setId( REPO_GROUP_ID );
-
+
String status = action.addRepositoryGroup();
assertEquals( Action.SUCCESS, status );
-
+
assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
-
+
archivaConfigurationControl.verify();
}
-
+
public void testAddEmptyRepositoryGroup()
throws Exception
{
Configuration configuration = new Configuration();
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 2 );
-
+
archivaConfiguration.save( configuration );
-
+
archivaConfigurationControl.replay();
-
+
action.prepare();
-
+
String status = action.addRepositoryGroup();
assertEquals( Action.ERROR, status );
-
+
assertEquals( 0, configuration.getRepositoryGroups().size() );
}
-
+
public void testAddDuplicateRepositoryGroup()
throws Exception
{
archivaConfigurationControl.setReturnValue( configuration, 3 );
archivaConfiguration.save( configuration );
-
+
archivaConfigurationControl.replay();
-
+
action.prepare();
RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
repositoryGroup.setId( REPO_GROUP_ID );
-
+
String status = action.addRepositoryGroup();
assertEquals( Action.SUCCESS, status );
-
+
assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
-
+
repositoryGroup.setId( REPO_GROUP_ID );
status = action.addRepositoryGroup();
-
+
assertEquals( Action.ERROR, status );
assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
}
ServletUnitClient sc = sr.newClient();
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.replay();
-
+
action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositoryGroups.action" ).getRequest() );
action.prepare();
String result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
-
+
assertNotNull( action.getRepositoryGroups() );
assertEquals( 1, action.getRepositoryGroups().size() );
assertEquals( 2, action.getManagedRepositories().size() );
-
+
RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
-
+
assertEquals( 1, repoGroup.getRepositories().size() );
assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
assertNotNull( action.getGroupToRepositoryMap() );
assertEquals( 1, action.getGroupToRepositoryMap().size() );
-
+
List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
assertEquals( 1, repos.size() );
assertEquals( REPO2_ID, repos.get( 0 ) );
}
-
+
public void testAddRepositoryToGroup()
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 6 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
+
action.prepare();
String result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
assertNotNull( action.getRepositoryGroups() );
assertEquals( 1, action.getRepositoryGroups().size() );
assertEquals( 2, action.getManagedRepositories().size() );
-
+
RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
- assertEquals( 1 , repoGroup.getRepositories().size() );
+ assertEquals( 1, repoGroup.getRepositories().size() );
assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
-
+
assertNotNull( action.getGroupToRepositoryMap() );
assertEquals( 1, action.getGroupToRepositoryMap().size() );
-
+
List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
assertEquals( 1, repos.size() );
assertEquals( REPO2_ID, repos.get( 0 ) );
-
+
action.setRepoGroupId( REPO_GROUP_ID );
action.setRepoId( REPO2_ID );
-
+
result = action.addRepositoryToGroup();
assertEquals( Action.SUCCESS, result );
-
+
action.prepare();
result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
assertEquals( 1, action.getRepositoryGroups().size() );
repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
assertEquals( 2, repoGroup.getRepositories().size() );
assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
assertEquals( REPO2_ID, repoGroup.getRepositories().get( 1 ) );
-
+
assertEquals( 0, action.getGroupToRepositoryMap().size() );
assertNull( action.getGroupToRepositoryMap().get( repoGroup.getId() ) );
}
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 6 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
+
action.prepare();
String result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
assertNotNull( action.getRepositoryGroups() );
assertEquals( 1, action.getRepositoryGroups().size() );
assertEquals( 2, action.getManagedRepositories().size() );
-
+
RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
- assertEquals( 1 , repoGroup.getRepositories().size() );
+ assertEquals( 1, repoGroup.getRepositories().size() );
assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
-
+
assertNotNull( action.getGroupToRepositoryMap() );
assertEquals( 1, action.getGroupToRepositoryMap().size() );
-
+
List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
assertEquals( 1, repos.size() );
assertEquals( REPO2_ID, repos.get( 0 ) );
-
+
action.setRepoGroupId( REPO_GROUP_ID );
action.setRepoId( REPO1_ID );
-
+
result = action.removeRepositoryFromGroup();
assertEquals( Action.SUCCESS, result );
-
+
action.prepare();
result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
assertEquals( 0, repoGroup.getRepositories().size() );
-
+
assertNotNull( action.getGroupToRepositoryMap() );
assertEquals( 1, action.getGroupToRepositoryMap().size() );
-
+
repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
assertEquals( 2, repos.size() );
assertEquals( REPO1_ID, repos.get( 0 ) );
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 6 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
+
action.prepare();
String result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
assertNotNull( action.getRepositoryGroups() );
assertEquals( 1, action.getRepositoryGroups().size() );
assertEquals( 2, action.getManagedRepositories().size() );
-
+
RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
- assertEquals( 1 , repoGroup.getRepositories().size() );
+ assertEquals( 1, repoGroup.getRepositories().size() );
assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
-
+
assertNotNull( action.getGroupToRepositoryMap() );
assertEquals( 1, action.getGroupToRepositoryMap().size() );
-
+
List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
assertEquals( 1, repos.size() );
assertEquals( REPO2_ID, repos.get( 0 ) );
-
+
action.setRepoGroupId( REPO_GROUP_ID );
action.setRepoId( REPO1_ID );
-
+
result = action.addRepositoryToGroup();
assertEquals( Action.ERROR, result );
}
-
+
public void testRemoveRepositoryNotInGroup()
throws Exception
{
Configuration configuration = createInitialConfiguration();
-
+
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration, 6 );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();
-
+
action.prepare();
String result = action.execute();
assertEquals( Action.SUCCESS, result );
-
+
assertNotNull( action.getRepositoryGroups() );
assertEquals( 1, action.getRepositoryGroups().size() );
assertEquals( 2, action.getManagedRepositories().size() );
-
+
RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
- assertEquals( 1 , repoGroup.getRepositories().size() );
+ assertEquals( 1, repoGroup.getRepositories().size() );
assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
-
+
assertNotNull( action.getGroupToRepositoryMap() );
assertEquals( 1, action.getGroupToRepositoryMap().size() );
-
+
List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
assertEquals( 1, repos.size() );
assertEquals( REPO2_ID, repos.get( 0 ) );
-
+
action.setRepoGroupId( REPO_GROUP_ID );
action.setRepoId( REPO2_ID );
-
+
result = action.removeRepositoryFromGroup();
assertEquals( Action.ERROR, result );
}
private Configuration createInitialConfiguration()
{
Configuration config = new Configuration();
-
+
ManagedRepositoryConfiguration managedRepo1 = new ManagedRepositoryConfiguration();
managedRepo1.setId( REPO1_ID );
-
+
config.addManagedRepository( managedRepo1 );
-
+
ManagedRepositoryConfiguration managedRepo2 = new ManagedRepositoryConfiguration();
managedRepo2.setId( REPO2_ID );
-
+
config.addManagedRepository( managedRepo2 );
-
+
RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
repoGroup.setId( REPO_GROUP_ID );
repoGroup.addRepository( REPO1_ID );
-
+
config.addRepositoryGroup( repoGroup );
-
+
return config;
}
}
public static final String DELETE_REPO_GROUP = "Deleted Repository Group";
+ public static final String MODIFY_REPO_GROUP = "Modify Repository Group";
+
public static final String ADD_REPO_TO_GROUP = "Added Repository to Group";
public static final String DELETE_REPO_FROM_GROUP = "Deleted Repository from Group";