class PayloadsInterfaceTestCase(TestCase):
    def setUp(self):
        """Set up the payload module."""
        self.payload_module = PayloadsService()
        self.payload_interface = PayloadsInterface(self.payload_module)

    def kickstart_properties_test(self):
        """Test kickstart properties."""
        self.assertEqual(self.payload_interface.KickstartCommands, ['liveimg'])
        self.assertEqual(self.payload_interface.KickstartSections,
                         ["packages"])
        self.assertEqual(self.payload_interface.KickstartAddons, [])

    def no_payload_set_test(self):
        """Test empty string is returned when no payload is set."""
        self.assertEqual(self.payload_interface.GetActivePayloadPath(), "")

    def generate_kickstart_without_payload_test(self):
        """Test kickstart parsing without payload set."""
        self.assertEqual(self.payload_interface.GenerateKickstart(), "")

    def process_kickstart_with_no_payload_test(self):
        """Test kickstart processing when no payload set or created based on KS data."""
        with self.assertLogs('anaconda.modules.payloads.payloads',
                             level="WARNING") as log:
            self.payload_interface.ReadKickstart("")

            self.assertTrue(
                any(map(lambda x: "No payload was created" in x, log.output)))

    @patch_dbus_publish_object
    def is_payload_set_test(self, publisher):
        """Test IsPayloadSet API."""
        self.assertFalse(self.payload_interface.IsPayloadSet())

        self.payload_interface.CreatePayload(PayloadType.DNF.value)
        self.assertTrue(self.payload_interface.IsPayloadSet())

    @patch_dbus_publish_object
    def create_dnf_payload_test(self, publisher):
        """Test creation and publishing of the DNF payload module."""
        self.payload_interface.CreatePayload(PayloadType.DNF.value)
        self.assertEqual(self.payload_interface.GetActivePayloadPath(),
                         PAYLOAD_DEFAULT.object_path)
        # here the publisher is called twice because the Packages section is also published
        self.assertEqual(publisher.call_count, 2)

    @patch_dbus_publish_object
    def create_live_os_payload_test(self, publisher):
        """Test creation and publishing of the Live OS payload module."""
        self.payload_interface.CreatePayload(PayloadType.LIVE_OS.value)
        self.assertEqual(self.payload_interface.GetActivePayloadPath(),
                         PAYLOAD_LIVE_OS.object_path)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_live_image_payload_test(self, publisher):
        """Test creation and publishing of the Live image payload module."""
        self.payload_interface.CreatePayload(PayloadType.LIVE_IMAGE.value)
        self.assertEqual(self.payload_interface.GetActivePayloadPath(),
                         PAYLOAD_LIVE_IMAGE.object_path)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_invalid_payload_test(self, publisher):
        """Test creation of the not existing payload."""
        with self.assertRaises(ValueError):
            self.payload_interface.CreatePayload("NotAPayload")

    @patch_dbus_publish_object
    def create_multiple_payloads_test(self, publisher):
        """Test creating two payloads."""
        self.payload_interface.CreatePayload(PayloadType.DNF.value)
        self.payload_interface.CreatePayload(PayloadType.LIVE_OS.value)

        # The last one should win
        self.assertEqual(self.payload_interface.GetActivePayloadPath(),
                         PAYLOAD_LIVE_OS.object_path)
        self.assertEqual(publisher.call_count, 3)

    @patch_dbus_publish_object
    def create_live_os_source_test(self, publisher):
        """Test creation of the Live OS source module."""
        source_path = self.payload_interface.CreateSource(
            SourceType.LIVE_OS_IMAGE.value)

        check_dbus_object_creation(self, source_path, publisher,
                                   LiveOSSourceModule)

    @patch_dbus_publish_object
    def create_invalid_source_test(self, publisher):
        """Test creation of the not existing source."""
        with self.assertRaises(ValueError):
            self.payload_interface.CreateSource("NotASource")
