"github.com/appc/cni/pkg/types"
	"github.com/cloudfoundry-incubator/ducati-daemon/client"
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/models"
	"github.com/onsi/gomega/ghttp"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Client", func() {
	var (
		c           client.DaemonClient
		server      *ghttp.Server
		marshaler   *lfakes.Marshaler
		unmarshaler *lfakes.Unmarshaler

		roundTripper *fakes.RoundTripper
		httpClient   *http.Client
	)

	BeforeEach(func() {
		server = ghttp.NewServer()
		marshaler = &lfakes.Marshaler{}
		unmarshaler = &lfakes.Unmarshaler{}

		roundTripper = &fakes.RoundTripper{}
		roundTripper.RoundTripStub = http.DefaultTransport.RoundTrip

		httpClient = &http.Client{
			Transport: roundTripper,
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/handlers"
	"github.com/cloudfoundry-incubator/ducati-daemon/models"
	"github.com/cloudfoundry-incubator/ducati-daemon/store"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/pivotal-golang/lager/lagertest"
	"github.com/tedsuo/rata"
)

var _ = Describe("GET /container/:container_id", func() {
	var (
		getHandler *handlers.GetContainer
		marshaler  *lfakes.Marshaler
		logger     *lagertest.TestLogger
		datastore  *fakes.Store
	)

	BeforeEach(func() {
		marshaler = &lfakes.Marshaler{}
		marshaler.MarshalStub = json.Marshal
		datastore = &fakes.Store{}
		logger = lagertest.NewTestLogger("test")
		getHandler = &handlers.GetContainer{
			Marshaler: marshaler,
			Logger:    logger,
			Datastore: datastore,
		}

		datastore.GetStub = func(id string) (models.Container, error) {
	"net/http/httptest"

	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/handlers"
	"github.com/cloudfoundry-incubator/ducati-daemon/models"
	"github.com/pivotal-golang/lager/lagertest"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
)

var _ = Describe("ListContainers", func() {
	var dataStore *fakes.Store
	var handler *handlers.ListContainers
	var marshaler *lfakes.Marshaler
	var containers []models.Container
	var logger *lagertest.TestLogger

	BeforeEach(func() {
		dataStore = &fakes.Store{}
		marshaler = &lfakes.Marshaler{}
		marshaler.MarshalStub = json.Marshal
		logger = lagertest.NewTestLogger("test")
		handler = &handlers.ListContainers{
			Marshaler: marshaler,
			Logger:    logger,
			Datastore: dataStore,
		}

		containers = []models.Container{
	"github.com/appc/cni/pkg/types"
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/handlers"
	"github.com/cloudfoundry-incubator/ducati-daemon/ipam"
	"github.com/cloudfoundry-incubator/ducati-daemon/models"
	"github.com/pivotal-golang/lager/lagertest"
	"github.com/tedsuo/rata"
)

var _ = Describe("CNIAdd", func() {
	var (
		unmarshaler         *lfakes.Unmarshaler
		logger              *lagertest.TestLogger
		controller          *fakes.AddController
		handler             http.Handler
		request             *http.Request
		marshaler           *lfakes.Marshaler
		expectedResultBytes []byte
		payload             models.CNIAddPayload
	)

	var setPayload = func() {
		payloadBytes, err := json.Marshal(payload)
		Expect(err).NotTo(HaveOccurred())
		request.Body = ioutil.NopCloser(bytes.NewBuffer(payloadBytes))
	}

	BeforeEach(func() {
		logger = lagertest.NewTestLogger("test")
		marshaler = &lfakes.Marshaler{}
		marshaler.MarshalStub = json.Marshal
	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
)

var _ = Describe("JSON Client", func() {
	Describe("BuildAndDo", func() {
		var (
			jsonClient  client.JSONClient
			server      *ghttp.Server
			marshaler   *lfakes.Marshaler
			unmarshaler *lfakes.Unmarshaler

			roundTripper *fakes.RoundTripper
			httpClient   *http.Client

			config client.ClientConfig

			requestPayload []int
			responseResult map[string]string
		)

		BeforeEach(func() {
			server = ghttp.NewServer()
			server.AppendHandlers(ghttp.CombineHandlers(
				ghttp.VerifyRequest("POST", "/some/path"),
				ghttp.RespondWithJSONEncoded(http.StatusCreated, map[string]string{"foo": "bar"}),
			))

			marshaler = &lfakes.Marshaler{}
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"

	"github.com/cloudfoundry-incubator/ducati-daemon/fakes"
	"github.com/cloudfoundry-incubator/ducati-daemon/handlers"
	"github.com/cloudfoundry-incubator/ducati-daemon/models"
	"github.com/pivotal-golang/lager/lagertest"
	"github.com/tedsuo/rata"
)

var _ = Describe("CNIDel", func() {
	var (
		logger      *lagertest.TestLogger
		handler     http.Handler
		controller  *fakes.DelController
		request     *http.Request
		unmarshaler *lfakes.Unmarshaler
		marshaler   *lfakes.Marshaler
		payload     models.CNIDelPayload
	)

	var setPayload = func() {
		payloadBytes, err := json.Marshal(payload)
		Expect(err).NotTo(HaveOccurred())
		request.Body = ioutil.NopCloser(bytes.NewBuffer(payloadBytes))
	}

	BeforeEach(func() {
		unmarshaler = &lfakes.Unmarshaler{}
		unmarshaler.UnmarshalStub = json.Unmarshal
		marshaler = &lfakes.Marshaler{}