@Test @RunWithCustomExecutor public void shouldImportScenario() throws Exception { GUID guid = GUIDFactory.newGUID(); VitamThreadUtils.getVitamSession().setTenantId(1); VitamThreadUtils.getVitamSession().setRequestId(guid); List<PreservationScenarioModel> allPreservationScenarioInDatabase = getPreservationScenarioModels("scenarii.json"); List<PreservationScenarioModel> listToImport = getPreservationScenarioModels("scenarii_all.json"); String requestId = getVitamSession().getRequestId(); File preservationScenarioFile = getResourceFile( "preservation_scenario_logbook_operation.json"); JsonNode preservationScenarioOperation = JsonHandler.getFromFile(preservationScenarioFile); List<FileFormatModel> listFormat = getFileFormatModels("fileformatModel.json"); List<GriffinModel> listGriffons = singletonList(new GriffinModel().setIdentifier("GRI-000001")); when(logbookOperationsClient.selectOperationById(requestId)).thenReturn(preservationScenarioOperation); when(dbRequestResult.getDocuments(Griffin.class, GriffinModel.class)) .thenReturn(listGriffons); when(mongoDbAccess.findDocuments(any(JsonNode.class), eq(GRIFFIN))).thenReturn(dbRequestResult); when(dbRequestResult.getDocuments(PreservationScenario.class, PreservationScenarioModel.class)) .thenReturn(allPreservationScenarioInDatabase); when(mongoDbAccess.findDocuments(any(JsonNode.class), eq(PRESERVATION_SCENARIO))).thenReturn(dbRequestResult); when(dbRequestResult.getDocuments(FileFormat.class, FileFormatModel.class)).thenReturn(listFormat); when(mongoDbAccess.findDocuments(any(JsonNode.class), eq(FORMATS))).thenReturn(dbRequestResult); RequestResponse<PreservationScenarioModel> requestResponse = preservationScenarioService.importScenarios(listToImport); ArgumentCaptor<LogbookOperationParameters> event1Captor = forClass(LogbookOperationParameters.class); ArgumentCaptor<LogbookOperationParameters> event2Captor = forClass(LogbookOperationParameters.class); JsonNode result = JsonHandler.toJsonNode(requestResponse); int total = result.get("$hits").get("total").asInt(); assertThat(total).isEqualTo(3); verify(logbookOperationsClient, times(1)).create(event1Captor.capture()); verify(logbookOperationsClient, times(1)).update(event2Captor.capture()); verify(mongoDbAccess, times(1)).insertDocuments(any(), eq(PRESERVATION_SCENARIO)); verify(mongoDbAccess, times(1)) .replaceDocument(any(), eq("IDENTIFIER2"), eq("Identifier"), eq(PRESERVATION_SCENARIO)); assertThat(event1Captor.getValue().getParameterValue(LogbookParameterName.outcomeDetail)) .isEqualTo("STP_IMPORT_PRESERVATION_SCENARIO.STARTED"); assertThat(event2Captor.getValue().getParameterValue(LogbookParameterName.outcomeDetail)) .isEqualTo("STP_IMPORT_PRESERVATION_SCENARIO.OK"); verify(functionalBackupService) .saveCollectionAndSequence(guid, "STP_BACKUP_SCENARIO", PRESERVATION_SCENARIO, guid.getId()); List<PreservationScenarioModel> scenarios = getPreservationScenarioModels("preservationScenarios/KO_scenario_false_type_creationDate.json"); assertThatThrownBy( () -> preservationScenarioService.importScenarios(scenarios)) .isInstanceOf(ReferentialException.class).hasMessageContaining("field 'CreationDate' format is invalid"); }
public RequestResponse<PreservationScenarioModel> importScenarios( @NotNull List<PreservationScenarioModel> listToImport) throws VitamException { String operationId = getVitamSession().getRequestId(); GUID guid = getGUID(operationId); createLogbook(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); try { validate(listToImport); final ObjectNode finalSelect = new Select().getFinalSelect(); DbRequestResult result = mongoDbAccess.findDocuments(finalSelect, PRESERVATION_SCENARIO); final List<PreservationScenarioModel> allScenariosInDatabase = result.getDocuments(PreservationScenario.class, PreservationScenarioModel.class); Set<String> newIdentifiers = listToImport.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> oldIdentifiers = allScenariosInDatabase.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> updatedIdentifiers = SetUtils.intersection(newIdentifiers, oldIdentifiers); if (!updatedIdentifiers.isEmpty()) { updateScenarios(listToImport, updatedIdentifiers); } Set<String> identifierToDelete = SetUtils.difference(oldIdentifiers, newIdentifiers); if (!identifierToDelete.isEmpty()) { deleteScenarios(identifierToDelete); } Set<String> identifierToAdd = SetUtils.difference(newIdentifiers, oldIdentifiers); if (!identifierToAdd.isEmpty()) { insertScenarios(listToImport, identifierToAdd); } functionalBackupService .saveCollectionAndSequence(guid, SCENARIO_BACKUP_EVENT, PRESERVATION_SCENARIO, operationId); PreservationScenarioReport preservationScenarioReport = generateReport(allScenariosInDatabase, listToImport, updatedIdentifiers, identifierToDelete, identifierToAdd); saveReport(guid, preservationScenarioReport); } catch (VitamException e) { createLogbookEventKo(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT, e.getMessage()); throw e; } createLogbookEventSuccess(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); return new RequestResponseOK<PreservationScenarioModel>().addAllResults(listToImport) .setHttpCode(Response.Status.CREATED.getStatusCode()); }
PreservationScenarioService { public RequestResponse<PreservationScenarioModel> importScenarios( @NotNull List<PreservationScenarioModel> listToImport) throws VitamException { String operationId = getVitamSession().getRequestId(); GUID guid = getGUID(operationId); createLogbook(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); try { validate(listToImport); final ObjectNode finalSelect = new Select().getFinalSelect(); DbRequestResult result = mongoDbAccess.findDocuments(finalSelect, PRESERVATION_SCENARIO); final List<PreservationScenarioModel> allScenariosInDatabase = result.getDocuments(PreservationScenario.class, PreservationScenarioModel.class); Set<String> newIdentifiers = listToImport.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> oldIdentifiers = allScenariosInDatabase.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> updatedIdentifiers = SetUtils.intersection(newIdentifiers, oldIdentifiers); if (!updatedIdentifiers.isEmpty()) { updateScenarios(listToImport, updatedIdentifiers); } Set<String> identifierToDelete = SetUtils.difference(oldIdentifiers, newIdentifiers); if (!identifierToDelete.isEmpty()) { deleteScenarios(identifierToDelete); } Set<String> identifierToAdd = SetUtils.difference(newIdentifiers, oldIdentifiers); if (!identifierToAdd.isEmpty()) { insertScenarios(listToImport, identifierToAdd); } functionalBackupService .saveCollectionAndSequence(guid, SCENARIO_BACKUP_EVENT, PRESERVATION_SCENARIO, operationId); PreservationScenarioReport preservationScenarioReport = generateReport(allScenariosInDatabase, listToImport, updatedIdentifiers, identifierToDelete, identifierToAdd); saveReport(guid, preservationScenarioReport); } catch (VitamException e) { createLogbookEventKo(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT, e.getMessage()); throw e; } createLogbookEventSuccess(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); return new RequestResponseOK<PreservationScenarioModel>().addAllResults(listToImport) .setHttpCode(Response.Status.CREATED.getStatusCode()); } }
PreservationScenarioService { public RequestResponse<PreservationScenarioModel> importScenarios( @NotNull List<PreservationScenarioModel> listToImport) throws VitamException { String operationId = getVitamSession().getRequestId(); GUID guid = getGUID(operationId); createLogbook(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); try { validate(listToImport); final ObjectNode finalSelect = new Select().getFinalSelect(); DbRequestResult result = mongoDbAccess.findDocuments(finalSelect, PRESERVATION_SCENARIO); final List<PreservationScenarioModel> allScenariosInDatabase = result.getDocuments(PreservationScenario.class, PreservationScenarioModel.class); Set<String> newIdentifiers = listToImport.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> oldIdentifiers = allScenariosInDatabase.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> updatedIdentifiers = SetUtils.intersection(newIdentifiers, oldIdentifiers); if (!updatedIdentifiers.isEmpty()) { updateScenarios(listToImport, updatedIdentifiers); } Set<String> identifierToDelete = SetUtils.difference(oldIdentifiers, newIdentifiers); if (!identifierToDelete.isEmpty()) { deleteScenarios(identifierToDelete); } Set<String> identifierToAdd = SetUtils.difference(newIdentifiers, oldIdentifiers); if (!identifierToAdd.isEmpty()) { insertScenarios(listToImport, identifierToAdd); } functionalBackupService .saveCollectionAndSequence(guid, SCENARIO_BACKUP_EVENT, PRESERVATION_SCENARIO, operationId); PreservationScenarioReport preservationScenarioReport = generateReport(allScenariosInDatabase, listToImport, updatedIdentifiers, identifierToDelete, identifierToAdd); saveReport(guid, preservationScenarioReport); } catch (VitamException e) { createLogbookEventKo(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT, e.getMessage()); throw e; } createLogbookEventSuccess(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); return new RequestResponseOK<PreservationScenarioModel>().addAllResults(listToImport) .setHttpCode(Response.Status.CREATED.getStatusCode()); } PreservationScenarioService(MongoDbAccessReferential mongoDbAccess,
        FunctionalBackupService functionalBackupService,
        LogbookOperationsClientFactory logbookOperationsClientFactory);  PreservationScenarioService(MongoDbAccessAdminImpl mongoAccess,
        FunctionalBackupService functionalBackupService); }
