Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private Messages() {
public static final String ERROR_PARSING_YAML_STREAM = "Error while parsing YAML stream: {0}";
public static final String ERROR_PARSING_YAML_STRING = "Error while parsing YAML string: %n%s%n%s";
public static final String COULD_NOT_PARSE_BOOLEAN_FLAG = "Cannot parse \"{0}\" flag - expected a boolean format.";
public static final String COULD_NOT_PARSE_MAP_FLAG = "Cannot parse \"{0}\" flag - expected a map format.";
public static final String COULD_NOT_CONSTRUCT_YAML_CONVERTER_0_BECAUSE_OF_1 = "Could not construct YAML converter \"{0}\": {1}";
public static final String INVALID_JSON_SERIALIZATION_STRATEGY_PROVIDED_0 = "Invalid JSON serialization strategy provided: \"{0}\"";
// Audit log messages:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ public static Boolean parseBooleanFlag(final Map<String, Object> parameters, Str

return (Boolean) flagValue;
}

@SuppressWarnings("unchecked")
public static Map<String, Object> parseMap(final Map<String, Object> parameters, String flagName,
Map<String, Object> defaultValue) throws ContentException {
Object flagValue = parameters.get(flagName);

if (flagValue == null) {
return defaultValue;
}

if (!(flagValue instanceof Map)) {
throw new ContentException(Messages.COULD_NOT_PARSE_MAP_FLAG, flagName);
}

return (Map<String, Object>) flagValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.cloudfoundry.multiapps.common.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.cloudfoundry.multiapps.common.ContentException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MapUtilTest {

public static final Map<String, Object> TEST_PARAMETERS = new HashMap<>();
Expand All @@ -24,6 +24,8 @@ class MapUtilTest {
TEST_PARAMETERS.put("emptyFlag", null);
TEST_PARAMETERS.put("incorrectTypeFlag1", "false");
TEST_PARAMETERS.put("incorrectTypeFlag2", "1");
TEST_PARAMETERS.put("mapFlag", Map.of("key", "value"));
TEST_PARAMETERS.put("incorrectMapFlag", "not-a-map");
}

@Test
Expand Down Expand Up @@ -128,4 +130,30 @@ void testMergeSafely_withBothMapsNull() {
assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void testParseMap_withPresentMapValue() {
Map<String, Object> expectedMap = Map.of("key", "value");

assertEquals(expectedMap, MapUtil.parseMap(TEST_PARAMETERS, "mapFlag", null));
}

@Test
void testParseMap_withMissingKey() {
Map<String, Object> defaultValue = Map.of("default", "val");

assertEquals(defaultValue, MapUtil.parseMap(TEST_PARAMETERS, "notPresentFlag", defaultValue));
}

@Test
void testParseMap_withNullValue() {
Map<String, Object> defaultValue = Map.of("default", "val");

assertEquals(defaultValue, MapUtil.parseMap(TEST_PARAMETERS, "emptyFlag", defaultValue));
}

@Test
void testParseMap_withIncorrectType() {
assertThrows(ContentException.class, () -> MapUtil.parseMap(TEST_PARAMETERS, "incorrectMapFlag", null));
}
}
Loading