package meerkat.bulletinboard;

import meerkat.protobuf.BulletinBoardApi.*;
import meerkat.rest.Constants;
import meerkat.rest.ProtobufMessageBodyReader;
import meerkat.rest.ProtobufMessageBodyWriter;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Created by talm on 10/11/15.
 */
public class HelloProtoIntegrationTest {
    private static String PROP_GETTY_URL = "gretty.httpBaseURI";
    private static String DEFAULT_BASE_URL = "http://localhost:8081/";
    private static String BASE_URL =  System.getProperty(PROP_GETTY_URL, DEFAULT_BASE_URL);
    private static String HELLO_URL = "proto";

    @Test
    public void testHello() throws Exception {
        Client client = ClientBuilder.newClient();
        client.register(ProtobufMessageBodyReader.class);
        client.register(ProtobufMessageBodyWriter.class);

        WebTarget webTarget = client.target(BASE_URL).path(HELLO_URL);
        BulletinBoardMessage response = webTarget.request(Constants.MEDIATYPE_PROTOBUF)
                .get(BulletinBoardMessage.class);

        System.out.println(response.getMsg().getData());

        assertThat(response.getMsg().getData().toStringUtf8(), is("Hello World!"));
        assertThat(response.getMsg().getTagCount(), is(2));
        assertThat(response.getMsg().getTag(0), is("Greetings"));
        assertThat(response.getMsg().getTag(1), is("FirstPrograms"));
    }
}
