From b7e7990a42de75178b66cd7a19bb97de2e018ed7 Mon Sep 17 00:00:00 2001 From: Yavor16 Date: Fri, 11 Oct 2024 14:36:56 +0300 Subject: [PATCH] Add unit tests --- .../multiapps/common/util/JsonUtilTest.java | 137 ++++++++++++++++-- .../multiapps/common/util/ListUtilTest.java | 64 ++++++++ .../multiapps/common/util/MapUtilTest.java | 105 ++++++++++++++ .../multiapps/common/util/MiscUtilTest.java | 82 +++++++++++ .../mta/schema/SchemaValidatorTest.java | 5 + .../mta/util/PropertiesUtilTest.java | 63 ++++++++ .../resources/mta/sample/v2/invalid-list.json | 1 + .../test/resources/mta/sample/v2/list.json | 1 + 8 files changed, 445 insertions(+), 13 deletions(-) create mode 100644 multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/ListUtilTest.java create mode 100644 multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MiscUtilTest.java create mode 100644 multiapps-mta/src/test/resources/mta/sample/v2/invalid-list.json create mode 100644 multiapps-mta/src/test/resources/mta/sample/v2/list.json diff --git a/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/JsonUtilTest.java b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/JsonUtilTest.java index 8865ac8a..5acbe497 100644 --- a/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/JsonUtilTest.java +++ b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/JsonUtilTest.java @@ -1,20 +1,29 @@ package org.cloudfoundry.multiapps.common.util; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; 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.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TreeMap; import org.apache.commons.io.IOUtils; import org.cloudfoundry.multiapps.common.Messages; import org.cloudfoundry.multiapps.common.ParsingException; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.core.type.TypeReference; + class JsonUtilTest { @Test @@ -22,32 +31,35 @@ void testConvertJsonToMapWithInvalidJsonInputStream() { String invalidJson = "{]"; InputStream invalidJsonInputStream = IOUtils.toInputStream(invalidJson, Charset.defaultCharset()); - ParsingException resultException = Assertions.assertThrows(ParsingException.class, - () -> JsonUtil.convertJsonToMap(invalidJsonInputStream)); + ParsingException resultException = assertThrows(ParsingException.class, () -> JsonUtil.convertJsonToMap(invalidJsonInputStream)); String errorMessage = resultException.getCause() .getMessage(); - ParsingException expectedException = new ParsingException(resultException.getCause(), Messages.CANNOT_CONVERT_JSON_STREAM_TO_MAP, - errorMessage, invalidJsonInputStream); - Assertions.assertEquals(expectedException.getMessage(), resultException.getMessage()); + ParsingException expectedException = new ParsingException(resultException.getCause(), + Messages.CANNOT_CONVERT_JSON_STREAM_TO_MAP, + errorMessage, + invalidJsonInputStream); + assertEquals(expectedException.getMessage(), resultException.getMessage()); } @Test void testConvertJsonToMapWithInvalidJsonString() { String invalidJson = "{]"; - ParsingException resultException = Assertions.assertThrows(ParsingException.class, () -> JsonUtil.convertJsonToMap(invalidJson)); + ParsingException resultException = assertThrows(ParsingException.class, () -> JsonUtil.convertJsonToMap(invalidJson)); String errorMessage = resultException.getCause() .getMessage(); - ParsingException expectedException = new ParsingException(resultException.getCause(), Messages.CANNOT_CONVERT_JSON_STRING_TO_MAP, - errorMessage, invalidJson); - Assertions.assertEquals(expectedException.getMessage(), resultException.getMessage()); + ParsingException expectedException = new ParsingException(resultException.getCause(), + Messages.CANNOT_CONVERT_JSON_STRING_TO_MAP, + errorMessage, + invalidJson); + assertEquals(expectedException.getMessage(), resultException.getMessage()); } @Test - void test1() throws Exception { + void test1() { Foo foo = new Foo(Map.of("test1", createTestProperties())); String json = JsonUtil.toJson(foo, true); @@ -62,7 +74,7 @@ void test1() throws Exception { } @Test - void test2() throws Exception { + void test2() { Map properties = new TreeMap<>(); properties.put("test1", createTestProperties()); Bar bar = new Bar(properties); @@ -80,7 +92,7 @@ void test2() throws Exception { } @Test - void test3() throws Exception { + void test3() { String json = JsonUtil.toJson(createTestProperties(), true); System.out.println(json); @@ -103,6 +115,105 @@ void testWithNullMap() { assertNull(barMap); } + @Test + void testConvertJsonToMap_withValidInputStream() { + String json = "{\"key1\":\"value1\", \"key2\":\"value2\"}"; + InputStream jsonInputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); + Map expectedMap = new HashMap<>(); + expectedMap.put("key1", "value1"); + expectedMap.put("key2", "value2"); + + Map result = JsonUtil.convertJsonToMap(jsonInputStream, new TypeReference>() { + }); + + assertEquals(expectedMap, result); + } + + @Test + void testConvertJsonToMap_withValidString() { + String json = "{\"key1\":\"value1\", \"key2\":\"value2\"}"; + Map expectedMap = new HashMap<>(); + expectedMap.put("key1", "value1"); + expectedMap.put("key2", "value2"); + + Map result = JsonUtil.convertJsonToMap(json, new TypeReference>() { + }); + assertEquals(expectedMap, result); + } + + @Test + void testConvertJsonToList_withValidInputStream() { + String json = "[\"value1\", \"value2\"]"; + InputStream jsonInputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); + List expectedList = Arrays.asList("value1", "value2"); + + List result = JsonUtil.convertJsonToList(jsonInputStream); + + assertEquals(expectedList, result); + } + + @Test + void testConvertJsonToList_withValidString() { + String json = "[\"value1\", \"value2\"]"; + List expectedList = Arrays.asList("value1", "value2"); + + List result = JsonUtil.convertJsonToList(json); + + assertEquals(expectedList, result); + } + + @Test + void testToJsonBinary_withValidObject() { + String json = "{\"key\":\"value\"}"; + Map object = new HashMap<>(); + object.put("key", "value"); + + byte[] result = JsonUtil.toJsonBinary(object); + + assertArrayEquals(json.getBytes(StandardCharsets.UTF_8), result); + } + + @Test + void testFromJsonBinary_withValidBinary() { + String json = "{\"key\":\"value\"}"; + byte[] jsonBinary = json.getBytes(StandardCharsets.UTF_8); + Map expectedMap = new HashMap<>(); + expectedMap.put("key", "value"); + + Map result = JsonUtil.fromJsonBinary(jsonBinary, new TypeReference>() { + }); + + assertEquals(expectedMap, result); + } + + @Test + void testFromJsonBinary_withEmptyBinary() { + byte[] emptyBinary = new byte[0]; + assertThrows(ParsingException.class, () -> JsonUtil.fromJsonBinary(emptyBinary, new TypeReference>() { + })); + } + + @Test + void testConvertJsonToMap_withInvalidJson_shouldThrowParsingException() { + String invalidJson = "invalid-json"; + InputStream jsonInputStream = new ByteArrayInputStream(invalidJson.getBytes(StandardCharsets.UTF_8)); + + assertThrows(ParsingException.class, () -> { + JsonUtil.convertJsonToMap(jsonInputStream, new TypeReference>() { + }); + }); + } + + @Test + void testConvertJsonToList_withInvalidJson_shouldThrowParsingException() { + String invalidJson = "invalid-json"; + InputStream jsonInputStream = new ByteArrayInputStream(invalidJson.getBytes(StandardCharsets.UTF_8)); + + assertThrows(ParsingException.class, () -> { + JsonUtil.convertJsonToList(jsonInputStream); + }); + } + private Map createTestProperties() { Map testProperties1 = new TreeMap<>(); testProperties1.put("host", "localhost"); diff --git a/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/ListUtilTest.java b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/ListUtilTest.java new file mode 100644 index 00000000..494a3b3c --- /dev/null +++ b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/ListUtilTest.java @@ -0,0 +1,64 @@ +package org.cloudfoundry.multiapps.common.util; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ListUtilTest { + + @Test + void testCast_withValidList() { + int testNumber = 123; + double testDouble = 45.67; + List originalList = Arrays.asList("String", testNumber, testDouble); + + List castList = ListUtil.cast(originalList); + + assertNotNull(castList); + assertEquals(3, castList.size()); + assertEquals("String", castList.get(0)); + assertEquals(testNumber, castList.get(1)); + assertEquals(testDouble, castList.get(2)); + } + + @Test + void testCast_withNullList() { + List result = ListUtil.cast(null); + + assertNull(result); + } + + @Test + void testCast_withEmptyList() { + List emptyList = Collections.emptyList(); + List result = ListUtil.cast(emptyList); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } + + @Test + void testAsList_withNonNullItem() { + String item = "Test Item"; + List result = ListUtil.asList(item); + + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals(item, result.get(0)); + } + + @Test + void testAsList_withNullItem() { + List result = ListUtil.asList(null); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } +} diff --git a/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MapUtilTest.java b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MapUtilTest.java index d71a19e4..ed69567a 100644 --- a/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MapUtilTest.java +++ b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MapUtilTest.java @@ -1,9 +1,13 @@ 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; @@ -36,4 +40,105 @@ void testParseBooleanFlag() { assertThrows(ContentException.class, () -> MapUtil.parseBooleanFlag(TEST_PARAMETERS, "incorrectTypeFlag1", true)); assertThrows(ContentException.class, () -> MapUtil.parseBooleanFlag(TEST_PARAMETERS, "incorrectTypeFlag2", true)); } + + @Test + void testCast_withNullMap() { + Map result = MapUtil.cast(null); + + assertNull(result); + } + + @Test + void testCast_withEmptyMap() { + Map emptyMap = Collections.emptyMap(); + + Map result = MapUtil.cast(emptyMap); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } + + @Test + void testAddNonNull_withNonNullValue() { + Map map = new HashMap<>(); + String key = "key1"; + String value = "value1"; + + MapUtil.addNonNull(map, key, value); + + assertEquals(1, map.size()); + assertEquals(value, map.get(key)); + } + + @Test + void testAddNonNull_withNullValue() { + Map map = new HashMap<>(); + String key = "key1"; + String value = null; + + MapUtil.addNonNull(map, key, value); + + assertTrue(map.isEmpty()); + } + + @Test + void testMergeSafely_withBothMapsNonNull() { + Map original = new HashMap<>(); + original.put("key1", "value1"); + Map override = new HashMap<>(); + override.put("key2", "value2"); + + Map result = MapUtil.mergeSafely(original, override); + + assertNotNull(result); + assertEquals(2, result.size()); + assertEquals("value1", result.get("key1")); + assertEquals("value2", result.get("key2")); + } + + @Test + void testMergeSafely_withTheValuesWithTheSameKey() { + Map original = new HashMap<>(); + original.put("key1", "value1"); + Map override = new HashMap<>(); + override.put("key1", "value2"); + + Map result = MapUtil.mergeSafely(original, override); + + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals("value2", result.get("key1")); + } + + @Test + void testMergeSafely_withOriginalNull() { + Map override = new HashMap<>(); + override.put("key1", "value1"); + + Map result = MapUtil.mergeSafely(null, override); + + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals("value1", result.get("key1")); + } + + @Test + void testMergeSafely_withOverrideNull() { + Map original = new HashMap<>(); + original.put("key1", "value1"); + + Map result = MapUtil.mergeSafely(original, null); + + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals("value1", result.get("key1")); + } + + @Test + void testMergeSafely_withBothMapsNull() { + Map result = MapUtil.mergeSafely(null, null); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } } diff --git a/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MiscUtilTest.java b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MiscUtilTest.java new file mode 100644 index 00000000..765c544b --- /dev/null +++ b/multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/MiscUtilTest.java @@ -0,0 +1,82 @@ +package org.cloudfoundry.multiapps.common.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MiscUtilTest { + + @Test + void testReplaceAll_withMultipleOccurrences() { + StringBuilder builder = new StringBuilder("Hello World! Hello Everyone!"); + String original = "Hello"; + String replacement = "Hi"; + + MiscUtil.replaceAll(builder, original, replacement); + + assertEquals("Hi World! Hi Everyone!", builder.toString()); + } + + @Test + void testReplaceAll_withNoOccurrences() { + StringBuilder builder = new StringBuilder("Hello World!"); + String original = "Hi"; + String replacement = "Bye"; + + MiscUtil.replaceAll(builder, original, replacement); + + assertEquals("Hello World!", builder.toString()); + } + + @Test + void testReplaceAll_withEmptyStringBuilder() { + StringBuilder builder = new StringBuilder(); + String original = "Hello"; + String replacement = "Hi"; + + MiscUtil.replaceAll(builder, original, replacement); + + assertEquals("", builder.toString()); + } + + @Test + void testGetCharacterRange_withStartLessThanEnd() { + char start = 'a'; + char end = 'e'; + + char[] result = MiscUtil.getCharacterRange(start, end); + + assertArrayEquals(new char[]{'a', 'b', 'c', 'd', 'e'}, result); + } + + @Test + void testGetCharacterRange_withStartGreaterThanEnd() { + char start = 'e'; + char end = 'a'; + + char[] result = MiscUtil.getCharacterRange(start, end); + + assertArrayEquals(new char[]{'a', 'b', 'c', 'd', 'e'}, result); + } + + @Test + void testGetCharacterRange_withEqualStartAndEnd() { + char start = 'a'; + char end = 'a'; + + char[] result = MiscUtil.getCharacterRange(start, end); + + assertArrayEquals(new char[]{'a'}, result); + } + + @Test + void testGetCharacterRange_withInvalidRange() { + char start = 'z'; + char end = 'a'; + + char[] result = MiscUtil.getCharacterRange(start, end); + + assertArrayEquals(new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}, result); + } +} diff --git a/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/schema/SchemaValidatorTest.java b/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/schema/SchemaValidatorTest.java index 3edc5b6d..8447b997 100644 --- a/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/schema/SchemaValidatorTest.java +++ b/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/schema/SchemaValidatorTest.java @@ -22,6 +22,11 @@ static Stream testValidateSchema() { Arguments.of("/mta/sample/v2/mtad-01-v2.yaml", Schemas.MTAD, new Expectation(null)), // Valid platform JSON: Arguments.of("/mta/sample/platform-01.json", Schemas.PLATFORM, new Expectation(null)), + Arguments.of("/mta/sample/v2/list.json", Schemas.LIST, new Expectation(null)), + // Invalid json list + Arguments.of("/mta/sample/v2/invalid-list.json", Schemas.LIST, + new Expectation(Expectation.Type.EXCEPTION, + "Unexpected end-of-input: expected close marker for Array")), // Deployment descriptor is missing a required key: Arguments.of("mtad-03.yaml", Schemas.MTAD, new Expectation(Expectation.Type.EXCEPTION, "Missing required key \"ID\"")), diff --git a/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/util/PropertiesUtilTest.java b/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/util/PropertiesUtilTest.java index 15292089..8f608a8a 100644 --- a/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/util/PropertiesUtilTest.java +++ b/multiapps-mta/src/test/java/org/cloudfoundry/multiapps/mta/util/PropertiesUtilTest.java @@ -7,17 +7,26 @@ import java.util.Map; import java.util.stream.Stream; +import org.cloudfoundry.multiapps.common.ContentException; import org.cloudfoundry.multiapps.common.test.TestUtil; import org.cloudfoundry.multiapps.common.test.Tester; import org.cloudfoundry.multiapps.common.test.Tester.Expectation; import org.cloudfoundry.multiapps.common.util.JsonUtil; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + class PropertiesUtilTest { private final Tester tester = Tester.forClass(getClass()); + private final String TEST_PARAMETER_KEY = "test-key"; + private final String TEST_MISSING_PARAMETER_KEY = "missing-test-key"; + private final String TEST_PARAMETER_VALUE = "test-value"; static Stream mergeTest() { return Stream.of( @@ -78,6 +87,60 @@ void testGetPluralOrSingular(String singular, List plural, Expectation e } + @Test + void testGetRequiredParameterWithProvidedParameter() { + Map parameters = new HashMap<>(); + parameters.put(TEST_PARAMETER_KEY, TEST_PARAMETER_VALUE); + String result = PropertiesUtil.getRequiredParameter(parameters, TEST_PARAMETER_KEY); + + assertEquals(TEST_PARAMETER_VALUE ,result); + } + + @Test + void testGetRequiredParameterWithoutProvidedParameter() { + Map parameters = new HashMap<>(); + parameters.put(TEST_PARAMETER_KEY, TEST_PARAMETER_VALUE); + + assertThrows(ContentException.class,() -> PropertiesUtil.getRequiredParameter(parameters, TEST_MISSING_PARAMETER_KEY)); + } + + @Test + void testGetRequiredParameterWithoutProvidedKey() { + Map parameters = new HashMap<>(); + parameters.put(TEST_PARAMETER_KEY, TEST_PARAMETER_VALUE); + + assertThrows(ContentException.class,() -> PropertiesUtil.getRequiredParameter(parameters, null)); + } + + @Test + void testGetOptionalParameterWithProvidedParameter() { + Map parameters = new HashMap<>(); + parameters.put(TEST_PARAMETER_KEY, TEST_PARAMETER_VALUE); + String result = PropertiesUtil.getOptionalParameter(parameters, TEST_PARAMETER_KEY); + + assertEquals(TEST_PARAMETER_VALUE ,result); + } + + @Test + void testGetOptionalParameterWithoutProvidedParameter() { + Map parameters = new HashMap<>(); + parameters.put(TEST_PARAMETER_KEY, TEST_PARAMETER_VALUE); + + String result = PropertiesUtil.getOptionalParameter(parameters, TEST_MISSING_PARAMETER_KEY); + + assertNull(result); + } + + @Test + void testGetOptionalParameterWithoutProvidedKey() { + Map parameters = new HashMap<>(); + parameters.put(TEST_PARAMETER_KEY, TEST_PARAMETER_VALUE); + + String result = PropertiesUtil.getOptionalParameter(parameters, null); + + assertNull(result); + } + static Stream testParsingGetPluralOrSingular() { return Stream.of( // formatter:off diff --git a/multiapps-mta/src/test/resources/mta/sample/v2/invalid-list.json b/multiapps-mta/src/test/resources/mta/sample/v2/invalid-list.json new file mode 100644 index 00000000..9a8573e7 --- /dev/null +++ b/multiapps-mta/src/test/resources/mta/sample/v2/invalid-list.json @@ -0,0 +1 @@ +["Ford", "BMW", "Fiat" \ No newline at end of file diff --git a/multiapps-mta/src/test/resources/mta/sample/v2/list.json b/multiapps-mta/src/test/resources/mta/sample/v2/list.json new file mode 100644 index 00000000..d18c26f3 --- /dev/null +++ b/multiapps-mta/src/test/resources/mta/sample/v2/list.json @@ -0,0 +1 @@ +["Ford", "BMW", "Fiat"]