package gost

import (
	"reflect"
	"testing"

	"github.com/future-architect/vuls/models"
	gostmodels "github.com/knqyf263/gost/models"
)

func TestSetPackageStates(t *testing.T) {
	var tests = []struct {
		pkgstats  []gostmodels.RedhatPackageState
		installed models.Packages
		release   string
		in        models.VulnInfo
		out       models.PackageFixStatuses
	}{

		//0 one
		{
			pkgstats: []gostmodels.RedhatPackageState{
				{
					FixState:    "Will not fix",
					PackageName: "bouncycastle",
					Cpe:         "cpe:/o:redhat:enterprise_linux:7",
				},
			},
			installed: models.Packages{
				"bouncycastle": models.Package{},
			},
			release: "7",
			in:      models.VulnInfo{},
			out: []models.PackageFixStatus{
				{
					Name:        "bouncycastle",
					FixState:    "Will not fix",
					NotFixedYet: true,
				},
			},
		},

		//1 two
		{
			pkgstats: []gostmodels.RedhatPackageState{
				{
					FixState:    "Will not fix",
					PackageName: "bouncycastle",
					Cpe:         "cpe:/o:redhat:enterprise_linux:7",
				},
				{
					FixState:    "Fix deferred",
					PackageName: "pack_a",
					Cpe:         "cpe:/o:redhat:enterprise_linux:7",
				},
				// ignore not-installed-package
				{
					FixState:    "Fix deferred",
					PackageName: "pack_b",
					Cpe:         "cpe:/o:redhat:enterprise_linux:7",
				},
			},
			installed: models.Packages{
				"bouncycastle": models.Package{},
				"pack_a":       models.Package{},
			},
			release: "7",
			in:      models.VulnInfo{},
			out: []models.PackageFixStatus{
				{
					Name:        "bouncycastle",
					FixState:    "Will not fix",
					NotFixedYet: true,
				},
				{
					Name:        "pack_a",
					FixState:    "Fix deferred",
					NotFixedYet: true,
				},
			},
		},

		//2 ignore affected
		{
			pkgstats: []gostmodels.RedhatPackageState{
				{
					FixState:    "affected",
					PackageName: "bouncycastle",
					Cpe:         "cpe:/o:redhat:enterprise_linux:7",
				},
			},
			installed: models.Packages{
				"bouncycastle": models.Package{},
			},
			release: "7",
			in: models.VulnInfo{
				AffectedPackages: models.PackageFixStatuses{},
			},
			out: models.PackageFixStatuses{},
		},

		//3 look only the same os release.
		{
			pkgstats: []gostmodels.RedhatPackageState{
				{
					FixState:    "Will not fix",
					PackageName: "bouncycastle",
					Cpe:         "cpe:/o:redhat:enterprise_linux:6",
				},
			},
			installed: models.Packages{
				"bouncycastle": models.Package{},
			},
			release: "7",
			in: models.VulnInfo{
				AffectedPackages: models.PackageFixStatuses{},
			},
			out: models.PackageFixStatuses{},
		},
	}

	r := RedHat{}
	for i, tt := range tests {
		out := r.mergePackageStates(tt.in, tt.pkgstats, tt.installed, tt.release)
		if ok := reflect.DeepEqual(tt.out, out); !ok {
			t.Errorf("[%d]\nexpected: %v:%T\n  actual: %v:%T\n", i, tt.out, tt.out, out, out)
		}
	}
}
