/*
 * A CCNx library test.
 *
 * Copyright (C) 2010 Palo Alto Research Center, Inc.
 *
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation. 
 * This work 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.
 */
package org.ccnx.ccn.test.profiles.context;

import java.io.File;
import java.util.ArrayList;

import junit.framework.Assert;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.SystemConfiguration;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.profiles.context.ServiceDiscoveryProfile;
import org.ccnx.ccn.protocol.ContentObject;
import org.ccnx.ccn.utils.CreateUserData;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test localhost key discovery. Need to use a multi-user test to find multiple keys.
 *
 */
public class ServiceKeyDiscoveryTestRepo {
	
	public static final String PUBLISHED_SERVICE = "PuffyPoodles";
	public static final String OTHER_PUBLISHED_SERVICE = "FluffyKittens";
	public static final String NOT_A_SERVICE = "NotAService";
	
	static CreateUserData serviceProviders = null;
	public static final String USER_DIRECTORY = "./TestUsers"; 
	public static final String [] SERVICE_PROVIDERS = new String[]{
		"PoodleServer", "KittenServer", "Repository", "Server"};

	public static final int TEST_TIMEOUT = SystemConfiguration.MEDIUM_TIMEOUT;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		UserConfiguration.setPublishKeys(false);
		serviceProviders = new CreateUserData(new File(USER_DIRECTORY), SERVICE_PROVIDERS, SERVICE_PROVIDERS.length,
				UserConfiguration.keystorePassword().toCharArray(), true);
	}

	@Test
	public void testGetLocalServiceKeys() throws Exception {
		CCNHandle server1Handle = serviceProviders.getHandleForUser(SERVICE_PROVIDERS[0]);
		CCNHandle server2Handle = serviceProviders.getHandleForUser(SERVICE_PROVIDERS[1]);
		CCNHandle server3Handle = serviceProviders.getHandleForUser(SERVICE_PROVIDERS[2]);
		CCNHandle readerHandle = CCNHandle.getHandle(); // use default
		
		ServiceDiscoveryProfile.publishLocalServiceKey(PUBLISHED_SERVICE, null, server1Handle.keyManager());
		
		ArrayList<ContentObject> results = ServiceDiscoveryProfile.getLocalServiceKeys(PUBLISHED_SERVICE, 
				TEST_TIMEOUT, readerHandle);
		
		printResults(results);
		Assert.assertTrue(results.size() == 1);
		
		// Expect to get nothing back.
		results = ServiceDiscoveryProfile.getLocalServiceKeys(NOT_A_SERVICE, 
				TEST_TIMEOUT, readerHandle);
		
		Assert.assertTrue(results.size() == 0);
		System.out.println("Got no results for a nonexistent service.");
		
		ServiceDiscoveryProfile.publishLocalServiceKey(OTHER_PUBLISHED_SERVICE, null, server1Handle.keyManager());
		results = ServiceDiscoveryProfile.getLocalServiceKeys(OTHER_PUBLISHED_SERVICE, 
				TEST_TIMEOUT, readerHandle);
		
		printResults(results);
		Assert.assertTrue(results.size() == 1);		

		ServiceDiscoveryProfile.publishLocalServiceKey(PUBLISHED_SERVICE, null, server2Handle.keyManager());
		ServiceDiscoveryProfile.publishLocalServiceKey(PUBLISHED_SERVICE, null, server3Handle.keyManager());
		
		results = ServiceDiscoveryProfile.getLocalServiceKeys(PUBLISHED_SERVICE, 
				TEST_TIMEOUT, readerHandle);
		
		printResults(results);
		Assert.assertTrue(results.size() == 3);	
	}
	
	@Test
	public void testGetRepoKeys() throws Exception {
		CCNHandle readerHandle = CCNHandle.getHandle(); // use default
		
		System.out.println("Retrieving " + ServiceDiscoveryProfile.REPOSITORY_SERVICE_NAME + " keys.");
		// we required the repo to be running merely so we can get its keys
		ArrayList<ContentObject> results = 
			ServiceDiscoveryProfile.getLocalServiceKeys(ServiceDiscoveryProfile.REPOSITORY_SERVICE_NAME, 
					TEST_TIMEOUT, readerHandle);
		printResults(results);
		Assert.assertTrue(results.size() == 1);		
		
	}

	@Test
	public void testGetCcndKeys() throws Exception {
		CCNHandle readerHandle = CCNHandle.getHandle(); // use default
		
		System.out.println("Retrieving " + ServiceDiscoveryProfile.CCND_SERVICE_NAME + " keys.");
		ArrayList<ContentObject> results = 
			ServiceDiscoveryProfile.getLocalServiceKeys(ServiceDiscoveryProfile.CCND_SERVICE_NAME, 
					TEST_TIMEOUT, readerHandle);
		
		printResults(results);
		Assert.assertTrue(results.size() == 1);		
		
	}

	public void printResults(ArrayList<ContentObject> results) {
		if (null == results) 
			return;
		if (0 == results.size()) {
			System.out.println("No results found.");
			return;
		}
		System.out.println("Found " + results.size() + 
				((results.size() == 1) ? " key" : " keys") + " for a local service of name " + 
				ServiceDiscoveryProfile.getLocalServiceName(results.get(0).name()));
		for (ContentObject result : results) {
			System.out.println("Result: " + result.name());
		}
	}
}

@@ -1083,16 +1083,54 @@
 Object;%0A
+import org.ccnx.ccn.test.CCNTestBase;%0A
 import o
@@ -1653,10 +1653,8 @@
  = %22
-./
 Test
@@ -1660,17 +1660,16 @@
 tUsers%22;
- 
 %0A%09public
@@ -1864,16 +1864,57 @@
 EOUT;%0A%09%0A
+%09public static String _testDir = %22./%22;%0A%09%0A
 %09@Before
@@ -1977,16 +1977,128 @@
 ption %7B%0A
+%09%09if (null != System.getProperty(CCNTestBase.TEST_DIR))%0A%09%09%09_testDir = System.getProperty(CCNTestBase.TEST_DIR);%0A
 %09%09UserCo
@@ -2181,16 +2181,27 @@
 ew File(
+_testDir + 
 USER_DIR

Allow outside control over where data is written refs #100179