/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * 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.utility.download.dataprov;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

import eu.dnetlib.common.ws.dataprov.ResultsResponse;
import eu.dnetlib.data.utility.download.utils.SpringUtils;

/**
 * Test for DownloadManager Class.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 */
public class DownloadDataProviderTest {

	/** The ctx. */
	private ApplicationContext ctx;
	
	private SimpleStoreDataProvider dataProvider;
	
	@Before
	public void  setUp() throws Exception {
		ctx = SpringUtils
			.getSpringContext(SpringUtils.DEFAULT_RESOURCE);
		dataProvider = (SimpleStoreDataProvider) ctx.getBean("SimpleDataProvider");
	}
	
	@Test
	public void testDataProvider() throws Exception{
		String dpd = "/tmp/testDataProvider";
		dataProvider.setDownloadDirectory(dpd);
		
		CreateBasicBulkDataDTO bulkDataDTO = new CreateBasicBulkDataDTO();
		dataProvider.createBulkData(bulkDataDTO);
		
		File dpdf = new File(dpd+"/"+bulkDataDTO.getId());
		dpdf.mkdirs();
		assertEquals("open",dataProvider.getBulkDataStatus(bulkDataDTO.getId()));
		
		ResultsResponse res = dataProvider.getNumberOfResults(bulkDataDTO.getId());
		assertEquals(0,res.getTotal());
		
		File host = new File(dpd+"/"+bulkDataDTO.getId()+"/localhost:8080--index.html");
		host.createNewFile();
		
		dataProvider.updateBulkData(bulkDataDTO.getId(), "open");
		
		res = dataProvider.getNumberOfResults(bulkDataDTO.getId());
		assertEquals(1,res.getTotal());
		
		List<String> dres = dataProvider.getBulkData(bulkDataDTO.getId(), 1, 2);
		assertEquals(1,dres.size());
		assertEquals(dpd+"/"+bulkDataDTO.getId()+"/localhost:8080--index.html",dres.get(0));
		
		dataProvider.updateBulkData(bulkDataDTO.getId(), "closed");
		assertEquals("closed",dataProvider.getBulkDataStatus(bulkDataDTO.getId()));
		
		host.delete();
		File file1 = new File (dpd+"/"+bulkDataDTO.getId());
		file1.delete();
		File file2 = new File (dpd);
		file2.delete();
	}
	
	
}
