package xyz.thastertyn;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import javax.naming.ConfigurationException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import xyz.thastertyn.Config.Config;
import xyz.thastertyn.Types.Credentials;

public class ConfigTest {

	Config config = Config.getInstance();

	File configFile;
	File configFileBackup;

	String path = config.getPath();

	@Before
	public void backupConfig() throws IOException
	{
		configFile = new File(path);
		configFileBackup = new File(path + ".bak");
		Files.copy(configFile.toPath(), configFileBackup.toPath(), StandardCopyOption.REPLACE_EXISTING);
	}

	@After
	public void moveConfigBack() throws IOException
	{
		Files.copy(configFileBackup.toPath(), configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		configFileBackup.delete();
	}

	@Test
	public void checkIfExists()
	{
		config.addConfig("testKey", "testValue");
		assertTrue(config.checkIfExists("testKey"));
		config.removeConfig("testKey");
		assertFalse(config.checkIfExists("testKey"));
	}

	@Test
	public void checkCorrectKeyValue() throws ConfigurationException
	{
		config.addConfig("testKey2", "testValue2");
		assertEquals("testValue2", config.getConfig("testKey2"));
	}

	@Test
	public void checkIfCredentialsExist()
	{
		config.addConfig("user", "myusername");
		config.addConfig("pass", "mypassword");
		assertTrue(config.checkIfCredentialsExist());
		config.removeCredentials();
		assertFalse(config.checkIfCredentialsExist());
	}

	@Test
	public void checkCorrectCredentials() throws ConfigurationException
	{
		config.addConfig("user", "MyUser123");
		config.addConfig("pass", "VeryStrongP4SSWORD!#");
		Credentials credentials = config.getCredentials();
		assertNotNull(credentials);
		assertEquals("MyUser123", credentials.getUsername());
		assertEquals("VeryStrongP4SSWORD!#", credentials.getPassword());
	}
}
