/*
 * Copyright 2021 ThoughtWorks, Inc.
 *
 * 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 com.thoughtworks.go.server.materials;

import com.thoughtworks.go.config.materials.git.GitMaterial;
import com.thoughtworks.go.domain.MaterialInstance;
import com.thoughtworks.go.domain.materials.git.GitTestRepo;
import org.apache.commons.io.FileUtils;
import org.aspectj.util.FileUtil;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.fail;

public class MaterialDatabaseGitUpdaterTest extends TestBaseForDatabaseUpdater {
    @Override
    protected GitMaterial material() {
        return new GitMaterial(testRepo.projectRepositoryUrl());
    }

    @Override
    protected GitTestRepo repo(Path tempDir) throws IOException {
        return new GitTestRepo(tempDir);
    }

    @Test
    public void shouldRemoveFlyweightWhenConfiguredBranchDoesNotExist() throws Exception {
        File flyweightDir = new File("pipelines", "flyweight");
        FileUtils.deleteQuietly(flyweightDir);

        material = new GitMaterial(testRepo.projectRepositoryUrl(), "bad-bad-branch");

        try {
            updater.updateMaterial(material);
            fail("material update should have failed as given branch does not exist in repository");
        } catch (Exception e) {
            //ignore
        }

        MaterialInstance materialInstance = materialRepository.findMaterialInstance(material);

        assertThat(materialInstance, is(nullValue()));
        assertThat(FileUtil.listFiles(flyweightDir).length, is(0));//no flyweight dir left behind
    }

}

@@ -707,60 +707,8 @@
 al;%0A
-import com.thoughtworks.go.domain.MaterialInstance;%0A
 impo
@@ -808,42 +808,8 @@
 ls;%0A
-import org.aspectj.util.FileUtil;%0A
 impo
@@ -939,30 +939,35 @@
 org.
-hamcrest.Matcher
+assertj.core.api.
 Assert
+ions
 .ass
@@ -997,128 +997,54 @@
 org.
-hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.nullValue;%0Aimport static org.junit.jupiter.api.Assertions.fail
+assertj.core.api.Assertions.assertThatThrownBy
 ;%0A%0Ap
@@ -1489,369 +1489,277 @@
     
-File flyweightDir = new File(%22pipelines%22, %22flyweight%22);%0A        FileUtils.deleteQuietly(flyweightDir);%0A%0A        material = new GitMaterial(testRepo.projectRepositoryUrl(), %22bad-bad-branch%22);%0A%0A        try %7B%0A            updater.updateMaterial(material);%0A            fail(%22material update should have failed as given branch does not exist in repository%22
+material = new GitMaterial(testRepo.projectRepositoryUrl(), %22bad-bad-branch%22);%0A%0A        // Ensure we start clean%0A        File flyweightDir = materialRepository.folderFor(new GitMaterial(%22dummy%22)).getParentFile();%0A        FileUtils.deleteQuietly(flyweightDir
 );%0A
+%0A
         
 %7D ca
@@ -1758,312 +1758,385 @@
     
-%7D catch (Exception e) %7B%0A            //ignore%0A        %7D%0A%0A        MaterialInstance materialInstance = materialRepository.findMaterialInstance(material);%0A%0A        assertThat(materialInstance, is(nullValue()));%0A        assertThat(FileUtil.listFiles(flyweightDir).length, is(0));//no flyweight dir left behind
+assertThatThrownBy(() -%3E updater.updateMaterial(material))%0A                .hasMessageContaining(%22bad-bad-branch not found%22);%0A%0A        assertThat(materialRepository.findMaterialInstance(material)).isNull();%0A        // Check there are no flyweight folders created by looking at the parent directory of a new folder generation%0A        assertThat(flyweightDir).isEmptyDirectory();
 %0A   

Remove an unintended rogue usage of an AspectJ class from unrelated test