/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.jboss.pnc.termdbuilddriver;

import org.jboss.pnc.common.Configuration;
import org.jboss.pnc.common.json.ConfigurationParseException;
import org.jboss.pnc.common.json.moduleconfig.SystemConfig;
import org.jboss.pnc.spi.builddriver.CompletedBuild;
import org.jboss.pnc.spi.builddriver.RunningBuild;
import org.jboss.pnc.spi.executor.BuildExecutionConfiguration;
import org.jboss.pnc.spi.executor.BuildExecutionSession;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicReference;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

public class TermdBuildDriverTest extends AbstractLocalBuildAgentTest {

    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    private Configuration configuration = mock(Configuration.class);

    @Before
    public void before() throws ConfigurationParseException {
        doReturn(new SystemConfig(null, null, null, null, null, null)).when(configuration).getModuleConfig(any());
    }

    @Test(timeout = 60_000)
    public void shouldFetchFromGitAndBuild() throws Exception {
        //given
        Path tmpRepo = Files.createTempDirectory("tmpRepo");
        String repoPath = "file://" + tmpRepo.toAbsolutePath().toString() + "/test-repo";
        ZipUtils.unzipToDir(tmpRepo, "/repo.zip");
        String dirName = "test-repo-cloned";

        TermdBuildDriver driver = new TermdBuildDriver(getConfiguration());
        BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
        BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
        doReturn(repoPath).when(buildExecutionConfiguration).getScmRepoURL();
        doReturn("master").when(buildExecutionConfiguration).getScmRevision();
        doReturn("mvn validate").when(buildExecutionConfiguration).getBuildScript();
        doReturn(dirName).when(buildExecutionConfiguration).getName();
        doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();

        AtomicReference<CompletedBuild> buildResult = new AtomicReference<>();

        //when
        RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer);
        runningBuild.monitor(buildResult::set, exception -> fail(exception.getMessage()));

        logger.info("==== shouldFetchFromGitAndBuild logs ====");
        logger.info(buildResult.get().getBuildResult().getBuildLog());
        logger.info("==== /shouldFetchFromGitAndBuild logs ====");

        //then
        assertThat(buildResult.get().getBuildResult()).isNotNull();
        assertThat(buildResult.get().getBuildResult().getBuildLog()).isNotEmpty();
        assertThat(Files.exists(localEnvironmentPointer.getWorkingDirectory())).isTrue();
        assertThat(Files.exists(localEnvironmentPointer.getWorkingDirectory().resolve(dirName))).isTrue();
    }

    private Configuration getConfiguration() throws ConfigurationParseException {
        SystemConfig systemConfig = mock(SystemConfig.class);
        Configuration configuration = mock(Configuration.class);
        doReturn(systemConfig).when(configuration).getModuleConfig(any());
        return configuration;
    }

}

@@ -1003,24 +1003,93 @@
 nningBuild;%0A
+import org.jboss.pnc.spi.builddriver.exception.BuildDriverException;%0A
 import org.j
@@ -1507,16 +1507,89 @@
 rtThat;%0A
+import static org.jboss.pnc.spi.builddriver.BuildDriverStatus.CANCELLED;%0A
 import s
@@ -4082,24 +4082,1629 @@
 e();%0A    %7D%0A%0A
+    @Test%0A    public void shouldStartAndCancelTheCommand() throws ConfigurationParseException, BuildDriverException %7B%0A        //given%0A        String dirName = %22test-workdir%22;%0A        String logStart = %22Running the command...%22;%0A        String logEnd = %22Command completed.%22;%0A%0A        TermdBuildDriver driver = new TermdBuildDriver(getConfiguration());%0A        BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);%0A        BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);%0A        doReturn(%22echo %5C%22%22 + logStart + %22%5C%22; mvn sleep%22).when(buildExecutionConfiguration).getBuildScript(); //TODO fix command%0A        doReturn(dirName).when(buildExecutionConfiguration).getName();%0A        doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();%0A%0A        AtomicReference%3CCompletedBuild%3E buildResult = new AtomicReference%3C%3E();%0A%0A        //when%0A        RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer);%0A        runningBuild.monitor(buildResult::set, exception -%3E fail(exception.getMessage()));%0A        runningBuild.cancel();%0A%0A        logger.info(buildResult.get().getBuildResult().getBuildLog());%0A%0A        //then%0A        assertThat(buildResult.get().getBuildResult()).isNotNull();%0A        assertThat(buildResult.get().getBuildResult().getBuildLog()).contains(logStart);%0A        assertThat(buildResult.get().getBuildResult().getBuildLog()).doesNotContain(logEnd);%0A        assertThat(buildResult.get().getBuildResult().getBuildDriverStatus()).isEqualTo(CANCELLED);%0A    %7D%0A%0A
     private 

Add test for build cancelation.