/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Gareth Jon Lynch
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gazbert.bxbot.core.config.engine;

import com.gazbert.bxbot.core.config.ConfigurationManager;
import com.gazbert.bxbot.core.config.engine.generated.Engine;
import org.junit.Test;

import java.math.BigDecimal;

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

/*
 * Tests the Trading Engine configuration is loaded as expected.
 * We're not testing the JAXB impl here - cherry pick the important stuff.
 */
public class TestEngineConfigurationManagement {

    /* Production XSD */
    private static final String XML_SCHEMA_FILENAME = "com/gazbert/bxbot/core/config/engine/engine.xsd";

    /* Test XML config */
    private static final String VALID_XML_CONFIG_FILENAME = "src/test/config/engine/valid-engine.xml";
    private static final String MISSING_XML_CONFIG_FILENAME = "src/test/config/engine/missing-engine.xml";


    @Test
    public void testLoadingValidXmlConfigFileIsSuccessful() {

        final Engine engine = ConfigurationManager.loadConfig(Engine.class,
                VALID_XML_CONFIG_FILENAME, XML_SCHEMA_FILENAME);

        assertEquals("BTC", engine.getEmergencyStopCurrency());
        assertTrue(new BigDecimal("0.5").compareTo(engine.getEmergencyStopBalance()) == 0);
        assertTrue(60 == engine.getTradeCycleInterval());
    }

    @Test(expected = IllegalStateException.class)
    public void testLoadingMissingXmlConfigThrowsException() {

        ConfigurationManager.loadConfig(Engine.class,
                MISSING_XML_CONFIG_FILENAME, XML_SCHEMA_FILENAME);
    }
}

@@ -1360,16 +1360,78 @@
 Decimal;
+%0Aimport java.nio.file.FileSystems;%0Aimport java.nio.file.Files;
 %0A%0Aimport
@@ -2077,16 +2077,327 @@
 e.xml%22;%0A
+    private static final String XML_CONFIG_TO_SAVE_FILENAME = %22src/test/config/engine/saved-engine.xml%22;%0A%0A    private static final String EMERGENCY_STOP_CURRENCY = %22BTC%22;%0A    private static final BigDecimal EMERGENCY_STOP_BALANCE = new BigDecimal(%220.5%22);%0A    private static final int TRADE_CYCLE_INTERVAL = 60;%0A
 %0A%0A    @T
@@ -2626,21 +2626,39 @@
 tEquals(
-%22BTC%22
+EMERGENCY_STOP_CURRENCY
 , engine
@@ -2706,37 +2706,38 @@
 ertTrue(
-new BigDecimal(%220.5%22)
+EMERGENCY_STOP_BALANCE
 .compare
@@ -2799,18 +2799,36 @@
 ertTrue(
-60
+TRADE_CYCLE_INTERVAL
  == engi
@@ -3103,11 +3103,985 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testSavingConfigToXmlIsSuccessful() throws Exception %7B%0A%0A        final Engine engineConfig = new Engine();%0A        engineConfig.setEmergencyStopCurrency(EMERGENCY_STOP_CURRENCY);%0A        engineConfig.setEmergencyStopBalance(EMERGENCY_STOP_BALANCE);%0A        engineConfig.setTradeCycleInterval(TRADE_CYCLE_INTERVAL);%0A%0A        ConfigurationManager.saveConfig(Engine.class, engineConfig, XML_CONFIG_TO_SAVE_FILENAME);%0A%0A        // Read it back in%0A        final Engine engine = ConfigurationManager.loadConfig(Engine.class,%0A                XML_CONFIG_TO_SAVE_FILENAME, XML_SCHEMA_FILENAME);%0A%0A        assertEquals(EMERGENCY_STOP_CURRENCY, engine.getEmergencyStopCurrency());%0A        assertTrue(EMERGENCY_STOP_BALANCE.compareTo(engine.getEmergencyStopBalance()) == 0);%0A        assertTrue(TRADE_CYCLE_INTERVAL == engine.getTradeCycleInterval());%0A%0A        // cleanup%0A        Files.delete(FileSystems.getDefault().getPath(XML_CONFIG_TO_SAVE_FILENAME));%0A    %7D
 %0A%7D%0A

Test for saving engine.xml config.