|
| 1 | +""" |
| 2 | +Copyright (c) 2020, Oracle Corporation and/or its affiliates. All rights reserved. |
| 3 | +The Universal Permissive License (UPL), Version 1.0 |
| 4 | +""" |
| 5 | + |
| 6 | +import unittest |
| 7 | + |
| 8 | +from wlsdeploy.aliases.aliases import Aliases |
| 9 | +from wlsdeploy.aliases.location_context import LocationContext |
| 10 | +from wlsdeploy.aliases.model_constants import MACHINE |
| 11 | +from wlsdeploy.tool.util import variable_injector_functions |
| 12 | +from wlsdeploy.util import variables |
| 13 | +from wlsdeploy.util.model_context import ModelContext |
| 14 | + |
| 15 | + |
| 16 | +class VariableInjectorFunctionsTests(unittest.TestCase): |
| 17 | + """ |
| 18 | + Test the variable injector functions methods. |
| 19 | + """ |
| 20 | + wls_version = '12.2.1.3' |
| 21 | + attribute_name = 'Notes' |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + model_context = ModelContext("test", {}) |
| 25 | + self.aliases = Aliases(model_context) |
| 26 | + self.location = LocationContext().append_location(MACHINE) |
| 27 | + self.name_token = self.aliases.get_name_token(self.location) |
| 28 | + self.short_name = self.aliases.get_folder_short_name(self.location) |
| 29 | + |
| 30 | + def testFormatVariableName(self): |
| 31 | + """ |
| 32 | + Verify that names from location are converted to valid variable names. |
| 33 | + """ |
| 34 | + # spaces in model name |
| 35 | + self._check_name('My Machine', 'My-Machine') |
| 36 | + |
| 37 | + # parenthesis can be in model name |
| 38 | + self._check_name('ohMy-(datasource)', 'ohMy--datasource-') |
| 39 | + |
| 40 | + # versioned library names may be model names |
| 41 | + self._check_name('Lib.oracle.wsm.idmrest.sharedlib#1.0@12.2.1.3.Target', |
| 42 | + 'Lib.oracle.wsm.idmrest.sharedlib-1.0-12.2.1.3.Target') |
| 43 | + |
| 44 | + def _check_name(self, name, expected_key): |
| 45 | + """ |
| 46 | + Verify that the specified name is converted to match the expected key. |
| 47 | + A machine location is created with the supplied name and converted to a variable name. |
| 48 | + An expected value is constructed using the expected key and known parameters. |
| 49 | + :param name: the name to be converted |
| 50 | + :param expected_key: the expected variable key |
| 51 | + """ |
| 52 | + self.location.add_name_token(self.name_token, name) |
| 53 | + variable_name = variable_injector_functions.format_variable_name(self.location, self.attribute_name, |
| 54 | + self.aliases) |
| 55 | + |
| 56 | + # verify that a property built with this value will parse correctly |
| 57 | + property_text = '@@PROP:%s@@' % variable_name |
| 58 | + matches = variables._property_pattern.findall(property_text) |
| 59 | + self.assertEqual(1, len(matches), "Property %s should parse correctly" % property_text) |
| 60 | + |
| 61 | + expected_name = "%s.%s.%s" % (self.short_name, expected_key, self.attribute_name) |
| 62 | + self.assertEqual(expected_name, variable_name) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + unittest.main() |
0 commit comments