@RunWithCustomExecutor @Test public void testUpdateRulesWithArchiveUnitProfileValidationFailure() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(TENANT_ID_0); VitamThreadUtils.getVitamSession().setRequestId("aeeaaaaaacagqkjjaaxpwallds4xu6iaaaaq"); String uuid = "aeaqaaaabeghay2jabzuaalbarkww4iaaaba"; final Unit initialUnit = new Unit( JsonHandler.getFromFile(PropertiesUtils.getResourceFile("unitRulesToUpdate.json"))); MetadataCollections.UNIT.getCollection().insertOne(initialUnit); MetadataCollections.UNIT.getEsClient().insertFullDocument(MetadataCollections.UNIT, 0, uuid, initialUnit); List<OntologyModel> ontologyModels = JsonHandler .getFromInputStreamAsTypeReference(OntologyTestHelper.loadOntologies(), new TypeReference<List<OntologyModel>>() { }); AdminManagementClientFactory adminManagementClientFactory = mock(AdminManagementClientFactory.class); AdminManagementClient adminManagementClient = mock(AdminManagementClient.class); doReturn(adminManagementClient).when(adminManagementClientFactory).getClient(); doReturn(new RequestResponseOK<ArchiveUnitProfileModel>().addResult(new ArchiveUnitProfileModel() .setControlSchema(PropertiesUtils.getResourceAsString("unitRulesAUP_KO.json")) .setStatus(ArchiveUnitProfileStatus.ACTIVE) )).when(adminManagementClient).findArchiveUnitProfilesByID("AUP_IDENTIFIER"); CachedArchiveUnitProfileLoader archiveUnitProfileLoader = new CachedArchiveUnitProfileLoader(adminManagementClientFactory, 100, 300); RuleActions ruleActions = new RuleActions(); ruleActions.getAdd().add(ImmutableMap.of( "AccessRule", new RuleCategoryAction() .setPreventInheritance(true) )); ruleActions.setAddOrUpdateMetadata( new ManagementMetadataAction() .setArchiveUnitProfile("AUP_IDENTIFIER") ); CachedSchemaValidatorLoader schemaValidatorLoader = new CachedSchemaValidatorLoader(100, 300); OntologyValidator ontologyValidator = new OntologyValidator(() -> ontologyModels); UnitValidator unitValidator = new UnitValidator(archiveUnitProfileLoader, schemaValidatorLoader); final DbRequest dbRequest = new DbRequest( new MongoDbMetadataRepository<Unit>(() -> MetadataCollections.UNIT.getCollection()), new MongoDbMetadataRepository<ObjectGroup>(() -> MetadataCollections.OBJECTGROUP.getCollection()), fieldHistoryManager); assertThatThrownBy( () -> dbRequest.execRuleRequest(uuid, ruleActions, emptyMap(), ontologyValidator, unitValidator, Collections.emptyList())) .isInstanceOf(MetadataValidationException.class); String expected = BsonHelper.stringify(initialUnit); String after = JsonHandler.unprettyPrint(MetadataCollections.UNIT.getCollection().find(Filters.eq("_id", uuid)).first()); JsonAssert.assertJsonEquals(expected, after); }
public UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions, Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator, List<OntologyModel> ontologyModels) throws InvalidParseOperationException, MetaDataExecutionException, InvalidCreateOperationException, MetaDataNotFoundException, MetadataValidationException { final Integer tenantId = ParameterHelper.getTenantParameter(); int tries = 0; while (tries < 3) { MetadataCollections metadataCollections = MetadataCollections.UNIT; MongoCollection<MetadataDocument<?>> collection = metadataCollections.getCollection(); MetadataDocument<?> document = collection.find(and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()) )).first(); if (document == null) { throw new MetaDataNotFoundException("Document not found by id " + documentId); } final JsonNode jsonDocument = JsonHandler.toJsonNode(document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("DEBUG update {} to update to {}", jsonDocument, JsonHandler.prettyPrint(ruleActions)); } DynamicParserTokens parserTokens = new DynamicParserTokens(metadataCollections.getVitamDescriptionResolver(), ontologyModels); final MongoDbInMemory mongoInMemory = new MongoDbInMemory(jsonDocument, parserTokens); UpdateMultiQuery updateQuery = new UpdateMultiQuery(); updateQuery .addActions(push(VitamFieldsHelper.operations(), VitamThreadUtils.getVitamSession().getRequestId())); final RequestParserMultiple updateRequest = new UpdateParserMultiple(new MongoDbVarNameAdapter()); updateRequest.parse(updateQuery.getFinalUpdateById()); mongoInMemory.getUpdateJson(updateRequest); final ObjectNode updatedJsonDocument = (ObjectNode) mongoInMemory.getUpdateJsonForRule(ruleActions, bindRuleToDuration); fieldHistoryManager.trigger(jsonDocument, updatedJsonDocument); Integer documentVersion = document.getVersion(); int newDocumentVersion = documentVersion + 1; Integer atomicVersion = document.getAtomicVersion(); int newAtomicVersion = atomicVersion == null ? newDocumentVersion : atomicVersion + 1; updatedJsonDocument.put(VitamDocument.VERSION, newDocumentVersion); updatedJsonDocument.put(MetadataDocument.ATOMIC_VERSION, newAtomicVersion); Unit updatedDocument = new Unit(updatedJsonDocument); final ObjectNode transformedUpdatedDocument = ontologyValidator.verifyAndReplaceFields(updatedJsonDocument); unitValidator.validateUnit(transformedUpdatedDocument); final Bson condition; if (atomicVersion == null) { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), exists(MetadataDocument.ATOMIC_VERSION, false)); } else { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), eq(MetadataDocument.ATOMIC_VERSION, atomicVersion)); } LOGGER.debug("DEBUG update {}", transformedUpdatedDocument); UpdateResult result = collection.replaceOne(condition, updatedDocument); if (result.getModifiedCount() == 1) { indexFieldsUpdated(updatedDocument, tenantId); return new UpdatedDocument(documentId, jsonDocument, JsonHandler.toJsonNode(updatedDocument)); } tries++; } throw new MetaDataExecutionException("Can not modify document " + documentId); }
DbRequest { public UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions, Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator, List<OntologyModel> ontologyModels) throws InvalidParseOperationException, MetaDataExecutionException, InvalidCreateOperationException, MetaDataNotFoundException, MetadataValidationException { final Integer tenantId = ParameterHelper.getTenantParameter(); int tries = 0; while (tries < 3) { MetadataCollections metadataCollections = MetadataCollections.UNIT; MongoCollection<MetadataDocument<?>> collection = metadataCollections.getCollection(); MetadataDocument<?> document = collection.find(and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()) )).first(); if (document == null) { throw new MetaDataNotFoundException("Document not found by id " + documentId); } final JsonNode jsonDocument = JsonHandler.toJsonNode(document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("DEBUG update {} to update to {}", jsonDocument, JsonHandler.prettyPrint(ruleActions)); } DynamicParserTokens parserTokens = new DynamicParserTokens(metadataCollections.getVitamDescriptionResolver(), ontologyModels); final MongoDbInMemory mongoInMemory = new MongoDbInMemory(jsonDocument, parserTokens); UpdateMultiQuery updateQuery = new UpdateMultiQuery(); updateQuery .addActions(push(VitamFieldsHelper.operations(), VitamThreadUtils.getVitamSession().getRequestId())); final RequestParserMultiple updateRequest = new UpdateParserMultiple(new MongoDbVarNameAdapter()); updateRequest.parse(updateQuery.getFinalUpdateById()); mongoInMemory.getUpdateJson(updateRequest); final ObjectNode updatedJsonDocument = (ObjectNode) mongoInMemory.getUpdateJsonForRule(ruleActions, bindRuleToDuration); fieldHistoryManager.trigger(jsonDocument, updatedJsonDocument); Integer documentVersion = document.getVersion(); int newDocumentVersion = documentVersion + 1; Integer atomicVersion = document.getAtomicVersion(); int newAtomicVersion = atomicVersion == null ? newDocumentVersion : atomicVersion + 1; updatedJsonDocument.put(VitamDocument.VERSION, newDocumentVersion); updatedJsonDocument.put(MetadataDocument.ATOMIC_VERSION, newAtomicVersion); Unit updatedDocument = new Unit(updatedJsonDocument); final ObjectNode transformedUpdatedDocument = ontologyValidator.verifyAndReplaceFields(updatedJsonDocument); unitValidator.validateUnit(transformedUpdatedDocument); final Bson condition; if (atomicVersion == null) { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), exists(MetadataDocument.ATOMIC_VERSION, false)); } else { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), eq(MetadataDocument.ATOMIC_VERSION, atomicVersion)); } LOGGER.debug("DEBUG update {}", transformedUpdatedDocument); UpdateResult result = collection.replaceOne(condition, updatedDocument); if (result.getModifiedCount() == 1) { indexFieldsUpdated(updatedDocument, tenantId); return new UpdatedDocument(documentId, jsonDocument, JsonHandler.toJsonNode(updatedDocument)); } tries++; } throw new MetaDataExecutionException("Can not modify document " + documentId); } }
DbRequest { public UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions, Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator, List<OntologyModel> ontologyModels) throws InvalidParseOperationException, MetaDataExecutionException, InvalidCreateOperationException, MetaDataNotFoundException, MetadataValidationException { final Integer tenantId = ParameterHelper.getTenantParameter(); int tries = 0; while (tries < 3) { MetadataCollections metadataCollections = MetadataCollections.UNIT; MongoCollection<MetadataDocument<?>> collection = metadataCollections.getCollection(); MetadataDocument<?> document = collection.find(and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()) )).first(); if (document == null) { throw new MetaDataNotFoundException("Document not found by id " + documentId); } final JsonNode jsonDocument = JsonHandler.toJsonNode(document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("DEBUG update {} to update to {}", jsonDocument, JsonHandler.prettyPrint(ruleActions)); } DynamicParserTokens parserTokens = new DynamicParserTokens(metadataCollections.getVitamDescriptionResolver(), ontologyModels); final MongoDbInMemory mongoInMemory = new MongoDbInMemory(jsonDocument, parserTokens); UpdateMultiQuery updateQuery = new UpdateMultiQuery(); updateQuery .addActions(push(VitamFieldsHelper.operations(), VitamThreadUtils.getVitamSession().getRequestId())); final RequestParserMultiple updateRequest = new UpdateParserMultiple(new MongoDbVarNameAdapter()); updateRequest.parse(updateQuery.getFinalUpdateById()); mongoInMemory.getUpdateJson(updateRequest); final ObjectNode updatedJsonDocument = (ObjectNode) mongoInMemory.getUpdateJsonForRule(ruleActions, bindRuleToDuration); fieldHistoryManager.trigger(jsonDocument, updatedJsonDocument); Integer documentVersion = document.getVersion(); int newDocumentVersion = documentVersion + 1; Integer atomicVersion = document.getAtomicVersion(); int newAtomicVersion = atomicVersion == null ? newDocumentVersion : atomicVersion + 1; updatedJsonDocument.put(VitamDocument.VERSION, newDocumentVersion); updatedJsonDocument.put(MetadataDocument.ATOMIC_VERSION, newAtomicVersion); Unit updatedDocument = new Unit(updatedJsonDocument); final ObjectNode transformedUpdatedDocument = ontologyValidator.verifyAndReplaceFields(updatedJsonDocument); unitValidator.validateUnit(transformedUpdatedDocument); final Bson condition; if (atomicVersion == null) { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), exists(MetadataDocument.ATOMIC_VERSION, false)); } else { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), eq(MetadataDocument.ATOMIC_VERSION, atomicVersion)); } LOGGER.debug("DEBUG update {}", transformedUpdatedDocument); UpdateResult result = collection.replaceOne(condition, updatedDocument); if (result.getModifiedCount() == 1) { indexFieldsUpdated(updatedDocument, tenantId); return new UpdatedDocument(documentId, jsonDocument, JsonHandler.toJsonNode(updatedDocument)); } tries++; } throw new MetaDataExecutionException("Can not modify document " + documentId); } @VisibleForTesting  DbRequest(MongoDbMetadataRepository<Unit> mongoDbUnitRepository,
        MongoDbMetadataRepository<ObjectGroup> mongoDbObjectGroupRepository,
        FieldHistoryManager fieldHistoryManager);  DbRequest(); }
