package edu.oregonstate.cope.eclipse.installer;

import static org.junit.Assert.*;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

import edu.oregonstate.cope.clientRecorder.Properties;
import edu.oregonstate.cope.eclipse.COPEPlugin;
import edu.oregonstate.cope.eclipse.PopulatedWorkspaceTest;
import edu.oregonstate.cope.eclipse.SnapshotManager;

public class PluginUpdateTest extends PopulatedWorkspaceTest {

	private static COPEPlugin plugin;
	private static SnapshotManager snapshotManager;
	private static List<String> allowedUnversionedFiles;

	@BeforeClass
	public static void beforeClass() throws Exception {
		PopulatedWorkspaceTest.beforeClass();
		plugin = COPEPlugin.getDefault();
		plugin.getSnapshotManager().knowProject(PopulatedWorkspaceTest.javaProject.getProject().getName());
		
		allowedUnversionedFiles = Arrays.asList(new String[]{COPEPlugin.getDefault()._getInstallationConfigFileName(), Installer.SURVEY_FILENAME, Installer.EMAIL_FILENAME});
	}

	@Test
	public void testVersionedLocalStorage() throws Exception {
		assertPathHasCurrentVersion(plugin.getVersionedLocalStorage().toPath());
		assertPathHasCurrentVersion(plugin.getVersionedBundleStorage().toPath());
	}

	private void assertPathHasCurrentVersion(Path versionedPath) {
		Path path = versionedPath;
		assertTrue(path.endsWith(plugin.getPluginVersion().toString()));
	}

	@Test
	public void testEverythingIsInVersionedFiles() throws Exception {
		for (File file : plugin.getLocalStorage().listFiles()) {
			if (file.isDirectory())
				checkDirectory(file);
			
			if (file.isFile())
				checkFile(file);
		}
	}

	private void checkFile(File file) {
		assertTrue(allowedUnversionedFiles.contains(file.getName()));
	}

	private void checkDirectory(File file) {
		assertTrue(file.getName().matches("\\d+\\.\\d+\\.\\d+\\.qualifier"));

		List<String> versionedFileChildren = Arrays.asList(file.list());

		assertTrue(versionedFileChildren.contains("eventFiles"));
		assertTrue(versionedFileChildren.contains("workspace_id"));
		assertTrue(versionedFileChildren.contains("known-projects"));
	}

	@SuppressWarnings("static-access")
	@Test
	public void testSnapshotAtUpdate() throws Exception {
		Properties properties = plugin.getWorkspaceProperties();

		new Installer().doUpdate("v1", "v2");

		boolean zipExists = false;

		for (File file : plugin.getVersionedLocalStorage().listFiles()) {
			if (file.toPath().toString().endsWith(".zip"))
				zipExists = true;
		}

		assertTrue(zipExists);
	}
}

@@ -125,16 +125,44 @@
 e.Path;%0A
+import java.util.ArrayList;%0A
 import j
@@ -178,16 +178,16 @@
 Arrays;%0A
-
 import j
@@ -955,121 +955,252 @@
 es =
+ new
  Array
-s.asList(new String%5B%5D%7BCOPEPlugin.getDefault()._getInstallationConfigFileName(), Installer.SURVEY_FILENAME, 
+List%3C%3E();%0A%09%09allowedUnversionedFiles.add(%22workspace_id%22);%0A%09%09allowedUnversionedFiles.add(COPEPlugin.getDefault()._getInstallationConfigFileName());%0A%09%09allowedUnversionedFiles.add(Installer.SURVEY_FILENAME);%0A%09%09allowedUnversionedFiles.add(
 Inst
@@ -1219,17 +1219,16 @@
 FILENAME
-%7D
 );%0A%09%7D%0A%0A%09
@@ -2141,16 +2141,16 @@
 st());%0A%0A
+
 %09%09assert
@@ -2205,70 +2205,8 @@
 ));%0A
-%09%09assertTrue(versionedFileChildren.contains(%22workspace_id%22));%0A
 %09%09as

Update test for unversioned workspace_id