Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yavor16 committed Oct 14, 2024
1 parent e57181b commit b7e7990
Show file tree
Hide file tree
Showing 8 changed files with 445 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
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
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);
Expand All @@ -62,7 +74,7 @@ void test1() throws Exception {
}

@Test
void test2() throws Exception {
void test2() {
Map<String, Object> properties = new TreeMap<>();
properties.put("test1", createTestProperties());
Bar bar = new Bar(properties);
Expand All @@ -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);

Expand All @@ -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<String, String> expectedMap = new HashMap<>();
expectedMap.put("key1", "value1");
expectedMap.put("key2", "value2");

Map<String, String> result = JsonUtil.convertJsonToMap(jsonInputStream, new TypeReference<Map<String, String>>() {
});

assertEquals(expectedMap, result);
}

@Test
void testConvertJsonToMap_withValidString() {
String json = "{\"key1\":\"value1\", \"key2\":\"value2\"}";
Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("key1", "value1");
expectedMap.put("key2", "value2");

Map<String, String> result = JsonUtil.convertJsonToMap(json, new TypeReference<Map<String, String>>() {
});
assertEquals(expectedMap, result);
}

@Test
void testConvertJsonToList_withValidInputStream() {
String json = "[\"value1\", \"value2\"]";
InputStream jsonInputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
List<Object> expectedList = Arrays.asList("value1", "value2");

List<Object> result = JsonUtil.convertJsonToList(jsonInputStream);

assertEquals(expectedList, result);
}

@Test
void testConvertJsonToList_withValidString() {
String json = "[\"value1\", \"value2\"]";
List<Object> expectedList = Arrays.asList("value1", "value2");

List<Object> result = JsonUtil.convertJsonToList(json);

assertEquals(expectedList, result);
}

@Test
void testToJsonBinary_withValidObject() {
String json = "{\"key\":\"value\"}";
Map<String, String> 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<String, String> expectedMap = new HashMap<>();
expectedMap.put("key", "value");

Map<String, String> result = JsonUtil.fromJsonBinary(jsonBinary, new TypeReference<Map<String, String>>() {
});

assertEquals(expectedMap, result);
}

@Test
void testFromJsonBinary_withEmptyBinary() {
byte[] emptyBinary = new byte[0];
assertThrows(ParsingException.class, () -> JsonUtil.fromJsonBinary(emptyBinary, new TypeReference<Map<String, String>>() {
}));
}

@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<Map<String, String>>() {
});
});
}

@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<String, Object> createTestProperties() {
Map<String, Object> testProperties1 = new TreeMap<>();
testProperties1.put("host", "localhost");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> originalList = Arrays.asList("String", testNumber, testDouble);

List<String> 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<Object> result = ListUtil.cast(null);

assertNull(result);
}

@Test
void testCast_withEmptyList() {
List<Object> emptyList = Collections.emptyList();
List<Object> result = ListUtil.cast(emptyList);

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void testAsList_withNonNullItem() {
String item = "Test Item";
List<String> result = ListUtil.asList(item);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals(item, result.get(0));
}

@Test
void testAsList_withNullItem() {
List<String> result = ListUtil.asList(null);

assertNotNull(result);
assertTrue(result.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<Object, Object> result = MapUtil.cast(null);

assertNull(result);
}

@Test
void testCast_withEmptyMap() {
Map<Object, Object> emptyMap = Collections.emptyMap();

Map<Object, Object> result = MapUtil.cast(emptyMap);

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void testAddNonNull_withNonNullValue() {
Map<String, String> 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<String, String> map = new HashMap<>();
String key = "key1";
String value = null;

MapUtil.addNonNull(map, key, value);

assertTrue(map.isEmpty());
}

@Test
void testMergeSafely_withBothMapsNonNull() {
Map<String, String> original = new HashMap<>();
original.put("key1", "value1");
Map<String, String> override = new HashMap<>();
override.put("key2", "value2");

Map<String, String> 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<String, String> original = new HashMap<>();
original.put("key1", "value1");
Map<String, String> override = new HashMap<>();
override.put("key1", "value2");

Map<String, String> result = MapUtil.mergeSafely(original, override);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals("value2", result.get("key1"));
}

@Test
void testMergeSafely_withOriginalNull() {
Map<String, String> override = new HashMap<>();
override.put("key1", "value1");

Map<String, String> result = MapUtil.mergeSafely(null, override);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals("value1", result.get("key1"));
}

@Test
void testMergeSafely_withOverrideNull() {
Map<String, String> original = new HashMap<>();
original.put("key1", "value1");

Map<String, String> result = MapUtil.mergeSafely(original, null);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals("value1", result.get("key1"));
}

@Test
void testMergeSafely_withBothMapsNull() {
Map<String, String> result = MapUtil.mergeSafely(null, null);

assertNotNull(result);
assertTrue(result.isEmpty());
}
}
Loading

0 comments on commit b7e7990

Please sign in to comment.