diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php b/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php index 0107189ad8..cfdb470b74 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php @@ -42,8 +42,8 @@ final class ColorCloud extends Item $colorCloudData = parent::decode($data); $colorCloudData['type'] = COLOR_CLOUD; $colorCloudData['color'] = $this->extractColor($data); - $colorCloudData['colorRanges'] = ''; - + $colorCloudData['colorRanges'] = $this->extractColorRanges($data); + $colorCloudData['label'] = null; return $colorCloudData; } @@ -68,12 +68,18 @@ final class ColorCloud extends Item null ); - $color_decode = \json_decode($color); - if (empty($color) === true || empty($color_decode->default_color) === true) { + if (empty($color) === true) { throw new \InvalidArgumentException( 'the color property is required and should be string' ); } else { + $color_decode = \json_decode($color); + if (empty($color_decode->default_color) === true) { + throw new \InvalidArgumentException( + 'the color property is required and should be string' + ); + } + return $color_decode->default_color; } } else { @@ -93,20 +99,32 @@ final class ColorCloud extends Item { if (isset($data['colorRanges']) && \is_array($data['colorRanges'])) { foreach ($data['colorRanges'] as $key => $value) { - if ((!isset($data['colorRanges'][$key]['fromValue']) || !\is_float($data['colorRanges'][$key]['fromValue'])) - || (!isset($data['colorRanges'][$key]['toValue']) || !\is_float($data['colorRanges'][$key]['toValue'])) - || (!isset($data['colorRanges'][$key]['color']) | !\is_string($data['colorRanges'][$key]['color']) || strlen($data['colorRanges'][$key]['color']) == 0) + if ((!isset($value['fromValue']) || !\is_numeric($value['fromValue'])) + || (!isset($value['toValue']) || !\is_numeric($value['toValue'])) + || (!isset($value['color']) || !\is_string($value['color']) + || \strlen($value['color']) == 0) ) { - throw new \InvalidArgumentException( - 'the color property is required and should be string' - ); + throw new \InvalidArgumentException( + 'the fromValue, toValue and color properties is required' + ); } } return $data['colorRanges']; } else if (isset($data['label']) === true) { $colorRanges_decode = \json_decode($data['label']); - return $colorRanges_decode->color_ranges; + $array_out = []; + if (!empty($colorRanges_decode->color_ranges)) { + foreach ($colorRanges_decode->color_ranges as $key => $value) { + $array_aux = []; + $array_aux['fromValue'] = $value->from_value; + $array_aux['toValue'] = $value->to_value; + $array_aux['color'] = $value->color; + array_push($array_out, $array_aux); + } + } + + return $array_out; } else { return []; } diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/EventsHistory.php b/pandora_console/include/rest-api/models/VisualConsole/Items/EventsHistory.php index bde91d979d..297c75d969 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/EventsHistory.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/EventsHistory.php @@ -7,7 +7,7 @@ use Models\VisualConsole\Item; use Models\Model; /** - * Model of a group item of the Visual Console. + * Model of a events history item of the Visual Console. */ final class EventsHistory extends Item { diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Label.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Label.php new file mode 100644 index 0000000000..84e63b7b4e --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Label.php @@ -0,0 +1,65 @@ +expectException(Exception::class); // Invalid clockTimezone. diff --git a/pandora_console/tests/Functional/Models/VisualConsole/Items/ColorCloudTest.php b/pandora_console/tests/Functional/Models/VisualConsole/Items/ColorCloudTest.php new file mode 100644 index 0000000000..02cccd1f75 --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/Items/ColorCloudTest.php @@ -0,0 +1,182 @@ +assertInstanceOf( + ColorCloud::class, + ColorCloud::fromArray( + [ + 'id' => 69, + 'type' => COLOR_CLOUD, + 'label' => '{"default_color":"#47b042","color_ranges":[{"from_value":20,"to_value":60,"color":"#d0da27"},{"from_value":61,"to_value":100,"color":"#ec1f1f"}]}', + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + ] + ) + ); + + $this->assertInstanceOf( + ColorCloud::class, + ColorCloud::fromArray( + [ + 'id' => 1000, + 'type' => COLOR_CLOUD, + 'width' => 100, + 'height' => 900, + 'color' => '#47b042', + 'colorRanges' => [ + [ + 'fromValue' => 50, + 'toValue' => 90, + 'color' => '#d0da27', + ], + [ + 'fromValue' => 910, + 'toValue' => 100, + 'color' => '#ec1f1f', + ], + ], + ] + ) + ); + } + + + /** + * Test if the instance is not created when using a invalid color. + * + * @return void + */ + public function testCannotBeCreatedWithInvalidColor(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid color. + ColorCloud::fromArray( + [ + 'id' => 69, + 'type' => COLOR_CLOUD, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'color' => '', + ] + ); + + // Invalid color. + ColorCloud::fromArray( + [ + 'id' => 69, + 'type' => COLOR_CLOUD, + 'label' => '{"default_col":"#47b042","color_ranges":[]}', + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + ] + ); + + // Missing color. + ColorCloud::fromArray( + [ + 'id' => 69, + 'type' => COLOR_CLOUD, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + ] + ); + } + + + /** + * Test if the model has a valid JSON representation. + * + * @return void + */ + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#47b042","colorRanges":[{"fromValue":20,"toValue":60,"color":"#d0da27"},{"fromValue":61,"toValue":100,"color":"#ec1f1f"}],"height":0,"id":69,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"type":20,"width":0,"x":-666,"y":76}', + (string) ColorCloud::fromArray( + [ + 'id' => 69, + 'type' => COLOR_CLOUD, + 'label' => '{"default_color":"#47b042","color_ranges":[{"from_value":20,"to_value":60,"color":"#d0da27"},{"from_value":61,"to_value":100,"color":"#ec1f1f"}]}', + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + ] + ) + ); + + $this->assertEquals( + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#47b042","colorRanges":[{"fromValue":50,"toValue":90,"color":"#d0da27"},{"fromValue":910,"toValue":100,"color":"#ec1f1f"}],"height":900,"id":1000,"isLinkEnabled":false,"isOnTop":false,"label":null,"labelPosition":"down","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"type":20,"width":100,"x":0,"y":0}', + (string) ColorCloud::fromArray( + [ + 'id' => 1000, + 'type' => COLOR_CLOUD, + 'width' => 100, + 'height' => 900, + 'color' => '#47b042', + 'colorRanges' => [ + [ + 'fromValue' => 50, + 'toValue' => 90, + 'color' => '#d0da27', + ], + [ + 'fromValue' => 910, + 'toValue' => 100, + 'color' => '#ec1f1f', + ], + ], + ] + ) + ); + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/Items/LabelTest.php b/pandora_console/tests/Functional/Models/VisualConsole/Items/LabelTest.php new file mode 100644 index 0000000000..a0063e2cd0 --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/Items/LabelTest.php @@ -0,0 +1,137 @@ +assertInstanceOf( + Label::class, + Label::fromArray( + [ + 'id' => 3, + 'type' => LABEL, + 'width' => '600', + 'height' => '500', + 'label' => 'test', + ] + ) + ); + } + + + /** + * Test if the instance is not created when using a invalid label. + * + * @return void + */ + public function testCannotBeCreatedWithInvalidLabel(): void + { + $this->expectException(InvalidArgumentException::class); + // Invalid id. + Label::fromArray( + [ + 'id' => 3, + 'type' => LABEL, + 'width' => '600', + 'height' => '500', + 'label' => null, + ] + ); + // Missing id. + Label::fromArray( + [ + 'id' => 3, + 'type' => LABEL, + 'width' => '600', + 'height' => '500', + ] + ); + } + + + /** + * Test if the model has a valid JSON representation. + * + * @return void + */ + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"test","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","parentId":null,"type":4,"width":0,"x":-666,"y":76}', + (string) Label::fromArray( + [ + 'id' => 7, + 'type' => LABEL, + 'label' => 'test', + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + ] + ) + ); + + $this->assertEquals( + '{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"test_pandora","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","parentId":null,"type":4,"width":0,"x":-666,"y":76}', + (string) Label::fromArray( + [ + 'id' => 7, + 'type' => LABEL, + 'label' => 'test_pandora', + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'id_layout_linked' => 1, + ] + ) + ); + + $this->assertEquals( + '{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"test_pandora","labelPosition":"up","linkedLayoutAgentId":3,"linkedLayoutId":2,"linkedLayoutStatusType":"default","metaconsoleId":5,"parentId":null,"type":4,"width":0,"x":-666,"y":76}', + (string) Label::fromArray( + [ + 'id' => 7, + 'type' => LABEL, + 'label' => 'test_pandora', + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'id_metaconsole' => 5, + 'linked_layout_node_id' => 3, + 'linkedLayoutId' => 2, + ] + ) + ); + } + + +}