@Test public void should_return_KO_when_duplicate_rule_to_add() throws Exception { RuleActions ruleActions = new RuleActions(); HashMap<String, RuleCategoryAction> addRules = new HashMap<>(); addRules.put(TAG_RULE_CLASSIFICATION, new RuleCategoryAction()); ruleActions.setAdd(Arrays.asList(addRules, addRules)); TestHandlerIO handlerIO = new TestHandlerIO(); handlerIO.setJsonFromWorkspace("actions.json", JsonHandler.toJsonNode(ruleActions)); handlerIO.setJsonFromWorkspace("query.json", JsonHandler.createObjectNode()); ItemStatus itemStatus = massUpdateRulesCheck.execute(EMPTY_WORKER_PARAMETER, handlerIO); assertThat(itemStatus.getGlobalStatus()).isEqualTo(KO); }
@Override public ItemStatus execute(WorkerParameters param, HandlerIO handler) { try { RuleActions ruleActions = JsonHandler.getFromJsonNode(handler.getJsonFromWorkspace("actions.json"), RuleActions.class); if (ruleActions.isRuleActionsEmpty()) { MassUpdateErrorInfo errorInfo = new MassUpdateErrorInfo("RULE_ACTION_EMPTY", "Rule actions used to update units is empty."); return buildItemStatus(PLUGIN_NAME, KO, errorInfo); } List<String> duplicateKeys = getDuplicateKeys(ruleActions); if (!duplicateKeys.isEmpty()) { String message = String.format("Invalid rule actions query: duplicate rules '%s'.", String.join(", ", duplicateKeys)); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } boolean deleteOrAddSameAUP = isTryingToAddAndDeleteAUP(ruleActions.getAddOrUpdateMetadata(), ruleActions.getDeleteMetadata()); if (deleteOrAddSameAUP) { String message = String.format("Invalid AUP query: duplicate in add or delete '%s'.", ruleActions.getAddOrUpdateMetadata().getArchiveUnitProfile()); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownAddClassificationLevels = ruleActions.getAdd() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownAddClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in added rule action.", String.join(", ", unknownAddClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownUpdateClassificationLevels = ruleActions.getUpdate() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownUpdateClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in updated rule action.", String.join(", ", unknownUpdateClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } return buildItemStatus(PLUGIN_NAME, OK, EventDetails.of("Step OK.")); } catch (VitamException e) { LOGGER.error(e); return buildItemStatus(PLUGIN_NAME, KO, EventDetails.of("Unexpected error.")); } }
MassUpdateRulesCheck extends ActionHandler { @Override public ItemStatus execute(WorkerParameters param, HandlerIO handler) { try { RuleActions ruleActions = JsonHandler.getFromJsonNode(handler.getJsonFromWorkspace("actions.json"), RuleActions.class); if (ruleActions.isRuleActionsEmpty()) { MassUpdateErrorInfo errorInfo = new MassUpdateErrorInfo("RULE_ACTION_EMPTY", "Rule actions used to update units is empty."); return buildItemStatus(PLUGIN_NAME, KO, errorInfo); } List<String> duplicateKeys = getDuplicateKeys(ruleActions); if (!duplicateKeys.isEmpty()) { String message = String.format("Invalid rule actions query: duplicate rules '%s'.", String.join(", ", duplicateKeys)); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } boolean deleteOrAddSameAUP = isTryingToAddAndDeleteAUP(ruleActions.getAddOrUpdateMetadata(), ruleActions.getDeleteMetadata()); if (deleteOrAddSameAUP) { String message = String.format("Invalid AUP query: duplicate in add or delete '%s'.", ruleActions.getAddOrUpdateMetadata().getArchiveUnitProfile()); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownAddClassificationLevels = ruleActions.getAdd() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownAddClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in added rule action.", String.join(", ", unknownAddClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownUpdateClassificationLevels = ruleActions.getUpdate() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownUpdateClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in updated rule action.", String.join(", ", unknownUpdateClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } return buildItemStatus(PLUGIN_NAME, OK, EventDetails.of("Step OK.")); } catch (VitamException e) { LOGGER.error(e); return buildItemStatus(PLUGIN_NAME, KO, EventDetails.of("Unexpected error.")); } } }
MassUpdateRulesCheck extends ActionHandler { @Override public ItemStatus execute(WorkerParameters param, HandlerIO handler) { try { RuleActions ruleActions = JsonHandler.getFromJsonNode(handler.getJsonFromWorkspace("actions.json"), RuleActions.class); if (ruleActions.isRuleActionsEmpty()) { MassUpdateErrorInfo errorInfo = new MassUpdateErrorInfo("RULE_ACTION_EMPTY", "Rule actions used to update units is empty."); return buildItemStatus(PLUGIN_NAME, KO, errorInfo); } List<String> duplicateKeys = getDuplicateKeys(ruleActions); if (!duplicateKeys.isEmpty()) { String message = String.format("Invalid rule actions query: duplicate rules '%s'.", String.join(", ", duplicateKeys)); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } boolean deleteOrAddSameAUP = isTryingToAddAndDeleteAUP(ruleActions.getAddOrUpdateMetadata(), ruleActions.getDeleteMetadata()); if (deleteOrAddSameAUP) { String message = String.format("Invalid AUP query: duplicate in add or delete '%s'.", ruleActions.getAddOrUpdateMetadata().getArchiveUnitProfile()); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownAddClassificationLevels = ruleActions.getAdd() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownAddClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in added rule action.", String.join(", ", unknownAddClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownUpdateClassificationLevels = ruleActions.getUpdate() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownUpdateClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in updated rule action.", String.join(", ", unknownUpdateClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } return buildItemStatus(PLUGIN_NAME, OK, EventDetails.of("Step OK.")); } catch (VitamException e) { LOGGER.error(e); return buildItemStatus(PLUGIN_NAME, KO, EventDetails.of("Unexpected error.")); } } MassUpdateRulesCheck(); @VisibleForTesting  MassUpdateRulesCheck(AlertService alertService, List<String> classificationLevels); }
MassUpdateRulesCheck extends ActionHandler { @Override public ItemStatus execute(WorkerParameters param, HandlerIO handler) { try { RuleActions ruleActions = JsonHandler.getFromJsonNode(handler.getJsonFromWorkspace("actions.json"), RuleActions.class); if (ruleActions.isRuleActionsEmpty()) { MassUpdateErrorInfo errorInfo = new MassUpdateErrorInfo("RULE_ACTION_EMPTY", "Rule actions used to update units is empty."); return buildItemStatus(PLUGIN_NAME, KO, errorInfo); } List<String> duplicateKeys = getDuplicateKeys(ruleActions); if (!duplicateKeys.isEmpty()) { String message = String.format("Invalid rule actions query: duplicate rules '%s'.", String.join(", ", duplicateKeys)); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } boolean deleteOrAddSameAUP = isTryingToAddAndDeleteAUP(ruleActions.getAddOrUpdateMetadata(), ruleActions.getDeleteMetadata()); if (deleteOrAddSameAUP) { String message = String.format("Invalid AUP query: duplicate in add or delete '%s'.", ruleActions.getAddOrUpdateMetadata().getArchiveUnitProfile()); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownAddClassificationLevels = ruleActions.getAdd() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownAddClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in added rule action.", String.join(", ", unknownAddClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownUpdateClassificationLevels = ruleActions.getUpdate() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownUpdateClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in updated rule action.", String.join(", ", unknownUpdateClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } return buildItemStatus(PLUGIN_NAME, OK, EventDetails.of("Step OK.")); } catch (VitamException e) { LOGGER.error(e); return buildItemStatus(PLUGIN_NAME, KO, EventDetails.of("Unexpected error.")); } } MassUpdateRulesCheck(); @VisibleForTesting  MassUpdateRulesCheck(AlertService alertService, List<String> classificationLevels); @Override ItemStatus execute(WorkerParameters param, HandlerIO handler); }
MassUpdateRulesCheck extends ActionHandler { @Override public ItemStatus execute(WorkerParameters param, HandlerIO handler) { try { RuleActions ruleActions = JsonHandler.getFromJsonNode(handler.getJsonFromWorkspace("actions.json"), RuleActions.class); if (ruleActions.isRuleActionsEmpty()) { MassUpdateErrorInfo errorInfo = new MassUpdateErrorInfo("RULE_ACTION_EMPTY", "Rule actions used to update units is empty."); return buildItemStatus(PLUGIN_NAME, KO, errorInfo); } List<String> duplicateKeys = getDuplicateKeys(ruleActions); if (!duplicateKeys.isEmpty()) { String message = String.format("Invalid rule actions query: duplicate rules '%s'.", String.join(", ", duplicateKeys)); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } boolean deleteOrAddSameAUP = isTryingToAddAndDeleteAUP(ruleActions.getAddOrUpdateMetadata(), ruleActions.getDeleteMetadata()); if (deleteOrAddSameAUP) { String message = String.format("Invalid AUP query: duplicate in add or delete '%s'.", ruleActions.getAddOrUpdateMetadata().getArchiveUnitProfile()); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownAddClassificationLevels = ruleActions.getAdd() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownAddClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in added rule action.", String.join(", ", unknownAddClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } List<String> unknownUpdateClassificationLevels = ruleActions.getUpdate() .stream() .filter(rule -> Objects.nonNull(rule.get(TAG_RULE_CLASSIFICATION))) .map(rule -> rule.get(TAG_RULE_CLASSIFICATION).getClassificationLevel()) .filter(Objects::nonNull) .filter(classificationLevel -> !classificationLevels.contains(classificationLevel)) .collect(Collectors.toList()); if (!unknownUpdateClassificationLevels.isEmpty()) { String message = String.format("Unknown classification level '%s' in updated rule action.", String.join(", ", unknownUpdateClassificationLevels)); alertService.createAlert(message); return buildItemStatusWithMessage(PLUGIN_NAME, KO, message); } return buildItemStatus(PLUGIN_NAME, OK, EventDetails.of("Step OK.")); } catch (VitamException e) { LOGGER.error(e); return buildItemStatus(PLUGIN_NAME, KO, EventDetails.of("Unexpected error.")); } } MassUpdateRulesCheck(); @VisibleForTesting  MassUpdateRulesCheck(AlertService alertService, List<String> classificationLevels); @Override ItemStatus execute(WorkerParameters param, HandlerIO handler);  }