DbRequest { public UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions, Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator, List<OntologyModel> ontologyModels) throws InvalidParseOperationException, MetaDataExecutionException, InvalidCreateOperationException, MetaDataNotFoundException, MetadataValidationException { final Integer tenantId = ParameterHelper.getTenantParameter(); int tries = 0; while (tries < 3) { MetadataCollections metadataCollections = MetadataCollections.UNIT; MongoCollection<MetadataDocument<?>> collection = metadataCollections.getCollection(); MetadataDocument<?> document = collection.find(and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()) )).first(); if (document == null) { throw new MetaDataNotFoundException("Document not found by id " + documentId); } final JsonNode jsonDocument = JsonHandler.toJsonNode(document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("DEBUG update {} to update to {}", jsonDocument, JsonHandler.prettyPrint(ruleActions)); } DynamicParserTokens parserTokens = new DynamicParserTokens(metadataCollections.getVitamDescriptionResolver(), ontologyModels); final MongoDbInMemory mongoInMemory = new MongoDbInMemory(jsonDocument, parserTokens); UpdateMultiQuery updateQuery = new UpdateMultiQuery(); updateQuery .addActions(push(VitamFieldsHelper.operations(), VitamThreadUtils.getVitamSession().getRequestId())); final RequestParserMultiple updateRequest = new UpdateParserMultiple(new MongoDbVarNameAdapter()); updateRequest.parse(updateQuery.getFinalUpdateById()); mongoInMemory.getUpdateJson(updateRequest); final ObjectNode updatedJsonDocument = (ObjectNode) mongoInMemory.getUpdateJsonForRule(ruleActions, bindRuleToDuration); fieldHistoryManager.trigger(jsonDocument, updatedJsonDocument); Integer documentVersion = document.getVersion(); int newDocumentVersion = documentVersion + 1; Integer atomicVersion = document.getAtomicVersion(); int newAtomicVersion = atomicVersion == null ? newDocumentVersion : atomicVersion + 1; updatedJsonDocument.put(VitamDocument.VERSION, newDocumentVersion); updatedJsonDocument.put(MetadataDocument.ATOMIC_VERSION, newAtomicVersion); Unit updatedDocument = new Unit(updatedJsonDocument); final ObjectNode transformedUpdatedDocument = ontologyValidator.verifyAndReplaceFields(updatedJsonDocument); unitValidator.validateUnit(transformedUpdatedDocument); final Bson condition; if (atomicVersion == null) { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), exists(MetadataDocument.ATOMIC_VERSION, false)); } else { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), eq(MetadataDocument.ATOMIC_VERSION, atomicVersion)); } LOGGER.debug("DEBUG update {}", transformedUpdatedDocument); UpdateResult result = collection.replaceOne(condition, updatedDocument); if (result.getModifiedCount() == 1) { indexFieldsUpdated(updatedDocument, tenantId); return new UpdatedDocument(documentId, jsonDocument, JsonHandler.toJsonNode(updatedDocument)); } tries++; } throw new MetaDataExecutionException("Can not modify document " + documentId); } @VisibleForTesting  DbRequest(MongoDbMetadataRepository<Unit> mongoDbUnitRepository,
        MongoDbMetadataRepository<ObjectGroup> mongoDbObjectGroupRepository,
        FieldHistoryManager fieldHistoryManager);  DbRequest(); UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions,
        Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator,
        List<OntologyModel> ontologyModels); Result execRequest(final RequestParserMultiple requestParser, List<OntologyModel> ontologies); UpdatedDocument execUpdateRequest(final RequestParserMultiple requestParser, String documentId,
        MetadataCollections metadataCollection, OntologyValidator ontologyValidator, UnitValidator unitValidator,
        List<OntologyModel> ontologyModels); void execInsertUnitRequest(InsertParserMultiple requestParser); void execInsertObjectGroupRequests(List<InsertParserMultiple> requestParsers); void execInsertUnitRequests(BulkUnitInsertRequest request); void deleteUnits(List<String> documentsToDelete); void deleteObjectGroups(List<String> documentsToDelete); }
