package node

import "testing"

var nodes = ListNodes{
	Data: []ItemNode{
		{
			GID:     212,
			GUID:    "1",
			ID:      1,
			Name:    "node_1",
			Release: "1.7_x86-64",
			Roles:   []string{"node", "controllernode"},
			Seps:    []uint64{1, 2, 3},
			Status:  "ENABLED",
			Version: "4.0.0",
		},
		{
			GID:     212,
			GUID:    "2",
			ID:      2,
			Name:    "node_2",
			Release: "",
			Roles:   []string{"node"},
			Seps:    []uint64{4, 5},
			Status:  "CREATED",
			Version: "3.8.8",
		},
		{
			GID:     212,
			GUID:    "3",
			ID:      3,
			Name:    "node_3",
			Release: "",
			Roles:   []string{"physical"},
			Seps:    []uint64{1},
			Status:  "CREATED",
			Version: "3.8.9",
		},
	},
	EntryCount: 3,
}

func TestFilterByID(t *testing.T) {
	var testId uint64 = 1
	actual := nodes.FilterByID(testId).FindOne()

	if actual.ID != testId {
		t.Fatalf("expected ID %d, found: %d", testId, actual.ID)
	}
}

func TestFilterByName(t *testing.T) {
	testName := "node_1"
	actual := nodes.FilterByName(testName).FindOne()

	if actual.Name != testName {
		t.Fatalf("expected Name '%s', found: %s", testName, actual.Name)
	}
}

func TestFilterByVersion(t *testing.T) {
	testVersion := "4.0.0"
	actual := nodes.FilterByVersion(testVersion).FindOne()

	if actual.Version != testVersion {
		t.Fatalf("expected Version '%s', found: %s", testVersion, actual.Version)
	}
}

func TestFilterByRelease(t *testing.T) {
	testRelease := "1.7_x86-64"
	actual := nodes.FilterByRelease(testRelease).FindOne()

	if actual.Release != testRelease {
		t.Fatalf("expected Release '%s', found: %s", testRelease, actual.Release)
	}
}

func TestFilterBySepID(t *testing.T) {
	var testSepId uint64 = 1
	actual := nodes.FilterBySepID(testSepId)

	if len(actual.Data) != 2 {
		t.Fatal("expected 2 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		var found bool

		for _, sep := range item.Seps {
			if sep == testSepId {
				found = true
			}
		}

		if !found {
			t.Fatalf("expected SepID %d, found seps: %v", testSepId, item.Seps)
		}
	}
}

func TestFilterByRole(t *testing.T) {
	testRole := "node"
	actual := nodes.FilterByRole(testRole)

	if len(actual.Data) != 2 {
		t.Fatal("expected 2 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		var found bool

		for _, role := range item.Roles {
			if role == testRole {
				found = true
			}
		}

		if !found {
			t.Fatalf("expected Role %s, found roles: %v", testRole, item.Roles)
		}
	}
}

func TestFilterByStatus(t *testing.T) {
	testStatus := "CREATED"
	actual := nodes.FilterByStatus(testStatus)

	if len(actual.Data) != 2 {
		t.Fatal("expected 2 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		if item.Status != testStatus {
			t.Fatalf("expected Status '%s', found: %s", testStatus, item.Status)
		}
	}
}

func TestFilterFunc(t *testing.T) {
	actual := nodes.FilterFunc(func(in ItemNode) bool {
		return len(in.Roles) > 0
	})

	if len(actual.Data) < 1 {
		t.Fatal("expected 3, found: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		if len(item.Roles) < 1 {
			t.Fatal("expected Roles to contain at least 1 element, found empty")
		}
	}
}
