package controller;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

import org.ini4j.Ini;
import org.junit.Before;
import org.junit.Test;
import controller.DbManager;

public class DbManagerTest {

	private static String configFile = "config.ini";
	
	
	@Before
	public void setup () {	
		
		if( new File(configFile).exists() ==  true ) {
			new File(configFile).delete();
		}
		
		DbManager.getInstance().disconnect();
	}
	
	/**
	 * Vérification du pattern Singleton
	 * 
	 * @author Matthieu HERRMANN
	 * 
	 */
	@Test
	public void testInstanceSingleton() {
		System.out.println("\n--------------------------------------------------");
		System.out.println("\t TEST SINGLETON PATTERN");
		System.out.println("--------------------------------------------------\n");
		
		DbManager db1 = DbManager.getInstance();
		DbManager db2 = DbManager.getInstance();
		
		assertEquals(db1.toString(), db2.toString());
	}
	
	/**
	 * Vérification en cas d'erreur du fichier de config
	 */
	@Test
	public void testFileConfigMissing() {

		System.out.println("\n--------------------------------------------------");
		System.out.println("\t TEST CONFIG FILE MISSING ");
		System.out.println("--------------------------------------------------\n");
		
		DbManager db = DbManager.getInstance();
		
		db.loadConfig( configFile );
		db.connect();
		
		assertEquals(false, db.isConnected());
	}
	
	@Test
	public void testFileFormatException() {
		System.out.println("\n--------------------------------------------------");
		System.out.println("\t TEST CONFIG FILE FORMAT EXCEPTION ");
		System.out.println("--------------------------------------------------\n");
		
		// préparation du fichier de configuration 
		
		try {
			PrintWriter fw = new PrintWriter(configFile, "UTF-8");
			
			fw.write("error in file Format");
			
			fw.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		DbManager db = DbManager.getInstance();
		
		db.loadConfig( configFile );
		db.connect();
		
		assertEquals(false, db.isConnected());
	}
	
	@Test
	public void testConnection() {

		System.out.println("\n--------------------------------------------------");
		System.out.println("\t TEST CORRECT CONNEXION ");
		System.out.println("--------------------------------------------------\n");
		
		Ini config = new Ini();
		
		config.put("database", "dbtype", "mysql");
		config.put("database", "host", "localhost");
		config.put("database", "port", 3306);
		config.put("database", "username", "travis");
		config.put("database", "password", "");
		
		try {
			config.store( new File(configFile) );
		} catch ( IOException e ) {
			e.printStackTrace();
		}
		
		DbManager db = DbManager.getInstance();
		
		db.loadConfig( configFile );
		db.connect();
		
		assertEquals(true, db.isConnected());
		
		db.disconnect();
		
		assertEquals(false, db.isConnected());
	}
	
	@Test
	public void testURLdbConnection() {
		
		System.out.println("\n--------------------------------------------------");
		System.out.println("\t TEST get URL ");
		System.out.println("--------------------------------------------------\n");
		
		Ini config = new Ini();
		
		config.put("database", "dbtype", "mysql");
		config.put("database", "host", "192.168.0.1");
		config.put("database", "port", 5000);
		config.put("database", "username", "root");
		config.put("database", "password", "test");
		
		try {
			config.store( new File(configFile) );
		} catch ( IOException e ) {
			e.printStackTrace();
		}
		
		DbManager db = DbManager.getInstance();
		
		db.loadConfig( configFile );
		String url = db.getURL();
		
		assertEquals(url, "jdbc:mysql://192.168.0.1:5000/transbeton");
	}
	
	@Test
	public void testConfigDatabase() {
		
		System.out.println("\n--------------------------------------------------");
		System.out.println("\t TEST CONFIG DATABASE VALUES ");
		System.out.println("--------------------------------------------------\n");

		DbManager db = DbManager.getInstance();
		
		db.setDbtype("mysql");
		db.setHost("127.0.0.1");
		db.setPort(10000);
		db.setUsername("user");
		db.setPassword("pwd");
		
		db.saveConfig();
		
		db.loadConfig();
		
		assertEquals("mysql", db.getDbtype());
		assertEquals("127.0.0.1", db.getHost());
		assertEquals(10000, db.getPort());
		assertEquals("user", db.getUsername());
		assertEquals("pwd", db.getPassword());
		
		System.out.println("");
	}
}
