Search in sources :

Example 6 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectsControllerTest method testAdd.

/**
 * Verify that added project correctly inherits a property
 * from configuration. Ideally, this should test all properties of Project.
 */
@Test
void testAdd() throws Exception {
    assertTrue(env.getRepositories().isEmpty());
    assertTrue(env.getProjects().isEmpty());
    // Add a group matching the project to be added.
    String groupName = "mercurialgroup";
    Group group = new Group(groupName, "mercurial.*");
    env.getGroups().add(group);
    assertTrue(env.hasGroups());
    assertEquals(1, env.getGroups().stream().filter(g -> g.getName().equals(groupName)).collect(Collectors.toSet()).size());
    assertEquals(0, group.getRepositories().size());
    assertEquals(0, group.getProjects().size());
    // Add a sub-repository.
    String repoPath = repository.getSourceRoot() + File.separator + "mercurial";
    File mercurialRoot = new File(repoPath);
    File subDir = new File(mercurialRoot, "usr");
    assertTrue(subDir.mkdir());
    String subRepoPath = repoPath + File.separator + "usr" + File.separator + "closed";
    File mercurialSubRoot = new File(subRepoPath);
    MercurialRepositoryTest.runHgCommand(mercurialRoot, "clone", mercurialRoot.getAbsolutePath(), subRepoPath);
    // Add the project.
    env.setScanningDepth(3);
    addProject("mercurial");
    // Check that the project was added properly.
    assertTrue(env.getProjects().containsKey("mercurial"));
    assertEquals(1, env.getProjects().size());
    assertEquals(2, env.getRepositories().size());
    assertEquals(1, group.getRepositories().size());
    assertEquals(0, group.getProjects().size());
    assertEquals(1, group.getRepositories().stream().filter(p -> p.getName().equals("mercurial")).collect(Collectors.toSet()).size());
    // Check that HistoryGuru now includes the project in its list.
    Set<String> directoryNames = HistoryGuru.getInstance().getRepositories().stream().map(RepositoryInfo::getDirectoryName).collect(Collectors.toSet());
    assertTrue(directoryNames.contains(repoPath) || directoryNames.contains(mercurialRoot.getCanonicalPath()), "though it should contain the top root,");
    assertTrue(directoryNames.contains(subRepoPath) || directoryNames.contains(mercurialSubRoot.getCanonicalPath()), "though it should contain the sub-root,");
    // Add more projects and check that they have been added incrementally.
    // At the same time, it checks that multiple projects can be added
    // with single message.
    addProject("git");
    assertEquals(2, env.getProjects().size());
    assertEquals(3, env.getRepositories().size());
    assertTrue(env.getProjects().containsKey("git"));
    assertFalse(HistoryGuru.getInstance().getRepositories().stream().map(RepositoryInfo::getDirectoryName).collect(Collectors.toSet()).contains("git"));
}
Also used : GrizzlyWebTestContainerFactory(org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) RepositoryFactory(org.opengrok.indexer.history.RepositoryFactory) SuggesterService(org.opengrok.web.api.v1.suggester.provider.service.SuggesterService) Arrays(java.util.Arrays) Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) MercurialRepositoryTest(org.opengrok.indexer.history.MercurialRepositoryTest) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) ApiTaskManager(org.opengrok.web.api.ApiTaskManager) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ApiUtils.waitForTask(org.opengrok.web.api.v1.controller.ApiUtils.waitForTask) Path(java.nio.file.Path) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) SUBVERSION(org.opengrok.indexer.condition.RepositoryInstalled.Type.SUBVERSION) IndexDatabase(org.opengrok.indexer.index.IndexDatabase) Collectors(java.util.stream.Collectors) Indexer(org.opengrok.indexer.index.Indexer) Entity(jakarta.ws.rs.client.Entity) Test(org.junit.jupiter.api.Test) IndexerException(org.opengrok.indexer.index.IndexerException) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) DeploymentContext(org.glassfish.jersey.test.DeploymentContext) Mock(org.mockito.Mock) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Response(jakarta.ws.rs.core.Response) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestRepository(org.opengrok.indexer.util.TestRepository) CommandTimeoutType(org.opengrok.indexer.configuration.CommandTimeoutType) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) Files(java.nio.file.Files) IOUtils.removeRecursive(org.opengrok.indexer.util.IOUtils.removeRecursive) IOException(java.io.IOException) File(java.io.File) GenericType(jakarta.ws.rs.core.GenericType) AfterEach(org.junit.jupiter.api.AfterEach) ServletDeploymentContext(org.glassfish.jersey.test.ServletDeploymentContext) Paths(java.nio.file.Paths) MERCURIAL(org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL) Collections(java.util.Collections) Group(org.opengrok.indexer.configuration.Group) File(java.io.File) MercurialRepositoryTest(org.opengrok.indexer.history.MercurialRepositoryTest) Test(org.junit.jupiter.api.Test)

