@Test @RunWithCustomExecutor public void givenContextImportedWithInvalidAccessAndIngestContract() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(TENANT_ID); String INGEST_CONTRACT_ID = "NON-EXISTING-INGEST_CONTRACT"; when(ingestContractService.findByIdentifier(INGEST_CONTRACT_ID)).thenReturn(null); String ACCESS_CONTRACT_ID = "NON-EXISTING-ACCESS_CONTRACT"; when(accessContractService.findByIdentifier(ACCESS_CONTRACT_ID)).thenReturn(null); final File fileContexts = PropertiesUtils.getResourceFile("KO_contexts_invalid_ingest_and_access_contract.json"); final List<ContextModel> contextModelList = JsonHandler.getFromFileAsTypeReference(fileContexts, new TypeReference<List<ContextModel>>() { }); RequestResponse<ContextModel> response = contextService.createContexts(contextModelList); assertThat(response.isOk()).isFalse(); assertThat(((VitamError) response).getErrors().get(0).getDescription()).isEqualTo("The ingest contract NON-EXISTING-INGEST_CONTRACT of tenant 0 does not exist"); assertThat(((VitamError) response).getErrors().get(1).getDescription()).isEqualTo("The access contract NON-EXISTING-ACCESS_CONTRACT of tenant 0 does not exist"); assertThat(((VitamError) response).getErrors().get(2).getDescription()).isEqualTo("The ingest contract NON-EXISTING-INGEST_CONTRACT of tenant 1 does not exist"); assertThat(((VitamError) response).getErrors().get(3).getDescription()).isEqualTo("The access contract NON-EXISTING-ACCESS_CONTRACT of tenant 1 does not exist"); verifyZeroInteractions(functionalBackupService); }
@Override public RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList) throws VitamException { ParametersChecker.checkParameter(CONTEXT_IS_MANDATORY_PARAMETER, contextModelList); if (contextModelList.isEmpty()) { return new RequestResponseOK<>(); } boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.CONTEXT_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); ContextManager manager = new ContextManager(logbookClient, accessContract, ingestContract, securityProfileService, eip); manager.logStarted(); final List<ContextModel> contextsListToPersist = new ArrayList<>(); final VitamError error = new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); ArrayNode contextsToPersist = JsonHandler.createArrayNode(); try { for (final ContextModel cm : contextModelList) { if (!slaveMode) { final String code = vitamCounterService .getNextSequenceAsString(ParameterHelper.getTenantParameter(), SequenceType.CONTEXT_SEQUENCE); cm.setIdentifier(code); } if (cm.getId() != null) { error.addToErrors(new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()).setMessage( ContextRejectionCause.rejectIdNotAllowedInCreate(cm.getName()).getReason())); continue; } if (manager.validateContext(cm, error)) { cm.setId(GUIDFactory.newContextGUID().getId()); cm.setCreationdate(LocalDateUtil.getString(LocalDateUtil.now())); cm.setLastupdate(LocalDateUtil.getString(LocalDateUtil.now())); final ObjectNode contextNode = (ObjectNode) JsonHandler.toJsonNode(cm); JsonNode jsonNode = contextNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { contextNode.set("_id", jsonNode); } change(contextNode, VitamFieldsHelper.tenant(), TENANT_ID); contextsToPersist.add(contextNode); final ContextModel ctxt = JsonHandler.getFromJsonNode(contextNode, ContextModel.class); contextsListToPersist.add(ctxt); } if (slaveMode) { List<ContextRejectionCause> results = manager.checkEmptyIdentifierSlaveModeValidator().validate(cm); for (ContextRejectionCause result : results) { error.addToErrors( new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setMessage(EMPTY_REQUIRED_FIELD) .setDescription(result.getReason()).setState(StatusCode.KO.name())); } } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(VitamError::getDescription).collect(Collectors.joining(",")); manager.logValidationError(errorsDetails, CONTEXTS_IMPORT_EVENT, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(contextsToPersist, FunctionalAdminCollections.CONTEXT).close(); functionalBackupService.saveCollectionAndSequence( eip, CONTEXTS_BACKUP_EVENT, FunctionalAdminCollections.CONTEXT, eip.toString() ); } catch (final Exception exp) { final String err = "Import contexts error > " + exp.getMessage(); manager.logFatalError(err); return error.setCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getItem()).setDescription(err).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(); return new RequestResponseOK<ContextModel>().addAllResults(contextsListToPersist) .setHttpCode(Response.Status.CREATED.getStatusCode()); }
ContextServiceImpl implements ContextService { @Override public RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList) throws VitamException { ParametersChecker.checkParameter(CONTEXT_IS_MANDATORY_PARAMETER, contextModelList); if (contextModelList.isEmpty()) { return new RequestResponseOK<>(); } boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.CONTEXT_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); ContextManager manager = new ContextManager(logbookClient, accessContract, ingestContract, securityProfileService, eip); manager.logStarted(); final List<ContextModel> contextsListToPersist = new ArrayList<>(); final VitamError error = new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); ArrayNode contextsToPersist = JsonHandler.createArrayNode(); try { for (final ContextModel cm : contextModelList) { if (!slaveMode) { final String code = vitamCounterService .getNextSequenceAsString(ParameterHelper.getTenantParameter(), SequenceType.CONTEXT_SEQUENCE); cm.setIdentifier(code); } if (cm.getId() != null) { error.addToErrors(new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()).setMessage( ContextRejectionCause.rejectIdNotAllowedInCreate(cm.getName()).getReason())); continue; } if (manager.validateContext(cm, error)) { cm.setId(GUIDFactory.newContextGUID().getId()); cm.setCreationdate(LocalDateUtil.getString(LocalDateUtil.now())); cm.setLastupdate(LocalDateUtil.getString(LocalDateUtil.now())); final ObjectNode contextNode = (ObjectNode) JsonHandler.toJsonNode(cm); JsonNode jsonNode = contextNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { contextNode.set("_id", jsonNode); } change(contextNode, VitamFieldsHelper.tenant(), TENANT_ID); contextsToPersist.add(contextNode); final ContextModel ctxt = JsonHandler.getFromJsonNode(contextNode, ContextModel.class); contextsListToPersist.add(ctxt); } if (slaveMode) { List<ContextRejectionCause> results = manager.checkEmptyIdentifierSlaveModeValidator().validate(cm); for (ContextRejectionCause result : results) { error.addToErrors( new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setMessage(EMPTY_REQUIRED_FIELD) .setDescription(result.getReason()).setState(StatusCode.KO.name())); } } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(VitamError::getDescription).collect(Collectors.joining(",")); manager.logValidationError(errorsDetails, CONTEXTS_IMPORT_EVENT, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(contextsToPersist, FunctionalAdminCollections.CONTEXT).close(); functionalBackupService.saveCollectionAndSequence( eip, CONTEXTS_BACKUP_EVENT, FunctionalAdminCollections.CONTEXT, eip.toString() ); } catch (final Exception exp) { final String err = "Import contexts error > " + exp.getMessage(); manager.logFatalError(err); return error.setCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getItem()).setDescription(err).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(); return new RequestResponseOK<ContextModel>().addAllResults(contextsListToPersist) .setHttpCode(Response.Status.CREATED.getStatusCode()); } }
ContextServiceImpl implements ContextService { @Override public RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList) throws VitamException { ParametersChecker.checkParameter(CONTEXT_IS_MANDATORY_PARAMETER, contextModelList); if (contextModelList.isEmpty()) { return new RequestResponseOK<>(); } boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.CONTEXT_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); ContextManager manager = new ContextManager(logbookClient, accessContract, ingestContract, securityProfileService, eip); manager.logStarted(); final List<ContextModel> contextsListToPersist = new ArrayList<>(); final VitamError error = new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); ArrayNode contextsToPersist = JsonHandler.createArrayNode(); try { for (final ContextModel cm : contextModelList) { if (!slaveMode) { final String code = vitamCounterService .getNextSequenceAsString(ParameterHelper.getTenantParameter(), SequenceType.CONTEXT_SEQUENCE); cm.setIdentifier(code); } if (cm.getId() != null) { error.addToErrors(new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()).setMessage( ContextRejectionCause.rejectIdNotAllowedInCreate(cm.getName()).getReason())); continue; } if (manager.validateContext(cm, error)) { cm.setId(GUIDFactory.newContextGUID().getId()); cm.setCreationdate(LocalDateUtil.getString(LocalDateUtil.now())); cm.setLastupdate(LocalDateUtil.getString(LocalDateUtil.now())); final ObjectNode contextNode = (ObjectNode) JsonHandler.toJsonNode(cm); JsonNode jsonNode = contextNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { contextNode.set("_id", jsonNode); } change(contextNode, VitamFieldsHelper.tenant(), TENANT_ID); contextsToPersist.add(contextNode); final ContextModel ctxt = JsonHandler.getFromJsonNode(contextNode, ContextModel.class); contextsListToPersist.add(ctxt); } if (slaveMode) { List<ContextRejectionCause> results = manager.checkEmptyIdentifierSlaveModeValidator().validate(cm); for (ContextRejectionCause result : results) { error.addToErrors( new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setMessage(EMPTY_REQUIRED_FIELD) .setDescription(result.getReason()).setState(StatusCode.KO.name())); } } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(VitamError::getDescription).collect(Collectors.joining(",")); manager.logValidationError(errorsDetails, CONTEXTS_IMPORT_EVENT, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(contextsToPersist, FunctionalAdminCollections.CONTEXT).close(); functionalBackupService.saveCollectionAndSequence( eip, CONTEXTS_BACKUP_EVENT, FunctionalAdminCollections.CONTEXT, eip.toString() ); } catch (final Exception exp) { final String err = "Import contexts error > " + exp.getMessage(); manager.logFatalError(err); return error.setCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getItem()).setDescription(err).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(); return new RequestResponseOK<ContextModel>().addAllResults(contextsListToPersist) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ContextServiceImpl(
        MongoDbAccessAdminImpl mongoAccess, VitamCounterService vitamCounterService,
        SecurityProfileService securityProfileService); @VisibleForTesting  ContextServiceImpl(
        MongoDbAccessAdminImpl mongoAccess, VitamCounterService vitamCounterService,
        ContractService<IngestContractModel> ingestContract,
        ContractService<AccessContractModel> accessContract,
        SecurityProfileService securityProfileService, FunctionalBackupService functionalBackupService); }
