package eu.stratuslab.storage.gzip;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import eu.stratuslab.storage.disk.utils.DiskUtils;
import eu.stratuslab.storage.disk.utils.FileUtils;


public class GZipRequestTest {

	@Before
	public void setupConfigFileLocation() {
		// PDisk config have to be valid.
		System.setProperty("pdisk.config.filename", "/tmp/pdisk.cfg");
		System.setProperty("iscsi.config.filename", "/dev/null");
	}
	
	@Test
	public void testImageBuldingDetection() throws IOException {
		String uuid = "junitTest";
		
		createFakeImageFile(uuid);
		
		if (!DiskUtils.isCompressedDiskBuilding(uuid)) {
			fail("Image should be in build state");
		}
		
		createFakeCompressedImageFile(uuid);
		
		if (!DiskUtils.isCompressedDiskBuilding(uuid)) {
			fail("Image should be in build state");
		}
		
		removeFakeImageFile(uuid);
		
		if (DiskUtils.isCompressedDiskBuilding(uuid)) {
			fail("Image should not be in build state");
		}
		
		removeCompressedFakeImageFile(uuid);		
	}
	
	private void createFakeImageFile(String uuid) throws IOException {
		String fakeImagePath = FileUtils.getCachedDiskLocation(uuid);
		File fakeImage = new File(fakeImagePath);
		fakeImage.createNewFile();
	}
	
	private void createFakeCompressedImageFile(String uuid) throws IOException {
		String fakeCompressedImagePath = FileUtils.getCompressedDiskLocation(uuid);
		File fakeCompressedImage = new File(fakeCompressedImagePath);
		fakeCompressedImage.createNewFile();
	}
	
	private void removeFakeImageFile(String uuid) {
		String fakeImagePath = FileUtils.getCachedDiskLocation(uuid);
		File fakeImage = new File(fakeImagePath);
		fakeImage.delete();
	}
	
	private void removeCompressedFakeImageFile(String uuid) {
		String fakeCompressedImagePath = FileUtils.getCachedDiskLocation(uuid);
		File fakeCompressedImage = new File(fakeCompressedImagePath);
		fakeCompressedImage.delete();
	}
}

@@ -162,16 +162,78 @@
 it.Test;
+%0A%0Aimport eu.stratuslab.storage.disk.main.ServiceConfiguration;
 %0Aimport 
@@ -327,16 +327,67 @@
 eUtils;%0A
+import eu.stratuslab.storage.disk.utils.MiscUtils;%0A
 %0A%0Apublic
@@ -412,16 +412,45 @@
 Test %7B%0A%0A
+%09String uuid = %22junitTest%22;%0A%0A
 %09@Before
@@ -730,37 +730,8 @@
 n %7B%0A
-%09%09String uuid = %22junitTest%22;%0A
 %09%09%0A%09
@@ -1945,35 +1945,39 @@
 = FileUtils.getC
-ach
+ompress
 edDiskLocation(u
@@ -2038,32 +2038,32 @@
 ssedImagePath);%0A
-
 %09%09fakeCompressed
@@ -2081,10 +2081,488 @@
 e();%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void testCacheExpiration() throws IOException %7B%0A%09%09%0A%09%09createFakeCompressedImageFile(uuid);%0A%09%09%0A%09%09if (DiskUtils.hasCompressedDiskExpire(uuid)) %7B%0A%09%09%09fail(%22Image should not have expire yet%22);%0A%09%09%7D%0A%0A%09%09waitCacheToExpire();%0A%09%09%0A%09%09if (!DiskUtils.hasCompressedDiskExpire(uuid)) %7B%0A%09%09%09fail(%22Image should have expire%22);%0A%09%09%7D%0A%0A%09%09removeCompressedFakeImageFile(uuid);%0A%09%7D%0A%0A%09private void waitCacheToExpire() %7B%0A%09%09MiscUtils.sleep(2*ServiceConfiguration.CACHE_EXPIRATION_DURATION);%0A%09%7D%0A
 %7D%0A

Add cache expiration test