Newer
Older
json-utils / src / test / java / io / ljunggren / json / utils / JsonUtilsTest.java
package io.ljunggren.json.utils;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class JsonUtilsTest {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class User {
        private String name;
        private int age;
        private boolean active;
    }

    @Test
    public void instantiationTest() throws Exception {
        Constructor<JsonUtils> constructor = JsonUtils.class.getDeclaredConstructor();
        assertTrue(Modifier.isPrivate(constructor.getModifiers()));
    }

    @Test
    public void objectToJsonTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String expected = "{\"name\":\"Alex\",\"age\":40,\"active\":true}";
        String actual = JsonUtils.objectToJson(user);
        assertEquals(expected, actual);
    }

    @Test
    public void objectToJsonNullTest() throws JsonProcessingException {
        String actual = JsonUtils.objectToJson(null);
        assertEquals("null", actual);
    }

    @Test
    public void jsonToObjectTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        User generatedUser = JsonUtils.jsonToObject(json, User.class);
        assertEquals(user, generatedUser);
    }

    @Test
    public void jsonToParameterizedObjectTest() throws JsonProcessingException {
        List<User> users = Arrays.asList(
                new User("Alex", 40, true)
        );
        String json = JsonUtils.objectToJson(users);
        List<User> generatedUsers = JsonUtils.jsonToParameterizedObject(json, List.class, User.class);
        assertEquals(users, generatedUsers);
    }

    @Test
    public void jsonToParameterizedObjectWithMapperTest() throws JsonProcessingException {
        List<User> users = Arrays.asList(
                new User("Alex", 40, true)
        );
        String json = JsonUtils.objectToJson(users);
        List<User> generatedUsers = JsonUtils.jsonToParameterizedObject(json, new ObjectMapper(), List.class, User.class);
        assertEquals(users, generatedUsers);
    }

    @Test
    public void jsonToListTest() throws JsonMappingException, JsonProcessingException {
        List<User> users = Arrays.asList(new User[] {
                new User("Alex", 40, true),
                new User("James", 10, false)
        });
        String json = JsonUtils.objectToJson(users);
        List<User> generatedUsers = JsonUtils.jsonToList(json, User.class);
        assertEquals(users, generatedUsers);
    }

    @Test
    public void jsonToSetTest() throws JsonMappingException, JsonProcessingException {
        Set<User> users = new HashSet<>(Arrays.asList(new User[] {
                new User("Alex", 40, true),
                new User("James", 10, false)
        }));
        String json = JsonUtils.objectToJson(users);
        Set<User> generatedUsers = JsonUtils.jsonToSet(json, User.class);
        assertEquals(users, generatedUsers);
    }

    @Test
    public void jsonToSetDeduplicationTest() throws JsonMappingException, JsonProcessingException {
        String json = "[\"a\",\"b\",\"a\",\"c\",\"b\"]";
        Set<String> result = JsonUtils.jsonToSet(json, String.class);
        assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), result);
    }

    @Test
    public void jsonToArrayTest() throws JsonProcessingException {
        User[] users = new User[] {
                new User("Alex", 40, true),
                new User("James", 10, false)
        };
        String json = JsonUtils.objectToJson(users);
        User[] generatedUsers = JsonUtils.jsonToArray(json, User.class);
        assertArrayEquals(users, generatedUsers);
    }

    @Test
    public void jsonToHashMapTest() throws JsonProcessingException {
        Map<String, User> map = new HashMap<>();
        map.put("Employee", new User("Alex", 40, true));
        map.put("Boss",new User("James", 10, false));
        String json = JsonUtils.objectToJson(map);
        Map<String, User> generatedMap = JsonUtils.jsonToHashMap(json, String.class, User.class);
        assertEquals(map, generatedMap);
    }

    @Test
    public void isValidTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        assertTrue(JsonUtils.isValid(json));
    }

    @Test
    public void isValidNullTest() {
        assertFalse(JsonUtils.isValid(null));
    }

    @Test
    public void isValidFalseTest() {
        assertFalse(JsonUtils.isValid("{\"name\":\""));
    }

    @Test
    public void areEqualTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String expected = "{\"name\":\"Alex\",\"age\":40,\"active\":true}";
        String actual = JsonUtils.objectToJson(user);
        assertTrue(JsonUtils.areEqual(expected, actual));
    }

    @Test
    public void areEqualFalseTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String expected = "{\"name\":\"Alexander\",\"age\":40,\"active\":true}";
        String actual = JsonUtils.objectToJson(user);
        assertFalse(JsonUtils.areEqual(expected, actual));
    }

    @Test
    public void areEqualInvalidJsonTest() {
        assertFalse(JsonUtils.areEqual("{\"name\":\"Alex\"}", "not valid json"));
    }

    @Test
    public void toCSVTest() throws IOException {
        String input = "[{\"name\":\"Alexander\",\"age\":40,\"active\":true}]";
        String expected = "name,age,active\nAlexander,40,true\n";
        String actual = JsonUtils.toCSV(input);
        assertEquals(expected, actual);
    }

    @Test
    public void toCSVWithDelimiterTest() throws IOException {
        String input = "[{\"name\":\"Alexander\",\"age\":40,\"active\":true}]";
        String expected = "name|age|active\nAlexander|40|true\n";
        String actual = JsonUtils.toCSV(input, '|');
        assertEquals(expected, actual);
    }

    @Test(expected = IllegalArgumentException.class)
    public void toCSVNonArrayTest() throws IOException {
        JsonUtils.toCSV("{}");
    }

    @Test(expected = IllegalArgumentException.class)
    public void toCSVEmptyArrayTest() throws IOException {
        JsonUtils.toCSV("[]");
    }

    @Test
    public void toYAMLTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        String expected = "---\n"
                + "name: \"Alex\"\n"
                + "age: 40\n"
                + "active: true\n";
        String actual = JsonUtils.toYAML(json);
        assertEquals(expected, actual);
    }

    @Test
    public void toTOMLTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        String actual = JsonUtils.toTOML(json);
        assertTrue(actual.contains("age = 40"));
        assertTrue(actual.contains("active = true"));
        // The TOML mapper uses single-quoted literal strings, which is valid TOML
        assertTrue(actual.contains("Alex"));
    }

    @Test
    public void prettyPrintObjectTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String prettyJson = JsonUtils.prettyPrint(user);
        assertTrue(prettyJson.contains("\n"));
        assertTrue(prettyJson.contains("    "));
        User roundTripped = JsonUtils.jsonToObject(prettyJson, User.class);
        assertEquals(user, roundTripped);
    }

    @Test
    public void prettyPrintStringTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        String prettyJson = JsonUtils.prettyPrint(json);
        assertTrue(prettyJson.contains("\n"));
        assertTrue(prettyJson.contains("    "));
        User roundTripped = JsonUtils.jsonToObject(prettyJson, User.class);
        assertEquals(user, roundTripped);
    }

    @Test
    public void jsonToObjectWithMapperTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        User generatedUser = JsonUtils.jsonToObject(json, User.class, new ObjectMapper());
        assertEquals(user, generatedUser);
    }

    @Test
    public void jsonToListWithMapperTest() throws JsonProcessingException {
        List<User> users = Arrays.asList(
                new User("Alex", 40, true),
                new User("James", 10, false)
        );
        String json = JsonUtils.objectToJson(users);
        List<User> generatedUsers = JsonUtils.jsonToList(json, User.class, new ObjectMapper());
        assertEquals(users, generatedUsers);
    }

    @Test
    public void toXMLTest() throws JsonProcessingException {
        User user = new User("Alex", 40, true);
        String json = JsonUtils.objectToJson(user);
        String xml = JsonUtils.toXML(json);
        assertTrue(xml.contains("Alex"));
        assertTrue(xml.contains("40"));
        assertTrue(xml.contains("true"));
    }

}