package com.gentics.mesh.core.admin;

import static com.gentics.mesh.assertj.MeshAssertions.assertThat;
import static com.gentics.mesh.test.ClientHelper.call;
import static com.gentics.mesh.test.TestSize.FULL;
import static io.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE;

import java.io.IOException;

import org.junit.Test;

import com.gentics.mesh.core.rest.common.GenericMessageResponse;
import com.gentics.mesh.core.rest.project.ProjectCreateRequest;
import com.gentics.mesh.core.rest.project.ProjectResponse;
import com.gentics.mesh.test.context.AbstractMeshTest;
import com.gentics.mesh.test.context.MeshTestSetting;

@MeshTestSetting(useElasticsearch = false, testSize = FULL, startServer = true, inMemoryDB = false, clusterMode = true)
public class AdminEndpointBackupClusteredTest extends AbstractMeshTest {

	@Test
	public void testBackup() throws IOException {
		final String NEW_PROJECT_NAME = "enemenemuh";
		final String backupDir = testContext.getOptions().getStorageOptions().getBackupDirectory();

		assertFilesInDir(backupDir, 0);
		grantAdminRole();
		GenericMessageResponse message = call(() -> client().invokeBackup());
		assertThat(message).matches("backup_finished");
		assertFilesInDir(backupDir, 1);

		// Now create a project which is not in the backup. The routes and data must vanish when inserting the backup
		ProjectCreateRequest request = new ProjectCreateRequest();
		request.setName(NEW_PROJECT_NAME);
		request.setSchemaRef("folder");
		ProjectResponse projectResponse = call(() -> client().createProject(request));
		String baseNodeUuid = projectResponse.getRootNode().getUuid();
		call(() -> client().findNodeByUuid(NEW_PROJECT_NAME, baseNodeUuid));
	}

	@Test
	public void testRestore() {
		call(() -> client().invokeRestore(), SERVICE_UNAVAILABLE, "restore_error_in_cluster_mode");
	}

}

@@ -286,16 +286,37 @@
 LABLE;%0A%0A
+import java.io.File;%0A
 import j
@@ -353,20 +353,111 @@
 g.junit.
-Test
+Before;%0Aimport org.junit.Test;%0Aimport org.testcontainers.shaded.org.apache.commons.io.FileUtils
 ;%0A%0Aimpor
@@ -943,16 +943,238 @@
 Test %7B%0A%0A
+%09@Before%0A%09public void clearBackupDir() throws IOException %7B%0A%09%09File backupDir = new File(testContext.getOptions().getStorageOptions().getBackupDirectory());%0A%09%09FileUtils.deleteDirectory(backupDir);%0A%09%09backupDir.mkdirs();%0A%09%7D%0A%0A
 %09@Test%0A%09

Fix backup test