summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/slime/SlimeUtilsTest.java
blob: 237b1575bfb7415ea72fcba6862cf25a5a93711e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;

import com.yahoo.text.Utf8;
import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Ulf Lilleengen
 */
public class SlimeUtilsTest {

    @Test
    public void test_copying_slime_types_into_cursor() {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        root.setString("foo", "foobie");
        Cursor subobj = root.setObject("bar");

        Slime slime2 = new Slime();
        Cursor root2 = slime2.setObject();
        root2.setString("a", "a");
        root2.setLong("b", 2);
        root2.setBool("c", true);
        root2.setDouble("d", 3.14);
        root2.setData("e", new byte[]{0x64});
        root2.setNix("f");

        SlimeUtils.copyObject(slime2.get(), subobj);

        assertThat(root.toString(), is("{\"foo\":\"foobie\",\"bar\":{\"a\":\"a\",\"b\":2,\"c\":true,\"d\":3.14,\"e\":\"0x64\",\"f\":null}}"));
    }

    @Test
    public void test_copying_slime_arrays_into_cursor() {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        root.setString("foo", "foobie");
        Cursor subobj = root.setObject("bar");

        Slime slime2 = new Slime();
        Cursor root2 = slime2.setObject();
        Cursor array = root2.setArray("a");
        array.addString("foo");
        array.addLong(4);
        array.addBool(true);
        array.addDouble(3.14);
        array.addNix();
        array.addData(new byte[]{0x64});
        Cursor objinner = array.addObject();
        objinner.setString("inner", "binner");

        SlimeUtils.copyObject(slime2.get(), subobj);

        assertThat(root.toString(), is("{\"foo\":\"foobie\",\"bar\":{\"a\":[\"foo\",4,true,3.14,null,\"0x64\",{\"inner\":\"binner\"}]}}"));
    }

    @Test
    public void test_slime_to_json() throws IOException {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        root.setString("foo", "foobie");
        root.setObject("bar");
        String json = Utf8.toString(SlimeUtils.toJsonBytes(slime));
        assertThat(json, is("{\"foo\":\"foobie\",\"bar\":{}}"));
    }

    @Test
    public void test_json_to_slime() {
        byte[] json = Utf8.toBytes("{\"foo\":\"foobie\",\"bar\":{}}");
        Slime slime = SlimeUtils.jsonToSlime(json);
        assertThat(slime.get().field("foo").asString(), is("foobie"));
        assertTrue(slime.get().field("bar").valid());
    }

    @Test
    public void test_json_to_slime_or_throw() {
        Slime slime = SlimeUtils.jsonToSlimeOrThrow("{\"foo\":\"foobie\",\"bar\":{}}");
        assertThat(slime.get().field("foo").asString(), is("foobie"));
        assertTrue(slime.get().field("bar").valid());
    }

    @Test
    public void test_invalid_json() {
        try {
            SlimeUtils.jsonToSlimeOrThrow("foo");
            fail();
        } catch (RuntimeException e) {
            assertEquals("Unexpected character 'o'", e.getMessage());
        }
    }

}