PreservationScenarioService { public RequestResponse<PreservationScenarioModel> importScenarios( @NotNull List<PreservationScenarioModel> listToImport) throws VitamException { String operationId = getVitamSession().getRequestId(); GUID guid = getGUID(operationId); createLogbook(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); try { validate(listToImport); final ObjectNode finalSelect = new Select().getFinalSelect(); DbRequestResult result = mongoDbAccess.findDocuments(finalSelect, PRESERVATION_SCENARIO); final List<PreservationScenarioModel> allScenariosInDatabase = result.getDocuments(PreservationScenario.class, PreservationScenarioModel.class); Set<String> newIdentifiers = listToImport.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> oldIdentifiers = allScenariosInDatabase.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> updatedIdentifiers = SetUtils.intersection(newIdentifiers, oldIdentifiers); if (!updatedIdentifiers.isEmpty()) { updateScenarios(listToImport, updatedIdentifiers); } Set<String> identifierToDelete = SetUtils.difference(oldIdentifiers, newIdentifiers); if (!identifierToDelete.isEmpty()) { deleteScenarios(identifierToDelete); } Set<String> identifierToAdd = SetUtils.difference(newIdentifiers, oldIdentifiers); if (!identifierToAdd.isEmpty()) { insertScenarios(listToImport, identifierToAdd); } functionalBackupService .saveCollectionAndSequence(guid, SCENARIO_BACKUP_EVENT, PRESERVATION_SCENARIO, operationId); PreservationScenarioReport preservationScenarioReport = generateReport(allScenariosInDatabase, listToImport, updatedIdentifiers, identifierToDelete, identifierToAdd); saveReport(guid, preservationScenarioReport); } catch (VitamException e) { createLogbookEventKo(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT, e.getMessage()); throw e; } createLogbookEventSuccess(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); return new RequestResponseOK<PreservationScenarioModel>().addAllResults(listToImport) .setHttpCode(Response.Status.CREATED.getStatusCode()); } PreservationScenarioService(MongoDbAccessReferential mongoDbAccess,
        FunctionalBackupService functionalBackupService,
        LogbookOperationsClientFactory logbookOperationsClientFactory);  PreservationScenarioService(MongoDbAccessAdminImpl mongoAccess,
        FunctionalBackupService functionalBackupService); RequestResponse<PreservationScenarioModel> importScenarios(
        @NotNull List<PreservationScenarioModel> listToImport); RequestResponse<PreservationScenarioModel> findPreservationScenario(JsonNode queryDsl); }