class PayloadsInterfaceTestCase(TestCase):
    def setUp(self):
        """Set up the payload module."""
        self.payload_module = PayloadsService()
        self.payload_interface = PayloadsInterface(self.payload_module)

        self.shared_ks_tests = PayloadKickstartSharedTest(
            self, self.payload_module, self.payload_interface)

    def kickstart_properties_test(self):
        """Test kickstart properties."""
        self.assertEqual(
            self.payload_interface.KickstartCommands,
            ["cdrom", "harddrive", "hmc", "liveimg", "nfs", "url"])
        self.assertEqual(self.payload_interface.KickstartSections, [])
        self.assertEqual(self.payload_interface.KickstartAddons, [])

    def no_kickstart_test(self):
        """Test kickstart is not set to the payloads service."""
        ks_in = None
        ks_out = ""
        self.shared_ks_tests.check_kickstart(ks_in,
                                             ks_out,
                                             expected_publish_calls=0)

    def kickstart_empty_test(self):
        """Test kickstart is empty for the payloads service."""
        ks_in = ""
        ks_out = ""
        self.shared_ks_tests.check_kickstart(ks_in,
                                             ks_out,
                                             expected_publish_calls=0)

    def no_payload_set_test(self):
        """Test empty string is returned when no payload is set."""
        with self.assertRaises(PayloadNotSetError):
            self.payload_interface.GetActivePayload()

    def generate_kickstart_without_payload_test(self):
        """Test kickstart parsing without payload set."""
        self.assertEqual(self.payload_interface.GenerateKickstart(), "")

    def process_kickstart_with_no_payload_test(self):
        """Test kickstart processing when no payload set or created based on KS data."""
        self.payload_interface.ReadKickstart("")

        with self.assertRaises(PayloadNotSetError):
            self.payload_interface.GetActivePayload()

    @patch_dbus_publish_object
    def is_payload_set_test(self, publisher):
        """Test IsPayloadSet API."""
        self.assertFalse(self.payload_interface.IsPayloadSet())

        self.payload_interface.CreatePayload(PayloadType.DNF.value)
        self.assertTrue(self.payload_interface.IsPayloadSet())

    @patch_dbus_publish_object
    def create_dnf_payload_test(self, publisher):
        """Test creation and publishing of the DNF payload module."""
        payload_path = self.payload_interface.CreatePayload(
            PayloadType.DNF.value)
        self.assertEqual(self.payload_interface.GetActivePayload(),
                         payload_path)
        self.assertIsInstance(PayloadContainer.from_object_path(payload_path),
                              DNFModule)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_live_os_payload_test(self, publisher):
        """Test creation and publishing of the Live OS payload module."""
        payload_path = self.payload_interface.CreatePayload(
            PayloadType.LIVE_OS.value)
        self.assertEqual(self.payload_interface.GetActivePayload(),
                         payload_path)
        self.assertIsInstance(PayloadContainer.from_object_path(payload_path),
                              LiveOSModule)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_live_image_payload_test(self, publisher):
        """Test creation and publishing of the Live image payload module."""
        payload_path = self.payload_interface.CreatePayload(
            PayloadType.LIVE_IMAGE.value)
        self.assertEqual(self.payload_interface.GetActivePayload(),
                         payload_path)
        self.assertIsInstance(PayloadContainer.from_object_path(payload_path),
                              LiveImageModule)
        publisher.assert_called_once()

    @patch_dbus_publish_object
    def create_invalid_payload_test(self, publisher):
        """Test creation of the not existing payload."""
        with self.assertRaises(ValueError):
            self.payload_interface.CreatePayload("NotAPayload")

    @patch_dbus_publish_object
    def create_multiple_payloads_test(self, publisher):
        """Test creating two payloads."""
        self.payload_interface.CreatePayload(PayloadType.DNF.value)
        path = self.payload_interface.CreatePayload(PayloadType.LIVE_OS.value)

        # The last one should win
        self.assertEqual(self.payload_interface.GetActivePayload(), path)
        self.assertEqual(publisher.call_count, 2)

    @patch_dbus_publish_object
    def create_live_os_source_test(self, publisher):
        """Test creation of the Live OS source module."""
        source_path = self.payload_interface.CreateSource(
            SOURCE_TYPE_LIVE_OS_IMAGE)

        check_dbus_object_creation(self, source_path, publisher,
                                   LiveOSSourceModule)

    @patch_dbus_publish_object
    def create_invalid_source_test(self, publisher):
        """Test creation of the not existing source."""
        with self.assertRaises(ValueError):
            self.payload_interface.CreateSource("NotASource")