ContextServiceImpl implements ContextService { @Override public RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList) throws VitamException { ParametersChecker.checkParameter(CONTEXT_IS_MANDATORY_PARAMETER, contextModelList); if (contextModelList.isEmpty()) { return new RequestResponseOK<>(); } boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.CONTEXT_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); ContextManager manager = new ContextManager(logbookClient, accessContract, ingestContract, securityProfileService, eip); manager.logStarted(); final List<ContextModel> contextsListToPersist = new ArrayList<>(); final VitamError error = new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); ArrayNode contextsToPersist = JsonHandler.createArrayNode(); try { for (final ContextModel cm : contextModelList) { if (!slaveMode) { final String code = vitamCounterService .getNextSequenceAsString(ParameterHelper.getTenantParameter(), SequenceType.CONTEXT_SEQUENCE); cm.setIdentifier(code); } if (cm.getId() != null) { error.addToErrors(new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()).setMessage( ContextRejectionCause.rejectIdNotAllowedInCreate(cm.getName()).getReason())); continue; } if (manager.validateContext(cm, error)) { cm.setId(GUIDFactory.newContextGUID().getId()); cm.setCreationdate(LocalDateUtil.getString(LocalDateUtil.now())); cm.setLastupdate(LocalDateUtil.getString(LocalDateUtil.now())); final ObjectNode contextNode = (ObjectNode) JsonHandler.toJsonNode(cm); JsonNode jsonNode = contextNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { contextNode.set("_id", jsonNode); } change(contextNode, VitamFieldsHelper.tenant(), TENANT_ID); contextsToPersist.add(contextNode); final ContextModel ctxt = JsonHandler.getFromJsonNode(contextNode, ContextModel.class); contextsListToPersist.add(ctxt); } if (slaveMode) { List<ContextRejectionCause> results = manager.checkEmptyIdentifierSlaveModeValidator().validate(cm); for (ContextRejectionCause result : results) { error.addToErrors( new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setMessage(EMPTY_REQUIRED_FIELD) .setDescription(result.getReason()).setState(StatusCode.KO.name())); } } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(VitamError::getDescription).collect(Collectors.joining(",")); manager.logValidationError(errorsDetails, CONTEXTS_IMPORT_EVENT, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(contextsToPersist, FunctionalAdminCollections.CONTEXT).close(); functionalBackupService.saveCollectionAndSequence( eip, CONTEXTS_BACKUP_EVENT, FunctionalAdminCollections.CONTEXT, eip.toString() ); } catch (final Exception exp) { final String err = "Import contexts error > " + exp.getMessage(); manager.logFatalError(err); return error.setCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getItem()).setDescription(err).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(); return new RequestResponseOK<ContextModel>().addAllResults(contextsListToPersist) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ContextServiceImpl(
        MongoDbAccessAdminImpl mongoAccess, VitamCounterService vitamCounterService,
        SecurityProfileService securityProfileService); @VisibleForTesting  ContextServiceImpl(
        MongoDbAccessAdminImpl mongoAccess, VitamCounterService vitamCounterService,
        ContractService<IngestContractModel> ingestContract,
        ContractService<AccessContractModel> accessContract,
        SecurityProfileService securityProfileService, FunctionalBackupService functionalBackupService); @Override RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList); @Override DbRequestResult findContexts(JsonNode queryDsl); @Override ContextModel findOneContextById(String id); @Override RequestResponse<ContextModel> deleteContext(String contextId, boolean forceDelete); @Override RequestResponse<ContextModel> updateContext(String id, JsonNode queryDsl); @Override void close(); }
