From 972b9388e3ab23bf01f884cf8a4978396ffa70d4 Mon Sep 17 00:00:00 2001 From: e551763 Date: Thu, 31 Oct 2024 12:42:50 +0100 Subject: [PATCH] fix: proper error message on attempt to set single value into multi-value field and vice-versa Refs: SchweizerischeBundesbahnen/ch.sbb.polarion.extension.diff-tool#83 --- .../extension/generic/service/PolarionService.java | 12 ++++++++++-- .../generic/service/PolarionServiceTest.java | 8 +++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ch/sbb/polarion/extension/generic/service/PolarionService.java b/app/src/main/java/ch/sbb/polarion/extension/generic/service/PolarionService.java index 010733f..a0c20d4 100644 --- a/app/src/main/java/ch/sbb/polarion/extension/generic/service/PolarionService.java +++ b/app/src/main/java/ch/sbb/polarion/extension/generic/service/PolarionService.java @@ -215,15 +215,23 @@ public void setFieldValue(@NotNull IPObject ipObject, @NotNull String fieldId, O FieldMetadata.fromPrototype(ipObject.getPrototype(), fieldId) : FieldMetadata.fromCustomField(ipObject.getCustomFieldPrototype(fieldId)); Object valueToSet = value == null ? null : Optional.ofNullable(IConverter.getSuitableConverter(value, fieldMetadata.getType())) .map(converter -> converter.convert(value, ConverterContext.builder().contextId(ipObject.getContextId()).enumsMapping(enumsMapping).build(), fieldMetadata)).orElse(value); + boolean multiValueField = fieldMetadata.getType() instanceof IListType; if (valueToSet == null) { if (fieldMetadata.isRequired()) { throw new IllegalArgumentException("Cannot set empty value to the required field"); - } else if (fieldMetadata.getType() instanceof IListType) { + } else if (multiValueField) { valueToSet = new ArrayList<>(); //Multi-value fields expect empty list in case of empty value } } + if (valueToSet instanceof List valuesList) { - valueToSet = new CustomTypedList(ipObject, (IListType) fieldMetadata.getType(), fieldMetadata.isRequired(), valuesList); + if (multiValueField) { + valueToSet = new CustomTypedList(ipObject, (IListType) fieldMetadata.getType(), fieldMetadata.isRequired(), valuesList); + } else { + throw new IllegalArgumentException("Cannot set multi-value into field '%s'".formatted(fieldId)); + } + } else if (multiValueField) { + throw new IllegalArgumentException("Cannot set single value to the multi-value field '%s'".formatted(fieldId)); } if (fieldMetadata.isCustom()) { ipObject.setCustomField(fieldId, valueToSet); diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/service/PolarionServiceTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/service/PolarionServiceTest.java index b0ae9c8..f46c886 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/service/PolarionServiceTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/service/PolarionServiceTest.java @@ -324,8 +324,9 @@ void testSetFieldValue() { List listValue = List.of("value1", "value2"); // primitive fields cannot receive lists - assertThrows(ClassCastException.class, + IllegalArgumentException multiValueException = assertThrows(IllegalArgumentException.class, () -> polarionService.setFieldValue(workItem, "genericFieldId", listValue, enumsMapping)); + assertEquals("Cannot set multi-value into field 'genericFieldId'", multiValueException.getMessage()); // lists must be supported genericFieldMetadata.setType(new ListType("listTypeId", FieldType.STRING.getType())); @@ -333,6 +334,11 @@ void testSetFieldValue() { verify(workItem, times(3)).setValue(anyString(), any()); verify(workItem, times(1)).setCustomField(anyString(), any()); + // multi fields cannot receive single values + IllegalArgumentException singleValueException = assertThrows(IllegalArgumentException.class, + () -> polarionService.setFieldValue(workItem, "genericFieldId", 42, enumsMapping)); + assertEquals("Cannot set single value to the multi-value field 'genericFieldId'", singleValueException.getMessage()); + // nulls for list-typed fields must be converted to an empty list polarionService.setFieldValue(workItem, "genericFieldId", null, enumsMapping); verify(workItem, times(1)).setValue("genericFieldId", new ArrayList<>());