Skip to content

Commit aec4604

Browse files
committed
Convert ActionReference to TypedField and add tests.
This fixes a bug that manifests itself in python-maec.
1 parent 1856414 commit aec4604

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

cybox/bindings/cybox_core.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,12 +1587,7 @@ def factory(*args_, **kwargs_):
15871587
def get_action_id(self): return self.action_id
15881588
def set_action_id(self, action_id): self.action_id = action_id
15891589
def hasContent_(self):
1590-
if (
1591-
1592-
):
1593-
return True
1594-
else:
1595-
return False
1590+
return False
15961591
def export(self, lwrite, level, namespace_='cybox:', name_='ActionReferenceType', namespacedef_='', pretty_print=True):
15971592
if pretty_print:
15981593
eol_ = '\n'
@@ -1610,7 +1605,6 @@ def export(self, lwrite, level, namespace_='cybox:', name_='ActionReferenceType'
16101605
lwrite('/>%s' % (eol_, ))
16111606
def exportAttributes(self, lwrite, level, already_processed, namespace_='cybox:', name_='ActionReferenceType'):
16121607
if self.action_id is not None:
1613-
16141608
lwrite(' action_id=%s' % (quote_attrib(self.action_id), ))
16151609
def exportChildren(self, lwrite, level, namespace_='cybox:', name_='ActionReferenceType', fromsubclass_=False, pretty_print=True):
16161610
pass
@@ -1623,7 +1617,6 @@ def build(self, node):
16231617
def buildAttributes(self, node, attrs, already_processed):
16241618
value = find_attr_value_('action_id', node)
16251619
if value is not None:
1626-
16271620
self.action_id = value
16281621
def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
16291622
pass

cybox/core/action_reference.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,16 @@
44
"""CybOX Action Reference Class"""
55

66
import cybox
7-
import cybox.bindings.cybox_core as cybox_core_binding
7+
import cybox.bindings.cybox_core as core_binding
88

99

1010
class ActionReference(cybox.Entity):
11+
_binding = core_binding
12+
_binding_class = core_binding.ActionReferenceType
1113
_namespace = 'http://cybox.mitre.org/cybox-2'
1214

15+
action_id = cybox.TypedField("action_id")
16+
1317
def __init__(self, action_id=None):
1418
super(ActionReference, self).__init__()
1519
self.action_id = action_id
16-
17-
def to_obj(self, return_obj=None, ns_info=None):
18-
self._collect_ns_info(ns_info)
19-
20-
action_reference_obj = cybox_core_binding.ActionReferenceType()
21-
if self.action_id is not None:
22-
action_reference_obj.action_id = self.action_id
23-
return action_reference_obj
24-
25-
def to_dict(self):
26-
action_reference_dict = {}
27-
if self.action_id is not None:
28-
action_reference_dict['action_id'] = self.action_id
29-
return action_reference_dict
30-
31-
@staticmethod
32-
def from_dict(action_reference_dict):
33-
if not action_reference_dict:
34-
return None
35-
action_reference_ = ActionReference()
36-
action_reference_.action_id = action_reference_dict.get('action_id')
37-
return action_reference_
38-
39-
@staticmethod
40-
def from_obj(action_reference_obj):
41-
if not action_reference_obj:
42-
return None
43-
action_reference_ = ActionReference()
44-
action_reference_.action_id = action_reference_obj.action_id
45-
return action_reference_
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
2+
# See LICENSE.txt for complete terms.
3+
4+
import unittest
5+
6+
from cybox.core import ActionReference
7+
from cybox.test import EntityTestCase
8+
9+
10+
class TestActionReference(EntityTestCase, unittest.TestCase):
11+
klass = ActionReference
12+
_full_dict = {
13+
'action_id': "example:Action-1",
14+
}
15+
16+
def test_construction(self):
17+
aref = ActionReference(action_id="example:Action-1")
18+
print aref.to_xml()
19+
print aref.to_dict()
20+
self.assertTrue("example:Action-1" in aref.to_xml())
21+
self.assertTrue("example:Action-1" in aref.to_json())
22+
23+
24+
if __name__ == "__main__":
25+
unittest.main()

0 commit comments

Comments
 (0)