. "github.com/onsi/ginkgo"
	"github.com/onsi/ginkgo/extensions/table"
	commonMocks "github.com/opencontrol/compliance-masonry/lib/common/mocks"
	"github.com/opencontrol/compliance-masonry/tools/vcs/mocks"
	"github.com/stretchr/testify/assert"
)

var _ = Describe("Downloader", func() {
	Describe("Constructing a new VCEntrySDownloader", func() {
		It("should return a downloader of type VCSEntryDownloader", func() {
			downloader := NewVCSDownloader()
			assert.IsType(GinkgoT(), vcsEntryDownloader{}, downloader)
		})
	})
	Describe("Downloading Entry from VCS", func() {
		table.DescribeTable("DownloadRepo", func(err error) {
			remoteSource := new(commonMocks.RemoteSource)
			remoteSource.On("GetURL").Return("https://github.com/opencontrol/notarealrepo")
			remoteSource.On("GetRevision").Return("master")
			m := new(mocks.RepoManager)
			m.On("Clone", remoteSource.GetURL(), remoteSource.GetRevision(), ".").Return(err)
			v := vcsEntryDownloader{m}
			v.DownloadRepo(remoteSource, ".")
			m.AssertExpectations(GinkgoT())
		},
			table.Entry("No error returned", nil),
			table.Entry("An error returned", errors.New("an error")),
		)
	})
})
)

var _ = Describe("Entry", func() {
	Describe("Retrieving the config file", func() {
		table.DescribeTable("GetConfigFile", func(e Entry, expectedPath string) {
			assert.Equal(GinkgoT(), e.GetConfigFile(), expectedPath)
		},
			table.Entry("Empty / new base struct to return default", Entry{}, constants.DefaultConfigYaml),
			table.Entry("overriden config file path", Entry{Path: "samplepath"}, "samplepath"),
		)
	})
	Describe("Constructing a new VCEntrySDownloader", func() {
		It("should return a downloader of type VCSEntryDownloader", func() {
			downloader := NewVCSDownloader()
			assert.IsType(GinkgoT(), vcsEntryDownloader{}, downloader)
		})
	})
	Describe("Downloading Entry from VCS", func() {
		table.DescribeTable("DownloadEntry", func(e Entry, err error) {
			m := new(mocks.RepoManager)
			m.On("Clone", e.URL, e.Revision, ".").Return(err)
			v := vcsEntryDownloader{m}
			v.DownloadEntry(e, ".")
			m.AssertExpectations(GinkgoT())
		},
			table.Entry("No error returned", Entry{}, nil),
			table.Entry("An error returned", Entry{}, errors.New("an error")),
		)
	})
})