org.kie.workbench.common.services.backend.compiler.utils.JGitUtilsTest.java Source code

Java tutorial

Introduction

Here is the source code for org.kie.workbench.common.services.backend.compiler.utils.JGitUtilsTest.java

Source

/*
 * Copyright 2018 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed 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.
 */
package org.kie.workbench.common.services.backend.compiler.utils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.UUID;

import org.eclipse.jgit.api.Git;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kie.workbench.common.services.backend.compiler.DefaultMavenCompilerTest;
import org.kie.workbench.common.services.backend.compiler.impl.utils.JGitUtils;
import org.kie.workbench.common.services.backend.utils.TestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.uberfire.io.IOService;
import org.uberfire.java.nio.file.Files;
import org.uberfire.java.nio.file.Path;
import org.uberfire.java.nio.file.Paths;
import org.uberfire.java.nio.fs.jgit.JGitFileSystem;
import org.uberfire.mocks.FileSystemTestingUtils;

import static org.assertj.core.api.Assertions.assertThat;

public class JGitUtilsTest {

    private static final Logger logger = LoggerFactory.getLogger(DefaultMavenCompilerTest.class);
    private FileSystemTestingUtils fileSystemTestingUtils = new FileSystemTestingUtils();
    private IOService ioService;
    private Path mavenRepoPath;

    @BeforeClass
    public static void setupSystemProperties() {
        //These are not needed for the tests
        System.setProperty("org.uberfire.nio.git.daemon.enabled", "false");
        System.setProperty("org.uberfire.nio.git.ssh.enabled", "false");
        System.setProperty("org.uberfire.sys.repo.monitor.disabled", "true");
    }

    @AfterClass
    public static void restoreSystemProperties() {
        System.clearProperty("org.uberfire.nio.git.daemon.enabled");
        System.clearProperty("org.uberfire.nio.git.ssh.enabled");
    }

    @Before
    public void setUp() throws Exception {
        fileSystemTestingUtils.setup();
        ioService = fileSystemTestingUtils.getIoService();

        mavenRepoPath = Paths.get(System.getProperty("user.home"), ".m2", "repository");

        if (!Files.exists(mavenRepoPath)) {
            logger.info("Creating a m2_repo into " + mavenRepoPath);
            if (!Files.exists(Files.createDirectories(mavenRepoPath))) {
                throw new Exception("Folder not writable in the project");
            }
        }
    }

    @After
    public void tearDown() throws IOException {
        fileSystemTestingUtils.cleanup();
        TestUtil.rm(new File("src/../.security/"));
    }

    @Test
    public void tempCloneTest() throws Exception {

        final String repoName = "myrepo";
        final JGitFileSystem fs = (JGitFileSystem) ioService.newFileSystem(URI.create("git://" + repoName),
                new HashMap<String, Object>() {
                    {
                        put("init", Boolean.TRUE);
                        put("internal", Boolean.TRUE);
                    }
                });

        ioService.startBatch(fs);

        ioService.write(fs.getPath("/pom.xml"), new String(java.nio.file.Files
                .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/pom.xml").toPath())));
        ioService.write(fs.getPath("/dummyA/src/main/java/dummy/DummyA.java"),
                new String(java.nio.file.Files.readAllBytes(new File(
                        "src/test/projects/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                                .toPath())));
        ioService.write(fs.getPath("/dummyB/src/main/java/dummy/DummyB.java"),
                new String(java.nio.file.Files.readAllBytes(new File(
                        "src/test/projects/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                                .toPath())));
        ioService.write(fs.getPath("/dummyA/pom.xml"), new String(java.nio.file.Files
                .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
        ioService.write(fs.getPath("/dummyB/pom.xml"), new String(java.nio.file.Files
                .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));

        ioService.endBatch();

        String uuid = UUID.randomUUID().toString();
        Git git = JGitUtils.tempClone(fs, uuid);

        assertThat(git).isNotNull();
        assertThat(git.getRepository().getBranch()).isEqualTo("master");
        assertThat(git.getRepository().getFullBranch()).isEqualTo("refs/heads/master");
        assertThat(git.getRepository().getDirectory()).exists();
        TestUtil.rm(git.getRepository().getDirectory());
    }

    @Test
    public void pullAndRebaseTest() throws Exception {

        //Setup origin in memory
        final URI originRepo = URI.create("git://repo");
        final JGitFileSystem origin = (JGitFileSystem) ioService.newFileSystem(originRepo,
                new HashMap<String, Object>() {
                    {
                        put("init", Boolean.TRUE);
                        put("internal", Boolean.TRUE);
                        put("listMode", "ALL");
                    }
                });
        ioService.startBatch(origin);

        ioService.write(origin.getPath("/pom.xml"), new String(java.nio.file.Files
                .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/pom.xml").toPath())));
        ioService.write(origin.getPath("/dummyA/src/main/java/dummy/DummyA.java"),
                new String(java.nio.file.Files.readAllBytes(new File(
                        "src/test/projects/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                                .toPath())));
        ioService.write(origin.getPath("/dummyB/src/main/java/dummy/DummyB.java"),
                new String(java.nio.file.Files.readAllBytes(new File(
                        "src/test/projects/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                                .toPath())));
        ioService.write(origin.getPath("/dummyA/pom.xml"), new String(java.nio.file.Files
                .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
        ioService.write(origin.getPath("/dummyB/pom.xml"), new String(java.nio.file.Files
                .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
        ioService.endBatch();

        String uuid = UUID.randomUUID().toString();
        Git git = JGitUtils.tempClone(origin, uuid);

        assertThat(git).isNotNull();
        assertThat(git.getRepository().getBranch()).isEqualTo("master");
        assertThat(git.getRepository().getFullBranch()).isEqualTo("refs/heads/master");
        assertThat(git.getRepository().getDirectory()).exists();

        assertThat(JGitUtils.pullAndRebase(git)).isTrue();

        TestUtil.rm(origin.getGit().getRepository().getDirectory());
    }
}