# Path: core/commands.py
# @command(autocomplete=lambda: list_campaigns(),
#          examples=["my-simulation-campaign"],
#          expand=('exp_file', {'into': EXPERIMENT_FOLDER, 'ext': 'json'}),
#          exists=('exp_file', {'on_boolean': 'ask', 'confirm': "Are you sure ? (yes|no) [default: no] "}),
#          start_msg=("REMOVING EXPERIMENT CAMPAIGN AT '{}'", 'exp_file'))
# def drop(exp_file, ask=True, **kwargs):
#     """
#     Remove a campaign of experiments.
# 
#     :param exp_file: experiments JSON filename or basename (absolute or relative path ; if no path provided,
#                      the JSON file is searched in the experiments folder)
#     :param ask: ask confirmation
#     :param kwargs: simulation keyword arguments (see the documentation for more information)
#     """
#     remove_files(EXPERIMENT_FOLDER, exp_file)
# 
# @command(autocomplete=lambda: list_campaigns(),
#          examples=["my-simulation-campaign"],
#          expand=('exp_file', {'into': EXPERIMENT_FOLDER, 'ext': 'json'}),
#          exists=('exp_file', {'loglvl': 'warning', 'msg': (" > Experiment campaign '{}' already exists !", 'exp_file'),
#                               'on_boolean': 'ask', 'confirm': "Overwrite ? (yes|no) [default: no] "}),
#          start_msg=("CREATING NEW EXPERIMENT CAMPAIGN AT '{}'", 'exp_file'))
# def prepare(exp_file, ask=True, **kwargs):
#     """
#     Create a campaign of experiments from a template.
# 
#     :param exp_file: experiments JSON filename or basename (absolute or relative path ; if no path provided,
#                      the JSON file is searched in the experiments folder)
#     :param ask: ask confirmation
#     :param kwargs: simulation keyword arguments (see the documentation for more information)
#     """
#     render_campaign(exp_file)
# 
# Path: core/conf/constants.py
# EXPERIMENT_FOLDER = abspath(expanduser(confparser.get("RPL Attacks Framework Configuration", "experiments_folder")))


import sh
import unittest
from jsmin import jsmin
from json import loads
from os.path import exists, expanduser, join
from core.commands import drop, prepare
from core.conf.constants import EXPERIMENT_FOLDER
class ExperimentTestCase(unittest.TestCase):
    path = expanduser(join(EXPERIMENT_FOLDER, JSON))
    backup = path + '.backup'


class Test6Prepare(ExperimentTestCase):
    """ 6. Prepare a simulation campaign """

    @classmethod
    def setUpClass(cls):
        if exists(cls.path):
            sh.mv(cls.path, cls.backup)
        prepare(JSON, ask=False, silent=True)

    def test1_campaign_format(self):
        """ > Is the new experiment campaign a correct JSON file ? """
        try:
            with open(self.path) as f:
                loads(jsmin(f.read()))
            passed = True
        except:
            passed = False
        self.assertTrue(passed)


class Test7Drop(ExperimentTestCase):
    """ 7. Drop the same simulation campaign """

    @classmethod
    def setUpClass(cls):

# Path: core/commands.py
# @command(autocomplete=lambda: list_campaigns(),
#          examples=["my-simulation-campaign"],
#          expand=('exp_file', {'into': EXPERIMENT_FOLDER, 'ext': 'json'}),
#          exists=('exp_file', {'on_boolean': 'ask', 'confirm': "Are you sure ? (yes|no) [default: no] "}),
#          start_msg=("REMOVING EXPERIMENT CAMPAIGN AT '{}'", 'exp_file'))
# def drop(exp_file, ask=True, **kwargs):
#     """
#     Remove a campaign of experiments.
# 
#     :param exp_file: experiments JSON filename or basename (absolute or relative path ; if no path provided,
#                      the JSON file is searched in the experiments folder)
#     :param ask: ask confirmation
#     :param kwargs: simulation keyword arguments (see the documentation for more information)
#     """
#     remove_files(EXPERIMENT_FOLDER, exp_file)
# 
# @command(autocomplete=lambda: list_campaigns(),
#          examples=["my-simulation-campaign"],
#          expand=('exp_file', {'into': EXPERIMENT_FOLDER, 'ext': 'json'}),
#          exists=('exp_file', {'loglvl': 'warning', 'msg': (" > Experiment campaign '{}' already exists !", 'exp_file'),
#                               'on_boolean': 'ask', 'confirm': "Overwrite ? (yes|no) [default: no] "}),
#          start_msg=("CREATING NEW EXPERIMENT CAMPAIGN AT '{}'", 'exp_file'))
# def prepare(exp_file, ask=True, **kwargs):
#     """
#     Create a campaign of experiments from a template.
# 
#     :param exp_file: experiments JSON filename or basename (absolute or relative path ; if no path provided,
#                      the JSON file is searched in the experiments folder)
#     :param ask: ask confirmation
#     :param kwargs: simulation keyword arguments (see the documentation for more information)
#     """
#     render_campaign(exp_file)
# 
# Path: core/conf/constants.py
# EXPERIMENT_FOLDER = abspath(expanduser(confparser.get("RPL Attacks Framework Configuration", "experiments_folder")))


# Path: tests/campaign.py
import sh
import unittest
from jsmin import jsmin
from json import loads
from os.path import exists, expanduser, join
from core.commands import drop, prepare
from core.conf.constants import EXPERIMENT_FOLDER

class ExperimentTestCase(unittest.TestCase):
    path = expanduser(join(EXPERIMENT_FOLDER, JSON))
    backup = path + '.backup'


class Test6Prepare(ExperimentTestCase):
    """ 6. Prepare a simulation campaign """

    @classmethod
    def setUpClass(cls):
        if exists(cls.path):
            sh.mv(cls.path, cls.backup)
        prepare(JSON, ask=False, silent=True)

    def test1_campaign_format(self):
        """ > Is the new experiment campaign a correct JSON file ? """
        try:
            with open(self.path) as f:
                loads(jsmin(f.read()))
            passed = True
        except:
            passed = False
        self.assertTrue(passed)


class Test7Drop(ExperimentTestCase):
    """ 7. Drop the same simulation campaign """

    @classmethod
    def setUpClass(cls):

        drop(JSON, ask=False, silent=True)