From c45cd9d34563ea5ffb00d635356cc31c7b247be9 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 11 Sep 2024 09:26:59 +0200 Subject: [PATCH] Fix tests --- .../Glpi/Inventory/tests/units/Converter.php | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/tests/Glpi/Inventory/tests/units/Converter.php b/tests/Glpi/Inventory/tests/units/Converter.php index 218bc2a..eaa8f6e 100644 --- a/tests/Glpi/Inventory/tests/units/Converter.php +++ b/tests/Glpi/Inventory/tests/units/Converter.php @@ -717,25 +717,35 @@ public function testNetdisco() { ); } - public function testValidate() { + public function testValidateOK() + { $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'hardware' => ['name' => 'my inventory']]])); $instance = new \Glpi\Inventory\Converter(); $this->assertTrue($instance->validate($json)); + } + public function testValidateVersionClient() + { //required "versionclient" is missing $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['hardware' => ['name' => 'my inventory']]])); $instance = new \Glpi\Inventory\Converter(); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Required property missing: versionclient'); $this->assertFalse($instance->validate($json)); + } + public function testValidateUnknownExtraPlugin_node() + { //extra "plugin_node" is unknown $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'plugin_node' => 'plugin node']])); $instance = new \Glpi\Inventory\Converter(); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Additional properties not allowed: plugin_node'); $this->assertFalse($instance->validate($json)); + } + public function testValidateExtraPlugin_node() + { //add extra "plugin_node" as string $extra_prop = ['plugin_node' => ['type' => 'string']]; $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'plugin_node' => 'plugin node']])); @@ -743,27 +753,40 @@ public function testValidate() { $this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraProperties($extra_prop)); $this->assertTrue($instance->validate($json)); + } + + public function testValidateUnknownHwPlugin_node() + { //extra "hardware/hw_plugin_node" is unknown $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'hardware' => ['hw_plugin_node' => 'plugin node']]])); $instance = new \Glpi\Inventory\Converter(); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Additional properties not allowed: hw_plugin_node'); $this->assertFalse($instance->validate($json)); + } + public function testValidateHwPlugin_node() + { //add extra "hardware/hw_plugin_node" as string $extra_sub_prop = ['hardware' => ['hw_plugin_node' => ['type' => 'string']]]; $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'hardware' => ['hw_plugin_node' => 'plugin node']]])); $instance = new \Glpi\Inventory\Converter(); $this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraSubProperties($extra_sub_prop)); $this->assertTrue($instance->validate($json)); + } + public function testValidateUnknownVmPlugin_node() + { //extra "virtualmachines/vm_plugin_node" is unknown $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'virtualmachines' => [['name' => 'My VM', 'vmtype' => 'libvirt', 'vm_plugin_node' => 'plugin node']]]])); $instance = new \Glpi\Inventory\Converter(); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Additional properties not allowed: vm_plugin_node'); $this->assertFalse($instance->validate($json)); + } + public function testValidateVmPlugin_node() + { //add extra "virtualmachines/vm_plugin_node" as string $extra_sub_prop = ['virtualmachines' => ['vm_plugin_node' => ['type' => 'string']]]; $json = json_decode(json_encode([ @@ -782,28 +805,41 @@ public function testValidate() { $instance = new \Glpi\Inventory\Converter(); $this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraSubProperties($extra_sub_prop)); $this->assertTrue($instance->validate($json)); + } + public function testValidateAlreadyExistingExtraNode() + { //try add extra node already existing $extra_prop = ['accesslog' => ['type' => 'string']]; $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0']])); $instance = new \Glpi\Inventory\Converter(); $this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraProperties($extra_prop)); - $this->assertTrue($instance->validate($json)); - + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Property accesslog already exists in schema.'); + $this->assertFalse($instance->validate($json)); + } + public function testValidateUAlreadyExistingExtraSubNode() + { //try add extra sub node already existing $extra_sub_prop = ['hardware' => ['chassis_type' => ['type' => 'string']]]; $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0']])); $instance = new \Glpi\Inventory\Converter(); $this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraSubProperties($extra_sub_prop)); - $this->assertTrue($instance->validate($json)); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Property hardware/chassis_type already exists in schema.'); + $this->assertFalse($instance->validate($json)); + } + public function testValidateMissingParentSubNode() { //try add extra sub node with missing parent $extra_sub_prop = ['unknown' => ['chassis_type' => ['type' => 'string']]]; $json = json_decode(json_encode(['deviceid' => 'myid', 'content' => ['versionclient' => 'GLPI-Agent_v1.0']])); $instance = new \Glpi\Inventory\Converter(); $this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraSubProperties($extra_sub_prop)); - $this->assertTrue($instance->validate($json)); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Property unknown does not exists in schema.'); + $this->assertFalse($instance->validate($json)); } public function testAssetTagFromDiscovery()