/*******************************************************************************
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2015-2019)
*
* contact.vitam@culture.gouv.fr
*
* This software is a computer program whose purpose is to implement a digital archiving back-office system managing
* high volumetry securely and efficiently.
*
* This software is governed by the CeCILL 2.1 license under French law and abiding by the rules of distribution of free
* software. You can use, modify and/ or redistribute the software under the terms of the CeCILL 2.1 license as
* circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
* users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the
* successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or
* developing or reproducing the software by the user in light of its specific status of free software, that may mean
* that it is complicated to manipulate, and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling the security of their systems and/or data
* to be ensured and, more generally, to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL 2.1 license and that you
* accept its terms.
*******************************************************************************/
package fr.gouv.vitam.processing.integration.test;
import static com.jayway.restassured.RestAssured.get;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.bson.Document;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import com.fasterxml.jackson.databind.JsonNode;
import com.jayway.restassured.RestAssured;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import fr.gouv.vitam.common.CommonMediaType;
import fr.gouv.vitam.common.GlobalDataRest;
import fr.gouv.vitam.common.PropertiesUtils;
import fr.gouv.vitam.common.SystemPropertyUtil;
import fr.gouv.vitam.common.VitamConfiguration;
import fr.gouv.vitam.common.client.configuration.ClientConfigurationImpl;
import fr.gouv.vitam.common.database.builder.query.QueryHelper;
import fr.gouv.vitam.common.database.builder.query.VitamFieldsHelper;
import fr.gouv.vitam.common.database.builder.request.configuration.BuilderToken.GLOBAL;
import fr.gouv.vitam.common.database.builder.request.configuration.BuilderToken.PROJECTION;
import fr.gouv.vitam.common.database.builder.request.configuration.BuilderToken.PROJECTIONARGS;
import fr.gouv.vitam.common.database.builder.request.multiple.Select;
import fr.gouv.vitam.common.format.identification.FormatIdentifierFactory;
import fr.gouv.vitam.common.guid.GUID;
import fr.gouv.vitam.common.guid.GUIDFactory;
import fr.gouv.vitam.common.json.JsonHandler;
import fr.gouv.vitam.common.junit.JunitHelper;
import fr.gouv.vitam.common.junit.JunitHelper.ElasticsearchTestConfiguration;
import fr.gouv.vitam.common.logging.SysErrLogger;
import fr.gouv.vitam.common.logging.VitamLogger;
import fr.gouv.vitam.common.logging.VitamLoggerFactory;
import fr.gouv.vitam.common.model.ProcessAction;
import fr.gouv.vitam.common.model.ProcessExecutionStatus;
import fr.gouv.vitam.common.model.StatusCode;
import fr.gouv.vitam.common.thread.RunWithCustomExecutor;
import fr.gouv.vitam.common.thread.RunWithCustomExecutorRule;
import fr.gouv.vitam.common.thread.VitamThreadPoolExecutor;
import fr.gouv.vitam.common.thread.VitamThreadUtils;
import fr.gouv.vitam.functional.administration.client.AdminManagementClient;
import fr.gouv.vitam.functional.administration.client.AdminManagementClientFactory;
import fr.gouv.vitam.functional.administration.rest.AdminManagementApplication;
import fr.gouv.vitam.logbook.common.exception.LogbookClientAlreadyExistsException;
import fr.gouv.vitam.logbook.common.exception.LogbookClientBadRequestException;
import fr.gouv.vitam.logbook.common.exception.LogbookClientNotFoundException;
import fr.gouv.vitam.logbook.common.exception.LogbookClientServerException;
import fr.gouv.vitam.logbook.common.parameters.LogbookOperationParameters;
import fr.gouv.vitam.logbook.common.parameters.LogbookParametersFactory;
import fr.gouv.vitam.logbook.common.parameters.LogbookTypeProcess;
import fr.gouv.vitam.logbook.lifecycles.client.LogbookLifeCyclesClientFactory;
import fr.gouv.vitam.logbook.operations.client.LogbookOperationsClient;
import fr.gouv.vitam.logbook.operations.client.LogbookOperationsClientFactory;
import fr.gouv.vitam.logbook.rest.LogbookApplication;
import fr.gouv.vitam.metadata.client.MetaDataClient;
import fr.gouv.vitam.metadata.client.MetaDataClientFactory;
import fr.gouv.vitam.metadata.core.UnitInheritedRule;
import fr.gouv.vitam.metadata.rest.MetaDataApplication;
import fr.gouv.vitam.processing.common.model.ProcessStep;
import fr.gouv.vitam.processing.engine.core.monitoring.ProcessMonitoringImpl;
import fr.gouv.vitam.processing.management.client.ProcessingManagementClient;
import fr.gouv.vitam.processing.management.client.ProcessingManagementClientFactory;
import fr.gouv.vitam.processing.management.rest.ProcessManagementApplication;
import fr.gouv.vitam.worker.server.rest.WorkerApplication;
import fr.gouv.vitam.workspace.client.WorkspaceClient;
import fr.gouv.vitam.workspace.client.WorkspaceClientFactory;
import fr.gouv.vitam.workspace.rest.WorkspaceApplication;
/**
* Processing integration test
*/
public class ProcessingWorkerIT {
private static final String JETTY_WORKER_PORT = "jetty.worker.port";
private static final VitamLogger LOGGER = VitamLoggerFactory.getInstance(ProcessingWorkerIT.class);
private static final int DATABASE_PORT = 12346;
private static MongodExecutable mongodExecutable;
static MongodProcess mongod;
private static final Integer tenantId = 0;
@Rule
public RunWithCustomExecutorRule runInThread =
new RunWithCustomExecutorRule(VitamThreadPoolExecutor.getDefaultExecutor());
@ClassRule
public static TemporaryFolder tempFolder = new TemporaryFolder();
private final static String CLUSTER_NAME = "vitam-cluster";
static JunitHelper junitHelper;
private static int TCP_PORT = 54321;
private static int HTTP_PORT = 54320;
private static final int PORT_SERVICE_WORKER = 8098;
private static final int PORT_SERVICE_WORKSPACE = 8094;
private static final int PORT_SERVICE_METADATA = 8096;
private static final int PORT_SERVICE_PROCESSING = 8097;
private static final int PORT_SERVICE_FUNCTIONAL_ADMIN = 8093;
private static final int PORT_SERVICE_LOGBOOK = 8099;
private static final String METADATA_PATH = "/metadata/v1";
private static final String PROCESSING_PATH = "/processing/v1";
private static final String WORKER_PATH = "/worker/v1";
private static final String WORKSPACE_PATH = "/workspace/v1";
private static final String LOGBOOK_PATH = "/logbook/v1";
private static String CONFIG_WORKER_PATH = "";
private static String CONFIG_BIG_WORKER_PATH = "";
private static String CONFIG_WORKSPACE_PATH = "";
private static String CONFIG_METADATA_PATH = "";
private static String CONFIG_PROCESSING_PATH = "";
private static String CONFIG_FUNCTIONAL_ADMIN_PATH = "";
private static String CONFIG_FUNCTIONAL_CLIENT_PATH = "";
private static String CONFIG_LOGBOOK_PATH = "";
private static String CONFIG_SIEGFRIED_PATH = "";
// private static VitamServer workerApplication;
private static MetaDataApplication metadataApplication;
private static WorkerApplication wkrapplication;
private static AdminManagementApplication adminApplication;
private static LogbookApplication lgbapplication;
private static WorkspaceApplication workspaceApplication;
private static ProcessManagementApplication processManagementApplication;
private WorkspaceClient workspaceClient;
private ProcessingManagementClient processingClient;
private static ProcessMonitoringImpl processMonitoring;
private static final String WORKSPACE_URL = "http://localhost:" + PORT_SERVICE_WORKSPACE;
private static final String PROCESSING_URL = "http://localhost:" + PORT_SERVICE_PROCESSING;
private static ElasticsearchTestConfiguration config = null;
private static String defautDataFolder = VitamConfiguration.getVitamDataFolder();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
VitamConfiguration.getConfiguration()
.setData(PropertiesUtils.getResourcePath("integration-processing/").toString());
CONFIG_METADATA_PATH = PropertiesUtils.getResourcePath("integration-processing/metadata.conf").toString();
CONFIG_WORKER_PATH = PropertiesUtils.getResourcePath("integration-processing/worker.conf").toString();
CONFIG_BIG_WORKER_PATH = PropertiesUtils.getResourcePath("integration-processing/bigworker.conf").toString();
CONFIG_WORKSPACE_PATH = PropertiesUtils.getResourcePath("integration-processing/workspace.conf").toString();
CONFIG_PROCESSING_PATH = PropertiesUtils.getResourcePath("integration-processing/processing.conf").toString();
CONFIG_SIEGFRIED_PATH =
PropertiesUtils.getResourcePath("integration-processing/format-identifiers.conf").toString();
CONFIG_FUNCTIONAL_ADMIN_PATH =
PropertiesUtils.getResourcePath("integration-processing/functional-administration.conf").toString();
CONFIG_FUNCTIONAL_CLIENT_PATH =
PropertiesUtils.getResourcePath("integration-processing/functional-administration-client-it.conf")
.toString();
CONFIG_LOGBOOK_PATH = PropertiesUtils.getResourcePath("integration-processing/logbook.conf").toString();
CONFIG_SIEGFRIED_PATH =
PropertiesUtils.getResourcePath("integration-processing/format-identifiers.conf").toString();
// ES
config = JunitHelper.startElasticsearchForTest(tempFolder, CLUSTER_NAME, TCP_PORT, HTTP_PORT);
final MongodStarter starter = MongodStarter.getDefaultInstance();
mongodExecutable = starter.prepare(new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(DATABASE_PORT, Network.localhostIsIPv6()))
.build());
mongod = mongodExecutable.start();
// launch metadata
SystemPropertyUtil.set(MetaDataApplication.PARAMETER_JETTY_SERVER_PORT,
Integer.toString(PORT_SERVICE_METADATA));
metadataApplication = new MetaDataApplication(CONFIG_METADATA_PATH);
metadataApplication.start();
SystemPropertyUtil.clear(MetaDataApplication.PARAMETER_JETTY_SERVER_PORT);
MetaDataClientFactory.changeMode(new ClientConfigurationImpl("localhost", PORT_SERVICE_METADATA));
// launch workspace
SystemPropertyUtil.set(WorkspaceApplication.PARAMETER_JETTY_SERVER_PORT,
Integer.toString(PORT_SERVICE_WORKSPACE));
workspaceApplication = new WorkspaceApplication(CONFIG_WORKSPACE_PATH);
workspaceApplication.start();
SystemPropertyUtil.clear(WorkspaceApplication.PARAMETER_JETTY_SERVER_PORT);
WorkspaceClientFactory.changeMode(WORKSPACE_URL);
// launch logbook
SystemPropertyUtil
.set(LogbookApplication.PARAMETER_JETTY_SERVER_PORT, Integer.toString(PORT_SERVICE_LOGBOOK));
lgbapplication = new LogbookApplication(CONFIG_LOGBOOK_PATH);
lgbapplication.start();
SystemPropertyUtil.clear(LogbookApplication.PARAMETER_JETTY_SERVER_PORT);
LogbookOperationsClientFactory.changeMode(new ClientConfigurationImpl("localhost", PORT_SERVICE_LOGBOOK));
LogbookLifeCyclesClientFactory.changeMode(new ClientConfigurationImpl("localhost", PORT_SERVICE_LOGBOOK));
// launch processing
SystemPropertyUtil.set(ProcessManagementApplication.PARAMETER_JETTY_SERVER_PORT,
Integer.toString(PORT_SERVICE_PROCESSING));
processManagementApplication = new ProcessManagementApplication(CONFIG_PROCESSING_PATH);
processManagementApplication.start();
SystemPropertyUtil.clear(ProcessManagementApplication.PARAMETER_JETTY_SERVER_PORT);
ProcessingManagementClientFactory.changeConfigurationUrl(PROCESSING_URL);
// launch worker
SystemPropertyUtil.set(JETTY_WORKER_PORT, Integer.toString(PORT_SERVICE_WORKER));
wkrapplication = new WorkerApplication(CONFIG_WORKER_PATH);
wkrapplication.start();
SystemPropertyUtil.clear(JETTY_WORKER_PORT);
FormatIdentifierFactory.getInstance().changeConfigurationFile(CONFIG_SIEGFRIED_PATH);
// launch functional Admin server
adminApplication = new AdminManagementApplication(CONFIG_FUNCTIONAL_ADMIN_PATH);
adminApplication.start();
AdminManagementClientFactory
.changeMode(new ClientConfigurationImpl("localhost", PORT_SERVICE_FUNCTIONAL_ADMIN));
processMonitoring = ProcessMonitoringImpl.getInstance();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
VitamConfiguration.getConfiguration().setData(defautDataFolder);
if (config != null) {
JunitHelper.stopElasticsearchForTest(config);
}
mongod.stop();
mongodExecutable.stop();
try {
workspaceApplication.stop();
adminApplication.stop();
wkrapplication.stop();
lgbapplication.stop();
processManagementApplication.stop();
metadataApplication.stop();
} catch (final Exception e) {
LOGGER.error(e);
}
}
@Test
public void testServersStatus() throws Exception {
try {
RestAssured.port = PORT_SERVICE_PROCESSING;
RestAssured.basePath = PROCESSING_PATH;
get("/status").then().statusCode(Status.NO_CONTENT.getStatusCode());
RestAssured.port = PORT_SERVICE_WORKSPACE;
RestAssured.basePath = WORKSPACE_PATH;
get("/status").then().statusCode(Status.NO_CONTENT.getStatusCode());
RestAssured.port = PORT_SERVICE_METADATA;
RestAssured.basePath = METADATA_PATH;
get("/status").then().statusCode(Status.NO_CONTENT.getStatusCode());
RestAssured.port = PORT_SERVICE_WORKER;
RestAssured.basePath = WORKER_PATH;
get("/status").then().statusCode(Status.NO_CONTENT.getStatusCode());
RestAssured.port = PORT_SERVICE_LOGBOOK;
RestAssured.basePath = LOGBOOK_PATH;
get("/status").then().statusCode(Status.NO_CONTENT.getStatusCode());
} catch (final Exception e) {
e.printStackTrace();
fail("should not raized an exception");
}
}
@RunWithCustomExecutor
@Test
public void testWorkerUnregister() throws Exception {
try {
VitamThreadUtils.getVitamSession().setTenantId(tenantId);
wkrapplication.stop();
SystemPropertyUtil.set(JETTY_WORKER_PORT, Integer.toString(PORT_SERVICE_WORKER));
wkrapplication = new WorkerApplication(CONFIG_WORKER_PATH);
wkrapplication.start();
processManagementApplication.stop();
SystemPropertyUtil.set(ProcessManagementApplication.PARAMETER_JETTY_SERVER_PORT,
Integer.toString(PORT_SERVICE_PROCESSING));
processManagementApplication = new ProcessManagementApplication(CONFIG_PROCESSING_PATH);
processManagementApplication.start();
SystemPropertyUtil.clear(ProcessManagementApplication.PARAMETER_JETTY_SERVER_PORT);
SystemPropertyUtil.clear(JETTY_WORKER_PORT);
} catch (final Exception e) {
e.printStackTrace();
fail("should not raized an exception");
}
}
}