package adapter

import (
	"encoding/json"
	"testing"

	"github.com/onsi/gomega"
)

func TestSys9EdgeBindingUnmarshalObject(t *testing.T) {
	g := gomega.NewWithT(t)

	var profile Sys9OEMProfile
	err := json.Unmarshal([]byte(`{"edge_binding":{"key":"value"}}`), &profile)
	g.Expect(err).ShouldNot(gomega.HaveOccurred())
	g.Expect(profile.EdgeBinding).Should(gomega.HaveKeyWithValue("key", "value"))
}

func TestSys9EdgeBindingUnmarshalJSONStringObject(t *testing.T) {
	g := gomega.NewWithT(t)

	var profile Sys9OEMProfile
	err := json.Unmarshal([]byte(`{"edge_binding":"{\"key\":\"value\"}"}`), &profile)
	g.Expect(err).ShouldNot(gomega.HaveOccurred())
	g.Expect(profile.EdgeBinding).Should(gomega.HaveKeyWithValue("key", "value"))
}

func TestSys9EdgeBindingUnmarshalPlainStringReject(t *testing.T) {
	g := gomega.NewWithT(t)

	var profile Sys9OEMProfile
	err := json.Unmarshal([]byte(`{"edge_binding":"mock"}`), &profile)
	g.Expect(err).Should(gomega.HaveOccurred())
	g.Expect(err.Error()).Should(gomega.ContainSubstring("edge_binding string must contain a JSON object"))
}
