github.com/onosproject/onos-api/go@v0.10.32/onos/topo/topo_test.go (about) 1 // SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package topo 6 7 import ( 8 "bytes" 9 "github.com/stretchr/testify/assert" 10 "testing" 11 ) 12 13 func TestAspects(t *testing.T) { 14 o := &Object{} 15 16 // Test retrieval of non-existent aspects (before any aspects exist) 17 err := o.GetAspect(&TLSOptions{}) 18 assert.Error(t, err) 19 _, err = o.GetAspectBytes("onos.topo.TLSOptions") 20 assert.Error(t, err) 21 22 // Set an aspect 23 err = o.SetAspect(&Location{Lat: 1.23, Lng: 3.21}) 24 assert.NoError(t, err) 25 26 // Fetch it... 27 loc := &Location{} 28 err = o.GetAspect(loc) 29 assert.NoError(t, err) 30 assert.NotNil(t, loc) 31 assert.Equal(t, 1.23, loc.Lat) 32 assert.Equal(t, 3.21, loc.Lng) 33 34 // Set it as bytes... 35 err = o.SetAspectBytes("onos.topo.Location", bytes.NewBufferString("{\"lat\":3.14,\"lng\":6.28}").Bytes()) 36 assert.NoError(t, err) 37 38 // Fetch it... again. 39 loc = &Location{} 40 err = o.GetAspect(loc) 41 assert.NoError(t, err) 42 assert.Equal(t, 3.14, loc.Lat) 43 assert.Equal(t, 6.28, loc.Lng) 44 45 // Fetch it... as bytes... 46 b, err := o.GetAspectBytes("onos.topo.Location") 47 assert.NoError(t, err) 48 assert.Equal(t, "{\"lat\":3.14,\"lng\":6.28}", bytes.NewBuffer(b).String()) 49 50 // Test retrieval of non-existent aspects (after some aspects exist) 51 tls := &TLSOptions{} 52 err = o.GetAspect(tls) 53 assert.Error(t, err) 54 _, err = o.GetAspectBytes("onos.topo.TLSOptions") 55 assert.Error(t, err) 56 } 57 58 func TestRelationID(t *testing.T) { 59 id := RelationID("foo", "is", "bar") 60 assert.Equal(t, ID("foo-is-bar"), id) 61 62 id = MultiRelationID("foo", "implies", "bar", 42) 63 assert.Equal(t, ID("foo-implies-bar-42"), id) 64 }