2018-10-11 09:26:00 +00:00
|
|
|
package activitystreams
|
2017-09-11 20:45:57 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-08 22:51:26 +00:00
|
|
|
"reflect"
|
2017-09-11 20:45:57 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestObjectNew(t *testing.T) {
|
2018-03-27 13:06:06 +00:00
|
|
|
var testValue = ObjectID("test")
|
2017-09-11 20:45:57 +00:00
|
|
|
var testType = ArticleType
|
|
|
|
|
2018-10-11 09:26:00 +00:00
|
|
|
o := ObjectNew(testType)
|
|
|
|
o.ID = testValue
|
2017-09-11 20:45:57 +00:00
|
|
|
|
2018-04-08 16:55:25 +00:00
|
|
|
if o.ID != testValue {
|
|
|
|
t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
|
2017-09-11 20:45:57 +00:00
|
|
|
}
|
|
|
|
if o.Type != testType {
|
2017-10-05 10:38:11 +00:00
|
|
|
t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, testType)
|
2017-09-11 20:45:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 09:26:00 +00:00
|
|
|
n := ObjectNew("")
|
|
|
|
n.ID = testValue
|
2018-04-08 16:55:25 +00:00
|
|
|
if n.ID != testValue {
|
|
|
|
t.Errorf("APObject Id '%v' different than expected '%v'", n.ID, testValue)
|
2017-09-11 20:45:57 +00:00
|
|
|
}
|
|
|
|
if n.Type != ObjectType {
|
2017-10-05 10:38:11 +00:00
|
|
|
t.Errorf("APObject Type '%v' different than expected '%v'", n.Type, ObjectType)
|
2017-09-11 20:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-16 18:38:09 +00:00
|
|
|
func TestValidGenericType(t *testing.T) {
|
2017-10-02 09:53:09 +00:00
|
|
|
for _, validType := range validGenericObjectTypes {
|
2017-09-16 18:38:09 +00:00
|
|
|
if !ValidObjectType(validType) {
|
|
|
|
t.Errorf("Generic Type '%v' should be valid", validType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:54:21 +00:00
|
|
|
func TestValidObjectType(t *testing.T) {
|
2017-10-02 09:53:09 +00:00
|
|
|
var invalidType ActivityVocabularyType = "RandomType"
|
2017-09-12 08:54:21 +00:00
|
|
|
|
|
|
|
if ValidObjectType(invalidType) {
|
2017-10-05 10:38:11 +00:00
|
|
|
t.Errorf("APObject Type '%v' should not be valid", invalidType)
|
2017-09-12 08:54:21 +00:00
|
|
|
}
|
|
|
|
for _, validType := range validObjectTypes {
|
|
|
|
if !ValidObjectType(validType) {
|
2017-10-05 10:38:11 +00:00
|
|
|
t.Errorf("APObject Type '%v' should be valid", validType)
|
2017-09-12 08:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:36:13 +00:00
|
|
|
func TestMarshalJSON(t *testing.T) {
|
2018-09-02 13:41:53 +00:00
|
|
|
m := NaturalLanguageValue{
|
|
|
|
{
|
2018-10-04 18:33:32 +00:00
|
|
|
"en", "test",
|
2018-09-02 13:41:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"de", "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result, err := m.MarshalJSON()
|
2017-09-12 20:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed marshaling '%v'", err)
|
|
|
|
}
|
2018-03-24 17:03:56 +00:00
|
|
|
mRes := "{\"de\":\"test\",\"en\":\"test\"}"
|
|
|
|
if string(result) != mRes {
|
|
|
|
t.Errorf("Different results '%v' vs. '%v'", string(result), mRes)
|
2017-09-12 20:36:13 +00:00
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
//n := NaturalLanguageValueNew()
|
|
|
|
//result, err := n.MarshalJSON()
|
2017-09-12 20:36:13 +00:00
|
|
|
|
|
|
|
s := make(map[LangRef]string)
|
|
|
|
s["en"] = "test"
|
2018-09-02 13:41:53 +00:00
|
|
|
n1 := NaturalLanguageValue{{
|
|
|
|
"en", "test",
|
|
|
|
}}
|
2017-09-12 20:36:13 +00:00
|
|
|
result1, err1 := n1.MarshalJSON()
|
|
|
|
if err1 != nil {
|
|
|
|
t.Errorf("Failed marshaling '%v'", err1)
|
|
|
|
}
|
2018-03-24 17:03:56 +00:00
|
|
|
mRes1 := "\"test\""
|
|
|
|
if string(result1) != mRes1 {
|
|
|
|
t.Errorf("Different results '%v' vs. '%v'", string(result1), mRes1)
|
2017-09-12 20:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-16 18:38:09 +00:00
|
|
|
|
|
|
|
func TestNaturalLanguageValue_MarshalJSON(t *testing.T) {
|
2018-09-02 13:41:53 +00:00
|
|
|
p := NaturalLanguageValue{
|
|
|
|
{
|
|
|
|
"en", "the test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fr", "le test",
|
|
|
|
},
|
|
|
|
}
|
2017-09-16 18:38:09 +00:00
|
|
|
js := "{\"en\":\"the test\",\"fr\":\"le test\"}"
|
|
|
|
out, err := p.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error: '%s'", err)
|
|
|
|
}
|
|
|
|
if js != string(out) {
|
|
|
|
t.Errorf("Different marshal result '%s', instead of '%s'", out, js)
|
|
|
|
}
|
2018-10-04 18:33:32 +00:00
|
|
|
p1 := NaturalLanguageValue{
|
2018-09-02 13:41:53 +00:00
|
|
|
{
|
|
|
|
"en", "the test",
|
|
|
|
},
|
|
|
|
}
|
2017-09-16 18:38:09 +00:00
|
|
|
|
|
|
|
out1, err1 := p1.MarshalJSON()
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
t.Errorf("Error: '%s'", err1)
|
|
|
|
}
|
|
|
|
txt := "\"the test\""
|
|
|
|
if txt != string(out1) {
|
|
|
|
t.Errorf("Different marshal result '%s', instead of '%s'", out1, txt)
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 09:53:09 +00:00
|
|
|
|
|
|
|
func TestObject_IsLink(t *testing.T) {
|
2018-10-11 09:26:00 +00:00
|
|
|
o := ObjectNew(ObjectType)
|
|
|
|
o.ID = "test"
|
2017-10-02 09:53:09 +00:00
|
|
|
if o.IsLink() {
|
|
|
|
t.Errorf("%#v should not be a valid link", o.Type)
|
|
|
|
}
|
2018-10-11 09:26:00 +00:00
|
|
|
m := ObjectNew(AcceptType)
|
|
|
|
m.ID = "test"
|
2017-10-02 09:53:09 +00:00
|
|
|
if m.IsLink() {
|
|
|
|
t.Errorf("%#v should not be a valid link", m.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObject_IsObject(t *testing.T) {
|
2018-10-11 09:26:00 +00:00
|
|
|
o := ObjectNew(ObjectType)
|
|
|
|
o.ID = "test"
|
2017-10-02 09:53:09 +00:00
|
|
|
if !o.IsObject() {
|
|
|
|
t.Errorf("%#v should be a valid object", o.Type)
|
|
|
|
}
|
2018-10-11 09:26:00 +00:00
|
|
|
m := ObjectNew(AcceptType)
|
|
|
|
m.ID = "test"
|
2017-10-02 09:53:09 +00:00
|
|
|
if !m.IsObject() {
|
|
|
|
t.Errorf("%#v should be a valid object", m.Type)
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 22:51:26 +00:00
|
|
|
|
2018-04-10 17:56:14 +00:00
|
|
|
func TestObjectsArr_Append(t *testing.T) {
|
2018-10-04 18:33:32 +00:00
|
|
|
d := make(ItemCollection, 0)
|
2018-04-10 17:56:14 +00:00
|
|
|
|
2018-06-09 12:05:46 +00:00
|
|
|
val := Object{ID: ObjectID("grrr")}
|
2018-04-10 17:56:14 +00:00
|
|
|
|
|
|
|
d.Append(val)
|
|
|
|
|
|
|
|
if len(d) != 1 {
|
|
|
|
t.Errorf("Objects array should have exactly an element")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(d[0], val) {
|
|
|
|
t.Errorf("First item in object array does not match %q", val.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecipientsDeduplication(t *testing.T) {
|
|
|
|
bob := PersonNew("bob")
|
|
|
|
alice := PersonNew("alice")
|
|
|
|
foo := OrganizationNew("foo")
|
|
|
|
bar := GroupNew("bar")
|
|
|
|
|
2018-10-04 18:33:32 +00:00
|
|
|
first := make(ItemCollection, 0)
|
2018-04-10 17:56:14 +00:00
|
|
|
if len(first) != 0 {
|
|
|
|
t.Errorf("Objects array should have exactly an element")
|
|
|
|
}
|
|
|
|
|
|
|
|
first.Append(bob)
|
|
|
|
first.Append(alice)
|
|
|
|
first.Append(foo)
|
|
|
|
first.Append(bar)
|
|
|
|
if len(first) != 4 {
|
|
|
|
t.Errorf("Objects array should have exactly 4(four) elements, not %d", len(first))
|
|
|
|
}
|
|
|
|
|
|
|
|
first.Append(bar)
|
|
|
|
first.Append(alice)
|
|
|
|
first.Append(foo)
|
|
|
|
first.Append(bob)
|
|
|
|
if len(first) != 8 {
|
|
|
|
t.Errorf("Objects array should have exactly 8(eight) elements, not %d", len(first))
|
|
|
|
}
|
|
|
|
|
2018-06-05 18:59:04 +00:00
|
|
|
recipientsDeduplication(&first)
|
2018-04-10 17:56:14 +00:00
|
|
|
if len(first) != 4 {
|
2018-04-10 18:09:06 +00:00
|
|
|
t.Errorf("Objects array should have exactly 4(four) elements, not %d", len(first))
|
2018-04-10 17:56:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 18:33:32 +00:00
|
|
|
second := make(ItemCollection, 0)
|
2018-04-10 18:09:06 +00:00
|
|
|
second.Append(bar)
|
|
|
|
second.Append(foo)
|
|
|
|
|
2018-06-05 18:59:04 +00:00
|
|
|
recipientsDeduplication(&first, &second)
|
2018-04-10 18:09:06 +00:00
|
|
|
if len(first) != 4 {
|
|
|
|
t.Errorf("First Objects array should have exactly 8(eight) elements, not %d", len(first))
|
|
|
|
}
|
|
|
|
if len(second) != 0 {
|
|
|
|
t.Errorf("Second Objects array should have exactly 0(zero) elements, not %d", len(second))
|
|
|
|
}
|
2018-04-10 18:12:08 +00:00
|
|
|
|
2018-06-05 18:59:04 +00:00
|
|
|
err := recipientsDeduplication(&first, &second, nil)
|
2018-04-10 18:12:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Deduplication with empty array failed")
|
|
|
|
}
|
2018-04-10 17:56:14 +00:00
|
|
|
}
|
2018-06-07 14:32:26 +00:00
|
|
|
|
2018-09-02 13:41:53 +00:00
|
|
|
func TestNaturalLanguageValue_Get(t *testing.T) {
|
|
|
|
testVal := "test"
|
|
|
|
a := NaturalLanguageValue{{NilLangRef, testVal}}
|
|
|
|
if a.Get(NilLangRef) != testVal {
|
|
|
|
t.Errorf("Invalid Get result. Expected %s received %s", testVal, a.Get(NilLangRef))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNaturalLanguageValue_Set(t *testing.T) {
|
|
|
|
testVal := "test"
|
|
|
|
a := NaturalLanguageValue{{NilLangRef, "ana are mere"}}
|
|
|
|
err := a.Set(LangRef("en"), testVal)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Received error when doing Set %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:32:26 +00:00
|
|
|
func TestNaturalLanguageValue_Append(t *testing.T) {
|
|
|
|
var a NaturalLanguageValue
|
|
|
|
|
|
|
|
if len(a) != 0 {
|
|
|
|
t.Errorf("Invalid initialization of %T. Size %d > 0 ", a, len(a))
|
|
|
|
}
|
|
|
|
langEn := LangRef("en")
|
|
|
|
valEn := "random value"
|
|
|
|
|
|
|
|
a.Append(langEn, valEn)
|
|
|
|
if len(a) != 1 {
|
|
|
|
t.Errorf("Invalid append of one element to %T. Size %d != 1", a, len(a))
|
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
if a.Get(langEn) != valEn {
|
|
|
|
t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langEn, valEn, a.Get(langEn))
|
2018-06-07 14:32:26 +00:00
|
|
|
}
|
|
|
|
langDe := LangRef("de")
|
|
|
|
valDe := "randomisch"
|
|
|
|
a.Append(langDe, valDe)
|
|
|
|
|
|
|
|
if len(a) != 2 {
|
|
|
|
t.Errorf("Invalid append of one element to %T. Size %d != 2", a, len(a))
|
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
if a.Get(langEn) != valEn {
|
|
|
|
t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langEn, valEn, a.Get(langEn))
|
2018-06-07 14:32:26 +00:00
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
if a.Get(langDe) != valDe {
|
|
|
|
t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langDe, valDe, a.Get(langDe))
|
2018-06-07 14:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLangRef_UnmarshalJSON(t *testing.T) {
|
|
|
|
lang := "en-US"
|
|
|
|
json := `"` + lang + `"`
|
|
|
|
|
|
|
|
var a LangRef
|
|
|
|
a.UnmarshalJSON([]byte(json))
|
|
|
|
|
|
|
|
if string(a) != lang {
|
|
|
|
t.Errorf("Invalid json unmarshal for %T. Expected %q, found %q", lang, lang, string(a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNaturalLanguageValue_UnmarshalFullObjectJSON(t *testing.T) {
|
|
|
|
langEn := "en-US"
|
|
|
|
valEn := "random"
|
|
|
|
langDe := "de-DE"
|
2018-08-31 11:38:15 +00:00
|
|
|
valDe := "zufällig\\n"
|
|
|
|
|
|
|
|
//m := make(map[string]string)
|
|
|
|
//m[langEn] = valEn
|
|
|
|
//m[langDe] = valDe
|
|
|
|
|
2018-06-07 14:32:26 +00:00
|
|
|
json := `{
|
|
|
|
"` + langEn + `": "` + valEn + `",
|
|
|
|
"` + langDe + `": "` + valDe + `"
|
|
|
|
}`
|
|
|
|
|
|
|
|
var a NaturalLanguageValue
|
|
|
|
a.Append(LangRef(langEn), valEn)
|
|
|
|
a.Append(LangRef(langDe), valDe)
|
|
|
|
err := a.UnmarshalJSON([]byte(json))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
for lang, val := range a {
|
2018-09-02 13:41:53 +00:00
|
|
|
if val.Ref != LangRef(langEn) && val.Ref != LangRef(langDe) {
|
2018-06-07 14:32:26 +00:00
|
|
|
t.Errorf("Invalid json unmarshal for %T. Expected lang %q or %q, found %q", a, langEn, langDe, lang)
|
|
|
|
}
|
|
|
|
|
2018-09-02 13:41:53 +00:00
|
|
|
if val.Ref == LangRef(langEn) && val.Value != valEn {
|
2018-06-07 14:32:26 +00:00
|
|
|
t.Errorf("Invalid json unmarshal for %T. Expected value %q, found %q", a, valEn, val)
|
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
if val.Ref == LangRef(langDe) && val.Value != valDe {
|
2018-06-07 14:32:26 +00:00
|
|
|
t.Errorf("Invalid json unmarshal for %T. Expected value %q, found %q", a, valDe, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|