// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.protocol;

import com.yahoo.foo.SimpletypesConfig;
import com.yahoo.text.Utf8Array;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.vespa.config.LZ4PayloadCompressor;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

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

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

    @Test
    public void require_that_slime_response_is_initialized() throws IOException {
        ConfigPayload configPayload = ConfigPayload.fromInstance(new SimpletypesConfig(new SimpletypesConfig.Builder()));
        ConfigResponse response = SlimeConfigResponse.fromConfigPayload(configPayload, 3, false, "mymd5");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.serialize(baos, CompressionType.UNCOMPRESSED);
        String payload = baos.toString(StandardCharsets.UTF_8);
        assertNotNull(payload);
        assertEquals("{\"boolval\":false,\"doubleval\":0.0,\"enumval\":\"VAL1\",\"intval\":0,\"longval\":0,\"stringval\":\"s\"}", payload);
        assertThat(response.getGeneration(), is(3L));
        assertThat(response.getConfigMd5(), is("mymd5"));

        baos = new ByteArrayOutputStream();
        response.serialize(baos, CompressionType.UNCOMPRESSED);
        assertThat(baos.toString(), is("{\"boolval\":false,\"doubleval\":0.0,\"enumval\":\"VAL1\",\"intval\":0,\"longval\":0,\"stringval\":\"s\"}"));
    }

    @Test
    public void require_that_slime_response_decompresses_on_serialize() throws IOException {
        ConfigPayload configPayload = ConfigPayload.fromInstance(new SimpletypesConfig(new SimpletypesConfig.Builder()));
        Utf8Array data = configPayload.toUtf8Array(true);
        Utf8Array bytes = new Utf8Array(new LZ4PayloadCompressor().compress(data.getBytes()));
        ConfigResponse response = new SlimeConfigResponse(bytes, 3, false, "mymd5", CompressionInfo.create(CompressionType.LZ4, data.getByteLength()));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.serialize(baos, CompressionType.UNCOMPRESSED);
        String payload = baos.toString(StandardCharsets.UTF_8);
        assertNotNull(payload);

        baos = new ByteArrayOutputStream();
        response.serialize(baos, CompressionType.UNCOMPRESSED);
        assertThat(baos.toString(), is("{\"boolval\":false,\"doubleval\":0.0,\"enumval\":\"VAL1\",\"intval\":0,\"longval\":0,\"stringval\":\"s\"}"));
    }

}

@@ -194,16 +194,57 @@
 Config;%0A
+import com.yahoo.text.AbstractUtf8Array;%0A
 import c
@@ -502,47 +502,8 @@
 s;%0A%0A
-import static org.hamcrest.core.Is.is;%0A
 impo
@@ -592,51 +592,8 @@
 ull;
-%0Aimport static org.junit.Assert.assertThat;
 %0A%0A/*
@@ -1339,24 +1339,35 @@
 %22%7D%22, payload
+.toString()
 );%0A        a
@@ -1363,36 +1363,38 @@
 ;%0A        assert
-That
+Equals
 (response.getGen
@@ -1408,15 +1408,11 @@
 (), 
-is(
 3L)
-)
 ;%0A  
@@ -1423,20 +1423,22 @@
   assert
-That
+Equals
 (respons
@@ -1459,19 +1459,16 @@
 (), 
-is(
 %22mymd5%22)
 );%0A%0A
@@ -1463,17 +1463,16 @@
 %22mymd5%22)
-)
 ;%0A%0A     
@@ -1580,36 +1580,38 @@
 ;%0A        assert
-That
+Equals
 (baos.toString()
@@ -1603,36 +1603,32 @@
 baos.toString(),
- is(
 %22%7B%5C%22boolval%5C%22:fa
@@ -1711,33 +1711,32 @@
 ngval%5C%22:%5C%22s%5C%22%7D%22)
-)
 ;%0A    %7D%0A%0A    @Te
@@ -1953,32 +1953,40 @@
 er()));%0A        
+Abstract
 Utf8Array data =
@@ -2104,16 +2104,12 @@
 ata.
-getBytes
+wrap
 ()))
@@ -2615,12 +2615,14 @@
 sert
-That
+Equals
 (bao
@@ -2639,11 +2639,8 @@
 (), 
-is(
 %22%7B%5C%22
@@ -2743,17 +2743,16 @@
 %5C%22s%5C%22%7D%22)
-)
 ;%0A    %7D%0A

Use ByteBuffer interface.