# Copyright ETSI Contributors and Others.
#
# 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.

from unittest import TestCase
from osm_lcm.temporal.juju_paas_activities import CharmInfoUtils
from osm_common.dataclasses.temporal_dataclasses import CharmInfo
import yaml

nsr_id = "ea958ba5-4e58-4405-bf42-6e3be15d4c3a"
vim_id = "70b47595-fafa-4f63-904b-fc3ada60eebb"
expected_default_ns_model = "6e3be15d4c3a-fc3ada60eebb"

vdu_nominal = """
---
vdu:
   - id: test-vdu-id
     name: test-vdu-name
     int-cpd:
       - id: internal
         int-virtual-link-desc: network1
       - id: mgmt
     virtual-compute-desc: compute-id
     virtual-storage-desc:
       - storage-id
     sw-image-desc: image-test2
     configurable-properties:
       - key: "track"
         value: "latest"
       - key: "channel"
         value: "edge"
"""

vdu_no_channel = """
---
vdu:
   - id: test-vdu-id
     name: test-vdu-name
     int-cpd:
       - id: internal
         int-virtual-link-desc: network1
       - id: mgmt
     virtual-compute-desc: compute-id
     virtual-storage-desc:
       - storage-id
     sw-image-desc: image-test2
     configurable-properties:
       - key: "track"
         value: "latest"
       - key: "key"
         value: "edge"
"""

vdu_invalid_image = """
---
vdu:
   - id: test-vdu-id
     name: test-vdu-name
     int-cpd:
       - id: internal
         int-virtual-link-desc: network1
       - id: mgmt
     virtual-compute-desc: compute-id
     virtual-storage-desc:
       - storage-id
     sw-image-desc: invalid_image
     configurable-properties:
       - key: "track"
         value: "latest"
       - key: "key"
         value: "edge"
"""

vdu_no_sw_image_desc = """
---
vdu:
   - id: test-vdu-id
     name: test-vdu-name
     int-cpd:
       - id: internal
         int-virtual-link-desc: network1
       - id: mgmt
     virtual-compute-desc: compute-id
     virtual-storage-desc:
       - storage-id
     sw-image-desc: invalid_image
     configurable-properties:
       - key: "track"
         value: "latest"
       - key: "key"
         value: "edge"
"""

sw_image_desc = """
---
sw-image-desc:
  - id: image-test1
    name: charm-name1
    image: ch:mysql
    version: "1.0"
  - id: image-test2
    name: charm-name2
    image: ch:my-charm
    version: "1.0"
"""


class TestCharmInfoUtils(TestCase):
    def setUp(self):
        self.charm_info_utils = CharmInfoUtils()

    def get_loaded_descriptor(self, descriptor):
        return yaml.load(descriptor, Loader=yaml.Loader)

    def test_get_charm_info_nominal_case(self):
        vdu_descriptor = self.get_loaded_descriptor(vdu_nominal).get("vdu")
        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
        expected = CharmInfo("test-vdu-id", "edge", "ch:my-charm")
        self.assertEqual(result, expected)

    def test_get_charm_info_no_channel(self):
        vdu_descriptor = self.get_loaded_descriptor(vdu_no_channel).get("vdu")
        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
        expected = CharmInfo("test-vdu-id", None, "ch:my-charm")
        self.assertEqual(result, expected)

    def test_get_charm_info_invalid_image(self):
        vdu_descriptor = self.get_loaded_descriptor(vdu_invalid_image).get("vdu")
        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
        expected = CharmInfo("test-vdu-id", None, None)
        self.assertEqual(result, expected)

    def test_get_charm_info_no_sw_image_desc(self):
        vdu_descriptor = self.get_loaded_descriptor(vdu_no_sw_image_desc).get("vdu")
        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
        expected = CharmInfo("test-vdu-id", None, None)
        self.assertEqual(result, expected)

    def test_get_charm_info_empty_sw_image_descs(self):
        vdu_descriptor = self.get_loaded_descriptor(vdu_nominal).get("vdu")
        sw_image_descs = []
        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
        expected = CharmInfo("test-vdu-id", "edge", None)
        self.assertEqual(result, expected)
