/*
 * Copyright 2013-2022 the original author or authors.
 *
 * 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
 *
 *      https://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 releaser.internal.docs;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Collections;

import org.assertj.core.api.BDDAssertions;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import releaser.SpringCloudReleaserProperties;
import releaser.internal.ReleaserProperties;
import releaser.internal.buildsystem.TestUtils;
import releaser.internal.git.GitTestUtils;
import releaser.internal.git.ProjectGitHandler;
import releaser.internal.github.ProjectGitHubHandler;
import releaser.internal.project.ProjectVersion;
import releaser.internal.project.Projects;
import releaser.internal.template.TemplateGenerator;

import org.springframework.util.FileSystemUtils;

/**
 * @author Marcin Grzejszczak
 */
public class ReleaseTrainContentsUpdaterTests {

	ReleaserProperties properties = SpringCloudReleaserProperties.get();

	ProjectGitHubHandler projectGitHubHandler = new ProjectGitHubHandler(this.properties, Collections.emptyList()) {
		@Override
		public String milestoneUrl(ProjectVersion releaseVersion) {
			return "http://www.foo.com/";
		}
	};

	TemplateGenerator templateGenerator = new TemplateGenerator(this.properties, this.projectGitHubHandler);

	ProjectGitHandler projectGitHandler = new ProjectGitHandler(this.properties);

	ReleaseTrainContentsUpdater updater = new ReleaseTrainContentsUpdater(this.properties, this.projectGitHandler,
			this.templateGenerator);

	File springCloudRepo;

	File wikiRepo;

	@TempDir
	File temporaryFolder;

	@BeforeEach
	public void setup() throws Exception {
		TestUtils.prepareLocalRepo();
		FileSystemUtils.copyRecursively(file("/projects"), this.temporaryFolder);
		this.springCloudRepo = new File(this.temporaryFolder, "spring-cloud/");
		this.wikiRepo = new File(this.temporaryFolder, "spring-cloud-wiki/");
	}

	private File file(String relativePath) throws URISyntaxException {
		return new File(ReleaseTrainContentsUpdaterTests.class.getResource(relativePath).toURI());
	}

	@Test
	public void should_do_nothing_when_switch_is_off_for_wiki_update() {
		this.properties.getGit().setUpdateReleaseTrainWiki(false);

		File file = this.updater.updateReleaseTrainWiki(oldReleaseTrain());

		BDDAssertions.then(file).isNull();
	}

	@Test
	public void should_do_nothing_when_switch_is_off_for_meta_release() {
		this.properties.getGit().setUpdateReleaseTrainWiki(true);
		this.properties.getMetaRelease().setEnabled(false);

		File file = this.updater.updateReleaseTrainWiki(oldReleaseTrain());

		BDDAssertions.then(file).isNull();
	}

	@Test
	public void should_not_update_the_contents_of_wiki_repo_when_release_train_smaller()
			throws GitAPIException, IOException {
		this.properties.getMetaRelease().setEnabled(true);
		this.properties.getGit().setUpdateReleaseTrainWiki(true);
		this.properties.getGit().setReleaseTrainWikiUrl(this.wikiRepo.getAbsolutePath() + "/");

		File file = this.updater.updateReleaseTrainWiki(oldReleaseTrain());

		BDDAssertions.then(file).isNotNull();
		BDDAssertions.then(edgwareWikiEntryContent(file)).doesNotContain("# Edgware.SR7")
				.doesNotContain("Spring Cloud Consul `2.0.1.RELEASE` ([issues](http://www.foo.com/))");
		BDDAssertions.then(GitTestUtils.openGitProject(file).log().call().iterator().next().getShortMessage())
				.doesNotContain("Updating project page to release train");
	}

	@Test
	public void should_update_the_contents_of_wiki_when_release_train_greater() throws GitAPIException, IOException {
		this.properties.getMetaRelease().setEnabled(true);
		this.properties.getGit().setUpdateReleaseTrainWiki(true);
		this.properties.getGit().setReleaseTrainWikiUrl(this.wikiRepo.getAbsolutePath() + "/");

		File file = this.updater.updateReleaseTrainWiki(newReleaseTrain());

		BDDAssertions.then(file).isNotNull();
		BDDAssertions.then(edgwareWikiEntryContent(file)).contains("# Edgware.SR7")
				.contains("Spring Cloud Consul `2.0.1.RELEASE` ([issues](http://www.foo.com/))");
		BDDAssertions.then(GitTestUtils.openGitProject(file).log().call().iterator().next().getShortMessage())
				.contains("Updating project page to release train [Edgware.SR7]");
	}

	@Test
	public void should_generate_the_contents_of_wiki_when_release_train_missing() throws GitAPIException, IOException {
		this.properties.getMetaRelease().setEnabled(true);
		this.properties.getGit().setUpdateReleaseTrainWiki(true);
		this.properties.getGit().setReleaseTrainWikiUrl(this.wikiRepo.getAbsolutePath() + "/");

		File file = this.updater.updateReleaseTrainWiki(freshNewReleaseTrain());

		BDDAssertions.then(file).isNotNull();
		BDDAssertions.then(greenwichWikiEntryContent(file)).contains("# Greenwich.RELEASE")
				.contains("Spring Cloud Consul `2.0.1.RELEASE` ([issues](http://www.foo.com/))");
		BDDAssertions.then(GitTestUtils.openGitProject(file).log().call().iterator().next().getShortMessage())
				.contains("Updating project page to release train [Greenwich.RELEASE]");
	}

