package io.castle.client.model;

import com.google.common.collect.ImmutableMap;
import io.castle.client.internal.json.CastleGsonModel;
import org.assertj.core.api.Assertions;
import org.junit.Test;

public class CastleMessageTest {

    private CastleGsonModel model = new CastleGsonModel();

    @Test
    public void jsonSerialized() {
        // Given
        CastleMessage message = new CastleMessage("event");

        // When
        String payloadJson = model.getGson().toJson(message);

        // Then
        Assertions.assertThat(payloadJson).isEqualTo("{\"event\":\"event\"}");
    }

    @Test
    public void fullBuilderJson() {
        // Given
        CastleMessage message = CastleMessage.builder("event")
            .createdAt("2018-01-01")
            .timestamp("2018-01-01")
            .deviceToken("1234")
            .reviewId("2345")
            .properties(ImmutableMap.builder()
                .put("key", "val")
                .build())
            .userId("3456")
            .userTraits(ImmutableMap.builder()
                .put("key", "val")
                .build())
            .build();

        // When
        String payloadJson = model.getGson().toJson(message);

        // Then
        Assertions.assertThat(payloadJson).isEqualTo("{\"created_at\":\"2018-01-01\",\"timestamp\":\"2018-01-01\",\"device_token\":\"1234\",\"event\":\"event\",\"properties\":{\"key\":\"val\"},\"review_id\":\"2345\",\"user_id\":\"3456\",\"user_traits\":{\"key\":\"val\"}}");
    }

    @Test
    public void otherProperties() {
        CastleMessage message = CastleMessage.builder("event")
                .put("key", "value")
                .build();

        String payloadJson = model.getGson().toJson(message);

        Assertions.assertThat(payloadJson).isEqualTo("{\"event\":\"event\",\"key\":\"value\"}");
    }
}

@@ -412,178 +412,804 @@
 %22);%0A
-%0A        // When%0A        String payloadJson = model.getGson().toJson(message);%0A%0A        // Then%0A        Assertions.assertThat(payloadJson).isEqualTo(%22%7B%5C%22event%5C%22:%5C%22event%5C%22
+        message.setCreatedAt(%222018-01-01%22);%0A        message.setTimestamp(%222018-01-01%22);%0A        message.setDeviceToken(%221234%22);%0A        message.setReviewId(%222345%22);%0A        message.setProperties(ImmutableMap.builder()%0A                .put(%22key%22, %22val%22)%0A                .build());%0A        message.setUserId(%223456%22);%0A        message.setUserTraits(ImmutableMap.builder()%0A                .put(%22key%22, %22val%22)%0A                .build());%0A%0A        // When%0A        String payloadJson = model.getGson().toJson(message);%0A%0A        // Then%0A        Assertions.assertThat(payloadJson).isEqualTo(%22%7B%5C%22created_at%5C%22:%5C%222018-01-01%5C%22,%5C%22timestamp%5C%22:%5C%222018-01-01%5C%22,%5C%22device_token%5C%22:%5C%221234%5C%22,%5C%22event%5C%22:%5C%22event%5C%22,%5C%22properties%5C%22:%7B%5C%22key%5C%22:%5C%22val%5C%22%7D,%5C%22review_id%5C%22:%5C%222345%5C%22,%5C%22user_id%5C%22:%5C%223456%5C%22,%5C%22user_traits%5C%22:%7B%5C%22key%5C%22:%5C%22val%5C%22%7D
 %7D%22);

Expand json serialization message test