package com.lazerycode.jmeter.json;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;

import java.io.File;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class TestConfigTest {

	private String testConfigFile = "/config.json";
	private String tempdir = System.getProperty("java.io.tmpdir");

	@Test
	public void createConfigFromResourceFile() throws MojoExecutionException, URISyntaxException {
		URL configFile = this.getClass().getResource(testConfigFile);
		File testConfigJSON = new File(configFile.toURI());
		TestConfig testConfig = new TestConfig(testConfigJSON);
		assertThat(testConfig.getFullConfig(), is(equalTo(String.format("{%1$s  \"resultFilesLocations\" : [],%1$s  \"resultsOutputIsCSVFormat\" : false,%1$s  \"someOtherElement\": \"foo\"%1$s}", System.lineSeparator()))));
	}

	@Test(expected = MojoExecutionException.class)
	public void testConfigFileDoesNotExist() throws MojoExecutionException {
		File testConfigJSON = new File("/does/not/exist");
		TestConfig testConfig = new TestConfig(testConfigJSON);
	}

	@Test
	public void createConfigFromInputStream() throws MojoExecutionException {
		InputStream configFile = this.getClass().getResourceAsStream(testConfigFile);
		TestConfig testConfig = new TestConfig(configFile);
		assertThat(testConfig.getFullConfig(), is(equalTo(String.format("{%1$s  \"resultFilesLocations\" : [],%1$s  \"resultsOutputIsCSVFormat\" : false,%1$s  \"someOtherElement\": \"foo\"%1$s}", System.lineSeparator()))));
	}

	@Test(expected = MojoExecutionException.class)
	public void testConfigResourceDoesNotExist() throws MojoExecutionException {
		InputStream configFile = this.getClass().getResourceAsStream("/does/not.exist");
		TestConfig testConfig = new TestConfig(configFile);
	}

	@Test
	public void changeCSVFormat() throws MojoExecutionException {
		InputStream configFile = this.getClass().getResourceAsStream(testConfigFile);
		TestConfig testConfig = new TestConfig(configFile);

		assertThat(testConfig.getResultsOutputIsCSVFormat(), is(equalTo(false)));

		testConfig.setResultsOutputIsCSVFormat(true);

		assertThat(testConfig.getResultsOutputIsCSVFormat(), is(equalTo(true)));

		testConfig.setResultsOutputIsCSVFormat(false);

		assertThat(testConfig.getResultsOutputIsCSVFormat(), is(equalTo(false)));
	}

	@Test
	public void changeResultsFileLocation() throws MojoExecutionException {
		InputStream configFile = this.getClass().getResourceAsStream(testConfigFile);
		TestConfig testConfig = new TestConfig(configFile);

		assertThat(testConfig.getResultsFileLocations().size(), is(equalTo(0)));

		List<String> resultFilenames = new ArrayList<>();
		resultFilenames.add(0, "c:\\windows\\temp");
		resultFilenames.add(1, "/usr/local/temp");

		testConfig.setResultsFileLocations(resultFilenames);

		assertThat(testConfig.getResultsFileLocations().size(), is(equalTo(2)));
		assertThat(testConfig.getResultsFileLocations().get(0), is(equalTo("c:\\windows\\temp")));
		assertThat(testConfig.getResultsFileLocations().get(1), is(equalTo("/usr/local/temp")));
	}

	@Test
	public void checkThatAWrittenFileCanBeReadInAgain() throws MojoExecutionException {

		String tempFileLocation = tempdir + File.separator + UUID.randomUUID() + File.separator + "test_config.json";
		File tempTestFile = new File(tempFileLocation);
		tempTestFile.getParentFile().mkdirs();
		tempTestFile.deleteOnExit();

		InputStream configFile = this.getClass().getResourceAsStream(testConfigFile);
		TestConfig testConfig = new TestConfig(configFile);
		testConfig.setResultsOutputIsCSVFormat(true);
		testConfig.writeResultFilesConfigTo(tempFileLocation);

		TestConfig newlyCreatedTestConfig = new TestConfig(tempTestFile);

		assertThat(testConfig, is(equalTo(newlyCreatedTestConfig)));
	}

	@Test(expected = MojoExecutionException.class)
	public void checkExceptionIsThrownIfFileCannotBeCreated() throws MojoExecutionException {

		String tempFileLocation = tempdir + File.separator + UUID.randomUUID() + File.separator + "test_config.json";
		File tempTestFile = new File(tempFileLocation);
		tempTestFile.deleteOnExit();

		InputStream configFile = this.getClass().getResourceAsStream(testConfigFile);
		TestConfig testConfig = new TestConfig(configFile);
		testConfig.setResultsOutputIsCSVFormat(true);
		testConfig.writeResultFilesConfigTo(tempFileLocation);
	}
}
@@ -41,76 +41,144 @@
 ort 
-org.apache.maven.plugin.MojoExecutionException;%0Aimport org.junit.Tes
+static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.MatcherAssert.assertTha
 t;%0A%0A
@@ -369,144 +369,76 @@
 ort 
-static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.MatcherAssert.assertTha
+org.apache.maven.plugin.MojoExecutionException;%0Aimport org.junit.Tes
 t;%0A%0A
@@ -1264,32 +1264,8 @@
 ;%0A%09%09
-TestConfig testConfig = 
 new 
@@ -1947,34 +1947,10 @@
 %22);%0A
+
 %09%09
-TestConfig testConfig = 
 new 

Remove unused variable