diff --git a/catalog/controller.go b/catalog/controller.go index 90c406a..5e1b018 100644 --- a/catalog/controller.go +++ b/catalog/controller.go @@ -82,10 +82,10 @@ func (c *Controller) get(id string) (ThingDescription, error) { return nil, err } - //tr := ThingRegistration(td) - //now := time.Now() - //tr.Retrieved = &now - //td[wot.KeyThingRegistration] = tr + tr := ThingRegistration(td) + now := time.Now().UTC() + tr.Retrieved = &now + td[wot.KeyThingRegistration] = tr return td, nil } diff --git a/catalog/controller_test.go b/catalog/controller_test.go index 80a26a6..b45c7e1 100644 --- a/catalog/controller_test.go +++ b/catalog/controller_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "os" - "reflect" "strconv" "strings" "testing" @@ -285,16 +284,14 @@ func TestControllerListPaginate(t *testing.T) { }, } - id, err := controller.add(td) + tdCopy := make(map[string]any) + copyMap(&td, &tdCopy) + _, err := controller.add(tdCopy) if err != nil { t.Fatal("Error adding a TD:", err.Error()) } - sd, err := controller.get(id) - if err != nil { - t.Fatal("Error retrieving TD:", err.Error()) - } - addedTDs = append(addedTDs, sd) + addedTDs = append(addedTDs, td) } var list []ThingDescription @@ -324,10 +321,12 @@ func TestControllerListPaginate(t *testing.T) { } // compare added and collection - for i, sd := range list { - if !reflect.DeepEqual(addedTDs[i], sd) { - t.Fatalf("TDs listed in catalog is different with the one stored:\n Stored:\n%v\n Listed\n%v\n", - addedTDs[i], sd) + for i, li := range list { + // remove the whole registration object because it contains system-generated and dynamic values + delete(li, "registration") + if !serializedEqual(addedTDs[i], li) { + t.Fatalf("TD added in catalog is different with the one listed:\n Added:\n%v\n Listed:\n%v\n", + addedTDs[i], li) } } } diff --git a/catalog/main_test.go b/catalog/main_test.go index 9f6603e..0c77692 100644 --- a/catalog/main_test.go +++ b/catalog/main_test.go @@ -1,7 +1,7 @@ - package catalog import ( + "bytes" "encoding/json" "os" "reflect" @@ -44,6 +44,12 @@ func serializedEqual(td1 ThingDescription, td2 ThingDescription) bool { return reflect.DeepEqual(tdBytes, storedTDBytes) } +func copyMap(in, out interface{}) { + buf := new(bytes.Buffer) + json.NewEncoder(buf).Encode(in) + json.NewDecoder(buf).Decode(out) +} + func TestMain(m *testing.M) { // run tests for each storage backend for b, supported := range TestSupportedBackends {