package com.emc.ecs.cloudfoundry.broker.config;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.ConfigFileApplicationContextInitializer;
import org.springframework.cloud.servicebroker.model.Catalog;
import org.springframework.cloud.servicebroker.model.Plan;
import org.springframework.cloud.servicebroker.model.ServiceDefinition;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.emc.ecs.cloudfoundry.broker.config.Application;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class,
	initializers = ConfigFileApplicationContextInitializer.class)
@ActiveProfiles("development")
public class CatalogConfigTest {

	@Autowired
	private Catalog catalog;

	@Test
	public void testEcsBucket() {
		ServiceDefinition ecsBucketService = catalog.getServiceDefinitions()
				.get(1);
		testServiceDefinition(ecsBucketService,
				"f3cbab6a-5172-4ff1-a5c7-72990f0ce2aa", "ecs-bucket",
				"Elastic Cloud S3 Object Storage Bucket", true, true,
				null, null);
	}

	@Test
	public void testEcsBucketMetadata() {
		ServiceDefinition ecsBucketService = catalog.getServiceDefinitions()
				.get(1);
		Map<String, Object> metadata = ecsBucketService.getMetadata();
		testServiceDefinitionMetadata(metadata, "ecs-bucket",
				"http://www.emc.com/images/products/header-image-icon-ecs.png",
				"EMC Corporation", "https://community.emc.com/docs/DOC-45012",
				"http://www.emc.com/products-solutions/trial-software-download/ecs.htm");
	}

	@Test
	public void testEcsBucketPlans() {
		// Service Definition Plans
		ServiceDefinition service = catalog.getServiceDefinitions().get(1);
		List<Plan> ecsBucketPlans = service.getPlans();
		Plan plan0 = ecsBucketPlans.get(0);

		testPlan(plan0, "8e777d49-0a78-4cf4-810a-b5f5173b019d", "5gb",
				"Free Trial", new Double(0.0), "MONTHLY",
				Arrays.asList("Shared object storage", "5 GB Storage",
						"S3 protocol and HDFS access"));

		Plan plan1 = ecsBucketPlans.get(1);
		testPlan(plan1, "89d20694-9ab0-4a98-bc6a-868d6d4ecf31", "unlimited",
				"Pay per GB for Month", new Double(0.03), "PER GB PER MONTH",
				Arrays.asList("Shared object storage", "Unlimited Storage",
						"S3 protocol and HDFS access"));
	}

	private void testServiceDefinition(ServiceDefinition service, String id,
			String name, String description, boolean bindable,
			boolean updatable, List<String> requires, String dashboardUrl) {
		assertEquals(id, service.getId());
		assertEquals(name, service.getName());
		assertEquals(description, service.getDescription());
		assertEquals(bindable, service.isBindable());
		assertEquals(updatable, service.isPlanUpdateable());
		assertEquals(requires, service.getRequires());
		assertEquals(dashboardUrl, service.getDashboardClient());
	}

	private void testServiceDefinitionMetadata(Map<String, Object> metadata,
			String displayName, String imageUrl, String providerDisplayName,
			String documentationUrl, String supportUrl) {
		assertEquals("ecs-bucket", metadata.get("displayName"));
		assertEquals(
				"http://www.emc.com/images/products/header-image-icon-ecs.png",
				metadata.get("imageUrl"));
		assertEquals("EMC Corporation", metadata.get("providerDisplayName"));
		assertEquals("https://community.emc.com/docs/DOC-45012",
				metadata.get("documentationUrl"));
		assertEquals(
				"http://www.emc.com/products-solutions/trial-software-download/ecs.htm",
				metadata.get("supportUrl"));

	}

	private void testPlan(Plan plan, String id, String name, String description,
			Double usdCost, String unit, List<String> bullets) {
		assertEquals(id, plan.getId());
		assertEquals(name, plan.getName());
		assertEquals(description, plan.getDescription());

		Map<String, Object> metadata = plan.getMetadata();
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> costs = (List<Map<String, Object>>) metadata
				.get("costs");
		Map<String, Object> cost = costs.get(0);
		@SuppressWarnings("unchecked")
		Map<String, Object> amount = (Map<String, Object>) cost.get("amount");
		assertEquals(usdCost, amount.get("usd"));
		assertEquals(unit, cost.get("unit"));
		assertEquals(bullets, metadata.get("bullets"));
	}

}

@@ -3280,33 +3280,36 @@
  supportUrl) %7B%0A%09
-%09
+    
 assertEquals(%22ec
@@ -3305,28 +3305,27 @@
 tEquals(
-%22ecs-bucket%22
+displayName
 , metada
@@ -3341,33 +3341,36 @@
 isplayName%22));%0A%09
-%09
+    
 assertEquals(%0A%09%09
@@ -3370,81 +3370,18 @@
 als(
-%0A%09%09%09%09%22http://www.emc.com/images/products/header-image-icon-ecs.png%22,%0A%09%09%09%09
+imageUrl, 
 meta
@@ -3396,33 +3396,36 @@
 (%22imageUrl%22));%0A%09
-%09
+    
 assertEquals(%22EM
@@ -3421,33 +3421,35 @@
 tEquals(
-%22EMC Corporation%22
+providerDisplayName
 , metada
@@ -3477,25 +3477,28 @@
 ayName%22));%0A%09
-%09
+    
 assertEquals
@@ -3502,56 +3502,26 @@
 als(
-%22https://community.emc.com/docs/DOC-45012%22,%0A%09%09%09%09
+documentationUrl, 
 meta
@@ -3548,25 +3548,28 @@
 ionUrl%22));%0A%09
-%09
+    
 assertEquals
@@ -3573,90 +3573,20 @@
 als(
-%0A%09%09%09%09%22http://www.emc.com/products-solutions/trial-software-download/ecs.htm%22,%0A%09%09%09%09
+supportUrl, 
 meta
@@ -3610,17 +3610,16 @@
 Url%22));%0A
-%0A
 %09%7D%0A%0A%09pri

Fix parameters names on test