From 6c0afa8b8f2210fa32644cf17a8fa004a98719db Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Sat, 25 May 2019 10:50:36 +0200 Subject: [PATCH] Adding test for repository group implementation --- .../repository/RepositoryRegistryMock.java | 48 ++++++++++ .../maven2/MavenRepositoryProviderTest.java | 89 +++++++++++++++++-- 2 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/RepositoryRegistryMock.java diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/RepositoryRegistryMock.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/RepositoryRegistryMock.java new file mode 100644 index 000000000..102a456ab --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/RepositoryRegistryMock.java @@ -0,0 +1,48 @@ +package org.apache.archiva.repository; + +/* + * 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.util.Map; +import java.util.TreeMap; + +public class RepositoryRegistryMock extends RepositoryRegistry { + + private Map managedRepositories = new TreeMap<>(); + + @Override + public ManagedRepository putRepository(ManagedRepository managedRepository) throws RepositoryException { + managedRepositories.put(managedRepository.getId(), managedRepository); + return managedRepository; + } + + @Override + public ManagedRepository getManagedRepository(String repoId) { + return managedRepositories.get(repoId); + } + + @Override + public Repository getRepository(String repoId) { + if (managedRepositories.containsKey(repoId)) { + return managedRepositories.get(repoId); + } else { + return null; + } + } +} diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java index 9030f6581..0ec850e47 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven2/MavenRepositoryProviderTest.java @@ -19,17 +19,15 @@ package org.apache.archiva.repository.maven2; * under the License. */ +import org.apache.archiva.common.filelock.DefaultFileLockManager; import org.apache.archiva.common.utils.FileUtils; import org.apache.archiva.configuration.ArchivaRuntimeConfiguration; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.RemoteRepositoryConfiguration; +import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.metadata.repository.storage.maven2.conf.MockConfiguration; -import org.apache.archiva.repository.ManagedRepository; -import org.apache.archiva.repository.PasswordCredentials; -import org.apache.archiva.repository.ReleaseScheme; -import org.apache.archiva.repository.RemoteRepository; -import org.apache.archiva.repository.RepositoryType; -import org.apache.archiva.repository.UnsupportedFeatureException; +import org.apache.archiva.repository.*; +import org.apache.archiva.repository.content.maven2.MavenContentProvider; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.repository.features.IndexCreationFeature; import org.apache.archiva.repository.features.RemoteIndexFeature; @@ -45,7 +43,9 @@ import java.nio.file.Paths; import java.time.Duration; import java.time.Period; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.Assert.*; @@ -57,6 +57,7 @@ public class MavenRepositoryProviderTest { MavenRepositoryProvider provider; + RepositoryRegistry reg; Path repoLocation; @@ -70,6 +71,10 @@ public class MavenRepositoryProviderTest mockConfiguration.getConfiguration().setArchivaRuntimeConfiguration( new ArchivaRuntimeConfiguration() ); mockConfiguration.getConfiguration().getArchivaRuntimeConfiguration().setRepositoryBaseDirectory( "repositories" ); provider.setArchivaConfiguration( mockConfiguration ); + + reg = new RepositoryRegistryMock(); + reg.setArchivaConfiguration(mockConfiguration); + provider.setRepositoryRegistry(reg); } @After @@ -292,4 +297,76 @@ public class MavenRepositoryProviderTest } + @Test + public void getRepositoryGroupConfiguration() throws RepositoryException { + MavenRepositoryGroup repositoryGroup = new MavenRepositoryGroup("group1","group1",Paths.get("target/groups"), + new DefaultFileLockManager()); + MavenManagedRepository repo1 = new MavenManagedRepository( "test01", "My Test repo", Paths.get("target/repositories") ); + MavenManagedRepository repo2 = new MavenManagedRepository( "test02", "My Test repo", Paths.get("target/repositories") ); + + + repositoryGroup.setDescription(repositoryGroup.getPrimaryLocale(), "Repository group"); + repositoryGroup.setLayout("non-default"); + repositoryGroup.setMergedIndexPath(".index2"); + repositoryGroup.setName(repositoryGroup.getPrimaryLocale(), "Repo Group 1"); + repositoryGroup.setMergedIndexTTL(1005); + repositoryGroup.setSchedulingDefinition("0 0 04 ? * THU"); + repositoryGroup.addRepository(repo1); + repositoryGroup.addRepository(repo2); + + + RepositoryGroupConfiguration cfg = provider.getRepositoryGroupConfiguration(repositoryGroup); + assertEquals("group1", cfg.getId()); + assertEquals(".index2", cfg.getMergedIndexPath()); + assertEquals("0 0 04 ? * THU", cfg.getCronExpression()); + assertEquals("Repo Group 1", cfg.getName()); + assertEquals(1005, cfg.getMergedIndexTtl()); + assertTrue(cfg.getRepositories().contains("test01")); + assertTrue(cfg.getRepositories().contains("test02")); + assertEquals(2, cfg.getRepositories().size()); + } + + + @Test + public void createRepositoryGroup() { + EditableRepositoryGroup gr = provider.createRepositoryGroup("group1", "Group 1"); + assertEquals("group1",gr.getId()); + assertEquals("Group 1", gr.getName()); + assertEquals(MavenRepositoryGroup.class, gr.getClass()); + } + + @Test + public void createRepositoryGroupWithCfg() throws RepositoryException { + MavenManagedRepository repo1 = new MavenManagedRepository( "test01", "My Test repo", Paths.get("target/repositories") ); + + MavenManagedRepository repo2 = new MavenManagedRepository( "test02", "My Test repo", Paths.get("target/repositories") ); + reg.putRepository(repo1); + reg.putRepository(repo2); + + assertNotNull(reg.getManagedRepository("test01")); + assertNotNull(reg.getManagedRepository("test02")); + + RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration(); + cfg.setId("group2"); + cfg.setName("Group 2"); + cfg.setCronExpression("0 0 03 ? * MON"); + cfg.setMergedIndexTtl(504); + cfg.setMergedIndexPath(".index-abc"); + ArrayList repos = new ArrayList<>(); + repos.add("test01"); + repos.add("test02"); + cfg.setRepositories(repos); + + RepositoryGroup grp = provider.createRepositoryGroup(cfg); + + assertEquals("group2", grp.getId()); + assertEquals("Group 2", grp.getName()); + assertEquals("0 0 03 ? * MON", grp.getSchedulingDefinition()); + assertEquals(".index-abc", grp.getMergedIndexPath().getName()); + assertEquals(504, grp.getMergedIndexTTL()); + assertEquals(2, grp.getRepositories().size()); + assertTrue(grp.getRepositories().stream().anyMatch(r -> "test01".equals(r.getId()))); + assertTrue(grp.getRepositories().stream().anyMatch(r -> "test02".equals(r.getId()))); + } + } \ No newline at end of file -- 2.39.5