package matcher_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pivotal-cf-experimental/opentsdb-firehose-nozzle/matcher"
)

var _ = Describe("matchers", func() {
	var beContainedIn *matcher.BeContainedInMatcher
	BeforeEach(func() {
		beContainedIn = matcher.BeContainedIn("a", "b", "c", "2")
	})

	var _ = Describe("Match", func() {
		It("compares actual to each Element", func() {
			for _, elem := range []string{"a", "b", "c"} {
				match, err := beContainedIn.Match(elem)
				Expect(err).ToNot(HaveOccurred())
				Expect(match).To(BeTrue())
			}
		})
		It("returns an error when actual is not contained", func() {
			match, err := beContainedIn.Match("d")
			Expect(err).ToNot(HaveOccurred())
			Expect(match).To(BeFalse())

		})
		It("works if actual is not a string and respects type", func() {
			match, err := beContainedIn.Match(2)
			Expect(err).ToNot(HaveOccurred())
	It("emits internal metrics with the correct tags", func() {
		err := c.PostMetrics()
		Expect(err).ToNot(HaveOccurred())

		var receivedBytes []byte
		Eventually(bodyChan).Should(Receive(&receivedBytes))

		var metrics []poster.Metric
		err = json.Unmarshal(util.UnzipIgnoreError(receivedBytes), &metrics)
		Expect(err).NotTo(HaveOccurred())
		Expect(metrics).To(HaveLen(3))

		for _, metric := range metrics {
			Expect(metric.Metric).To(matcher.BeContainedIn("opentsdb.nozzle.totalMessagesReceived",
				"opentsdb.nozzle.totalMetricsSent",
				"opentsdb.nozzle.slowConsumerAlert"))
			Expect(metric.Tags).To(Equal(poster.Tags{
				Deployment: "test-deployment",
				Job:        "test-job",
				Index:      "SOME-GUID",
				IP:         "dummy-ip",
			}))
		}
	})

	It("posts ValueMetrics in JSON format", func() {
		c = opentsdbclient.New(p, "", "test-deployment", "test-job", "SOMETHING-IRRELEVANT", "dummy-ip")

		c.AddMetric(&events.Envelope{
			Origin:    proto.String("origin"),