package rules

import (
	"encoding/json"
	"fmt"
	"testing"
)

func TestActionSyslogMarshal(t *testing.T) {
	action := SyslogAction{Host: "localhost", Port: 514, Proto: "udp", Name: "my logger", Template: "{{.EventId}}"}

	bytes, err := json.Marshal(action)
	if err != nil {
		t.Error(err)
	}

	if string(bytes) != `{"type":"syslog","host":"localhost","port":"514","protocol":"udp","name":"my logger","format":"","template":"{{.EventId}}"}` {
		t.Error(fmt.Sprintf("Got bad json: %s", string(bytes)))
	}
}

func TestActionSyslogUnmarshalGood(t *testing.T) {
	str := `{"type":"syslog","host":"localhost","port":"514","protocol":"udp","name":"my logger","format":"","template":"{{.EventId}}"}`

	var action SyslogAction

	err := json.Unmarshal([]byte(str), &action)
	if err != nil {
		t.Error(err)
	}

	if action.Host != "localhost" {
		t.Error(fmt.Sprintf("Got bad host: %v", action.Host))
	}

	if action.Port != 514 {
		t.Error(fmt.Sprintf("Got bad port: %v", action.Port))
	}

	if action.Proto != SyslogProtoUdp {
		t.Error(fmt.Sprintf("Got bad protocol: %v", action.Proto))
	}

	if action.Name != "my logger" {
		t.Error(fmt.Sprintf("Got bad name: %v", action.Name))
	}

	if action.Template != "{{.EventId}}" {
		t.Error(fmt.Sprintf("Got bad template: %v", action.Name))
	}
}

func TestActionSyslogUnmarshalBadType(t *testing.T) {
	str := `{"type":"http","url":"http://localhost:543/http","template":"{{.event_id}}"}`

	var action SyslogAction

	err := json.Unmarshal([]byte(str), &action)
	if err == nil {
		t.Error("Cant read json with bad action type")
	}
}

func TestActionSyslogUnmarshalBad(t *testing.T) {
	str := `Some text`

	action := new(SyslogAction)

	err := action.UnmarshalJSON([]byte(str))
	if err == nil {
		t.Error("No errow eith not a json")
	}
}

func TestActionSyslogParseInterfaceBad(t *testing.T) {
	data := make([]interface{}, 1)
	data[0] = 10

	action := new(SyslogAction)

	err := action.ParseInterface(data)

	if err == nil {
		t.Error("No error with bad interfce")
	}
}

func TestActionSyslogParseInterfaceBadNoHost(t *testing.T) {
	data := make(map[string]interface{})

	action := new(SyslogAction)

	err := action.ParseInterface(data)

	if err == nil {
		t.Error("No error when no host set")
	}
}

func TestActionSyslogParseInterfaceBadNoPort(t *testing.T) {
	data := make(map[string]interface{})
	data["host"] = "localhost"

	action := new(SyslogAction)

	err := action.ParseInterface(data)

	if err == nil {
		t.Error("No error when no host set")
	}
}

func TestActionSyslogParseInterfaceBadNoProto(t *testing.T) {
	data := make(map[string]interface{})
	data["host"] = "localhost"
	data["port"] = 134.0

	action := new(SyslogAction)

	err := action.ParseInterface(data)

	if err == nil {
		t.Error("No error when no host set")
	}
}

func TestActionSyslogParseInterfaceBadNoName(t *testing.T) {
	data := make(map[string]interface{})
	data["host"] = "localhost"
	data["port"] = 134.0
	data["protocol"] = "TCP"

	action := new(SyslogAction)

	err := action.ParseInterface(data)

	if err == nil {
		t.Error("No error when no host set")
	}
}

func TestActionSyslogParseInterfaceBadNoData(t *testing.T) {
	data := make(map[string]interface{})
	data["host"] = "localhost"
	data["port"] = 134.0
	data["protocol"] = "TCP"
	data["name"] = "ass"

	action := new(SyslogAction)

	err := action.ParseInterface(data)

	if err == nil {
		t.Error("No error when no host set")
	}
}
