<commit_before>#  Copyright (c) 2015 SONATA-NFV, UBIWHERE
# ALL RIGHTS RESERVED.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Neither the name of the SONATA-NFV, UBIWHERE
# nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# This work has been performed in the framework of the SONATA project,
# funded by the European Commission under Grant number 671517 through
# the Horizon 2020 and 5G-PPP programmes. The authors would like to
# acknowledge the contributions of their colleagues of the SONATA
# partner consortium (www.sonata-nfv.eu).

import os
import unittest
from son.workspace.workspace import Workspace
from unittest.mock import patch
from unittest import mock


class CreateWorkspaceTests(unittest.TestCase):

    def test___init__(self):
        """
        Verify variable assignments in init function
        :return:
        """
        # Create a new workspace
        ws = Workspace("workspace/root/dir",
                       ws_name="workspace_name",
                       log_level='log_level')

        # Verify its properties
        self.assertEqual(ws.ws_root, "workspace/root/dir")
        self.assertEqual(ws.ws_name, "workspace_name")
        self.assertEqual(ws.log_level, "log_level")

    @patch('os.makedirs')
    def test_create_dirs(self, m_makedirs):
        """
        Verify if the workspace directory structure
        is being well created.
        """
        # Create a new workspace
        ws = Workspace("workspace/root/dir",
                       ws_name="workspace_name",
                       log_level='log_level')

        # Assure directory structure is well created
        m_makedirs.return_value = None
        ws.create_dirs()
        for call in m_makedirs.call_args_list:
            assert '\'workspace/root/dir' in str(call)

    @patch('son.workspace.workspace.log')
    @patch('son.workspace.workspace.yaml')
    @patch('builtins.open')
    @patch('son.workspace.workspace.os.path')
    def test__create_from_descriptor__(self, m_path, m_open, m_yaml, m_log):
        """
        Perform several tests to the static function
        "__create_from_descriptor__" to ensure that
        workspaces are correctly created from a
        configuration descriptor"
        """
        # Make sure the workspace root dir and
        # config file do not exist by patching os.path
        m_path.join.return_value = None
        m_path.isdir.return_value = False
        m_path.isfile.return_value = False

        # Assure that None is returned using
        # non-existent root dir and config file
        self.assertEqual(
            Workspace.__create_from_descriptor__("/some/root/dir"),
            None
        )

        # Assure that an error message was logged
        self.assertTrue(m_log.error.called)

        # Make the root dir and config file exist
        m_path.isdir.return_value = True
        m_path.isfile.return_value = True

        # Create an invalid config descriptor for workspace
        conf_d = {
            'version': '0.01',
            'catalogue_servers': "['http://10.10.1.101:4011',"
                                 "'http://10.10.1.102:4011']",
            'descriptor_extension': 'yml',
            'schemas_local_master': '~/.son-schema',
            'schemas_remote_master': 'https://raw.githubusercontent.com/'
                                     'sonata-nfv/son-schema/master/',
            'platforms_dir': 'platforms',
            'catalogues_dir': 'catalogues',
            'configuration_dir': 'configuration'
        }

        # Feed this descriptor as a config file<commit_msg>corrected workspace unit tests<commit_after>#  Copyright (c) 2015 SONATA-NFV, UBIWHERE
# ALL RIGHTS RESERVED.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Neither the name of the SONATA-NFV, UBIWHERE
# nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# This work has been performed in the framework of the SONATA project,
# funded by the European Commission under Grant number 671517 through
# the Horizon 2020 and 5G-PPP programmes. The authors would like to
# acknowledge the contributions of their colleagues of the SONATA
# partner consortium (www.sonata-nfv.eu).

import os
import unittest
from son.workspace.workspace import Workspace
from unittest.mock import patch
from unittest import mock


class CreateWorkspaceTests(unittest.TestCase):

    def test___init__(self):
        """
        Verify variable assignments in init function
        :return:
        """
        # Create a new workspace
        ws = Workspace("workspace/root/dir",
                       ws_name="workspace_name",
                       log_level='log_level')

        # Verify its properties
        self.assertEqual(ws.ws_root, "workspace/root/dir")
        self.assertEqual(ws.ws_name, "workspace_name")
        self.assertEqual(ws.log_level, "log_level")

    @patch('os.makedirs')
    def test_create_dirs(self, m_makedirs):
        """
        Verify if the workspace directory structure
        is being well created.
        """
        # Create a new workspace
        ws = Workspace("workspace/root/dir",
                       ws_name="workspace_name",
                       log_level='log_level')

        # Assure directory structure is well created
        m_makedirs.return_value = None
        ws.create_dirs()
        for call in m_makedirs.call_args_list:
            assert '\'workspace/root/dir' in str(call)

    @patch('son.workspace.workspace.log')
    @patch('son.workspace.workspace.yaml')
    @patch('builtins.open')
    @patch('son.workspace.workspace.os.path')
    def test__create_from_descriptor__(self, m_path, m_open, m_yaml, m_log):
        """
        Perform several tests to the static function
        "__create_from_descriptor__" to ensure that
        workspaces are correctly created from a
        configuration descriptor"
        """
        # Make sure the workspace root dir and
        # config file do not exist by patching os.path
        m_path.join.return_value = None
        m_path.isdir.return_value = False
        m_path.isfile.return_value = False

        # Assure that None is returned using
        # non-existent root dir and config file
        self.assertEqual(
            Workspace.__create_from_descriptor__("/some/root/dir"),
            None
        )

        # Assure that an error message was logged
        self.assertTrue(m_log.error.called)

        # Make the root dir and config file exist
        m_path.isdir.return_value = True
        m_path.isfile.return_value = True

        # Create an invalid config descriptor for workspace
        conf_d = {
            'version': Workspace.WORKSPACE_VERSION,
            'catalogue_servers': "['http://10.10.1.101:4011',"
                                 "'http://10.10.1.102:4011']",
            'default_descriptor_extension': 'yml',
            'schemas_local_master': '~/.son-schema',
            'schemas_remote_master': 'https://raw.githubusercontent.com/'
                                     'sonata-nfv/son-schema/master/',
            'platforms_dir': 'platforms',
            'catalogues_dir': 'catalogues',
            'configuration_dir': 'configuration'
        }

        # Feed this descriptor as a config file
{'fraction_non_alphanumeric': 0.08414316188670301, 'fraction_numerical': 0.010547523109741644, 'mean_word_length': 2.4728395061728397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2227491', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2263, 'n_words': 728}