summaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonHttpResultTest.java
blob: 19fc27669af82603b71eabc3e067feb59ad64160 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JsonHttpResultTest {

    @Test
    void testCopyConstructor() {
        assertEquals("{}", new JsonHttpResult(new HttpResult()).getJson().toString());
    }

    @Test
    void testOutput() {
        assertEquals("HTTP 200/OK\n"
                + "\n"
                + "JSON: {\"foo\": 3}",
                new JsonHttpResult(new HttpResult().setContent("{ \"foo\" : 3 }")).toString(true));
        assertEquals("HTTP 200/OK\n"
                + "\n"
                + "{ \"foo\" : }",
                new JsonHttpResult(new HttpResult().setContent("{ \"foo\" : }")).toString(true));
    }

    @Test
    void testNonJsonOutput() {
        JsonHttpResult result = new JsonHttpResult();
        result.setContent("Foo");
        StringBuilder sb = new StringBuilder();
        result.printContent(sb);
        assertEquals("Foo", sb.toString());
    }

    @Test
    void testInvalidJsonOutput() {
        JsonHttpResult result = new JsonHttpResult();
        result.setJson(new JSONObject() {
            @Override
            public String toString(int indent) throws JSONException {
                throw new JSONException("Foo");
            }
        });
        StringBuilder sb = new StringBuilder();
        result.printContent(sb);
        assertEquals("JSON: {}", sb.toString());
    }

}