Expand types.Entity tests
package types
import (
+ "encoding/json"
"testing"
"github.com/stretchr/testify/assert"
@@ -37,12 +38,43 @@ func TestFixtureEntityIsValid(t *testing.T) {
}
func TestEntityGet(t *testing.T) {
- e := FixtureEntity("entity")
- e.ID = "Test"
- e.ExtendedAttributes = []byte(`{}`)
+ e := FixtureEntity("myAgent")
+ e.SetExtendedAttributes([]byte(`{"foo": "bar"}`))
+
+ // Find extended attr
+ val, err := e.Get("foo")
+ require.NoError(t, err)
+ assert.EqualValues(t, "bar", val)
+
+ // Find exported field
+ val, err = e.Get("ID")
+ require.NoError(t, err)
+ assert.EqualValues(t, "myAgent", val)
+}
+
+func TestEntityUnmarshal(t *testing.T) {
+ entity := Entity{}
+
+ // Unmarshal
+ err := json.Unmarshal([]byte(`{"id": "myAgent", "foo": "bar"}`), &entity)
+ require.NoError(t, err)
+
+ // Existing exported fields were properly set
+ assert.Equal(t, "myAgent", entity.ID)
+
+ // ExtendedAttribute
+ f, err := entity.Get("foo")
+ require.NoError(t, err)
+ assert.EqualValues(t, "bar", f)
+}
+
+func TestEntityMarshal(t *testing.T) {
+ entity := FixtureEntity("myAgent")
+ entity.SetExtendedAttributes([]byte(`{"foo": "bar"}`))
- // Simple value
- val, err := e.Get("ID")
+ bytes, err := json.Marshal(entity)
require.NoError(t, err)
- assert.EqualValues(t, "Test", val)
+ assert.Contains(t, string(bytes), "myAgent")
+ assert.Contains(t, string(bytes), "foo")
+ assert.Contains(t, string(bytes), "bar")
}
