package io.wealthwizards.appsynctesting;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName("App main method")
public class AppTest {
    @Test
    @DisplayName("should decode JSON context, evaluate template, and log result out")
    public void shouldDecodeContextEvaluateAndLog() throws Exception {
        var args = new String[] {
                "#set($values = {})\n#set($name = $context.prev.result.name)\n$util.qr($values.put(\"name\", $name))\n#set($value = \"Hello, $values.name\")\n{ \"value\": \"$value\", \"auth\": \"$util.authType()\" }",
                "{ \"prev\": { \"result\": { \"name\": \"World\" } } }",
                "{ \"authType\": \"Open ID Connect Authorization\" }"
        };

        var actual = tapSystemOut(() -> App.main(args));
        var expected = "{\"result\":\"\\n{ \\\"value\\\": \\\"Hello, World\\\", \\\"auth\\\": \\\"Open ID Connect Authorization\\\" }\",\"error\":null,\"errors\":[]}";

        assertEquals(expected, actual);
    }
}