	@Test
	public void should_generate_the_contents_of_wiki_when_release_train_missing_calvar()
			throws GitAPIException, IOException {
		this.properties.getMetaRelease().setEnabled(true);
		this.properties.getGit().setUpdateReleaseTrainWiki(true);
		this.properties.getGit().setReleaseTrainWikiUrl(this.wikiRepo.getAbsolutePath() + "/");

		File file = this.updater.updateReleaseTrainWiki(freshNewReleaseTrainCalver());

		BDDAssertions.then(file).isNotNull();
		BDDAssertions.then(calverWikiEntryContent(file)).contains("# 2020.0.5")
				.contains("Spring Cloud Consul `2.0.1.RELEASE` ([issues](http://www.foo.com/))");
		BDDAssertions.then(GitTestUtils.openGitProject(file).log().call().iterator().next().getShortMessage())
				.contains("Updating project page to release train [2020.0.5]");
	}

	private String edgwareWikiEntryContent(File file) throws IOException {
		return string(file, "Spring-Cloud-Edgware-Release-Notes.md");
	}

	private String greenwichWikiEntryContent(File file) throws IOException {
		return string(file, "Spring-Cloud-Greenwich-Release-Notes.md");
	}

	private String calverWikiEntryContent(File file) throws IOException {
		return string(file, "Spring-Cloud-2020.0-Release-Notes.md");
	}

	private String string(File file, String s) throws IOException {
		return new String(Files.readAllBytes(new File(file, s).toPath()));
	}

	Projects oldReleaseTrain() {
		return new Projects(new ProjectVersion("spring-cloud-aws", "3.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-bus", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cli", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-commons", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-contract", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-config", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-netflix", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-security", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cloudfoundry", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-consul", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-sleuth", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-stream", "Elmhurst.SR1"),
				new ProjectVersion("spring-cloud-zookeeper", "2.0.0.RELEASE"),
				new ProjectVersion("spring-boot", "2.0.4.RELEASE"),
				new ProjectVersion("spring-cloud-task", "2.0.0.RELEASE"),
				// old release train
				new ProjectVersion("spring-cloud-release", "Dalston.SR1"),
				new ProjectVersion("spring-cloud-vault", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-gateway", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-openfeign", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-function", "1.0.0.RELEASE"));
	}

	Projects newReleaseTrain() {
		return new Projects(new ProjectVersion("spring-cloud-aws", "3.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-bus", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cli", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-commons", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-contract", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-config", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-netflix", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-security", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cloudfoundry", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-consul", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-sleuth", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-stream", "Elmhurst.SR1"),
				new ProjectVersion("spring-cloud-zookeeper", "2.0.0.RELEASE"),
				new ProjectVersion("spring-boot", "2.0.4.RELEASE"),
				new ProjectVersion("spring-cloud-task", "2.0.0.RELEASE"),
				// newer release train
				new ProjectVersion("spring-cloud-release", "Edgware.SR7"),
				new ProjectVersion("spring-cloud-vault", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-gateway", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-openfeign", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-function", "1.0.0.RELEASE"));
	}

	Projects freshNewReleaseTrain() {
		return new Projects(new ProjectVersion("spring-cloud-aws", "3.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-bus", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cli", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-commons", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-contract", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-config", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-netflix", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-security", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cloudfoundry", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-consul", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-sleuth", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-stream", "Elmhurst.SR1"),
				new ProjectVersion("spring-cloud-zookeeper", "2.0.0.RELEASE"),
				new ProjectVersion("spring-boot", "2.0.4.RELEASE"),
				new ProjectVersion("spring-cloud-task", "2.0.0.RELEASE"),
				// newer release train
				new ProjectVersion("spring-cloud-release", "Greenwich.RELEASE"),
				new ProjectVersion("spring-cloud-vault", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-gateway", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-openfeign", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-function", "1.0.0.RELEASE"));
	}

	Projects freshNewReleaseTrainCalver() {
		return new Projects(new ProjectVersion("spring-cloud-aws", "3.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-bus", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cli", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-commons", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-contract", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-config", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-netflix", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-security", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-cloudfoundry", "2.0.0.RELEASE"),
				new ProjectVersion("spring-cloud-consul", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-sleuth", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-stream", "2020.0.0"),
				new ProjectVersion("spring-cloud-zookeeper", "2.0.0.RELEASE"),
				new ProjectVersion("spring-boot", "2.0.4.RELEASE"),
				new ProjectVersion("spring-cloud-task", "2.0.0.RELEASE"),
				// newer release train, current version in file is 2020.0.4
				new ProjectVersion("spring-cloud-release", "2020.0.5"),
				new ProjectVersion("spring-cloud-vault", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-gateway", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-openfeign", "2.0.1.RELEASE"),
				new ProjectVersion("spring-cloud-function", "1.0.0.RELEASE"));
	}

}