ContextServiceImpl implements ContextService { @Override public RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList) throws VitamException { ParametersChecker.checkParameter(CONTEXT_IS_MANDATORY_PARAMETER, contextModelList); if (contextModelList.isEmpty()) { return new RequestResponseOK<>(); } boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.CONTEXT_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); ContextManager manager = new ContextManager(logbookClient, accessContract, ingestContract, securityProfileService, eip); manager.logStarted(); final List<ContextModel> contextsListToPersist = new ArrayList<>(); final VitamError error = new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); ArrayNode contextsToPersist = JsonHandler.createArrayNode(); try { for (final ContextModel cm : contextModelList) { if (!slaveMode) { final String code = vitamCounterService .getNextSequenceAsString(ParameterHelper.getTenantParameter(), SequenceType.CONTEXT_SEQUENCE); cm.setIdentifier(code); } if (cm.getId() != null) { error.addToErrors(new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()).setMessage( ContextRejectionCause.rejectIdNotAllowedInCreate(cm.getName()).getReason())); continue; } if (manager.validateContext(cm, error)) { cm.setId(GUIDFactory.newContextGUID().getId()); cm.setCreationdate(LocalDateUtil.getString(LocalDateUtil.now())); cm.setLastupdate(LocalDateUtil.getString(LocalDateUtil.now())); final ObjectNode contextNode = (ObjectNode) JsonHandler.toJsonNode(cm); JsonNode jsonNode = contextNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { contextNode.set("_id", jsonNode); } change(contextNode, VitamFieldsHelper.tenant(), TENANT_ID); contextsToPersist.add(contextNode); final ContextModel ctxt = JsonHandler.getFromJsonNode(contextNode, ContextModel.class); contextsListToPersist.add(ctxt); } if (slaveMode) { List<ContextRejectionCause> results = manager.checkEmptyIdentifierSlaveModeValidator().validate(cm); for (ContextRejectionCause result : results) { error.addToErrors( new VitamError(VitamCode.CONTEXT_VALIDATION_ERROR.getItem()) .setMessage(EMPTY_REQUIRED_FIELD) .setDescription(result.getReason()).setState(StatusCode.KO.name())); } } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(VitamError::getDescription).collect(Collectors.joining(",")); manager.logValidationError(errorsDetails, CONTEXTS_IMPORT_EVENT, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(contextsToPersist, FunctionalAdminCollections.CONTEXT).close(); functionalBackupService.saveCollectionAndSequence( eip, CONTEXTS_BACKUP_EVENT, FunctionalAdminCollections.CONTEXT, eip.toString() ); } catch (final Exception exp) { final String err = "Import contexts error > " + exp.getMessage(); manager.logFatalError(err); return error.setCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getItem()).setDescription(err).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(); return new RequestResponseOK<ContextModel>().addAllResults(contextsListToPersist) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ContextServiceImpl(
        MongoDbAccessAdminImpl mongoAccess, VitamCounterService vitamCounterService,
        SecurityProfileService securityProfileService); @VisibleForTesting  ContextServiceImpl(
        MongoDbAccessAdminImpl mongoAccess, VitamCounterService vitamCounterService,
        ContractService<IngestContractModel> ingestContract,
        ContractService<AccessContractModel> accessContract,
        SecurityProfileService securityProfileService, FunctionalBackupService functionalBackupService); @Override RequestResponse<ContextModel> createContexts(List<ContextModel> contextModelList); @Override DbRequestResult findContexts(JsonNode queryDsl); @Override ContextModel findOneContextById(String id); @Override RequestResponse<ContextModel> deleteContext(String contextId, boolean forceDelete); @Override RequestResponse<ContextModel> updateContext(String id, JsonNode queryDsl); @Override void close(); static final String CONTEXTS_BACKUP_EVENT; }