DbRequest { public UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions, Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator, List<OntologyModel> ontologyModels) throws InvalidParseOperationException, MetaDataExecutionException, InvalidCreateOperationException, MetaDataNotFoundException, MetadataValidationException { final Integer tenantId = ParameterHelper.getTenantParameter(); int tries = 0; while (tries < 3) { MetadataCollections metadataCollections = MetadataCollections.UNIT; MongoCollection<MetadataDocument<?>> collection = metadataCollections.getCollection(); MetadataDocument<?> document = collection.find(and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()) )).first(); if (document == null) { throw new MetaDataNotFoundException("Document not found by id " + documentId); } final JsonNode jsonDocument = JsonHandler.toJsonNode(document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("DEBUG update {} to update to {}", jsonDocument, JsonHandler.prettyPrint(ruleActions)); } DynamicParserTokens parserTokens = new DynamicParserTokens(metadataCollections.getVitamDescriptionResolver(), ontologyModels); final MongoDbInMemory mongoInMemory = new MongoDbInMemory(jsonDocument, parserTokens); UpdateMultiQuery updateQuery = new UpdateMultiQuery(); updateQuery .addActions(push(VitamFieldsHelper.operations(), VitamThreadUtils.getVitamSession().getRequestId())); final RequestParserMultiple updateRequest = new UpdateParserMultiple(new MongoDbVarNameAdapter()); updateRequest.parse(updateQuery.getFinalUpdateById()); mongoInMemory.getUpdateJson(updateRequest); final ObjectNode updatedJsonDocument = (ObjectNode) mongoInMemory.getUpdateJsonForRule(ruleActions, bindRuleToDuration); fieldHistoryManager.trigger(jsonDocument, updatedJsonDocument); Integer documentVersion = document.getVersion(); int newDocumentVersion = documentVersion + 1; Integer atomicVersion = document.getAtomicVersion(); int newAtomicVersion = atomicVersion == null ? newDocumentVersion : atomicVersion + 1; updatedJsonDocument.put(VitamDocument.VERSION, newDocumentVersion); updatedJsonDocument.put(MetadataDocument.ATOMIC_VERSION, newAtomicVersion); Unit updatedDocument = new Unit(updatedJsonDocument); final ObjectNode transformedUpdatedDocument = ontologyValidator.verifyAndReplaceFields(updatedJsonDocument); unitValidator.validateUnit(transformedUpdatedDocument); final Bson condition; if (atomicVersion == null) { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), exists(MetadataDocument.ATOMIC_VERSION, false)); } else { condition = and( eq(MetadataDocument.ID, documentId), eq(MetadataDocument.TENANT_ID, VitamThreadUtils.getVitamSession().getTenantId()), eq(MetadataDocument.ATOMIC_VERSION, atomicVersion)); } LOGGER.debug("DEBUG update {}", transformedUpdatedDocument); UpdateResult result = collection.replaceOne(condition, updatedDocument); if (result.getModifiedCount() == 1) { indexFieldsUpdated(updatedDocument, tenantId); return new UpdatedDocument(documentId, jsonDocument, JsonHandler.toJsonNode(updatedDocument)); } tries++; } throw new MetaDataExecutionException("Can not modify document " + documentId); } @VisibleForTesting  DbRequest(MongoDbMetadataRepository<Unit> mongoDbUnitRepository,
        MongoDbMetadataRepository<ObjectGroup> mongoDbObjectGroupRepository,
        FieldHistoryManager fieldHistoryManager);  DbRequest(); UpdatedDocument execRuleRequest(final String documentId, final RuleActions ruleActions,
        Map<String, DurationData> bindRuleToDuration, OntologyValidator ontologyValidator, UnitValidator unitValidator,
        List<OntologyModel> ontologyModels); Result execRequest(final RequestParserMultiple requestParser, List<OntologyModel> ontologies); UpdatedDocument execUpdateRequest(final RequestParserMultiple requestParser, String documentId,
        MetadataCollections metadataCollection, OntologyValidator ontologyValidator, UnitValidator unitValidator,
        List<OntologyModel> ontologyModels); void execInsertUnitRequest(InsertParserMultiple requestParser); void execInsertObjectGroupRequests(List<InsertParserMultiple> requestParsers); void execInsertUnitRequests(BulkUnitInsertRequest request); void deleteUnits(List<String> documentsToDelete); void deleteObjectGroups(List<String> documentsToDelete);  }