/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * 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 eu.dnetlib.data.index.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.apache.log4j.Logger;
import org.junit.Test;

import eu.dnetlib.common.utils.EprUtils;
import eu.dnetlib.resultset.impl.PullResultSetObject;
import eu.dnetlib.resultset.impl.ResultSetObject;



/**
 * EprUtils test case.
 * @author mhorat
 *
 */
public class ISUtilsTest {
	
	protected static final Logger log = Logger.getLogger(ISUtilsTest.class);

	Map <String,ResultSetObject> rsData = Collections.synchronizedMap(
			new HashMap<String, ResultSetObject>());
	
	@Test
	public void testDateFormat() {
//		required: 2007-10-13T15:32:39+02:00
		String statusDateFormatPattern = "yyyy-MM-dd'T'hh:mm:ssZ";
		String statusTimeZone = "GMT";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(statusDateFormatPattern);
		sdf.setTimeZone(TimeZone.getTimeZone(statusTimeZone));
		System.out.println(sdf.format(date));
	}
	
	@Test
	@Deprecated
	public void testParseResultSetEPR() {
		String serviceAddress = "someServiceAddress";
		String resultSetId = "someResultSetId";
		String wsdlLocation = "someWsdlLocation";
		String resultSetEPR = EprUtils.buildResultSetEPR(serviceAddress, resultSetId, 
				wsdlLocation);
		assertNotNull(resultSetEPR);
		
		String[] result = EprUtils.parseResultSetEPR(resultSetEPR);
		assertNotNull(result);
		assertEquals(2, result.length);
		assertEquals(serviceAddress, result[0]);
		assertEquals(resultSetId, result[1]);
	}
	
	@Test
	public void testParseCharTable() {
		int bufferSize = 5000;
		char[] buffer = new char[bufferSize];
		for (int i=0; i<bufferSize; i++ ) {
			buffer[i] = 'x';
		}
		String str = new String(buffer, 1, 4000);
		System.out.println(str);
		assertEquals(4000,str.length());
	}

	@Test
	public void ntestIteratingSynchronizedMap() throws InterruptedException {
		rsData.put("key1", new PullResultSetObject("someId",0 , 
				null, null, 1, 1, null));
		rsData.put("key2", new PullResultSetObject("someId",0 , 
				null, null, 1, 1, null));
		rsData.put("key3", new PullResultSetObject("someId",0 , 
				null, null, 1, 1, null));
		cleanup();
		assertEquals(0, rsData.size());
	}
	
	public void cleanup() throws InterruptedException {
		Thread.sleep(1);
		System.out.println("starting cleanup operations...");
		long currentTime = System.currentTimeMillis();
		Set<String> keySet = rsData.keySet();
		synchronized(rsData) {
			Iterator<String> keysIt = keySet.iterator();
			while (keysIt.hasNext()) {
				String currentKey = keysIt.next();
				ResultSetObject currentRsObj = rsData.get(currentKey);
				System.out.println("checking rs: "+currentKey);
				if (currentTime > currentRsObj.getExpirationTime()) {
					System.out.println("removing rs: "+currentKey);
					keysIt.remove();
				}
			}
		}
		System.out.println("cleanup operations finished");
	}
}
