# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

import unittest

from cybox.core import Action, ActionRelationship, ActionType
from cybox.test import EntityTestCase, round_trip


class TestAction(EntityTestCase, unittest.TestCase):
    klass = Action

    _full_dict = {
        'id': "example:Action-1",
        'idref': "example:Action-2",
        'ordinal_position': 42,
        'action_status': "Success",
        'context': "Host",
        'timestamp': "2013-10-24T09:54:13",
        'type': u"Modify",
        'name': u"Modify File",
        'description': {'value': "An action!", 'structuring_format': "Text"},
        'action_aliases': ['an alias', 'another_alias'],
        'action_arguments': [
            {
                'argument_name': u"infile",
                'argument_value': "/tmp/somefile.txt",
            },
            {
                'argument_name': u"outfile",
                'argument_value': "/tmp/someotherfile.txt",
            }
        ],
        'discovery_method': {'name': "A tool"},
        'associated_objects': [
            {
                'idref': "example:File-1",
            }
        ],
        'relationships': [
            {
                'type': u"Followed_By",
                'action_reference': [{'action_id': "example:Action-2"}]
            }
        ],
        'frequency': {'rate': 1.0},
    }


class TestActionRelationship(EntityTestCase, unittest.TestCase):
    klass = ActionRelationship

    _full_dict = {
        'type': u"Add",
        'action_reference': [
            {'action_id': "example:Action-1"},
            {'action_id': "example:Action-3"},
        ]
    }

    def test_nonstandard_type_vocab(self):
        ar = ActionRelationship()
        ar.type = ActionType(u"AddedMultipleTimes")
        ar.type.vocab_reference = "http://example.com/action-types/"
        ar.type.xsi_type = None
        ar2 = round_trip(ar)
        self.assertEqual(ar.to_dict(), ar2.to_dict())


#TODO: Test AssociateObjects


if __name__ == "__main__":
    unittest.main()

@@ -109,17 +109,79 @@
 ort 
-unittest%0A
+copy%0Aimport unittest%0A%0Afrom cybox.bindings.cybox_core import parseString
 %0Afro
@@ -1472,16 +1472,435 @@
 %0A    %7D%0A%0A
+    # Test that should be fixed by%0A    # https://github.com/CybOXProject/python-cybox/pull/236%0A    def test_tzinfo_copy(self):%0A        action = Action()%0A        action.timestamp = %222015-03-28T16:39:28.127296+03:00%22%0A        action_xml = action.to_xml()%0A%0A        action2 = Action.from_obj(parseString(action_xml))%0A        action2_copy = copy.deepcopy(action2)%0A        self.assertEqual(action_xml, action2_copy.to_xml())%0A%0A
 %0Aclass T

Add failing test for #236