"github.com/satori/go.uuid"
)

var _ = Describe("Handle commands", func() {

	var (
		client mqtt.Client
		app    common.App
		enID   string
		user   common.User
	)

	AssertionHandleCommands := func() {
		JustBeforeEach(func() {
			By("init mqtt client")
			c, err := util.NewClient(tConfig.MqttBroker, app, uuid.NewV4().String())
			Expect(err).To(Succeed())
			client = c
		})

		AfterEach(func() {
			if client.IsConnected() {
				client.Disconnect(0)
			}

			By("Delete endnode after test")
			// Delete Endnode
			Expect(util.DeleteThing(app, user, enID)).To(Succeed())
		})

		It("Should success", func() {
		gwID, _, err := util.GetGWID(addr, master, t)
		Expect(err).To(Succeed())
		Expect(gwID).To(Equal(mGwID))

		By("Get local token of end-node app")
		et, _, err := util.LocalAuth(addr, en, "admin_user", "admin_pass")
		Expect(err).To(Succeed())
		Expect(t).NotTo(Equal(""))

		By("Get gateway id of end-node")
		gwID, _, err = util.GetGWID(addr, en, et)
		Expect(err).To(Succeed())
		Expect(gwID).NotTo(Equal(""))

		By("Init converter for master app")
		c, err := util.NewClient(tConfig.MqttBroker, master, uuid.NewV4().String())
		client = c
		Expect(err).To(Succeed())

		By("Subscribe to receiving commands for master app")
		var (
			command common.Command
			lock    sync.Mutex
		)
		topic := fmt.Sprintf("%s/%s/e/%s/commands", master.Site, master.ID, mEnVID)
		var onRcv mqtt.MessageHandler = func(client mqtt.Client, m mqtt.Message) {
			p := m.Payload()
			defer GinkgoRecover()
			lock.Lock()
			err := json.Unmarshal(p, &command)
			lock.Unlock()