Example 7 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectsControllerTest method testSetGet.

@Test
void testSetGet() {
    assertTrue(env.isHandleHistoryOfRenamedFiles());
    String[] projects = new String[] { "mercurial", "git" };
    for (String proj : projects) {
        addProject(proj);
    }
    assertEquals(2, env.getProjectList().size());
    for (String proj : projects) {
        Project project = env.getProjects().get(proj);
        assertNotNull(project);
        assertTrue(project.isHandleRenamedFiles());
        List<RepositoryInfo> riList = env.getProjectRepositoriesMap().get(project);
        assertNotNull(riList);
        for (RepositoryInfo ri : riList) {
            ri.setHandleRenamedFiles(true);
            assertTrue(ri.isHandleRenamedFiles());
        }
    }
    // Change their property via RESTful API call.
    for (String proj : projects) {
        setHandleRenamedFilesToFalse(proj);
    }
    // Verify the property was set on each project and its repositories.
    for (String proj : projects) {
        Project project = env.getProjects().get(proj);
        assertNotNull(project);
        assertFalse(project.isHandleRenamedFiles());
        List<RepositoryInfo> riList = env.getProjectRepositoriesMap().get(project);
        assertNotNull(riList);
        for (RepositoryInfo ri : riList) {
            assertFalse(ri.isHandleRenamedFiles());
        }
    }
    // Verify the property can be retrieved via message.
    for (String proj : projects) {
        boolean value = target("projects").path(proj).path("property/handleRenamedFiles").request().get(boolean.class);
        assertFalse(value);
    }
}
Also used : Project(org.opengrok.indexer.configuration.Project) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) MercurialRepositoryTest(org.opengrok.indexer.history.MercurialRepositoryTest) Test(org.junit.jupiter.api.Test)

Example 8 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectsControllerTest method testDelete.

/**
 * This test needs to perform indexing so that it can be verified that
 * delete handling does remove the index data.
 */
