package nodes

import (
	"encoding/json"
	"sparrow/pkg/ruleEngine"
	"strings"
	"testing"
	"text/template"
)

func TestCreateNodeByType(t *testing.T) {
	config, err := json.Marshal(&MsgTypeFilterNodeConfig{MessageTypes: []string{"POST_ATTRIBUTES_REQUEST"}})
	if err != nil {
		t.Error(err)
	}
	s, ok := CreateNodeByConfig("MsgTypeFilterNode", &ruleEngine.DefaultContext{}, string(config))
	if !ok {
		t.Fatalf("not found")
	}
	node := s.(ruleEngine.Node)

	t.Log(node)
}

func TestMQTTBrokerNode_OnMessage(t *testing.T) {
	var meteData MeteData
	tpl, err := template.New("topic").Parse(defaultTopicTemp)
	if err != nil {
		t.Error(err)
	}
	meteData.VendorId = "lijian"
	meteData.DeviceId = "deviceId"

	stringBuf := &strings.Builder{}
	if err = tpl.Execute(stringBuf, &meteData); err != nil {
		t.Error(err)
	}
	t.Log(stringBuf.String())
}
