# -*- coding:utf-8 -*-
##############################################################################
#
# Copyright (c) 2019 Nexedi SA and Contributors. All Rights Reserved.
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
from erp5.component.test.SlapOSTestCaseMixin import \
  SlapOSTestCaseMixin

class TestSlapOSSubscriptionRequestProcessAlarm(SlapOSTestCaseMixin):

  def _test_alarm_slapos_subscription_request_create_from_orphaned_item(self, portal_type):
    script_name = "Item_createSubscriptionRequest"
    alarm = self.portal.portal_alarms.slapos_subscription_request_create_from_orphaned_item

    #####################################################
    # Instance Tree without Subscription Request
    document = self.portal.getDefaultModule(portal_type).newContent(
        portal_type=portal_type,
        title="Test %s no subscription %s" % (portal_type, self.new_id)
    )
    self._test_alarm(alarm, document, script_name)

    #####################################################
    # Instance Tree with Subscription Request
    document = self.portal.getDefaultModule(portal_type).newContent(
        portal_type=portal_type,
        title="Test %s no subscription %s" % (portal_type, self.new_id)
    )
    self.portal.subscription_request_module.newContent(
        portal_type='Subscription Request',
        title="Test Subscription Request %s" % self.new_id,
        aggregate_value=document
    )
    self._test_alarm_not_visited(alarm, document, script_name)

    #####################################################
    # Instance Tree aggregated to another portal type
    document = self.portal.getDefaultModule(portal_type).newContent(
        portal_type=portal_type,
        title="Test %s another portal type %s" % (portal_type, self.new_id)
    )
    self.portal.sale_packing_list_module.newContent(
        portal_type='Sale Packing List',
        title="Test Sale Packing List %s" % self.new_id,
    ).newContent(
      portal_type="Sale Packing List Line",
      aggregate_value=document
    )
    self._test_alarm(alarm, document, script_name)

  def test_alarm_slapos_subscription_request_create_from_orphaned_instance_tree(self):
    self._test_alarm_slapos_subscription_request_create_from_orphaned_item("Instance Tree")

  def test_alarm_slapos_subscription_request_create_from_orphaned_compute_node(self):
    self._test_alarm_slapos_subscription_request_create_from_orphaned_item("Compute Node")
