// Copyright 2014 Boundary, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.boundary.metrics.vmware.poller;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.boundary.metrics.vmware.client.VMWareClientFactory;
import com.boundary.metrics.vmware.client.metrics.Metric;
import com.google.common.io.Resources;
import com.vmware.connection.Connection;
import com.vmware.vim25.InvalidPropertyFaultMsg;
import com.vmware.vim25.PerfCounterInfo;
import com.vmware.vim25.RuntimeFaultFaultMsg;

public class PerformanceCounterCollectorTest {
	
	private final static String PERFORMANCE_COUNTER_PROPERTIES="vmware-performance-counters.properties";
	private static Properties performanceCounters = null;

	@BeforeClass
	public static void setUpBeforeClass() {
		try {
			File propertiesFile = new File(Resources.getResource(PERFORMANCE_COUNTER_PROPERTIES).toURI());
			Reader reader = new FileReader(propertiesFile);
			performanceCounters = new Properties();
			performanceCounters.load(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		performanceCounters = null;
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Tests collecting of Performance Counters against the vSphere end point. Compare the returned values against our
	 * known list. Test will fail if we can not find the performance counter in our property list.
	 * 
	 * @throws URISyntaxException {@link URISyntaxException}
	 * @throws IOException {@link IOException}
	 * @throws InvalidPropertyFaultMsg {@link InvalidPropertyFaultMsg}
	 * @throws RuntimeFaultFaultMsg {@link RuntimeFaultFaultMsg}
	 */
	@Test
	public void testPerformanceCounterCollector() throws URISyntaxException, IOException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
		Connection client = VMWareClientFactory.createClient();
		client.connect();

		
		Map<String,Metric> metrics = new HashMap<String,Metric>();
		Metric one = new Metric("VMWARE_DISK_READ_AVERAGE","Disk Read Average");
		Metric two = new Metric("VMWARE_DISK_WRITE_AVERAGE","Disk Write Average");
		Metric three = new Metric("VMWARE_CPU_USAGE_MINIMUM","CPU Usage Minimum");
		metrics.put("disk.read.AVERAGE",one);
		metrics.put("disk.write.AVERAGE",two);
		metrics.put("cpu.usage.MINIMUM",three);
		PerformanceCounterCollector collector = new PerformanceCounterCollector(client);
		PerformanceCounterMetadata metadata = collector.fetchPerformanceCounters();
		
		Map<String,Integer> nameMap = metadata.getNameMap();
		Map<Integer,PerfCounterInfo> infoMap = metadata.getInfoMap();
		
		for (PerfCounterInfo info : infoMap.values()) {
			String key = String.format("%s.%s.%s",info.getGroupInfo().getKey(),info.getNameInfo().getKey(),info.getRollupType().toString());
			boolean counter = Boolean.valueOf(performanceCounters.getProperty(key)).booleanValue();
			assertEquals("check for performance counter info: " + key,true,counter);
		}
		
		for (String name : nameMap.keySet()) {
			boolean counter = Boolean.valueOf(performanceCounters.getProperty(name)).booleanValue();
			assertEquals("check for performance counter name: " + name,true,counter);
		}
	}
}

@@ -864,24 +864,47 @@
 il.HashMap;%0A
+import java.util.List;%0A
 import java.
@@ -1366,16 +1366,54 @@
 erInfo;%0A
+import com.vmware.vim25.PerfMetricId;%0A
 import c
@@ -4175,13 +4175,412 @@
 r);%0A%09%09%7D%0A
+%09%09%0A%09%09List%3CPerfMetricId%3E perfMetricIds = metadata.getPerformanceMetricIds(metrics);%0A%09%09System.out.printf(%22%25s: %25d%5Cn%22,%22perfMetricIds size: %22,perfMetricIds.size());%0A%09%09for(PerfMetricId id: perfMetricIds) %7B%0A%09%09%09assertEquals(%22Check class%22,com.vmware.vim25.PerfMetricId.class,id.getClass());%0A%09%09%09assertNull(%22Check dynamic type%22,id.getDynamicType());%0A%09%09%09assertEquals(%22Check instance%22,%22*%22,id.getInstance());%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Add additional assertions on returned PerfMetricId