package rules

import (
	"encoding/json"
	"testing"
)

func TestAssetActionType(t *testing.T) {
	var action AssetAction

	if action.GetType() != AssetActionType {
		t.Errorf("Got bad type: %v", action.GetType())
	}
}

func TestAssetActionToInterface(t *testing.T) {
	action := AssetAction{
		Name:            "Test1",
		Description:     "Test2",
		Manufacturer:    "Test3",
		Model:           "Test4",
		Ip:              "Test5",
		Os:              "Test6",
		Ports:           "Test7",
		Vulnerabilities: make([]string, 1),
		Group:           "Test9",
		AssetType:       "Test10",
		Status:          "Test11",
	}
	action.Vulnerabilities[0] = "8"

	m, err := action.ToInterface()
	if err != nil {
		t.Errorf("Can't create interface from action: %v", err)
	}

	testMap(m, "name", "Test1", t)
	testMap(m, "description", "Test2", t)
	testMap(m, "manufacturer", "Test3", t)
	testMap(m, "model", "Test4", t)
	testMap(m, "ip", "Test5", t)
	testMap(m, "os", "Test6", t)
	testMap(m, "ports", "Test7", t)

	v, ok := m["vulnerabilities"].([]interface{})
	if !ok {
		t.Errorf("Bad vulnerabilities type: %T", m["vulnerabilities"])
	}

	if len(v) != 1 {
		t.Errorf("Bad vulnerabilities len: %v", len(v))
	}

	if v[0].(int) != 8 {
		t.Errorf("Bad vulnerabilities value: %v", v[0])
	}

	testMap(m, "group", "Test9", t)
	testMap(m, "asset_type", "Test10", t)
	testMap(m, "status", "Test11", t)
}

func TestAssetActionMarshal(t *testing.T) {
	action := AssetAction{
		Name:            "Test1",
		Description:     "Test2",
		Manufacturer:    "Test3",
		Model:           "Test4",
		Ip:              "Test5",
		Os:              "Test6",
		Ports:           "Test7",
		Vulnerabilities: make([]string, 1),
		Group:           "Test9",
		AssetType:       "Test10",
		Status:          "Test11",
	}
	action.Vulnerabilities[0] = "8"

	data, err := json.Marshal(action)
	if err != nil {
		t.Errorf("Can't marshall action: %v", err)
	}

	if string(data) != `{"asset_type":"Test10","description":"Test2","group":"Test9","ip":"Test5","manufacturer":"Test3","model":"Test4","name":"Test1","os":"Test6","ports":"Test7","status":"Test11","type":"asset","vulnerabilities":[8]}` {
		t.Errorf("Got bad json: %v", string(data))
	}
}

func TestAssetActionUnmarshall(t *testing.T) {
	str := `{"asset_type":"Test10","description":"Test2","group":"Test9","ip":"Test5","manufacturer":"Test3","model":"Test4","name":"Test1","os":"Test6","ports":"Test7","status":"Test11","type":"asset","vulnerabilities":[8]}`
	var action AssetAction

	err := json.Unmarshal([]byte(str), &action)
	if err != nil {
		t.Errorf("Can't unmarshall action: %v", err)
	}

	if action.Name != "Test1" {
		t.Errorf("Got bad Name: %v", action.Name)
	}

	if action.Description != "Test2" {
		t.Errorf("Got bad Description: %v", action.Description)
	}

	if action.Manufacturer != "Test3" {
		t.Errorf("Got bad Manufacturer: %v", action.Manufacturer)
	}

	if action.Model != "Test4" {
		t.Errorf("Got bad Model: %v", action.Model)
	}

	if action.Ip != "Test5" {
		t.Errorf("Got bad Ip: %v", action.Ip)
	}

	if action.Os != "Test6" {
		t.Errorf("Got bad Os: %v", action.Os)
	}

	if action.Ports != "Test7" {
		t.Errorf("Got bad Ports: %v", action.Ports)
	}

	if len(action.Vulnerabilities) != 1 {
		t.Errorf("Got bad vulnerabilities len: %v", len(action.Vulnerabilities))
	}

	if action.Vulnerabilities[0] != "8" {
		t.Errorf("Got bad Vulnerabilities: %v", action.Vulnerabilities)
	}

	if action.Group != "Test9" {
		t.Errorf("Got bad Group: %v", action.Group)
	}

	if action.AssetType != "Test10" {
		t.Errorf("Got bad AssetType: %v", action.AssetType)
	}

	if action.Status != "Test11" {
		t.Errorf("Got bad Status: %v", action.Status)
	}
}

func TestAssetActionParseInterfaceBad1(t *testing.T) {
	var data interface{}
	action := &AssetAction{}

	err := action.ParseInterface(data)
	if err == nil {
		t.Errorf("Can parse bad interface")
	}
}

func TestAssetActionParseInterfaceBad2(t *testing.T) {
	data := make(map[string]interface{})
	action := &AssetAction{}

	err := action.ParseInterface(data)
	if err == nil {
		t.Errorf("Can parse bad interface")
	}
}

func TestAssetActionParseInterfaceBad3(t *testing.T) {
	data := make(map[string]interface{})
	action := &AssetAction{}

	data["type"] = "ass"

	err := action.ParseInterface(data)
	if err == nil {
		t.Errorf("Can parse bad interface")
	}
}

func TestAssetActionParseInterfaceBad4(t *testing.T) {
	data := make(map[string]interface{})
	action := &AssetAction{}

	data["type"] = AssetActionType

	err := action.ParseInterface(data)
	if err == nil {
		t.Errorf("Can parse bad interface")
	}
}

func TestAssetActionParseInterfaceBad5(t *testing.T) {
	data := make(map[string]interface{})
	action := &AssetAction{}

	data["type"] = AssetActionType
	data["name"] = "ASS"

	err := action.ParseInterface(data)
	if err == nil {
		t.Errorf("Can parse bad interface")
	}
}
