diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:18:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:18:42 +0000 |
commit | 9f39660f50004ca7c49ea171e2a6f199487cd667 (patch) | |
tree | 4a77cd3e323c37b0e5b3d7578b9718cdf1a89262 /test/php/library/Eventdb/Data | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-eventdb-9f39660f50004ca7c49ea171e2a6f199487cd667.tar.xz icingaweb2-module-eventdb-9f39660f50004ca7c49ea171e2a6f199487cd667.zip |
Adding upstream version 1.3.0.upstream/1.3.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/php/library/Eventdb/Data')
-rw-r--r-- | test/php/library/Eventdb/Data/LegacyFilterParserTest.php | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/test/php/library/Eventdb/Data/LegacyFilterParserTest.php b/test/php/library/Eventdb/Data/LegacyFilterParserTest.php new file mode 100644 index 0000000..51b34ab --- /dev/null +++ b/test/php/library/Eventdb/Data/LegacyFilterParserTest.php @@ -0,0 +1,77 @@ +<?php + +namespace Tests\Icinga\Module\Eventdb\CustomVariable; + +use Icinga\Module\Eventdb\Data\LegacyFilterParser; +use Icinga\Module\Eventdb\Test\BaseTestCase; + +class LegacyFilterParserTest extends BaseTestCase +{ + /** + * Some filter examples that should work and match the result + * + * Note: This is not always clean JSON, but it should work! + * + * @var array + */ + protected $validFilters = array( + '{}' => 'host_name=testhost', // default filter + '{ host: "test" }' => 'host_name=test', + "{ host: 'test' }" => 'host_name=test', + "{ host: 'otherhostname' }" => 'host_name=otherhostname', + "{ host: 'specialhostname', priorityExclusion: [] }" => 'host_name=specialhostname', + "{ host: 'specialhostname', priorityExclusion: [6,7,8] }" => 'host_name=specialhostname&priority!=6&priority!=7&priority!=8', + '{ "host": "*" }' => '', // doesn't make much sense, but well... + '{ "host": "*", "programInclusion": ["cloud-monitoring"] }' => 'program=cloud-monitoring', + '{ "host": ".*", "programInclusion": ["cloud-monitoring"] }' => 'program=cloud-monitoring', + '{ "host": "myhost.*.example.com" }' => 'host_name=myhost%2A.example.com', + "{ programInclusion: ['test1', 'test2'] }" => 'host_name=testhost&(program=test1|program=test2)', + "{ programInclusion: ['test'] }" => 'host_name=testhost&program=test', + "{ programExclusion: ['test'] }" => 'host_name=testhost&program!=test', + "{ programExclusion: ['test1', 'test2'] }" => 'host_name=testhost&program!=test1&program!=test2', + ); + + public function testFiltersThatContainSomeJson() + { + $filters = array( + ' { host: "test" } ', + ' {} ', + '{}', + "{\n\"multiline\": 1\n}", + ); + foreach ($filters as $filter) { + $this->assertTrue(LegacyFilterParser::isJsonFilter($filter)); + } + } + + public function testFiltersThatDoNotContainJson() + { + $filters = array( + ' {xxxx ', + 1337, + 'sometext', + "{\nbrokenjson\n", + ); + foreach ($filters as $filter) { + $this->assertFalse(LegacyFilterParser::isJsonFilter($filter), 'Filter: ' . $filter); + } + + } + + public function testParsingFilters() + { + foreach ($this->validFilters as $json => $result) { + $this->assertTrue( + LegacyFilterParser::isJsonFilter($json), + 'Should be recognized as JSON filter by isJsonFilter' + ); + + $filter = LegacyFilterParser::parse($json, 'testhost'); + $this->assertEquals( + $result, + $filter->toQueryString(), + 'Resulting URL filter should match for json: ' . $json + ); + } + } +} |