PreservationScenarioService { public RequestResponse<PreservationScenarioModel> importScenarios( @NotNull List<PreservationScenarioModel> listToImport) throws VitamException { String operationId = getVitamSession().getRequestId(); GUID guid = getGUID(operationId); createLogbook(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); try { validate(listToImport); final ObjectNode finalSelect = new Select().getFinalSelect(); DbRequestResult result = mongoDbAccess.findDocuments(finalSelect, PRESERVATION_SCENARIO); final List<PreservationScenarioModel> allScenariosInDatabase = result.getDocuments(PreservationScenario.class, PreservationScenarioModel.class); Set<String> newIdentifiers = listToImport.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> oldIdentifiers = allScenariosInDatabase.stream().map(PreservationScenarioModel::getIdentifier).collect(toSet()); Set<String> updatedIdentifiers = SetUtils.intersection(newIdentifiers, oldIdentifiers); if (!updatedIdentifiers.isEmpty()) { updateScenarios(listToImport, updatedIdentifiers); } Set<String> identifierToDelete = SetUtils.difference(oldIdentifiers, newIdentifiers); if (!identifierToDelete.isEmpty()) { deleteScenarios(identifierToDelete); } Set<String> identifierToAdd = SetUtils.difference(newIdentifiers, oldIdentifiers); if (!identifierToAdd.isEmpty()) { insertScenarios(listToImport, identifierToAdd); } functionalBackupService .saveCollectionAndSequence(guid, SCENARIO_BACKUP_EVENT, PRESERVATION_SCENARIO, operationId); PreservationScenarioReport preservationScenarioReport = generateReport(allScenariosInDatabase, listToImport, updatedIdentifiers, identifierToDelete, identifierToAdd); saveReport(guid, preservationScenarioReport); } catch (VitamException e) { createLogbookEventKo(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT, e.getMessage()); throw e; } createLogbookEventSuccess(logbookOperationsClientFactory, guid, SCENARIO_IMPORT_EVENT); return new RequestResponseOK<PreservationScenarioModel>().addAllResults(listToImport) .setHttpCode(Response.Status.CREATED.getStatusCode()); } PreservationScenarioService(MongoDbAccessReferential mongoDbAccess,
        FunctionalBackupService functionalBackupService,
        LogbookOperationsClientFactory logbookOperationsClientFactory);  PreservationScenarioService(MongoDbAccessAdminImpl mongoAccess,
        FunctionalBackupService functionalBackupService); RequestResponse<PreservationScenarioModel> importScenarios(
        @NotNull List<PreservationScenarioModel> listToImport); RequestResponse<PreservationScenarioModel> findPreservationScenario(JsonNode queryDsl);  }