package mapping

import (
	"bytes"
	"io/ioutil"
	"os"
	"testing"
)

const (
	goodComponentVersion = 4
	goodComponent        = `{
		"template": {
			"mappings": {
				"properties": {
					"event_hash": {
						"type": "text"
					},
					"event_first": {
						"type": "date",
						"format": "date_optional_time"
					},
					"event_last": {
						"type": "date",
						"format": "date_optional_time"
					},
					"event_count": {
						"type": "long"
					},
					"@created": {
						"type": "date"
					},
					"rule_tags": {
						"type": "keyword"
					}
				}
			}
		},
		"version": 4
	}`
	goodComponentResponse = `
	{
		"component_templates" : [
		  {
			"name" : "aggregated_component",
			"component_template" : {
			  "template" : {
				"mappings" : {
				  "properties" : {
					"@created" : {
					  "type" : "date"
					},
					"rule_tags" : {
					  "type" : "keyword"
					},
					"event_last" : {
					  "format" : "date_optional_time",
					  "type" : "date"
					},
					"event_hash" : {
					  "type" : "text"
					},
					"event_count" : {
					  "type" : "long"
					},
					"event_first" : {
					  "format" : "date_optional_time",
					  "type" : "date"
					}
				  }
				}
			  },
			  "version" : 4
			}
		  }
		]
	  }`
)

func TestReadComponentWork(t *testing.T) {
	buf := bytes.NewBuffer([]byte(goodComponent))

	component, err := ReadComponent(buf)
	if err != nil {
		t.Errorf("Can't read good component: %v", err)
		return
	}

	if component.Version != goodComponentVersion {
		t.Errorf("Bad component version. Expect %v, got %v", goodComponentVersion, component.Version)
	}
}

func TestReadComponentBad(t *testing.T) {
	buf := bytes.NewBuffer(make([]byte, 1))
	_, err := ReadComponent(buf)
	if err == nil {
		t.Error("No error, when we need one")
	}
}

func TestReadComponentFromFileWork(t *testing.T) {
	file, err := ioutil.TempFile("", "component.*.json")
	if err != nil {
		t.Errorf("Can't create temp file: %v", err)
		return
	}

	n, err := file.Write([]byte(goodComponent))
	if err != nil {
		t.Errorf("Can't write temp file: %v", err)
	}

	if n != len([]byte(goodComponent)) {
		t.Errorf("Bad write count size: %v expect %v", n, len([]byte(goodComponent)))
	}

	file.Close()

	defer os.Remove(file.Name())

	component, err := ReadComponentFromFile(file.Name())
	if err != nil {
		t.Errorf("Can't read component from file: %v", err)
		return
	}

	if component.Version != goodComponentVersion {
		t.Errorf("Bad component version. Expect %v, got %v", goodComponentVersion, component.Version)
	}
}

func TestReadComponentFromFileBad(t *testing.T) {
	_, err := ReadComponentFromFile("/tmp/11111111dddddddddddddd")
	if err == nil {
		t.Error("No error when we need one")
	}
}

func TestReadComponentResponseWork(t *testing.T) {
	buf := bytes.NewBuffer([]byte(goodComponentResponse))

	response, err := ReadComponentResponse(buf)
	if err != nil {
		t.Errorf("Can't read component response: %v", err)
		return
	}

	if len(response.Templates) != 1 {
		t.Errorf("Bad response size, expect 1, got %v", len(response.Templates))
	}

	if response.Templates[0].Value.Version != goodComponentVersion {
		t.Errorf("Got bad component version, expect %v got %v", response.Templates[0].Value.Version, goodComponentVersion)
	}
}
