package de.lcag.common;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Map;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import routines.LcagFileTools;

class LcagPropertiesTest {
	private static LcagProperties fsProps = null;

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
		String filePath = LcagFileTools.getResourceFilePath("Filesystem.properties");
		fsProps = new LcagProperties(filePath);
	}

	@BeforeEach
	void setUp() throws Exception {
	}

	@Test
	void testGetPropertyString() {
		String result = fsProps.getProperty("FILE_STORAGE_HOME_DIR_PATH");

		assertEquals("V:/EBX/data/01_outbound", result);

		result = fsProps.getProperty("DOES_NOT_EXIST");
		assertNull(result);

		result = fsProps.getProperty("OS_BIN_DIR");
		assertEquals("V:/EBX/Tools/bin/Windows_NT", result);

	}

	@Test
	void testGetOptionalProperty() {
		String result = fsProps.getOptionalProperty("FILE_STORAGE_HOME_DIR_PATH");

		assertEquals("V:/EBX/data/01_outbound", result);

		result = fsProps.getOptionalProperty("DOES_NOT_EXIST");
		assertNull(result);
	}

	@Test
	void testGetPropertyStringString() {
		String result = fsProps.getProperty("DOES_NOT_EXIST", "n/a");

		assertEquals("n/a", result);
	}

	@Test
	void testGetProperties() {
		Map<String, String> result = fsProps.getProperties();
		String value = result.get("OS_BIN_DIR");

		assertEquals("V:/EBX/Tools/bin/Windows_NT", value);
	}

	@Test
	void testResolveIn() {
		String result, s;

		// TC1: Resolve environment variables in property values but not environment
		// variables or other unknown variables:
		s = "P1: ${FILE_STORAGE_HOME_DIR_PATH}, P2: ${OS}, P3: ${DOES_NOT_EXIST} are all I want";
		result = fsProps.resolveIn(s);
		assertEquals("P1: V:/EBX/data/01_outbound, P2: ${OS}, P3: ${DOES_NOT_EXIST} are all I want", result);

		// TC1: Resolve environment variables in property values:
		s = "Operating system-dependend executables are localted in: ${OS_BIN_DIR} !";
		result = fsProps.resolveIn(s);
		assertEquals("Operating system-dependend executables are localted in: V:/EBX/Tools/bin/Windows_NT !", result);
	}

}
