@Test public void givenIncorrectXMLFileFromWorkspaceWhenExecuteThenReturnResponseFatal() throws Exception { action.addOutIOParameters(out); final JsonNode archiveUnitToBeUpdated = JsonHandler.getFromInputStream(PropertiesUtils.getResourceAsStream(AU_DETAIL)); when(metadataClient.selectUnitbyId(any(), eq(guidAu))).thenReturn(archiveUnitToBeUpdated); when(workspaceClient.getObject(any(), eq(UpdateWorkflowConstants.UNITS_FOLDER + "/" + guidAu + ".json"))) .thenReturn(Response.status(Status.OK) .entity(IOUtils.toInputStream("<root><random>Random XML tags</random></root>", "UTF-8")).build()); final ItemStatus response2 = plugin.execute(params, action); assertEquals(StatusCode.FATAL, response2.getGlobalStatus()); }
@Override public ItemStatus execute(WorkerParameters params, HandlerIO handler) { checkMandatoryParameters(params); LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin running ..."); final ItemStatus itemStatus = new ItemStatus(UPDATE_UNIT_RULES_TASK_ID); try (MetaDataClient metaDataClient = metaDataClientFactory.getClient()) { String archiveUnitId = params.getObjectName().split("\\.")[0]; final SelectParserMultiple selectRequest = new SelectParserMultiple(); ObjectNode projectionNode = JsonHandler.createObjectNode(); ObjectNode objectNode = JsonHandler.createObjectNode(); objectNode.put(MANAGEMENT_KEY, 1); projectionNode.set(FIELDS_KEY, objectNode); final SelectMultiQuery request = selectRequest.getRequest().reset().addProjection(projectionNode); JsonNode jsonResponse = metaDataClient.selectUnitbyId(request.getFinalSelect(), archiveUnitId); JsonNode archiveUnitNode = jsonResponse.get(RESULTS); if (archiveUnitNode.size() == 0) { throw new MetaDataNotFoundException(ARCHIVE_UNIT_NOT_FOUND); } archiveUnitNode = archiveUnitNode.get(0); JsonNode managementNode = archiveUnitNode.get(MANAGEMENT_KEY); UpdateMultiQuery query = new UpdateMultiQuery(); Map<String, List<JsonNode>> updatedRulesByType = new HashMap<>(); final JsonNode rulesForAU = handler.getJsonFromWorkspace( UpdateWorkflowConstants.UNITS_FOLDER + "/" + params.getObjectName()); for (final JsonNode rule : rulesForAU) { if (!updatedRulesByType.containsKey(rule.get(RULE_TYPE).asText())) { List<JsonNode> listRulesByType = new ArrayList<>(); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } else { List<JsonNode> listRulesByType = updatedRulesByType.get(rule.get(RULE_TYPE).asText()); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } } int nbUpdates = 0; for (Map.Entry<String, List<JsonNode>> entry : updatedRulesByType.entrySet()) { JsonNode categoryNode = managementNode.get(entry.getKey()); if (categoryNode != null && categoryNode.get(RULES_KEY) != null && archiveUnitUpdateUtils.updateCategoryRules((ArrayNode) categoryNode.get(RULES_KEY), entry.getValue(), query, entry.getKey())) { nbUpdates++; } } if (nbUpdates > 0) { query .addActions(UpdateActionHelper.push(VitamFieldsHelper.operations(), params.getContainerName())); JsonNode updateResultJson = metaDataClient.updateUnitById(query.getFinalUpdate(), archiveUnitId); String diffMessage = archiveUnitUpdateUtils.getDiffMessageFor(updateResultJson, archiveUnitId); archiveUnitUpdateUtils.logLifecycle(params, archiveUnitId, StatusCode.OK, diffMessage, handler.getLifecyclesClient()); archiveUnitUpdateUtils.commitLifecycle(params.getContainerName(), archiveUnitId, handler.getLifecyclesClient()); } itemStatus.increment(StatusCode.OK); } catch (ProcessingException e) { LOGGER.error("Exception while getting AU information : ", e); itemStatus.increment(StatusCode.FATAL); } catch (InvalidParseOperationException e) { LOGGER.error("Exception while parsing json : ", e); itemStatus.increment(StatusCode.KO); } catch (MetaDataExecutionException | MetaDataDocumentSizeException | MetaDataClientServerException | InvalidCreateOperationException e) { LOGGER.error("Exception with metadata: ", e); itemStatus.increment(StatusCode.FATAL); } catch (MetaDataNotFoundException e) { LOGGER.error("Archive unit not found : ", e); itemStatus.increment(StatusCode.KO); } LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin response: " + itemStatus.getGlobalStatus()); return new ItemStatus(UPDATE_UNIT_RULES_TASK_ID).setItemsStatus(UPDATE_UNIT_RULES_TASK_ID, itemStatus); }
ArchiveUnitRulesUpdateActionPlugin extends ActionHandler implements VitamAutoCloseable { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handler) { checkMandatoryParameters(params); LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin running ..."); final ItemStatus itemStatus = new ItemStatus(UPDATE_UNIT_RULES_TASK_ID); try (MetaDataClient metaDataClient = metaDataClientFactory.getClient()) { String archiveUnitId = params.getObjectName().split("\\.")[0]; final SelectParserMultiple selectRequest = new SelectParserMultiple(); ObjectNode projectionNode = JsonHandler.createObjectNode(); ObjectNode objectNode = JsonHandler.createObjectNode(); objectNode.put(MANAGEMENT_KEY, 1); projectionNode.set(FIELDS_KEY, objectNode); final SelectMultiQuery request = selectRequest.getRequest().reset().addProjection(projectionNode); JsonNode jsonResponse = metaDataClient.selectUnitbyId(request.getFinalSelect(), archiveUnitId); JsonNode archiveUnitNode = jsonResponse.get(RESULTS); if (archiveUnitNode.size() == 0) { throw new MetaDataNotFoundException(ARCHIVE_UNIT_NOT_FOUND); } archiveUnitNode = archiveUnitNode.get(0); JsonNode managementNode = archiveUnitNode.get(MANAGEMENT_KEY); UpdateMultiQuery query = new UpdateMultiQuery(); Map<String, List<JsonNode>> updatedRulesByType = new HashMap<>(); final JsonNode rulesForAU = handler.getJsonFromWorkspace( UpdateWorkflowConstants.UNITS_FOLDER + "/" + params.getObjectName()); for (final JsonNode rule : rulesForAU) { if (!updatedRulesByType.containsKey(rule.get(RULE_TYPE).asText())) { List<JsonNode> listRulesByType = new ArrayList<>(); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } else { List<JsonNode> listRulesByType = updatedRulesByType.get(rule.get(RULE_TYPE).asText()); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } } int nbUpdates = 0; for (Map.Entry<String, List<JsonNode>> entry : updatedRulesByType.entrySet()) { JsonNode categoryNode = managementNode.get(entry.getKey()); if (categoryNode != null && categoryNode.get(RULES_KEY) != null && archiveUnitUpdateUtils.updateCategoryRules((ArrayNode) categoryNode.get(RULES_KEY), entry.getValue(), query, entry.getKey())) { nbUpdates++; } } if (nbUpdates > 0) { query .addActions(UpdateActionHelper.push(VitamFieldsHelper.operations(), params.getContainerName())); JsonNode updateResultJson = metaDataClient.updateUnitById(query.getFinalUpdate(), archiveUnitId); String diffMessage = archiveUnitUpdateUtils.getDiffMessageFor(updateResultJson, archiveUnitId); archiveUnitUpdateUtils.logLifecycle(params, archiveUnitId, StatusCode.OK, diffMessage, handler.getLifecyclesClient()); archiveUnitUpdateUtils.commitLifecycle(params.getContainerName(), archiveUnitId, handler.getLifecyclesClient()); } itemStatus.increment(StatusCode.OK); } catch (ProcessingException e) { LOGGER.error("Exception while getting AU information : ", e); itemStatus.increment(StatusCode.FATAL); } catch (InvalidParseOperationException e) { LOGGER.error("Exception while parsing json : ", e); itemStatus.increment(StatusCode.KO); } catch (MetaDataExecutionException | MetaDataDocumentSizeException | MetaDataClientServerException | InvalidCreateOperationException e) { LOGGER.error("Exception with metadata: ", e); itemStatus.increment(StatusCode.FATAL); } catch (MetaDataNotFoundException e) { LOGGER.error("Archive unit not found : ", e); itemStatus.increment(StatusCode.KO); } LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin response: " + itemStatus.getGlobalStatus()); return new ItemStatus(UPDATE_UNIT_RULES_TASK_ID).setItemsStatus(UPDATE_UNIT_RULES_TASK_ID, itemStatus); } }
ArchiveUnitRulesUpdateActionPlugin extends ActionHandler implements VitamAutoCloseable { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handler) { checkMandatoryParameters(params); LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin running ..."); final ItemStatus itemStatus = new ItemStatus(UPDATE_UNIT_RULES_TASK_ID); try (MetaDataClient metaDataClient = metaDataClientFactory.getClient()) { String archiveUnitId = params.getObjectName().split("\\.")[0]; final SelectParserMultiple selectRequest = new SelectParserMultiple(); ObjectNode projectionNode = JsonHandler.createObjectNode(); ObjectNode objectNode = JsonHandler.createObjectNode(); objectNode.put(MANAGEMENT_KEY, 1); projectionNode.set(FIELDS_KEY, objectNode); final SelectMultiQuery request = selectRequest.getRequest().reset().addProjection(projectionNode); JsonNode jsonResponse = metaDataClient.selectUnitbyId(request.getFinalSelect(), archiveUnitId); JsonNode archiveUnitNode = jsonResponse.get(RESULTS); if (archiveUnitNode.size() == 0) { throw new MetaDataNotFoundException(ARCHIVE_UNIT_NOT_FOUND); } archiveUnitNode = archiveUnitNode.get(0); JsonNode managementNode = archiveUnitNode.get(MANAGEMENT_KEY); UpdateMultiQuery query = new UpdateMultiQuery(); Map<String, List<JsonNode>> updatedRulesByType = new HashMap<>(); final JsonNode rulesForAU = handler.getJsonFromWorkspace( UpdateWorkflowConstants.UNITS_FOLDER + "/" + params.getObjectName()); for (final JsonNode rule : rulesForAU) { if (!updatedRulesByType.containsKey(rule.get(RULE_TYPE).asText())) { List<JsonNode> listRulesByType = new ArrayList<>(); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } else { List<JsonNode> listRulesByType = updatedRulesByType.get(rule.get(RULE_TYPE).asText()); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } } int nbUpdates = 0; for (Map.Entry<String, List<JsonNode>> entry : updatedRulesByType.entrySet()) { JsonNode categoryNode = managementNode.get(entry.getKey()); if (categoryNode != null && categoryNode.get(RULES_KEY) != null && archiveUnitUpdateUtils.updateCategoryRules((ArrayNode) categoryNode.get(RULES_KEY), entry.getValue(), query, entry.getKey())) { nbUpdates++; } } if (nbUpdates > 0) { query .addActions(UpdateActionHelper.push(VitamFieldsHelper.operations(), params.getContainerName())); JsonNode updateResultJson = metaDataClient.updateUnitById(query.getFinalUpdate(), archiveUnitId); String diffMessage = archiveUnitUpdateUtils.getDiffMessageFor(updateResultJson, archiveUnitId); archiveUnitUpdateUtils.logLifecycle(params, archiveUnitId, StatusCode.OK, diffMessage, handler.getLifecyclesClient()); archiveUnitUpdateUtils.commitLifecycle(params.getContainerName(), archiveUnitId, handler.getLifecyclesClient()); } itemStatus.increment(StatusCode.OK); } catch (ProcessingException e) { LOGGER.error("Exception while getting AU information : ", e); itemStatus.increment(StatusCode.FATAL); } catch (InvalidParseOperationException e) { LOGGER.error("Exception while parsing json : ", e); itemStatus.increment(StatusCode.KO); } catch (MetaDataExecutionException | MetaDataDocumentSizeException | MetaDataClientServerException | InvalidCreateOperationException e) { LOGGER.error("Exception with metadata: ", e); itemStatus.increment(StatusCode.FATAL); } catch (MetaDataNotFoundException e) { LOGGER.error("Archive unit not found : ", e); itemStatus.increment(StatusCode.KO); } LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin response: " + itemStatus.getGlobalStatus()); return new ItemStatus(UPDATE_UNIT_RULES_TASK_ID).setItemsStatus(UPDATE_UNIT_RULES_TASK_ID, itemStatus); } ArchiveUnitRulesUpdateActionPlugin(); @VisibleForTesting  ArchiveUnitRulesUpdateActionPlugin(MetaDataClientFactory metaDataClientFactory); }
ArchiveUnitRulesUpdateActionPlugin extends ActionHandler implements VitamAutoCloseable { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handler) { checkMandatoryParameters(params); LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin running ..."); final ItemStatus itemStatus = new ItemStatus(UPDATE_UNIT_RULES_TASK_ID); try (MetaDataClient metaDataClient = metaDataClientFactory.getClient()) { String archiveUnitId = params.getObjectName().split("\\.")[0]; final SelectParserMultiple selectRequest = new SelectParserMultiple(); ObjectNode projectionNode = JsonHandler.createObjectNode(); ObjectNode objectNode = JsonHandler.createObjectNode(); objectNode.put(MANAGEMENT_KEY, 1); projectionNode.set(FIELDS_KEY, objectNode); final SelectMultiQuery request = selectRequest.getRequest().reset().addProjection(projectionNode); JsonNode jsonResponse = metaDataClient.selectUnitbyId(request.getFinalSelect(), archiveUnitId); JsonNode archiveUnitNode = jsonResponse.get(RESULTS); if (archiveUnitNode.size() == 0) { throw new MetaDataNotFoundException(ARCHIVE_UNIT_NOT_FOUND); } archiveUnitNode = archiveUnitNode.get(0); JsonNode managementNode = archiveUnitNode.get(MANAGEMENT_KEY); UpdateMultiQuery query = new UpdateMultiQuery(); Map<String, List<JsonNode>> updatedRulesByType = new HashMap<>(); final JsonNode rulesForAU = handler.getJsonFromWorkspace( UpdateWorkflowConstants.UNITS_FOLDER + "/" + params.getObjectName()); for (final JsonNode rule : rulesForAU) { if (!updatedRulesByType.containsKey(rule.get(RULE_TYPE).asText())) { List<JsonNode> listRulesByType = new ArrayList<>(); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } else { List<JsonNode> listRulesByType = updatedRulesByType.get(rule.get(RULE_TYPE).asText()); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } } int nbUpdates = 0; for (Map.Entry<String, List<JsonNode>> entry : updatedRulesByType.entrySet()) { JsonNode categoryNode = managementNode.get(entry.getKey()); if (categoryNode != null && categoryNode.get(RULES_KEY) != null && archiveUnitUpdateUtils.updateCategoryRules((ArrayNode) categoryNode.get(RULES_KEY), entry.getValue(), query, entry.getKey())) { nbUpdates++; } } if (nbUpdates > 0) { query .addActions(UpdateActionHelper.push(VitamFieldsHelper.operations(), params.getContainerName())); JsonNode updateResultJson = metaDataClient.updateUnitById(query.getFinalUpdate(), archiveUnitId); String diffMessage = archiveUnitUpdateUtils.getDiffMessageFor(updateResultJson, archiveUnitId); archiveUnitUpdateUtils.logLifecycle(params, archiveUnitId, StatusCode.OK, diffMessage, handler.getLifecyclesClient()); archiveUnitUpdateUtils.commitLifecycle(params.getContainerName(), archiveUnitId, handler.getLifecyclesClient()); } itemStatus.increment(StatusCode.OK); } catch (ProcessingException e) { LOGGER.error("Exception while getting AU information : ", e); itemStatus.increment(StatusCode.FATAL); } catch (InvalidParseOperationException e) { LOGGER.error("Exception while parsing json : ", e); itemStatus.increment(StatusCode.KO); } catch (MetaDataExecutionException | MetaDataDocumentSizeException | MetaDataClientServerException | InvalidCreateOperationException e) { LOGGER.error("Exception with metadata: ", e); itemStatus.increment(StatusCode.FATAL); } catch (MetaDataNotFoundException e) { LOGGER.error("Archive unit not found : ", e); itemStatus.increment(StatusCode.KO); } LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin response: " + itemStatus.getGlobalStatus()); return new ItemStatus(UPDATE_UNIT_RULES_TASK_ID).setItemsStatus(UPDATE_UNIT_RULES_TASK_ID, itemStatus); } ArchiveUnitRulesUpdateActionPlugin(); @VisibleForTesting  ArchiveUnitRulesUpdateActionPlugin(MetaDataClientFactory metaDataClientFactory); @Override ItemStatus execute(WorkerParameters params, HandlerIO handler); @Override void checkMandatoryIOParameter(HandlerIO handler); }
ArchiveUnitRulesUpdateActionPlugin extends ActionHandler implements VitamAutoCloseable { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handler) { checkMandatoryParameters(params); LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin running ..."); final ItemStatus itemStatus = new ItemStatus(UPDATE_UNIT_RULES_TASK_ID); try (MetaDataClient metaDataClient = metaDataClientFactory.getClient()) { String archiveUnitId = params.getObjectName().split("\\.")[0]; final SelectParserMultiple selectRequest = new SelectParserMultiple(); ObjectNode projectionNode = JsonHandler.createObjectNode(); ObjectNode objectNode = JsonHandler.createObjectNode(); objectNode.put(MANAGEMENT_KEY, 1); projectionNode.set(FIELDS_KEY, objectNode); final SelectMultiQuery request = selectRequest.getRequest().reset().addProjection(projectionNode); JsonNode jsonResponse = metaDataClient.selectUnitbyId(request.getFinalSelect(), archiveUnitId); JsonNode archiveUnitNode = jsonResponse.get(RESULTS); if (archiveUnitNode.size() == 0) { throw new MetaDataNotFoundException(ARCHIVE_UNIT_NOT_FOUND); } archiveUnitNode = archiveUnitNode.get(0); JsonNode managementNode = archiveUnitNode.get(MANAGEMENT_KEY); UpdateMultiQuery query = new UpdateMultiQuery(); Map<String, List<JsonNode>> updatedRulesByType = new HashMap<>(); final JsonNode rulesForAU = handler.getJsonFromWorkspace( UpdateWorkflowConstants.UNITS_FOLDER + "/" + params.getObjectName()); for (final JsonNode rule : rulesForAU) { if (!updatedRulesByType.containsKey(rule.get(RULE_TYPE).asText())) { List<JsonNode> listRulesByType = new ArrayList<>(); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } else { List<JsonNode> listRulesByType = updatedRulesByType.get(rule.get(RULE_TYPE).asText()); listRulesByType.add(rule); updatedRulesByType.put(rule.get(RULE_TYPE).asText(), listRulesByType); } } int nbUpdates = 0; for (Map.Entry<String, List<JsonNode>> entry : updatedRulesByType.entrySet()) { JsonNode categoryNode = managementNode.get(entry.getKey()); if (categoryNode != null && categoryNode.get(RULES_KEY) != null && archiveUnitUpdateUtils.updateCategoryRules((ArrayNode) categoryNode.get(RULES_KEY), entry.getValue(), query, entry.getKey())) { nbUpdates++; } } if (nbUpdates > 0) { query .addActions(UpdateActionHelper.push(VitamFieldsHelper.operations(), params.getContainerName())); JsonNode updateResultJson = metaDataClient.updateUnitById(query.getFinalUpdate(), archiveUnitId); String diffMessage = archiveUnitUpdateUtils.getDiffMessageFor(updateResultJson, archiveUnitId); archiveUnitUpdateUtils.logLifecycle(params, archiveUnitId, StatusCode.OK, diffMessage, handler.getLifecyclesClient()); archiveUnitUpdateUtils.commitLifecycle(params.getContainerName(), archiveUnitId, handler.getLifecyclesClient()); } itemStatus.increment(StatusCode.OK); } catch (ProcessingException e) { LOGGER.error("Exception while getting AU information : ", e); itemStatus.increment(StatusCode.FATAL); } catch (InvalidParseOperationException e) { LOGGER.error("Exception while parsing json : ", e); itemStatus.increment(StatusCode.KO); } catch (MetaDataExecutionException | MetaDataDocumentSizeException | MetaDataClientServerException | InvalidCreateOperationException e) { LOGGER.error("Exception with metadata: ", e); itemStatus.increment(StatusCode.FATAL); } catch (MetaDataNotFoundException e) { LOGGER.error("Archive unit not found : ", e); itemStatus.increment(StatusCode.KO); } LOGGER.debug("ArchiveUnitRulesUpdateActionPlugin response: " + itemStatus.getGlobalStatus()); return new ItemStatus(UPDATE_UNIT_RULES_TASK_ID).setItemsStatus(UPDATE_UNIT_RULES_TASK_ID, itemStatus); } ArchiveUnitRulesUpdateActionPlugin(); @VisibleForTesting  ArchiveUnitRulesUpdateActionPlugin(MetaDataClientFactory metaDataClientFactory); @Override ItemStatus execute(WorkerParameters params, HandlerIO handler); @Override void checkMandatoryIOParameter(HandlerIO handler);  }