2019-02-05 06:27:22 +09:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/go-fed/activity/streams/vocab"
|
|
|
|
"github.com/go-test/deep"
|
2019-02-06 08:26:21 +09:00
|
|
|
"net/url"
|
2019-02-05 06:27:22 +09:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serializer interface {
|
|
|
|
Serialize() (map[string]interface{}, error)
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:47:51 +09:00
|
|
|
// IsKnownResolverError returns true if it is known that an example from
|
|
|
|
// GetTestTable will trigger a JSONResolver error.
|
|
|
|
func IsKnownResolverError(t TestTable) (b bool, reason string) {
|
|
|
|
if t.name == "Example 61" {
|
|
|
|
b = true
|
|
|
|
reason = "no \"type\" property is on the root object"
|
|
|
|
} else if t.name == "Example 62" {
|
|
|
|
b = true
|
|
|
|
reason = "an unknown \"type\" property is on the root object"
|
|
|
|
} else if t.name == "Example 153" {
|
|
|
|
b = true
|
|
|
|
reason = "no \"type\" property is on the root object"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 06:27:22 +09:00
|
|
|
func TestJSONResolver(t *testing.T) {
|
2019-02-09 21:47:51 +09:00
|
|
|
for _, example := range GetTestTable() {
|
|
|
|
if skip, reason := IsKnownResolverError(example); skip {
|
|
|
|
t.Logf("Skipping table test case %q as it's known an error will be returned because %s", example.name, reason)
|
|
|
|
continue
|
|
|
|
}
|
2019-02-05 06:27:22 +09:00
|
|
|
name := example.name
|
2019-02-09 21:47:51 +09:00
|
|
|
t.Logf("Testing table test case %q", name)
|
|
|
|
ex := example.expectedJSON
|
2019-02-05 06:27:22 +09:00
|
|
|
resFn := func(s serializer) error {
|
|
|
|
m, err := s.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-09 21:47:51 +09:00
|
|
|
m["@context"] = "https://www.w3.org/ns/activitystreams"
|
2019-02-05 06:27:22 +09:00
|
|
|
actual, err := json.Marshal(m)
|
|
|
|
if diff, err := GetJSONDiff(actual, []byte(ex)); err == nil && diff != nil {
|
|
|
|
t.Errorf("%s: Serialize JSON equality is false:\n%s", name, diff)
|
|
|
|
} else if err != nil {
|
|
|
|
t.Errorf("%s: GetJSONDiff returned error: %s", name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-06 08:26:21 +09:00
|
|
|
r, err := NewJSONResolver(
|
|
|
|
func(c context.Context, x vocab.AcceptInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ActivityInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.AddInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.AnnounceInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ApplicationInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ArriveInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ArticleInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.AudioInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.BlockInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.CollectionInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.CollectionPageInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.CreateInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.DeleteInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.DislikeInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.DocumentInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.EventInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.FlagInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.FollowInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.GroupInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.IgnoreInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ImageInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.IntransitiveActivityInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.InviteInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.JoinInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.LeaveInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.LikeInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.LinkInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ListenInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.MentionInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.MoveInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.NoteInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ObjectInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.OfferInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.OrderedCollectionInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.OrderedCollectionPageInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.OrganizationInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.PageInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.PersonInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.PlaceInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ProfileInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.QuestionInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ReadInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.RejectInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.RelationshipInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.RemoveInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ServiceInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.TentativeAcceptInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.TentativeRejectInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.TombstoneInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.TravelInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.UndoInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.UpdateInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.VideoInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
func(c context.Context, x vocab.ViewInterface) error {
|
2019-02-05 06:27:22 +09:00
|
|
|
return resFn(x)
|
|
|
|
},
|
2019-02-06 08:26:21 +09:00
|
|
|
)
|
2019-02-05 06:27:22 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot create JSONResolver: %s", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal([]byte(ex), &m)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot json.Unmarshal: %s", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = r.Resolve(context.Background(), m)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot JSONResolver.Deserialize: %s", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:47:51 +09:00
|
|
|
func TestJSONResolverErrors(t *testing.T) {
|
|
|
|
for _, example := range GetTestTable() {
|
|
|
|
isError, reason := IsKnownResolverError(example)
|
|
|
|
if !isError {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := example.name
|
|
|
|
t.Logf("Testing table test case %q", name)
|
|
|
|
ex := example.expectedJSON
|
|
|
|
resFn := func(s serializer) error { return nil }
|
|
|
|
r, err := NewJSONResolver(
|
|
|
|
func(c context.Context, x vocab.AcceptInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ActivityInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.AddInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.AnnounceInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ApplicationInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ArriveInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ArticleInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.AudioInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.BlockInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.CollectionInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.CollectionPageInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.CreateInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.DeleteInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.DislikeInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.DocumentInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.EventInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.FlagInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.FollowInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.GroupInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.IgnoreInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ImageInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.IntransitiveActivityInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.InviteInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.JoinInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.LeaveInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.LikeInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.LinkInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ListenInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.MentionInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.MoveInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.NoteInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ObjectInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.OfferInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.OrderedCollectionInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.OrderedCollectionPageInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.OrganizationInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.PageInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.PersonInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.PlaceInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ProfileInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.QuestionInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ReadInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.RejectInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.RelationshipInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.RemoveInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ServiceInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.TentativeAcceptInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.TentativeRejectInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.TombstoneInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.TravelInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.UndoInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.UpdateInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.VideoInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
func(c context.Context, x vocab.ViewInterface) error {
|
|
|
|
return resFn(x)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot create JSONResolver: %s", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal([]byte(ex), &m)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot json.Unmarshal: %s", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = r.Resolve(context.Background(), m)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%s: Expected error because %s", name, reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 06:27:22 +09:00
|
|
|
func TestNulls(t *testing.T) {
|
|
|
|
const (
|
|
|
|
samIRIInboxString = "https://example.com/sam/inbox"
|
|
|
|
samIRIString = "https://example.com/sam"
|
|
|
|
noteURIString = "https://example.com/note/123"
|
|
|
|
sallyIRIString = "https://example.com/sally"
|
|
|
|
activityIRIString = "https://example.com/test/new/iri"
|
|
|
|
)
|
|
|
|
samIRIInbox, err := url.Parse(samIRIInboxString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
samIRI, err := url.Parse(samIRIString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
noteIRI, err := url.Parse(noteURIString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sallyIRI, err := url.Parse(sallyIRIString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
activityIRI, err := url.Parse(activityIRIString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-06 08:26:21 +09:00
|
|
|
noteIdProperty := NewActivityStreamsIdProperty()
|
|
|
|
noteIdProperty.SetIRI(noteIRI)
|
|
|
|
expectedNote := NewActivityStreamsNote()
|
|
|
|
expectedNote.SetId(noteIdProperty)
|
|
|
|
noteNameProperty := NewActivityStreamsNameProperty()
|
|
|
|
noteNameProperty.AppendString("A Note")
|
|
|
|
expectedNote.SetName(noteNameProperty)
|
|
|
|
noteContentProperty := NewActivityStreamsContentProperty()
|
|
|
|
noteContentProperty.AppendString("This is a simple note")
|
|
|
|
expectedNote.SetContent(noteContentProperty)
|
|
|
|
noteToProperty := NewActivityStreamsToProperty()
|
|
|
|
expectedSamActor := NewActivityStreamsPerson()
|
|
|
|
samInboxProperty := NewActivityStreamsInboxProperty()
|
|
|
|
samInboxProperty.SetIRI(samIRIInbox)
|
|
|
|
expectedSamActor.SetInbox(samInboxProperty)
|
|
|
|
samIdProperty := NewActivityStreamsIdProperty()
|
|
|
|
samIdProperty.SetIRI(samIRI)
|
|
|
|
expectedSamActor.SetId(samIdProperty)
|
|
|
|
noteToProperty.AppendPerson(expectedSamActor)
|
|
|
|
expectedNote.SetTo(noteToProperty)
|
|
|
|
expectedUpdate := NewActivityStreamsUpdate()
|
|
|
|
sallyIdProperty := NewActivityStreamsIdProperty()
|
|
|
|
sallyIdProperty.SetIRI(sallyIRI)
|
|
|
|
sallyPerson := NewActivityStreamsPerson()
|
|
|
|
sallyPerson.SetId(sallyIdProperty)
|
|
|
|
sallyActor := NewActivityStreamsActorProperty()
|
|
|
|
sallyActor.AppendPerson(sallyPerson)
|
|
|
|
expectedUpdate.SetActor(sallyActor)
|
|
|
|
summaryProperty := NewActivityStreamsSummaryProperty()
|
|
|
|
summaryProperty.AppendString("Sally updated her note")
|
|
|
|
expectedUpdate.SetSummary(summaryProperty)
|
|
|
|
updateIdProperty := NewActivityStreamsIdProperty()
|
|
|
|
updateIdProperty.SetIRI(activityIRI)
|
|
|
|
expectedUpdate.SetId(updateIdProperty)
|
|
|
|
objectNote := NewActivityStreamsObjectProperty()
|
|
|
|
objectNote.AppendNote(expectedNote)
|
|
|
|
expectedUpdate.SetObject(objectNote)
|
|
|
|
|
|
|
|
// Variable to aid in deserialization in tests
|
|
|
|
var actual serializer
|
2019-02-05 06:27:22 +09:00
|
|
|
tables := []struct {
|
2019-02-08 08:58:34 +09:00
|
|
|
name string
|
|
|
|
expected serializer
|
|
|
|
callback interface{}
|
|
|
|
input string
|
|
|
|
inputWithoutNulls string
|
2019-02-05 06:27:22 +09:00
|
|
|
}{
|
|
|
|
{
|
2019-02-06 08:55:41 +09:00
|
|
|
name: "JSON nulls are not preserved",
|
2019-02-05 06:27:22 +09:00
|
|
|
expected: expectedUpdate,
|
2019-02-06 08:26:21 +09:00
|
|
|
callback: func(c context.Context, v vocab.UpdateInterface) error {
|
|
|
|
actual = v
|
|
|
|
return nil
|
|
|
|
},
|
2019-02-05 06:27:22 +09:00
|
|
|
input: `
|
|
|
|
{
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"summary": "Sally updated her note",
|
|
|
|
"type": "Update",
|
|
|
|
"actor": "https://example.com/sally",
|
|
|
|
"id": "https://example.com/test/new/iri",
|
|
|
|
"object": {
|
|
|
|
"id": "https://example.com/note/123",
|
|
|
|
"type": "Note",
|
|
|
|
"to": {
|
|
|
|
"id": "https://example.com/sam",
|
|
|
|
"inbox": "https://example.com/sam/inbox",
|
|
|
|
"type": "Person",
|
|
|
|
"name": null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2019-02-06 08:55:41 +09:00
|
|
|
inputWithoutNulls: `
|
|
|
|
{
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"summary": "Sally updated her note",
|
|
|
|
"type": "Update",
|
|
|
|
"actor": "https://example.com/sally",
|
|
|
|
"id": "https://example.com/test/new/iri",
|
|
|
|
"object": {
|
|
|
|
"id": "https://example.com/note/123",
|
|
|
|
"type": "Note",
|
|
|
|
"to": {
|
|
|
|
"id": "https://example.com/sam",
|
|
|
|
"inbox": "https://example.com/sam/inbox",
|
|
|
|
"type": "Person"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2019-02-05 06:27:22 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, r := range tables {
|
2019-02-09 21:28:35 +09:00
|
|
|
t.Logf("Testing table test case %q", r.name)
|
2019-02-06 08:26:21 +09:00
|
|
|
res, err := NewJSONResolver(r.callback)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: cannot create resolver: %s", r.name, err)
|
|
|
|
continue
|
2019-02-05 06:27:22 +09:00
|
|
|
}
|
|
|
|
m := make(map[string]interface{})
|
2019-02-06 08:26:21 +09:00
|
|
|
err = json.Unmarshal([]byte(r.input), &m)
|
2019-02-05 06:27:22 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot json.Unmarshal: %s", r.name, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-02-06 08:26:21 +09:00
|
|
|
err = res.Resolve(context.Background(), m)
|
2019-02-05 06:27:22 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot Deserialize: %s", r.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(actual, r.expected); diff != nil {
|
|
|
|
t.Errorf("%s: Deserialize deep equal is false: %s", r.name, diff)
|
|
|
|
}
|
2019-02-06 08:26:21 +09:00
|
|
|
m, err = actual.Serialize()
|
2019-02-05 06:27:22 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot Serialize: %s", r.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m["@context"] = "https://www.w3.org/ns/activitystreams"
|
2019-02-06 08:26:21 +09:00
|
|
|
reser, err := json.Marshal(m)
|
2019-02-05 06:27:22 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Cannot json.Marshal: %s", r.name, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-02-06 08:55:41 +09:00
|
|
|
if diff, err := GetJSONDiff(reser, []byte(r.inputWithoutNulls)); err == nil && diff != nil {
|
2019-02-06 08:26:21 +09:00
|
|
|
t.Errorf("%s: Serialize JSON equality is false:\n%s", r.name, diff)
|
|
|
|
} else if err != nil {
|
|
|
|
t.Errorf("%s: GetJSONDiff returned error: %s", r.name, err)
|
|
|
|
}
|
2019-02-05 06:27:22 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 08:26:21 +09:00
|
|
|
|
2019-02-05 06:27:22 +09:00
|
|
|
func GetJSONDiff(str1, str2 []byte) ([]string, error) {
|
|
|
|
var i1 interface{}
|
|
|
|
var i2 interface{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(str1, &i1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(str2, &i2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return deep.Equal(i1, i2), nil
|
|
|
|
}
|