@Test
void testDelete() throws Exception {
    String[] projectsToDelete = { "git" };
    // Add a group matching the project to be added.
    String groupName = "gitgroup";
    Group group = new Group(groupName, "git.*");
    env.getGroups().add(group);
    assertTrue(env.hasGroups());
    assertEquals(1, env.getGroups().stream().filter(g -> g.getName().equals(groupName)).collect(Collectors.toSet()).size());
    assertEquals(0, group.getRepositories().size());
    assertEquals(0, group.getProjects().size());
    assertEquals(0, env.getProjects().size());
    assertEquals(0, env.getRepositories().size());
    assertEquals(0, env.getProjectRepositoriesMap().size());
    addProject("mercurial");
    addProject("git");
    assertEquals(2, env.getProjects().size());
    assertEquals(2, env.getRepositories().size());
    assertEquals(2, env.getProjectRepositoriesMap().size());
    // Check the group was populated properly.
    assertEquals(1, group.getRepositories().size());
    assertEquals(0, group.getProjects().size());
    assertEquals(1, group.getRepositories().stream().filter(p -> p.getName().equals("git")).collect(Collectors.toSet()).size());
    // Run the indexer so that data directory is populated.
    ArrayList<String> subFiles = new ArrayList<>();
    subFiles.add("/git");
    subFiles.add("/mercurial");
    ArrayList<String> repos = new ArrayList<>();
    repos.add("/git");
    repos.add("/mercurial");
    // This is necessary so that repositories in HistoryGuru get populated.
    // For per project reindex this is called from setConfiguration() because
    // of the -R option is present.
    HistoryGuru.getInstance().invalidateRepositories(env.getRepositories(), null, CommandTimeoutType.INDEXER);
    env.setHistoryEnabled(true);
    Indexer.getInstance().prepareIndexer(env, // don't search for repositories
    false, // don't scan and add projects
    false, // don't create dictionary
    false, // subFiles - needed when refreshing history partially
    subFiles, // repositories - needed when refreshing history partially
    repos);
    Indexer.getInstance().doIndexerExecution(true, null, null);
    for (String proj : projectsToDelete) {
        deleteProject(proj);
    }
    assertEquals(1, env.getProjects().size());
    assertEquals(1, env.getRepositories().size());
    assertEquals(1, env.getProjectRepositoriesMap().size());
    // Test data removal.
    for (String projectName : projectsToDelete) {
        for (String dirName : new String[] { "historycache", IndexDatabase.XREF_DIR, IndexDatabase.INDEX_DIR }) {
            File dir = new File(env.getDataRootFile(), dirName + File.separator + projectName);
            assertFalse(dir.exists());
        }
    }
    // Check that HistoryGuru no longer maintains the removed projects.
    for (String p : projectsToDelete) {
        assertFalse(HistoryGuru.getInstance().getRepositories().stream().map(RepositoryInfo::getDirectoryName).collect(Collectors.toSet()).contains(repository.getSourceRoot() + File.separator + p));
    }
    // Check the group no longer contains the removed project.
    assertEquals(0, group.getRepositories().size());
    assertEquals(0, group.getProjects().size());
}
Also used : GrizzlyWebTestContainerFactory(org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) RepositoryFactory(org.opengrok.indexer.history.RepositoryFactory) SuggesterService(org.opengrok.web.api.v1.suggester.provider.service.SuggesterService) Arrays(java.util.Arrays) Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) MercurialRepositoryTest(org.opengrok.indexer.history.MercurialRepositoryTest) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) ApiTaskManager(org.opengrok.web.api.ApiTaskManager) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ApiUtils.waitForTask(org.opengrok.web.api.v1.controller.ApiUtils.waitForTask) Path(java.nio.file.Path) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) SUBVERSION(org.opengrok.indexer.condition.RepositoryInstalled.Type.SUBVERSION) IndexDatabase(org.opengrok.indexer.index.IndexDatabase) Collectors(java.util.stream.Collectors) Indexer(org.opengrok.indexer.index.Indexer) Entity(jakarta.ws.rs.client.Entity) Test(org.junit.jupiter.api.Test) IndexerException(org.opengrok.indexer.index.IndexerException) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) DeploymentContext(org.glassfish.jersey.test.DeploymentContext) Mock(org.mockito.Mock) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Response(jakarta.ws.rs.core.Response) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestRepository(org.opengrok.indexer.util.TestRepository) CommandTimeoutType(org.opengrok.indexer.configuration.CommandTimeoutType) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) Files(java.nio.file.Files) IOUtils.removeRecursive(org.opengrok.indexer.util.IOUtils.removeRecursive) IOException(java.io.IOException) File(java.io.File) GenericType(jakarta.ws.rs.core.GenericType) AfterEach(org.junit.jupiter.api.AfterEach) ServletDeploymentContext(org.glassfish.jersey.test.ServletDeploymentContext) Paths(java.nio.file.Paths) MERCURIAL(org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL) Collections(java.util.Collections) Group(org.opengrok.indexer.configuration.Group) ArrayList(java.util.ArrayList) File(java.io.File) MercurialRepositoryTest(org.opengrok.indexer.history.MercurialRepositoryTest) Test(org.junit.jupiter.api.Test)

