/**
 * 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.resultset.impl;


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

import org.junit.Test;

/**
 * PullResultSetObject Test class.
 * @author mhorst
 *
 */
public class PullResultSetObjectTest {

	String resultSetId = "dummyResultSetId";
	int expiryTime = 30000;
	String dataProviderServiceAddress = "DummydataProviderServiceAddress";
	String bdId = "dummyBdId";
	
	
	@Test
	public void testCalculatePagesCount() {
		int initialPageSize;
		int pullRsSize;
		
		initialPageSize = 50;
		pullRsSize = 100;
		PullResultSetObject pullRsObject = new PullResultSetObject(resultSetId,
				expiryTime, dataProviderServiceAddress, bdId, 
				initialPageSize, pullRsSize, null);
		
		assertEquals(2,pullRsObject.calculatePagesCount(initialPageSize, pullRsSize));
		
		initialPageSize = 1;
		pullRsSize = 100;
		assertEquals(100,pullRsObject.calculatePagesCount(initialPageSize, pullRsSize));
		
		initialPageSize = 100;
		pullRsSize = 100;
		assertEquals(1,pullRsObject.calculatePagesCount(initialPageSize, pullRsSize));
	}
	
	@Test
	public void testGetData() {
		String[] pageData;
		int initialPageSize = 10;
		int pullRsSize = 50;
		PullResultSetObject pullRsObject = new PullResultSetObject(resultSetId,
				expiryTime, dataProviderServiceAddress, bdId, 
				initialPageSize, pullRsSize, null);
		int pagesCount = pullRsObject.calculatePagesCount(initialPageSize, pullRsSize);
		for (int i=0; i<pagesCount; i++) {
			pageData = new String[initialPageSize];
			int currentPage = i+1;
			for (int k=0; k<initialPageSize; k++)
				pageData[k] = "p"+currentPage+"d"+(k+1);
			pullRsObject.setPageData(i+1, pageData);
		}
		
		for(int i=0; i<pagesCount; i++) {
			String[] currentData = pullRsObject.getPageData(i+1, 
					1, initialPageSize);
			assertNotNull(currentData);
			assertEquals(initialPageSize, currentData.length);
		}
		
		int fromPosition;
		int toPosition;
		String[] allData;

		fromPosition = 1;
		toPosition = pullRsSize;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(pullRsSize, allData.length);
		
		fromPosition = 1;
		toPosition = 1;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(1, allData.length);
		assertEquals("p1d1",allData[0]);
		
		fromPosition = 9;
		toPosition = 11;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(3, allData.length);
		assertEquals("p1d9",allData[0]);
		assertEquals("p1d10",allData[1]);
		assertEquals("p2d1",allData[2]);
		
		fromPosition = 50;
		toPosition = pullRsSize;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(1, allData.length);
		assertEquals("p5d10",allData[0]);
//		
		initialPageSize = 5;
		pullRsObject = new PullResultSetObject(resultSetId,
				expiryTime, dataProviderServiceAddress, bdId, 
				initialPageSize, pullRsSize, null);
		pagesCount = pullRsObject.calculatePagesCount(initialPageSize, pullRsSize);
		assertEquals(10, pagesCount);
		for (int i=0; i<pagesCount; i++) {
			pageData = new String[initialPageSize];
			int currentPage = i+1;
			for (int k=0; k<initialPageSize; k++)
				pageData[k] = "p"+currentPage+"d"+(k+1);
			pullRsObject.setPageData(i+1, pageData);
		}
		
		fromPosition = 50;
		toPosition = pullRsSize;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(1, allData.length);
		assertEquals("p10d5",allData[0]);
		
		fromPosition = 1;
		toPosition = 1;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(1, allData.length);
		assertEquals("p1d1",allData[0]);
		
		fromPosition = 2;
		toPosition = 49;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(48, allData.length);
		
//		
		initialPageSize = 1;
		pullRsObject = new PullResultSetObject(resultSetId,
				expiryTime, dataProviderServiceAddress, bdId, 
				initialPageSize, pullRsSize, null);
		pagesCount = pullRsObject.calculatePagesCount(initialPageSize, pullRsSize);
		assertEquals(50, pagesCount);
		for (int i=0; i<pagesCount; i++) {
			pageData = new String[initialPageSize];
			int currentPage = i+1;
			for (int k=0; k<initialPageSize; k++)
				pageData[k] = "p"+currentPage+"d"+(k+1);
			pullRsObject.setPageData(i+1, pageData);
		}
		
		fromPosition = 50;
		toPosition = pullRsSize;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(1, allData.length);
		assertEquals("p50d1",allData[0]);
		
		fromPosition = 1;
		toPosition = 1;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(1, allData.length);
		assertEquals("p1d1",allData[0]);
		
		fromPosition = 2;
		toPosition = 49;
		allData = pullRsObject.getData(fromPosition, toPosition);
		assertNotNull(allData);
		assertEquals(48, allData.length);
	}

	@Test
	public void testCleanStoredData() throws CloneNotSupportedException {
		String[] pageData;
		int initialPageSize = 10;
		int pullRsSize = 50;
		CreatePullRSType createPullRSType = new CreatePullRSType();
		createPullRSType.setKeepAliveTime(10);
		createPullRSType.setStyleSheet("someStyleSheet");
		createPullRSType.setTotal(10);
		
		PullResultSetObject pullRsObject = new PullResultSetObject(resultSetId,
				expiryTime, dataProviderServiceAddress, bdId, 
				initialPageSize, pullRsSize, createPullRSType);
		int pagesCount = pullRsObject.calculatePagesCount(initialPageSize, pullRsSize);
		for (int i=0; i<pagesCount; i++) {
			pageData = new String[initialPageSize];
			int currentPage = i+1;
			for (int k=0; k<initialPageSize; k++)
				pageData[k] = "p"+currentPage+"d"+(k+1);
			pullRsObject.setPageData(i+1, pageData);
			pullRsObject.setPageRetrieved(i+1);
		}
		
		for(int i=0; i<pagesCount; i++) {
			assertTrue(pullRsObject.isPageRetreived(i+1));
			String[] currentData = pullRsObject.getPageData(i+1, 
					1, initialPageSize);
			assertNotNull(currentData);
			assertEquals(initialPageSize, currentData.length);
		}

		pullRsObject.cleanAllStoredData();
		for(int i=0; i<pagesCount; i++) {
			assertFalse(pullRsObject.isPageRetreived(i+1));
			String[] currentData = pullRsObject.getPageData(i+1, 
					1, initialPageSize);
			assertNotNull(currentData);
			assertEquals(0, currentData.length);
		}
	}
	
}
