/*
 * Copyright (c) 2013 mgm technology partners GmbH
 *
 * 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.mgmtp.perfload.perfalyzer.reporting.email;

import static com.google.common.io.Closeables.closeQuietly;
import static com.google.common.io.Files.newWriter;
import static com.mgmtp.perfload.perfalyzer.util.PropertiesUtils.loadProperties;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.testng.annotations.Test;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import com.mgmtp.perfload.perfalyzer.reporting.email.EmailSkeleton;
import com.mgmtp.perfload.perfalyzer.util.ResourceBundleProvider;
import com.mgmtp.perfload.perfalyzer.util.TestMetadata;
import com.mgmtp.perfload.perfalyzer.util.ResourceBundleProvider.Utf8Control;

/**
 * @author rnaegele
 */
public class EmailSkeletonTest {

	private final List<? extends List<String>> data = ImmutableList.of(
			ImmutableList.of("operation", "time", "numRequests", "numErrors", "minReqPerSec", "medianReqPerSec", "maxReqPerSec",
					"minReqPerMin",	"medianReqPerMin", "maxReqPerMin", "minExecutionTime", "medianExecutionTime", "maxExecutionTime"),
			ImmutableList.of("myoperation", "2012-10-26T17:03:58.077+02:00", "79,320", "0", "0", "76", "222", "0", "4,378",
					"8,585", "242", "3,174", "198,476"),
			ImmutableList.of("myoperation", "2012-10-26T17:03:58.077+02:00", "79,320", "0", "0", "76", "222", "0", "4,378",
					"8,585", "242", "3,174", "198,476"),
			ImmutableList.of("myoperation", "2012-10-26T17:03:58.077+02:00", "79,320", "0", "0", "76", "222", "0", "4,378",
					"8,585", "242", "3,174", "198,476"),
			ImmutableList.of("myoperation", "2012-10-26T17:03:58.077+02:00", "79,320", "0", "0", "76", "222", "0", "4,378",
					"8,585", "242", "3,174", "198,476")
			);

	private final Map<String, List<? extends List<String>>> comparisonData = ImmutableMap.of("myoperation1", data,
			"myoperation2", data);

	@Test
	public void testEmailSkeleton() throws IOException {
		Properties perfLoadMetaProps = loadProperties(new File("src/test/resources/email/perfload.meta.utf8.props"));
		TestMetadata testMetadata = TestMetadata.create("rawResultsDir", perfLoadMetaProps);
		Locale locale = Locale.US;
		ResourceBundle bundle = new ResourceBundleProvider(locale, new Utf8Control(new File("distribution/strings"))).get();

		EmailSkeleton email = new EmailSkeleton(testMetadata, bundle, locale, data, comparisonData, "http://link.to/report.html");

		Writer wr = null;
		try {
			File file = new File("tmp", "email.html");
			Files.createParentDirs(file);
			wr = newWriter(file, Charsets.UTF_8);
			email.write(wr);
		} finally {
			closeQuietly(wr);
		}
	}
}

@@ -1312,37 +1312,35 @@
 zer.
-reporting.email.EmailSkeleton
+util.ResourceBundleProvider
 ;%0Aim
@@ -1393,32 +1393,44 @@
 ceBundleProvider
+.Utf8Control
 ;%0Aimport com.mgm
@@ -1474,86 +1474,8 @@
 ata;
-%0Aimport com.mgmtp.perfload.perfalyzer.util.ResourceBundleProvider.Utf8Control;
 %0A%0A/*
@@ -1743,17 +1743,17 @@
 PerMin%22,
-%09
+ 
 %22medianR
@@ -1821,17 +1821,22 @@
 onTime%22,
- 
+%0A%09%09%09%09%09
 %22maxExec
@@ -3209,16 +3209,23 @@
 w File(%22
+target/
 tmp%22, %22e

Write test mail underneath target folder.