package riemann.java.client.tests;

import com.google.protobuf.ProtocolStringList;
import io.riemann.riemann.Proto;
import io.riemann.riemann.client.EventBuilder;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;

public class EventBuilderTest {

    @Test
    public void should_create_event() {
        Map<String, String> attributes = new HashMap<String, String>();
        attributes.put("network_zone", "dmz");
        List<String> tags = new ArrayList<String>();
        tags.add("fast");
        Proto.Event event = new EventBuilder()
                .host("riemann.localhost")
                .metric(42.2)
                .service("cpu_percent_usage")
                .state("fine")
                .tags("high", "backend")
                .tag("storage")
                .tags(tags)
                .description("Processor usage")
                .attribute("health", "good")
                .attribute("environment", "production")
                .attribute("datacenter", "eu_west")
                .attributes(attributes)
                .ttl(60)
                .build();

        assertEquals("cpu_percent_usage", event.getService());
        assertEquals("riemann.localhost", event.getHost());
        assertEquals("fine", event.getState());
        assertEquals(42.2, event.getMetricD(), 0D);
        ProtocolStringList eventTags = event.getTagsList();
        assertEquals(4, eventTags.size());
        assertEquals("high", eventTags.get(0));
        assertEquals("backend", eventTags.get(1));
        assertEquals("storage", eventTags.get(2));
        assertEquals("fast", eventTags.get(3));
        assertEquals("Processor usage", event.getDescription());
        List<Proto.Attribute> eventAttributes = getAttributesSortedByKey(event);
        assertEquals(4, eventAttributes.size());
        // datacenter -> eu_west
        assertEquals("datacenter", eventAttributes.get(0).getKey());
        assertEquals("eu_west", eventAttributes.get(0).getValue());
        // environment -> production
        assertEquals("environment", eventAttributes.get(1).getKey());
        assertEquals("production", eventAttributes.get(1).getValue());
        // health -> good
        assertEquals("health", eventAttributes.get(2).getKey());
        assertEquals("good", eventAttributes.get(2).getValue());
        // health -> good
        assertEquals("network_zone", eventAttributes.get(3).getKey());
        assertEquals("dmz", eventAttributes.get(3).getValue());
        assertEquals(60F, event.getTtl(), 0F);
    }

    @Test
    public void should_create_event_with_metric_float() {
        Proto.Event event = new EventBuilder()
                .metric(123.4F)
                .build();

        assertEquals(123.4F, event.getMetricF(), 0F);
    }

    @Test
    public void should_create_event_with_metric_int() {
        Proto.Event event = new EventBuilder()
                .metric(123)
                .build();

        assertEquals(123, event.getMetricSint64());
    }

    @Test
    public void should_create_event_with_metric_short() {
        Proto.Event event = new EventBuilder()
                .metric((short) 1)
                .build();

        assertEquals(1, event.getMetricSint64());
    }

    @Test
    public void should_create_event_with_metric_long() {
        Proto.Event event = new EventBuilder()
                .metric(1234567891011L)
                .build();

        assertEquals(1234567891011L, event.getMetricSint64());
    }

    @Test
    public void should_create_event_with_metric_byte() {
        Proto.Event event = new EventBuilder()
                .metric((byte) 1)
                .build();

        assertEquals(1, event.getMetricSint64());
    }

    @Test
    public void should_clear_metric() {
        EventBuilder eventBuilder = new EventBuilder();
        Proto.Event event = eventBuilder
                .host("riemann.localhost")
                .metric(123)
                .metric()
                .build();

        assertEquals(false, event.hasMetricD());
        assertEquals(false, event.hasMetricF());
        assertEquals(false, event.hasMetricSint64());
        assertEquals(0D, event.getMetricD(), 0D);
        assertEquals(0F, event.getMetricF(), 0F);
        assertEquals(0L, event.getMetricSint64(), 0L);
    }

    @Test
    public void should_clear_time() {
        EventBuilder eventBuilder = new EventBuilder();
        Proto.Event event = eventBuilder
                .host("riemann.localhost")
                .time(System.currentTimeMillis())
                .time()
                .build();

        assertEquals(false, event.hasTime());
        assertEquals(0L, event.getTime());
        assertEquals(0L, event.getTimeMicros());
    }

    @Test
    public void should_clear_ttl() {
        EventBuilder eventBuilder = new EventBuilder();
        Proto.Event event = eventBuilder
                .host("riemann.localhost")
                .ttl(20)
                .ttl()
                .build();

        assertEquals(false, event.hasTtl());
        assertEquals(0F, event.getTtl(), 0F);
    }

    private List<Proto.Attribute> getAttributesSortedByKey(Proto.Event event) {
        List<Proto.Attribute> attributes = new ArrayList<Proto.Attribute>(event.getAttributesList());
        Collections.sort(attributes, new Comparator<Proto.Attribute>() {
            @Override
            public int compare(Proto.Attribute o1, Proto.Attribute o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        return attributes;
    }
}

@@ -1842,41 +1842,8 @@
 ));%0A
-        // datacenter -%3E eu_west%0A
     
@@ -1858,16 +1858,25 @@
 rtEquals
+Attribute
 (%22datace
@@ -1881,16 +1881,27 @@
 center%22,
+ %22eu_west%22,
  eventAt
@@ -1915,25 +1915,16 @@
 s.get(0)
-.getKey()
 );%0A     
@@ -1942,67 +1942,19 @@
 uals
-(%22eu_west%22, eventAttributes.get(0).getValue());%0A        // 
+Attribute(%22
 envi
@@ -1960,20 +1960,20 @@
 ironment
- -%3E 
+%22, %22
 producti
@@ -1978,42 +1978,8 @@
 tion
-%0A        assertEquals(%22environment
 %22, e
@@ -1999,25 +1999,16 @@
 s.get(1)
-.getKey()
 );%0A     
@@ -2026,113 +2026,33 @@
 uals
-(%22production%22, eventAttributes.get(1).getValue());%0A        // health -%3E good%0A        assertEquals(%22health
+Attribute(%22health%22, %22good
 %22, e
@@ -2072,25 +2072,16 @@
 s.get(2)
-.getKey()
 );%0A     
@@ -2099,171 +2099,34 @@
 uals
-(%22good%22, eventAttributes.get(2).getValue());%0A        // health -%3E good%0A        assertEquals(%22network_zone%22, eventAttributes.get(3).getKey());%0A        assertEquals(
+Attribute(%22network_zone%22, 
 %22dmz
@@ -2154,19 +2154,8 @@
 t(3)
-.getValue()
 );%0A 
@@ -5234,11 +5234,233 @@
 s;%0A    %7D
+%0A%0A    private void assertEqualsAttribute(String expectedKey, String expectedValue, Proto.Attribute actual) %7B%0A        assertEquals(expectedKey, actual.getKey());%0A        assertEquals(expectedValue, actual.getValue());%0A    %7D
 %0A%7D%0A

Improve readability (and remove wrong code comments)