Example 9 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class ProjectsControllerTest method testIndexed.

@Test
void testIndexed() throws IOException {
    String projectName = "mercurial";
    // When a project is added, it should be marked as not indexed.
    addProject(projectName);
    assertFalse(env.getProjects().get(projectName).isIndexed());
    // Get repository info for the project.
    Project project = env.getProjects().get(projectName);
    assertNotNull(project);
    List<RepositoryInfo> riList = env.getProjectRepositoriesMap().get(project);
    assertNotNull(riList);
    assertEquals(1, riList.size(), "there should be just 1 repository");
    RepositoryInfo ri = riList.get(0);
    assertNotNull(ri);
    assertTrue(ri.getCurrentVersion().contains("8b340409b3a8"));
    // Add some changes to the repository.
    File mercurialRoot = new File(repository.getSourceRoot() + File.separator + "mercurial");
    // copy file from jar to a temp file
    Path temp = Files.createTempFile("opengrok", "temp");
    Files.copy(HistoryGuru.getInstance().getClass().getResourceAsStream("/history/hg-export-subdir.txt"), temp, StandardCopyOption.REPLACE_EXISTING);
    // prevent 'uncommitted changes' error
    MercurialRepositoryTest.runHgCommand(mercurialRoot, "revert", "--all");
    MercurialRepositoryTest.runHgCommand(mercurialRoot, "import", temp.toString());
    assertTrue(temp.toFile().delete());
    // Test that the project's indexed flag becomes true only after
    // the message is applied.
    assertEquals(markIndexed(projectName).getStatusInfo().getFamily(), Response.Status.Family.SUCCESSFUL);
    assertTrue(env.getProjects().get(projectName).isIndexed(), "indexed flag should be set to true");
    // Test that the "indexed" message triggers refresh of current version
    // info in related repositories.
    riList = env.getProjectRepositoriesMap().get(project);
    assertNotNull(riList);
    ri = riList.get(0);
    assertNotNull(ri);
    assertTrue(ri.getCurrentVersion().contains("c78fa757c524"), "current version should be refreshed");
}
Also used : Path(java.nio.file.Path) Project(org.opengrok.indexer.configuration.Project) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) File(java.io.File) MercurialRepositoryTest(org.opengrok.indexer.history.MercurialRepositoryTest) Test(org.junit.jupiter.api.Test)

Example 10 with RepositoryInfo

use of org.opengrok.indexer.history.RepositoryInfo in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method generateProjectRepositoriesMap.

/**
 * Generate a TreeMap of projects with corresponding repository information.
 * <p>
 * Project with some repository information is considered as a repository
 * otherwise it is just a simple project.
 */
private void generateProjectRepositoriesMap() throws IOException {
    repository_map.clear();
    for (RepositoryInfo r : getRepositories()) {
        Project proj;
        String repoPath;
        try {
            repoPath = getPathRelativeToSourceRoot(new File(r.getDirectoryName()));
        } catch (ForbiddenSymlinkException e) {
            LOGGER.log(Level.FINER, e.getMessage());
            continue;
        }
        if ((proj = Project.getProject(repoPath)) != null) {
            List<RepositoryInfo> values = repository_map.computeIfAbsent(proj, k -> new ArrayList<>());
            // the map is held under the lock because the next call to
            // values.add(r) which should not be called from multiple threads at the same time
            values.add(r);
        }
    }
}
Also used : ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) File(java.io.File)

Aggregations

RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)21 Project (org.opengrok.indexer.configuration.Project)16 Test (org.junit.jupiter.api.Test)10 File (java.io.File)9 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Path (jakarta.ws.rs.Path)5 Group (org.opengrok.indexer.configuration.Group)5 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)5 MercurialRepositoryTest (org.opengrok.indexer.history.MercurialRepositoryTest)5 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)4 GET (jakarta.ws.rs.GET)3 Produces (jakarta.ws.rs.Produces)3 Response (jakarta.ws.rs.core.Response)3 Paths (java.nio.file.Paths)3 Collections (java.util.Collections)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3