-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/src/main/java/ch/sbb/polarion/extension/generic/util/OptionsMappingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ch.sbb.polarion.extension.generic.util; | ||
|
||
import com.polarion.alm.shared.util.StringUtils; | ||
import lombok.experimental.UtilityClass; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@UtilityClass | ||
public class OptionsMappingUtils { | ||
|
||
String EMPTY_VALUE = "(empty)"; | ||
|
||
@NotNull | ||
public Map<String, String> getMappingForFieldId(String fieldId, Map<String, Map<String, String>> commonMapping) { | ||
return Optional.ofNullable(commonMapping) | ||
.orElse(new HashMap<>()) | ||
.getOrDefault(fieldId, new HashMap<>()) | ||
.entrySet().stream() | ||
.filter(entry -> !StringUtils.isEmptyTrimmed(entry.getValue())) //remove entries with null/empty/blank values | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
@Nullable | ||
public String getMappedOptionKey(String fieldId, @Nullable Object initialValue, @Nullable Map<String, Map<String, String>> commonMapping) { | ||
if (initialValue == null || initialValue instanceof String) { | ||
String value = StringUtils.isEmptyTrimmed((String) initialValue) ? EMPTY_VALUE : ((String) initialValue); | ||
for (Map.Entry<String, String> entry : getMappingForFieldId(fieldId, commonMapping).entrySet()) { | ||
if (Stream.of(StringUtils.getNotNull(entry.getValue()).split(",")).anyMatch(v -> StringUtils.areEqualTrimmed(v, value))) { | ||
return entry.getKey(); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/test/java/ch/sbb/polarion/extension/generic/util/OptionsMappingUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ch.sbb.polarion.extension.generic.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
class OptionsMappingUtilsTest { | ||
|
||
@Test | ||
void testGetMappingForFieldId() { | ||
assertEquals(new HashMap<>(), OptionsMappingUtils.getMappingForFieldId("fieldId", null)); | ||
assertEquals(new HashMap<>(), OptionsMappingUtils.getMappingForFieldId("fieldId", Map.of())); | ||
assertEquals(new HashMap<>(), OptionsMappingUtils.getMappingForFieldId("fieldId", Map.of("fieldId", new HashMap<>()))); | ||
assertEquals(Map.of("key3", "someValue"), OptionsMappingUtils.getMappingForFieldId("fieldId", Map.of("fieldId", Map.of("key1", "", "key2", " ", "key3", "someValue")))); | ||
} | ||
|
||
@Test | ||
void testGetMappedOptionKey() { | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", null, null)); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", Boolean.TRUE, null)); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", 42, Map.of("field", Map.of("someKey", "42")))); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", "", null)); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", " ", null)); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", " ", Map.of("field", Map.of()))); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", null, Map.of("field", Map.of("someKey", "someValue")))); | ||
assertEquals("someKey", OptionsMappingUtils.getMappedOptionKey("field", "someValue", Map.of("field", Map.of("someKey", "someValue")))); | ||
assertEquals("someKey", OptionsMappingUtils.getMappedOptionKey("field", " someValue ", Map.of("field", Map.of("someKey", "someValue")))); | ||
assertEquals("someKey", OptionsMappingUtils.getMappedOptionKey("field", "someValue", Map.of("field", Map.of("someKey", " someValue")))); | ||
assertNull(OptionsMappingUtils.getMappedOptionKey("field", "", Map.of("field", Map.of("someKey", "")))); | ||
assertEquals("someKey", OptionsMappingUtils.getMappedOptionKey("field", "", Map.of("field", Map.of("someKey", "(empty),someValue")))); | ||
assertEquals("someKey", OptionsMappingUtils.getMappedOptionKey("field", " ", Map.of("field", Map.of("someKey", "(empty) ,someValue")))); | ||
assertEquals("someKey", OptionsMappingUtils.getMappedOptionKey("field", null, Map.of("field", Map.of("someKey", "(empty) ,someValue")))); | ||
} | ||
|
||
} |