go.ligato.io/vpp-agent/v3@v3.5.0/pkg/models/item_test.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package models_test 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 "google.golang.org/protobuf/encoding/prototext" 22 "google.golang.org/protobuf/proto" 23 24 "go.ligato.io/vpp-agent/v3/pkg/models" 25 testmodel "go.ligato.io/vpp-agent/v3/pkg/models/testdata/proto" 26 ) 27 28 func TestEncode(t *testing.T) { 29 tc := setupTest(t) 30 defer tc.teardownTest() 31 32 instance := &testmodel.Basic{ 33 Name: "basic1", 34 ValueInt: -20, 35 ValueUint: 3, 36 ValueInt64: 99000000123, 37 RepeatedString: []string{"alpha", "beta", "gama"}, 38 } 39 t.Logf("instance: %#v", instance) 40 41 item, err := models.MarshalItem(instance) 42 if err != nil { 43 t.Fatalf("marshal error: %v", err) 44 } 45 t.Logf("marshalled:\n%+v", prototext.Format(item)) 46 47 tc.Expect(item.GetData().GetAny().GetTypeUrl()). 48 To(Equal("models.ligato.io/model.Basic")) 49 50 out, err := models.UnmarshalItem(item) 51 if err != nil { 52 t.Fatalf("unmarshal error: %v", err) 53 } 54 t.Logf("unmarshalled:\n%+v", prototext.Format(out)) 55 } 56 57 func TestDecode(t *testing.T) { 58 tc := setupTest(t) 59 defer tc.teardownTest() 60 61 in := &testmodel.Basic{ 62 Name: "basic1", 63 ValueInt: -20, 64 ValueUint: 3, 65 ValueInt64: 99000000123, 66 RepeatedString: []string{"alpha", "beta", "gama"}, 67 } 68 t.Logf("in: %#v", in) 69 70 item, err := models.MarshalItem(in) 71 if err != nil { 72 t.Fatalf("marshal error: %v", err) 73 } 74 t.Logf("marshalled:\n%+v", prototext.Format(item)) 75 76 tc.Expect(item.GetId().GetModel()).To(Equal("module.basic")) 77 tc.Expect(item.GetId().GetName()).To(Equal("basic1")) 78 79 tc.Expect(item.GetData().GetAny().GetTypeUrl()). 80 To(Equal("models.ligato.io/model.Basic")) 81 82 out, err := models.UnmarshalItem(item) 83 if err != nil { 84 t.Fatalf("unmarshal error: %v", err) 85 } 86 t.Logf("unmarshalled:\n%+v", prototext.Format(out)) 87 88 tc.Expect(proto.Equal(in, out)).To(BeTrue()) 89 }