<?php
/*
** Zabbix
** Copyright (C) 2001-2016 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


class CImportDataAdapterTest extends PHPUnit_Framework_TestCase {

	/**
	 * Cached XMl sources
	 *
	 * @var array
	 */
	protected $sources = [];

	public function testEmptyXml() {
		$adapter = $this->getAdapter($this->getEmptyXml());

		$this->assertEquals($adapter->getGroups(), []);
		$this->assertEquals($adapter->getHosts(), []);
		$this->assertEquals($adapter->getTemplates(), []);
		$this->assertEquals($adapter->getApplications(), []);
		$this->assertEquals($adapter->getItems(), []);
		$this->assertEquals($adapter->getTriggers(), []);
		$this->assertEquals($adapter->getGraphs(), []);
		$this->assertEquals($adapter->getDiscoveryRules(), []);
		$this->assertEquals($adapter->getTemplateScreens(), []);
		$this->assertEquals($adapter->getScreens(), []);
		$this->assertEquals($adapter->getImages(), []);
		$this->assertEquals($adapter->getMaps(), []);
	}

	public function testGetGroups() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getGroups(), [
			[
				'name' => 'Linux servers'
			],
			[
				'name' => 'Zabbix servers'
			],
			[
				'name' => 'Templates'
			]
		]);
	}

	public function testGetHosts() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getHosts(), [
			[
				'inventory' => [],
				'proxy' => [],
				'groups' => [
					[
						'name' => 'Linux servers'
					]
				],
				'templates' => [],
				'macros' =>	[],
				'interfaces' => [
					[
						'type' => '1',
						'useip' => '1',
						'ip' => '127.0.0.1',
						'dns' => '',
						'port' => '10050',
						'interface_ref' => 'if1',
						'main' => '1'
					]
				],
				'host' => 'empty-host',
				'status' => '0',
				'description' => '',
				'ipmi_authtype' => '-1',
				'ipmi_privilege' => '2',
				'ipmi_username' => '',
				'ipmi_password' => '',
				'name' => 'empty-host',
				'inventory_mode' => -1
			],
			[
				'inventory' => [
					'type' => 'my-type',
					'type_full' => '',
					'name' => '',
					'alias' => '',
					'os' => '',
					'os_full' => '',
					'os_short' => '',
					'serialno_a' => '',
					'serialno_b' => '',
					'tag' => '',
					'asset_tag' => '',
					'macaddress_a' => '',
					'macaddress_b' => '',
					'hardware' => '',
					'hardware_full' => '',
					'software' => '',
					'software_full' => '',
					'software_app_a' => '',
					'software_app_b' => '',
					'software_app_c' => '',
					'software_app_d' => '',
					'software_app_e' => '',
					'contact' => '',
					'location' => '',
					'location_lat' => '',
					'location_lon' => '',
					'notes' => '',
					'chassis' => '',
					'model' => '',
					'hw_arch' => '',
					'vendor' => '',
					'contract_number' => '',
					'installer_name' => '',
					'deployment_status' => '',
					'url_a' => '',
					'url_b' => '',
					'url_c' => '',
					'host_networks' => '',
					'host_netmask' => '',
					'host_router' => '',
					'oob_ip' => '',
					'oob_netmask' => '',
					'oob_router' => '',
					'date_hw_purchase' => '',
					'date_hw_install' => '',
					'date_hw_expiry' => '',
					'date_hw_decomm' => '',
					'site_address_a' => '',
					'site_address_b' => '',
					'site_address_c' => '',
					'site_city' => '',
					'site_state' => '',
					'site_country' => '',
					'site_zip' => '',
					'site_rack' => '',
					'site_notes' => '',
					'poc_1_name' => '',
					'poc_1_email' => '',
					'poc_1_phone_a' => '',
					'poc_1_phone_b' => '',
					'poc_1_cell' => '',
					'poc_1_screen' => '',
					'poc_1_notes' => '',
					'poc_2_name' => '',
					'poc_2_email' => '',
					'poc_2_phone_a' => '',
					'poc_2_phone_b' => '',
					'poc_2_cell' => '',
					'poc_2_screen' => '',
					'poc_2_notes' => ''
				],
				'proxy' => [],
				'groups' => [
						[
							'name' => 'Linux servers'
						]
					],
				'templates' => [
					[
						'name' => 'Template App FTP Service'
					],
					[
						'name' => 'Template OS Linux'
					]
				],
				'macros' => [
						[
							'macro' => '{$MACRO}',
							'value' => '2'
						],
						[
							'macro' => '{$SNMP_COMMUNITY}',
							'value' => '1'
						]
					],
				'interfaces' => [
					[
						'type' => '1',
						'useip' => '1',
						'ip' => '0.0.0.0',
						'dns' => '',
						'port' => '10050',
						'interface_ref' => 'if1',
						'main' => '1'
					],
					[
						'type' => '3',
						'useip' => '1',
						'ip' => '127.0.0.1',
						'dns' => '',
						'port' => '623',
						'interface_ref' => 'if4',
						'main' => '1'
					],
					[
						'type' => '2',
						'useip' => '1',
						'ip' => '127.0.0.1',
						'dns' => '',
						'port' => '161',
						'bulk' => '1',
						'interface_ref' => 'if2',
						'main' => '1'
					],
					[
						'type' => '4',
						'useip' => '1',
						'ip' => '127.0.0.1',
						'dns' => '',
						'port' => '12345',
						'interface_ref' => 'if3',
						'main' => '1'
					]
				],
				'host' => 'export-host',
				'status' => '0',
				'description' => '',
				'ipmi_authtype' => '-1',
				'ipmi_privilege' => '2',
				'ipmi_username' => 'impi username',
				'ipmi_password' => 'impi password',
				'name' => 'export-host',
				'inventory_mode' => '0'
			]
		]);
	}

	public function testGetTemplates() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getTemplates(), [
			[
				'groups' => [
					[
						'name' => 'Templates'
					]
				],
				'macros' => [],
				'templates' => [],
				'host' => 'empty-template',
				'name' => 'empty-template',
				'description' => ''
			],
			[
				'groups' =>	[
					[
						'name' => 'Templates'
					]
				],
				'macros' => [
					[
						'macro' => '{$MACRO}',
						'value' => '2'
					],
					[
						'macro' => '{$SNMP_COMMUNITY}',
						'value' => '1'
					]
				],
				'templates' => [
					[
						'name' => 'Template App FTP Service'
					],
					[
						'name' => 'Template OS Linux'
					]
				],
				'host' => 'export-template',
				'name' => 'export-template',
				'description' => ''
			]
		]);
	}

	public function testGetApplications() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getApplications(), [
			'export-host' => [
				'app' => [
					'name' => 'app'
				]
			],
			'export-template' => [
				'app' => [
					'name' => 'app'
				]
			]
		]);
	}

	public function testGetItems() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getItems(), [
			'export-host' => [
				'item' => [
					'name' => 'item',
					'type' => '0',
					'snmp_community' => '',
					'multiplier' => '0',
					'snmp_oid' => '',
					'delay' => '30',
					'history' => '90',
					'trends' => '365',
					'status' => '0',
					'value_type' => '3',
					'units' => '',
					'delta' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'formula' => '1',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'data_type' => '0',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'description' => '',
					'inventory_link' => '0',
					'applications' => [],
					'valuemap' => [],
					'logtimefmt' => '',
					'interface_ref' => 'if1',
					'key_' => 'item',
					'trapper_hosts' => ''
				],
				'item2' => [
					'name' => 'item2',
					'type' => '0',
					'snmp_community' => '',
					'multiplier' => '0',
					'snmp_oid' => '',
					'delay' => '30',
					'history' => '90',
					'trends' => '365',
					'status' => '0',
					'value_type' => '3',
					'units' => '',
					'delta' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'formula' => '1',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'data_type' => '0',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'description' => '',
					'inventory_link' => '0',
					'applications' => [
						[
							'name' => 'app'
						]
					],
					'valuemap' => [],
					'logtimefmt' => '',
					'interface_ref' => 'if1',
					'key_' => 'item2',
					'trapper_hosts' => ''
				]
			],
			'export-template' => [
				'item' => [
					'name' => 'item',
					'type' => '0',
					'snmp_community' => '',
					'multiplier' => '0',
					'snmp_oid' => '',
					'delay' => '30',
					'history' => '90',
					'trends' => '365',
					'status' => '0',
					'value_type' => '3',
					'units' => '',
					'delta' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'formula' => '1',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'data_type' => '0',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'description' => '',
					'inventory_link' => '0',
					'applications' => [],
					'valuemap' => [],
					'logtimefmt' => '',
					'key_' => 'item',
					'trapper_hosts' => ''
				],
				'item2' => [
					'name' => 'item2',
					'type' => '0',
					'snmp_community' => '',
					'multiplier' => '0',
					'snmp_oid' => '',
					'delay' => '30',
					'history' => '90',
					'trends' => '365',
					'status' => '0',
					'value_type' => '3',
					'units' => '',
					'delta' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'formula' => '1',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'data_type' => '0',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'description' => '',
					'inventory_link' => '0',
					'applications' => [
						[
							'name' => 'app'
						]
					],
					'valuemap' => [],
					'logtimefmt' => '',
					'key_' => 'item2',
					'trapper_hosts' => ''
				]
			]
		]);
	}

	public function testGetTriggers() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getTriggers(), [
			[
				'expression' => '{export-host:item.last(0)}<>0',
				'url' => '',
				'status' => '0',
				'priority' => '0',
				'type' => '0',
				'dependencies' =>[
					[
						'name' => 'trigger2',
						'expression' => '{export-host:item.last(0)}<>0'
					]
				],
				'comments' => '',
				'description' => 'trigger'
			],
			[
				'expression' => '{export-host:item.last(0)}<>0',
				'url' => '',
				'status' => '0',
				'priority' => '0',
				'type' => '0',
				'dependencies' => [],
				'comments' => '',
				'description' => 'trigger2'
			],
			[
				'expression' => '{export-template:item.last(0)}<>0',
				'url' => '',
				'status' => '0',
				'priority' => '0',
				'type' => '0',
				'dependencies' =>[
					[
						'name' => 'trigger2',
						'expression' => '{export-template:item.last(0)}<>0'
					]
				],
				'comments' => '',
				'description' => 'trigger'
			],
			[
				'expression' => '{export-template:item.last(0)}<>0',
				'url' => '',
				'status' => '0',
				'priority' => '0',
				'type' => '0',
				'dependencies' => [],
				'comments' => '',
				'description' => 'trigger2'
			]
		]);
	}

	public function testGetGraphs() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getGraphs(), [
			[
				'name' => 'simple',
				'width' => '900',
				'height' => '200',
				'yaxismin' => '0.0000',
				'yaxismax' => '100.0000',
				'show_work_period' => '0',
				'show_triggers' => '0',
				'show_legend' => '1',
				'show_3d' => '0',
				'percent_left' => '0.0000',
				'percent_right' => '0.0000',
				'ymin_item_1' => [],
				'ymax_item_1' => [],
				'graphtype' => '0',
				'ymin_type' => '0',
				'ymax_type' => '0',
				'gitems' => [
					[
						'sortorder' => '0',
						'drawtype' => '0',
						'color' => '009900',
						'yaxisside' => '0',
						'calc_fnc' => '2',
						'type' => '0',
						'item' => [
							'host' => 'export-host',
							'key' => 'item'
						]
					]
				]
			],
			[
				'name' => 'simple',
				'width' => '900',
				'height' => '200',
				'yaxismin' => '0.0000',
				'yaxismax' => '100.0000',
				'show_work_period' => '0',
				'show_triggers' => '0',
				'show_legend' => '1',
				'show_3d' => '0',
				'percent_left' => '0.0000',
				'percent_right' => '0.0000',
				'ymin_item_1' => [],
				'ymax_item_1' => [],
				'graphtype' => '0',
				'ymin_type' => '0',
				'ymax_type' => '0',
				'gitems' => [
					[
						'sortorder' => '0',
						'drawtype' => '0',
						'color' => '009900',
						'yaxisside' => '0',
						'calc_fnc' => '2',
						'type' => '0',
						'item' => [
							'host' => 'export-template',
							'key' => 'item'
						]
					]
				]
			]
		]);
	}

	public function testGetDiscoveryRules() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getDiscoveryRules(), [
			'export-host' => [
				'empty-lld-rule' => [
					'name' => 'empty-lld-rule',
					'type' => '0',
					'snmp_community' => '',
					'snmp_oid' => '',
					'delay' => '30',
					'status' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'filter' => [
						'evaltype' => 0,
						'formula' => '',
						'conditions' => []
					],
					'lifetime' => '30',
					'description' => '',
					'item_prototypes' => [],
					'trigger_prototypes' => [],
					'graph_prototypes' => [],
					'host_prototypes' => [],
					'interface_ref' => 'if1',
					'key_' => 'empty-lld-rule',
					'trapper_hosts' => ''
				],
				'lld-rule' => [
					'name' => 'lld-rule',
					'type' => '0',
					'snmp_community' => '',
					'snmp_oid' => '',
					'delay' => '30',
					'status' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'filter' => [
						'evaltype' => '0',
						'formula' => '',
						'conditions' => [
							[
								'macro' => '{#FSTYPE}',
								'value' => '1',
								'operator' => '8',
								'formulaid' => 'A'
							],
							[
								'macro' => '{#FSTYPE2}',
								'value' => '2',
								'operator' => '8',
								'formulaid' => 'B'
							]
						]
					],
					'lifetime' => '30',
					'description' => '',
					'item_prototypes' => [
						[
							'name' => 'lld-item',
							'type' => '0',
							'snmp_community' => '',
							'multiplier' => '0',
							'snmp_oid' => '',
							'delay' => '30',
							'history' => '90',
							'trends' => '365',
							'status' => '0',
							'value_type' => '3',
							'units' => '',
							'delta' => '0',
							'snmpv3_contextname' => '',
							'snmpv3_securityname' => '',
							'snmpv3_securitylevel' => '0',
							'snmpv3_authprotocol' => '0',
							'snmpv3_authpassphrase' => '',
							'snmpv3_privprotocol' => '0',
							'snmpv3_privpassphrase' => '',
							'formula' => '1',
							'delay_flex' => '',
							'params' => '',
							'ipmi_sensor' => '',
							'data_type' => '0',
							'authtype' => '0',
							'username' => '',
							'password' => '',
							'publickey' => '',
							'privatekey' => '',
							'port' => '',
							'description' => '',
							'inventory_link' => '0',
							'applications' => [],
							'valuemap' => [],
							'logtimefmt' => '',
							'interface_ref' => 'if1',
							'key_' => 'lld-item',
							'trapper_hosts' => ''
						],
						[
							'name' => 'lld-item2',
							'type' => '0',
							'snmp_community' => '',
							'multiplier' => '0',
							'snmp_oid' => '',
							'delay' => '30',
							'history' => '90',
							'trends' => '365',
							'status' => '0',
							'value_type' => '3',
							'units' => '',
							'delta' => '0',
							'snmpv3_contextname' => '',
							'snmpv3_securityname' => '',
							'snmpv3_securitylevel' => '0',
							'snmpv3_authprotocol' => '0',
							'snmpv3_authpassphrase' => '',
							'snmpv3_privprotocol' => '0',
							'snmpv3_privpassphrase' => '',
							'formula' => '1',
							'delay_flex' => '',
							'params' => '',
							'ipmi_sensor' => '',
							'data_type' => '0',
							'authtype' => '0',
							'username' => '',
							'password' => '',
							'publickey' => '',
							'privatekey' => '',
							'port' => '',
							'description' => '',
							'inventory_link' => '0',
							'applications' => [
								[
									'name' => 'app'
								]
							],
							'valuemap' => [],
							'logtimefmt' => '',
							'interface_ref' => 'if1',
							'key_' => 'lld-item2',
							'trapper_hosts' => ''
						]
					],
					'trigger_prototypes' => [
						[
							'expression' => '{export-host:lld-item.last()}=0',
							'url' => '',
							'status' => '0',
							'priority' => '0',
							'type' => '0',
							'comments' => '',
							'description' => 'lld-trigger'
						]
					],
					'graph_prototypes' => [
						[
							'name' => 'lld-graph',
							'width' => '900',
							'height' => '200',
							'yaxismin' => '0.0000',
							'yaxismax' => '100.0000',
							'show_work_period' => '1',
							'show_triggers' => '1',
							'show_legend' => '1',
							'show_3d' => '0',
							'percent_left' => '0.0000',
							'percent_right' => '0.0000',
							'ymin_item_1' => [],
							'ymax_item_1' => [],
							'graphtype' => '0',
							'ymin_type' => '0',
							'ymax_type' => '0',
							'gitems' => [
								[
									'sortorder' => '0',
									'drawtype' => '0',
									'color' => '00C800',
									'yaxisside' => '0',
									'calc_fnc' => '2',
									'type' => '0',
									'item' => [
										'host' => 'export-host',
										'key' => 'lld-item'
									]
								]
							]
						]
					],
					'host_prototypes' => [
						[
							'host' => '{#VMID}',
							'name' => '{#VMID}',
							'status' => '0',
							'group_links' => [
								[
									'group' => [
										'name' => 'Zabbix servers'
									]
								]
							],
							'group_prototypes' => [
								[
									'name' => '{#VMNAME}'
								]
							],
							'templates' => []
						]
					],
					'interface_ref' => 'if1',
					'key_' => 'lld-rule',
					'trapper_hosts' => ''
				]
			],
			'export-template' => [
				'empty-lld-rule' => [
					'name' => 'empty-lld-rule',
					'type' => '0',
					'snmp_community' => '',
					'snmp_oid' => '',
					'delay' => '30',
					'status' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'filter' => [
						'evaltype' => 0,
						'formula' => '',
						'conditions' => []
					],
					'lifetime' => '30',
					'description' => '',
					'item_prototypes' => [],
					'trigger_prototypes' => [],
					'graph_prototypes' => [],
					'host_prototypes' => [],
					'key_' => 'empty-lld-rule',
					'trapper_hosts' => ''
				],
				'lld-rule' => [
					'name' => 'lld-rule',
					'type' => '0',
					'snmp_community' => '',
					'snmp_oid' => '',
					'delay' => '30',
					'status' => '0',
					'snmpv3_contextname' => '',
					'snmpv3_securityname' => '',
					'snmpv3_securitylevel' => '0',
					'snmpv3_authprotocol' => '0',
					'snmpv3_authpassphrase' => '',
					'snmpv3_privprotocol' => '0',
					'snmpv3_privpassphrase' => '',
					'delay_flex' => '',
					'params' => '',
					'ipmi_sensor' => '',
					'authtype' => '0',
					'username' => '',
					'password' => '',
					'publickey' => '',
					'privatekey' => '',
					'port' => '',
					'filter' => [
						'evaltype' => '0',
						'formula' => '',
						'conditions' => [
							[
								'macro' => '{#FSTYPE}',
								'value' => '1',
								'operator' => '8',
								'formulaid' => 'A'
							],
							[
								'macro' => '{#FSTYPE2}',
								'value' => '2',
								'operator' => '8',
								'formulaid' => 'B'
							]
						]
					],
					'lifetime' => '30',
					'description' => '',
					'item_prototypes' => [
						[
							'name' => 'lld-item',
							'type' => '0',
							'snmp_community' => '',
							'multiplier' => '0',
							'snmp_oid' => '',
							'delay' => '30',
							'history' => '90',
							'trends' => '365',
							'status' => '0',
							'value_type' => '3',
							'units' => '',
							'delta' => '0',
							'snmpv3_contextname' => '',
							'snmpv3_securityname' => '',
							'snmpv3_securitylevel' => '0',
							'snmpv3_authprotocol' => '0',
							'snmpv3_authpassphrase' => '',
							'snmpv3_privprotocol' => '0',
							'snmpv3_privpassphrase' => '',
							'formula' => '1',
							'delay_flex' => '',
							'params' => '',
							'ipmi_sensor' => '',
							'data_type' => '0',
							'authtype' => '0',
							'username' => '',
							'password' => '',
							'publickey' => '',
							'privatekey' => '',
							'port' => '',
							'description' => '',
							'inventory_link' => '0',
							'applications' => [],
							'valuemap' => [],
							'logtimefmt' => '',
							'key_' => 'lld-item',
							'trapper_hosts' => ''
						],
						[
							'name' => 'lld-item2',
							'type' => '0',
							'snmp_community' => '',
							'multiplier' => '0',
							'snmp_oid' => '',
							'delay' => '30',
							'history' => '90',
							'trends' => '365',
							'status' => '0',
							'value_type' => '3',
							'units' => '',
							'delta' => '0',
							'snmpv3_contextname' => '',
							'snmpv3_securityname' => '',
							'snmpv3_securitylevel' => '0',
							'snmpv3_authprotocol' => '0',
							'snmpv3_authpassphrase' => '',
							'snmpv3_privprotocol' => '0',
							'snmpv3_privpassphrase' => '',
							'formula' => '1',
							'delay_flex' => '',
							'params' => '',
							'ipmi_sensor' => '',
							'data_type' => '0',
							'authtype' => '0',
							'username' => '',
							'password' => '',
							'publickey' => '',
							'privatekey' => '',
							'port' => '',
							'description' => '',
							'inventory_link' => '0',
							'applications' => [
								[
									'name' => 'app'
								]
							],
							'valuemap' => [],
							'logtimefmt' => '',
							'key_' => 'lld-item2',
							'trapper_hosts' => ''
						]
					],
					'trigger_prototypes' => [
						[
							'expression' => '{export-template:lld-item.last()}=0',
							'url' => '',
							'status' => '0',
							'priority' => '0',
							'type' => '0',
							'comments' => '',
							'description' => 'lld-trigger'
						]
					],
					'graph_prototypes' => [
						[
							'name' => 'lld-graph',
							'width' => '900',
							'height' => '200',
							'yaxismin' => '0.0000',
							'yaxismax' => '100.0000',
							'show_work_period' => '1',
							'show_triggers' => '1',
							'show_legend' => '1',
							'show_3d' => '0',
							'percent_left' => '0.0000',
							'percent_right' => '0.0000',
							'ymin_item_1' => [],
							'ymax_item_1' => [],
							'graphtype' => '0',
							'ymin_type' => '0',
							'ymax_type' => '0',
							'gitems' => [
								[
									'sortorder' => '0',
									'drawtype' => '0',
									'color' => '00C800',
									'yaxisside' => '0',
									'calc_fnc' => '2',
									'type' => '0',
									'item' => [
										'host' => 'export-template',
										'key' => 'lld-item'
									]
								]
							]
						]
					],
					'host_prototypes' => [
						[
							'host' => '{#VMID}',
							'name' => '{#VMID}',
							'status' => '0',
							'group_links' => [
								[
									'group' => [
										'name' => 'Zabbix servers'
									]
								]
							],
							'group_prototypes' => [
								[
									'name' => '{#VMNAME}'
								]
							],
							'templates' => []
						]
					],
					'key_' => 'lld-rule',
					'trapper_hosts' => ''
				]
			]
		]);
	}

	public function testGetTemplateScreens() {
		$adapter = $this->getAdapter($this->getHostAndTemplateXml());

		$this->assertEquals($adapter->getTemplateScreens(), [
			'export-template' => [
				'screen' => [
					'name' => 'screen',
					'hsize' => '1',
					'vsize' => '1',
					'screenitems' => [
						[
							'resourcetype' => '0',
							'width' => '500',
							'height' => '100',
							'x' => '0',
							'y' => '0',
							'colspan' => '1',
							'rowspan' => '1',
							'elements' => '0',
							'valign' => '0',
							'halign' => '0',
							'style' => '0',
							'url' => '',
							'dynamic' => '0',
							'sort_triggers' => '0',
							'resource' => [
								'name' => 'graph',
								'host' => 'export-template'
							],
							'max_columns' => '1',
							'application' => ''
						]
					]
				]
			]
		]);
	}

	public function testGetImages() {
		$adapter = $this->getAdapter($this->getMapXml());

		$this->assertEquals($adapter->getImages(), [
			[
				'name' => 'Server_(96)',
				'imagetype' => '1',
				'image' => ' ',
			]
		]);
	}

	public function testGetMaps() {
		$adapter = $this->getAdapter($this->getMapXml());

		$this->assertEquals($adapter->getMaps(), [
			[
				'name' => 'empty-map',
				'width' => '800',
				'height' => '600',
				'label_type' => '0',
				'label_location' => '0',
				'highlight' => '0',
				'expandproblem' => '0',
				'markelements' => '0',
				'show_unack' => '0',
				'severity_min' => '0',
				'grid_size' => '50',
				'grid_show' => '1',
				'grid_align' => '1',
				'label_format' => '0',
				'label_type_host' => '2',
				'label_type_hostgroup' => '2',
				'label_type_trigger' => '2',
				'label_type_map' => '2',
				'label_type_image' => '2',
				'label_string_host' => '',
				'label_string_hostgroup' => '',
				'label_string_trigger' => '',
				'label_string_map' => '',
				'label_string_image' => '',
				'expand_macros' => '0',
				'background' => [],
				'iconmap' => [],
				'urls' => [],
				'selements' => [],
				'links' => []
			],
			[
				'name' => 'map',
				'width' => '800',
				'height' => '600',
				'label_type' => '0',
				'label_location' => '0',
				'highlight' => '0',
				'expandproblem' => '0',
				'markelements' => '0',
				'show_unack' => '0',
				'severity_min' => '0',
				'grid_size' => '50',
				'grid_show' => '1',
				'grid_align' => '1',
				'label_format' => '0',
				'label_type_host' => '2',
				'label_type_hostgroup' => '2',
				'label_type_trigger' => '2',
				'label_type_map' => '2',
				'label_type_image' => '2',
				'label_string_host' => '',
				'label_string_hostgroup' => '',
				'label_string_trigger' => '',
				'label_string_map' => '',
				'label_string_image' => '',
				'expand_macros' => '0',
				'background' => [],
				'iconmap' => [],
				'urls' => [
					[
						'name' => 'url',
						'url' => 'http://google.com',
						'elementtype' => '0'
					]
				],
				'selements' => [
					[
						'elementtype' => '2',
						'label' => 'New element',
						'label_location' => '-1',
						'x' => '239',
						'y' => '127',
						'elementsubtype' => '0',
						'areatype' => '0',
						'width' => '200',
						'height' => '200',
						'viewtype' => '0',
						'use_iconmap' => '0',
						'selementid' => '32',
						'element' => [
							'description' => 'trigger',
							'expression' => '{export-host:item.last(0)}<>0 or {export-host:item.last(0)}<>0 and {export-host:item.last(0)}<>0'
						],
						'icon_off' => [
							'name' => 'Server_(96)'
						],
						'icon_on' => [],
						'icon_disabled' => [],
						'icon_maintenance' => [],
						'application' => '',
						'urls' => [
							[
								'name' => 'url',
								'url' => 'http://google.com'
							]
						]
					],
					[
						'elementtype' => '0',
						'label' => 'New element',
						'label_location' => '-1',
						'x' => '489',
						'y' => '127',
						'elementsubtype' => '0',
						'areatype' => '0',
						'width' => '200',
						'height' => '200',
						'viewtype' => '0',
						'use_iconmap' => '0',
						'selementid' => '33',
						'element' => [
							'host' => 'export-host'
						],
						'icon_off' => [
							'name' => 'Server_(96)'
						],
						'icon_on' => [],
						'icon_disabled' => [],
						'icon_maintenance' => [],
						'application' => '',
						'urls' => []
					],
					[
						'elementtype' => '3',
						'label' => 'New element',
						'label_location' => '-1',
						'x' => '239',
						'y' => '377',
						'elementsubtype' => '0',
						'areatype' => '0',
						'width' => '200',
						'height' => '200',
						'viewtype' => '0',
						'use_iconmap' => '0',
						'selementid' => '34',
						'element' => [
							'name' => 'Zabbix servers'
						],
						'icon_off' => [
							'name' => 'Server_(96)'
						],
						'icon_on' => [],
						'icon_disabled' => [],
						'icon_maintenance' => [],
						'application' => '',
						'urls' => []
					]
				],
				'links' => [
					[
						'drawtype' => '0',
						'color' => '00CC00',
						'label' => '',
						'selementid1' => '32',
						'selementid2' => '33',
						'linktriggers' => [
							[
								'drawtype' => '0',
								'color' => 'DD0000',
								'trigger' => [
									'description' => 'trigger',
									'expression' => '{export-host:item.last(0)}<>0 or {export-host:item.last(0)}<>0 and {export-host:item.last(0)}<>0'
								]
							]
						]
					],
					[
						'drawtype' => '0',
						'color' => '00CC00',
						'label' => '',
						'selementid1' => '32',
						'selementid2' => '34',
						'linktriggers' => []
					]
				]
			]
		]);
	}

	public function testGetScreens() {
		$adapter = $this->getAdapter($this->getScreenXml());

		$this->assertEquals($adapter->getScreens(), [
			[
				'name' => 'empty-screen',
				'hsize' => '1',
				'vsize' => '1',
				'screenitems' => []
			],
			[
				'name' => 'screen',
				'hsize' => '1',
				'vsize' => '1',
				'screenitems' => [
					[
						'resourcetype' => '0',
						'width' => '500',
						'height' => '100',
						'x' => '0',
						'y' => '0',
						'colspan' => '1',
						'rowspan' => '1',
						'elements' => '0',
						'valign' => '0',
						'halign' => '0',
						'style' => '0',
						'url' => '',
						'dynamic' => '0',
						'sort_triggers' => '0',
						'resource' => [
							'name' => 'simple',
							'host' => 'export-host'
						],
						'max_columns' => '1',
						'application' => ''
					]
				]
			]
		]);
	}

	public function testConversion() {
		$adapter = $this->getAdapter($this->get18Xml());

		$this->assertEquals(
			[
				[
					'name' => 'Linux servers'
				],
				[
					'name' => 'Templates'
				]
			],
			$adapter->getGroups()
		);

		$this->assertEquals(
			[
				[
					'macros' => [],
					'interfaces' => [
						[
							'type' => 1,
							'useip' => '1',
							'ip' => '10.0.0.6',
							'dns' => 'localhost',
							'port' => '10050',
							'interface_ref' => 'if0',
							'main' => 1
						]
					],
					'host' => 'host',
					'status' => '0',
					'ipmi_authtype' => '0',
					'ipmi_privilege' => '2',
					'ipmi_username' => '',
					'ipmi_password' => '',
					'inventory_mode' => -1,
					'inventory' => [],
					'groups' =>	[
						[
							'name' => 'Linux servers'
						]
					],
					'templates' => []
				]
			],
			$adapter->getHosts()
		);

		$this->assertEquals(
			[
				[
					'macros' => [
						[
							'value' => '21',
							'macro' => '{$PORT.FTP}'
						],
						[
							'value' => '22',
							'macro' => '{$PORT.SSH}'
						]
					],
					'host' => 'Template_Linux',
					'groups' =>	[
						[
							'name' => 'Templates'
						]
					],
					'templates' => []
				],
				[
					'macros' => [],
					'host' => 'Template_Simple',
					'groups' =>	[
						[
							'name' => 'Templates'
						]
					],
					'templates' => []
				]
			],
			$adapter->getTemplates()
		);

		$this->assertEquals(
			[
				'Template_Linux' => [
					'vfs.fs.size[/,pfree]' => [
						'type' => '0',
						'value_type' => '0',
						'ipmi_sensor' => '',
						'delay' => '30',
						'history' => '7',
						'trends' => '365',
						'status' => '0',
						'data_type' => '0',
						'units' => '%',
						'multiplier' => '0',
						'delta' => '0',
						'formula' => '1',
						'logtimefmt' => '',
						'delay_flex' => '',
						'authtype' => '0',
						'username' => '',
						'password' => '',
						'publickey' => '',
						'privatekey' => '',
						'params' => '',
						'trapper_hosts' => '',
						'snmp_community' => '',
						'snmp_oid' => '',
						'snmp_port' => '161',
						'snmpv3_securityname' => '',
						'snmpv3_securitylevel' => '0',
						'snmpv3_authpassphrase' => '',
						'snmpv3_privpassphrase' => '',
						'valuemapid' => '0',
						'applications' => [
							[
								'name' => 'Filesystem'
							],
							[
								'name' => 'Availability'
							]
						],
						'name' => 'Free disk space on $1 in %',
						'key_' => 'vfs.fs.size[/,pfree]'
					]
				],
				'Template_Simple' => [
					'net.tcp.service[ftp,,21]' => [
						'type' => '3',
						'value_type' => '3',
						'ipmi_sensor' => '',
						'delay' => '30',
						'history' => '90',
						'trends' => '365',
						'status' => '0',
						'data_type' => '0',
						'units' => '',
						'multiplier' => '0',
						'delta' => '0',
						'formula' => '1',
						'logtimefmt' => '',
						'delay_flex' => '',
						'authtype' => '0',
						'username' => '',
						'password' => '',
						'publickey' => '',
						'privatekey' => '',
						'params' => '',
						'trapper_hosts' => '',
						'snmp_community' => '',
						'snmp_oid' => '',
						'snmp_port' => '161',
						'snmpv3_securityname' => '',
						'snmpv3_securitylevel' => '0',
						'snmpv3_authpassphrase' => '',
						'snmpv3_privpassphrase' => '',
						'valuemapid' => '0',
						'applications' => [
							[
								'name' => 'Simple checks'
							]
						],
						'name' => 'FTP check',
						'key_' => 'net.tcp.service[ftp,,21]'
					],
					'net.tcp.service[ftp,,{$PORT.FTP}]' => [
						'type' => '3',
						'value_type' => '3',
						'ipmi_sensor' => '',
						'delay' => '30',
						'history' => '90',
						'trends' => '365',
						'status' => '0',
						'data_type' => '0',
						'units' => '',
						'multiplier' => '0',
						'delta' => '0',
						'formula' => '1',
						'logtimefmt' => '',
						'delay_flex' => '',
						'authtype' => '0',
						'username' => '',
						'password' => '',
						'publickey' => '',
						'privatekey' => '',
						'params' => '',
						'trapper_hosts' => '',
						'snmp_community' => '',
						'snmp_oid' => '',
						'snmp_port' => '161',
						'snmpv3_securityname' => '',
						'snmpv3_securitylevel' => '0',
						'snmpv3_authpassphrase' => '',
						'snmpv3_privpassphrase' => '',
						'valuemapid' => '0',
						'applications' => [
							[
								'name' => 'Simple checks'
							]
						],
						'name' => 'FTP check with macro',
						'key_' => 'net.tcp.service[ftp,,{$PORT.FTP}]'
					]
				]
			],
			$adapter->getItems()
		);

		$this->assertEquals(
			[
				'Template_Linux' => [
					'Filesystem' => [
						'name' => 'Filesystem'
					],
					'Availability' => [
						'name' => 'Availability'
					]
				],
				'Template_Simple' => [
					'Simple checks' => [
						'name' => 'Simple checks'
					]
				]
			],
			$adapter->getApplications()
		);

		$this->assertEquals(
			[
				[
					'type' => '0',
					'expression' => '{Template_Linux:vfs.fs.size[/,pfree].last(0)}<10',
					'url' => 'http://www.zabbix.com/',
					'status' => '0',
					'priority' => '4',
					'comments' => 'test comments',
					'description' => 'Low free disk space on {HOSTNAME} volume /'
				],
				[
					'type' => '1',
					'expression' => '{Template_Simple:net.tcp.service[ftp,,21].last(0)}<>0 or {Template_Simple:net.tcp.service[ftp,,{$PORT.FTP}].last(0)}<>0',
					'url' => 'triggers.php',
					'status' => '1',
					'priority' => '3',
					'comments' => 'comments',
					'description' => 'simple triggert'
				]
			],
			$adapter->getTriggers()
		);

		$this->assertEquals(
			[
				[
					'name' => 'simple graph fixed',
					'width' => '755',
					'height' => '332',
					'ymin_type' => '1',
					'ymax_type' => '1',
					'show_work_period' => '1',
					'show_triggers' => '1',
					'yaxismin' => '5.5000',
					'yaxismax' => '95.6000',
					'show_3d' => '0',
					'percent_left' => '25.5000',
					'percent_right' => '27.6000',
					'ymin_item_1' => [],
					'ymax_item_1' => [],
					'graphtype' => '0',
					'gitems' => [
						[
							'item' => [
								'host' => 'Template_Simple',
								'key' => 'net.tcp.service[ftp,,21]'
							],
							'drawtype' => '0',
							'sortorder' => '0',
							'color' => '3333FF',
							'yaxisside' => '0',
							'calc_fnc' => '7',
							'type' => '0'
						],
						[
							'item' => [
								'host' => 'Template_Simple',
								'key' => 'net.tcp.service[ftp,,{$PORT.FTP}]'
							],
							'drawtype' => '1',
							'sortorder' => '1',
							'color' => '009999',
							'yaxisside' => '1',
							'calc_fnc' => '4',
							'type' => '0'
						]
					]
				],
				[
					'name' => 'simple graph',
					'width' => '900',
					'height' => '200',
					'ymin_type' => '0',
					'ymax_type' => '0',
					'show_work_period' => '0',
					'show_triggers' => '0',
					'yaxismin' => '0.0000',
					'yaxismax' => '100.0000',
					'show_3d' => '0',
					'percent_left' => '0.0000',
					'percent_right' => '0.0000',
					'ymin_item_1' => [],
					'ymax_item_1' => [],
					'graphtype' => '0',
					'gitems' => [
						[
							'item' => [
								'host' => 'Template_Simple',
								'key' => 'net.tcp.service[ftp,,21]'
							],
							'drawtype' => '0',
							'sortorder' => '0',
							'color' => '3333FF',
							'yaxisside' => '0',
							'calc_fnc' => '2',
							'type' => '0'
						],
						[
							'item' => [
								'host' => 'Template_Simple',
								'key' => 'net.tcp.service[ftp,,{$PORT.FTP}]'
							],
							'drawtype' => '0',
							'sortorder' => '1',
							'color' => '009999',
							'yaxisside' => '0',
							'calc_fnc' => '2',
							'type' => '0'
						]
					]
				],
				[
					'name' => 'simple graph min/max',
					'width' => '1024',
					'height' => '768',
					'ymin_type' => '2',
					'ymax_type' => '2',
					'show_work_period' => '1',
					'show_triggers' => '1',
					'yaxismin' => '0.0000',
					'yaxismax' => '0.0000',
					'show_3d' => '0',
					'percent_left' => '0.0000',
					'percent_right' => '0.0000',
					'ymin_item_1' => [
						'host' => 'Template_Simple',
						'key' => 'net.tcp.service[ftp,,21]'
					],
					'ymax_item_1' => [
						'host' => 'Template_Simple',
						'key' => 'net.tcp.service[ftp,,{$PORT.FTP}]'
					],
					'graphtype' => '1',
					'gitems' => [
						[
							'item' => [
								'host' => 'Template_Simple',
								'key' => 'net.tcp.service[ftp,,21]'
							],
							'drawtype' => '0',
							'sortorder' => '0',
							'color' => '3333FF',
							'yaxisside' => '0',
							'calc_fnc' => '2',
							'type' => '0'
						],
						[
							'item' => [
								'host' => 'Template_Simple',
								'key' => 'net.tcp.service[ftp,,{$PORT.FTP}]'
							],
							'drawtype' => '0',
							'sortorder' => '1',
							'color' => '009999',
							'yaxisside' => '0',
							'calc_fnc' => '2',
							'type' => '0'
						]
					]
				]
			],
			$adapter->getGraphs()
		);

//		echo print_r($adapter->getGraphs(), true);
	}

	public function testUnsupportedVersion() {
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
			<zabbix_export>
				<version>0.1</version>
				<date>2014-11-14T09:41:02Z</date>
			</zabbix_export>';

		$this->setExpectedException('Exception', 'Invalid tag "/zabbix_export/version": unsupported version number.');
		$this->getAdapter($xml);
	}

	protected function getAdapter($source) {
		$reader = CImportReaderFactory::getReader(CImportReaderFactory::XML);
		$source = $reader->read($source);
		$source = (new CXmlValidator())->validate($source, 'xml');

		$importConverterFactory = new CImportConverterFactory();

		$converterChain = new CConverterChain();
		$converterChain->addConverter('1.0', $importConverterFactory->getObject('1.0'));
		$converterChain->addConverter('2.0', $importConverterFactory->getObject('2.0'));

		$adapter = new CImportDataAdapter(ZABBIX_EXPORT_VERSION, $converterChain);
		$adapter->load($source);

		return $adapter;
	}

	protected function get18Xml() {
		return $this->getFile('host18.xml');
	}

	protected function getEmptyXml() {
		return '<?xml version="1.0" encoding="UTF-8"?>
			<zabbix_export>
				<version>2.0</version>
				<date>2014-11-14T09:41:02Z</date>
			</zabbix_export>';
	}

	protected function getHostAndTemplateXml() {
		return $this->getFile('hostAndTemplate.xml');
	}

	protected function getMapXml() {
		return $this->getFile('map.xml');
	}

	protected function getScreenXml() {
		return $this->getFile('screen.xml');
	}

	protected function getFile($name) {
		if (!isset($this->sources[$name])) {
			$this->sources[$name] = file_get_contents(__DIR__.'/xml/'.$name);
		}

		return $this->sources[$name];
	}

}
