summaryrefslogtreecommitdiffstats
path: root/app/tests/test_value_templates.py
blob: bc98fa8e6a64a7d3e5a9d88072a3e1a184af817f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import unittest
from toscalib.templates.value import _is_function
from tests.utils.test_utils import init_template


class TestValueMethods(unittest.TestCase):

    def test_value_is_get_input_function(self):
        value = {'get_input': 'some_input_param'}
        res = _is_function(value)
        self.assertEqual(res.target_property, 'some_input_param')
        self.assertEqual(res.type, 'get_input')

    def test_value_is_simple_string(self):
        value = 'not_a_function'
        res = _is_function(value)
        self.assertIsNone(res)

    def test_value_is_not_valid_function(self):
        value = {'function': 'unknown'}
        res = _is_function(value)
        self.assertIsNone(res)

    def test_value_too_many_functions(self):
        value = {'get_input': 'some_input_param', 'get_property': 'some_value'}
        res = _is_function(value)
        self.assertIsNone(res)

    def test_value_is_concat_function(self):
        value = {'concat': ['first', 'second']}
        res = _is_function(value)
        self.assertEqual(res.type, 'concat')
        self.assertEqual(res.extra_data[0].raw_value, 'first')
        self.assertEqual(res.extra_data[0].type, 'string')
        self.assertEqual(res.extra_data[0].value, 'first')
        self.assertEqual(res.extra_data[1].raw_value, 'second')
        self.assertEqual(res.extra_data[1].type, 'string')
        self.assertEqual(res.extra_data[1].value, 'second')

    def test_invalid_function_values_not_list(self):
        value = {'concat': {'first': 'one', 'second': 'two'}}
        res = _is_function(value)
        self.assertIsNone(res)

    def test_value_is_valid_function(self):
        value = {'get_property': ['first', 'second']}
        res = _is_function(value)
        self.assertEqual(res.type, 'get_property')
        self.assertListEqual(res.extra_data, value['get_property'])

    def test_update_get_input_value_prefix(self):
        value = {'get_input': 'some_input_param'}
        res = _is_function(value)
        self.assertEqual(res.target_property, 'some_input_param')
        res._update_prefix('PREFIX_')
        self.assertEqual(res.target_property, 'PREFIX_some_input_param')

    def test_update_get_property_value_no_prefix(self):
        value = {'get_property': ['NO_PREFIX', 'some_property']}
        res = _is_function(value)
        self.assertEqual(res.extra_data[0], 'NO_PREFIX')
        res._update_prefix('PREFIX_')
        self.assertEqual(res.extra_data[0], 'PREFIX')

    def test_update_concat_get_input_value_prefix(self):
        value = {'concat': [{'get_input': 'some_input_param'}, 'second']}
        res = _is_function(value)
        self.assertEqual(res.extra_data[0].function.target_property, 'some_input_param')
        self.assertEqual(res.extra_data[1].value, 'second')
        res._update_prefix('PREFIX_')
        self.assertEqual(res.extra_data[0].function.target_property, 'PREFIX_some_input_param')
        self.assertEqual(res.extra_data[1].value, 'second')

    def test_update_get_input_function_reference(self):
        template = init_template()
        value = {'get_input': 'inputName'}
        res = _is_function(value)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertEqual(res.value_from_item, template.inputs.get('inputName'))

    # TODO uncomment after merging latest ecomp code
    # def test_update_get_input_function_reference_auto_generate_input(self):
    #     template = init_template()
    #     node = template.node_dict.get('nodeName')
    #     value = {'get_input': 'propertyName'}
    #     res = _is_function(value)
    #     self.assertIsNone(res.value_from_item)
    #     self.assertIsNone(template.inputs.get('nodeName_propertyName'))
    #     self.assertEqual(res.target_property, 'propertyName')
    #     res._update_function_reference(template, node, node.properties.get('propertyName'))
    #     self.assertIsNotNone(res.value_from_item)
    #     self.assertEqual(res.value_from_item, template.inputs.get('nodeName_propertyName'))

    def test_update_get_property_function_reference_node_not_found(self):
        value = {'get_property': ['node_name', 'property_name']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(init_template())
        self.assertIsNone(res.value_from_item)

    def test_update_get_property_function_reference_self_property(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'get_property': ['SELF', 'propertyName']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template, node)
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node.properties.get('propertyName'))

    def test_update_get_property_function_reference_other_node_property(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'get_property': ['nodeName', 'propertyName']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertIsNotNone(res.value_from_node)
        self.assertIsNotNone(res.value_from_item)
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node._get_property_item('propertyName'))

    def test_update_get_property_function_reference_capability_property(self):
        template = init_template()
        value = {'get_property': ['nodeName', 'capabilityName', 'capabilityProperty']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertIsNotNone(res.value_from_node)
        self.assertIsNotNone(res.value_from_item)
        node = template.node_dict.get('nodeName')
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node._get_capability_property('capabilityName', 'capabilityProperty'))

    def test_update_get_property_function_reference_requirement_capability_property(self):
        template = init_template()
        value = {'get_property': ['node2', 'dummyRequirement', 'capabilityProperty']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertIsNotNone(res.value_from_node)
        self.assertIsNotNone(res.value_from_item)
        node = template.node_dict.get('nodeName')
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node._get_capability_property('capabilityName', 'capabilityProperty'))

    def test_update_get_property_function_reference_requirement_target_property(self):
        template = init_template()
        value = {'get_property': ['node2', 'dummyRequirement', 'propertyName']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertIsNotNone(res.value_from_node)
        self.assertIsNotNone(res.value_from_item)
        node = template.node_dict.get('nodeName')
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node._get_property_item('propertyName'))

    def test_update_get_attribute_function_reference_invalid_params(self):
        value = {'get_attribute': ['no_such_node', 'some_attribute']}
        res = _is_function(value)
        res._update_function_reference(init_template())
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)

    def test_update_get_attribute_function_self_attribute(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'get_attribute': ['SELF', 'attributeName']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template, node)
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node.attributes.get('attributeName'))

    def test_update_get_attribute_function_node_id_attribute(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'get_attribute': ['SELF', 'id']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template, node)
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node.id)

    def test_update_get_attribute_function_other_node_id_attribute(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'get_attribute': ['nodeName', 'id']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node.id)

    def test_update_get_attribute_function_requirement_target_attribute(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'get_attribute': ['node2', 'dummyRequirement', 'attributeName']}
        res = _is_function(value)
        self.assertIsNone(res.value_from_node)
        self.assertIsNone(res.value_from_item)
        res._update_function_reference(template)
        self.assertEqual(res.value_from_node, node)
        self.assertEqual(res.value_from_item, node.attributes.get('attributeName'))

    def test_update_concat_function_reference(self):
        template = init_template()
        node = template.node_dict.get('nodeName')
        value = {'concat': [{'get_attribute': ['nodeName', 'id']}, 'second']}
        res = _is_function(value)
        self.assertIsNone(res.extra_data[0].function.value_from_node)
        res._update_function_reference(template)
        self.assertIsNotNone(res.extra_data[0].function.value_from_node)
        self.assertIsNotNone(res.extra_data[0].function.value_from_item)
        self.assertEqual(res.extra_data[0].function.value_from_node, node)
        self.assertEqual(res.extra_data[0].function.value_from_item, node.id)