blob: 7b6f5cff4c13e73ddc103f7d211697c82d9a5ced [file] [log] [blame]
Hardik Windlass10014042020-01-30 17:59:58 +00001/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package model
18
19import (
20 "github.com/golang/protobuf/ptypes/any"
21 "github.com/opencord/voltha-protos/v3/go/common"
22 "github.com/opencord/voltha-protos/v3/go/openflow_13"
23 "github.com/opencord/voltha-protos/v3/go/voltha"
24 "github.com/stretchr/testify/assert"
25 "reflect"
26 "testing"
27)
28
29const (
30 testValidValPorts = "ports"
31 testValidValItems = "items"
32 testInvalidVal = "invalid_val"
33 wantResultPorts = "Ports"
34 wantResultItems = "Items"
35)
36
37var (
38 TestUtilsPort = []*voltha.Port{
39 {
40 PortNo: 123,
41 Label: "test-etcd_port-0",
42 Type: voltha.Port_PON_OLT,
43 AdminState: common.AdminState_ENABLED,
44 OperStatus: common.OperStatus_ACTIVE,
45 DeviceId: "etcd_port-0-device-id",
46 Peers: []*voltha.Port_PeerPort{},
47 },
48 }
49
50 TestUtilsStats = &openflow_13.OfpFlowStats{
51 Id: 1111,
52 }
53
54 TestUtilsFlows = &openflow_13.Flows{
55 Items: []*openflow_13.OfpFlowStats{TestProxyStats},
56 }
57
58 TestUtilsDevice = &voltha.Device{
59 Id: "Config-Node-1",
60 Type: "simulated_olt",
61 Root: true,
62 ParentId: "",
63 ParentPortNo: 0,
64 Vendor: "voltha-test",
65 Model: "Modelxx",
66 HardwareVersion: "0.0.1",
67 FirmwareVersion: "0.0.1",
68 Images: &voltha.Images{},
69 SerialNumber: "1234567890",
70 VendorId: "XXBB-INC",
71 Adapter: "simulated_olt",
72 Vlan: 1234,
73 Address: &voltha.Device_HostAndPort{HostAndPort: "127.0.0.1:4321"},
74 ExtraArgs: "",
75 ProxyAddress: &voltha.Device_ProxyAddress{},
76 AdminState: voltha.AdminState_PREPROVISIONED,
77 OperStatus: common.OperStatus_ACTIVE,
78 Reason: "",
79 ConnectStatus: common.ConnectStatus_REACHABLE,
80 Custom: &any.Any{},
81 Ports: TestUtilsPort,
82 Flows: TestUtilsFlows,
83 FlowGroups: &openflow_13.FlowGroups{},
84 PmConfigs: &voltha.PmConfigs{},
85 ImageDownloads: []*voltha.ImageDownload{},
86 }
87
88 TestUtilsEmpty interface{}
89)
90
91func TestIsProtoMessage(t *testing.T) {
92 result := IsProtoMessage(TestUtilsDevice)
93 assert.True(t, result)
94
95 result1 := IsProtoMessage(TestUtilsEmpty)
96 assert.False(t, result1)
97}
98
99func TestFindOwnerType(t *testing.T) {
100 result := FindOwnerType(reflect.ValueOf(TestUtilsDevice), testValidValPorts, 0, false)
101 if result != reflect.TypeOf(&voltha.Port{}) {
102 t.Errorf("TestFindOwnerType: Case0: result: %v, expected: %v", result, reflect.TypeOf(&voltha.Port{}))
103 }
104
105 result1 := FindOwnerType(reflect.ValueOf(TestUtilsDevice), testValidValItems, 1, false)
106 if result1 != reflect.TypeOf(&openflow_13.OfpFlowStats{}) {
107 t.Errorf("TestFindOwnerType: Case1: result: %v, expected: %v", result1, reflect.TypeOf(&openflow_13.OfpFlowStats{}))
108 }
109
110 result2 := FindOwnerType(reflect.ValueOf(TestUtilsDevice), testInvalidVal, 1, false)
111 assert.Nil(t, result2)
112}
113
114func TestKeyOwner(t *testing.T) {
115 result := FindKeyOwner(TestUtilsDevice, testValidValPorts, 0)
116 if result != reflect.TypeOf(TestUtilsPort) {
117 t.Errorf("TestKeyOwner: Case0: result: %v, expected: %v", result, reflect.TypeOf(TestUtilsPort))
118 }
119
120 result1 := FindKeyOwner(TestUtilsDevice, testValidValItems, 1)
121 if result1 != reflect.TypeOf(TestUtilsFlows.Items) {
122 t.Errorf("TestKeyOwner: Case1: result: %v, expected: %v", result1, reflect.TypeOf(TestUtilsFlows.Items))
123 }
124
125 result2 := FindKeyOwner(TestUtilsDevice, testInvalidVal, 1)
126 assert.Nil(t, result2)
127}
128
129func TestGetAttributeValue(t *testing.T) {
130 result1, result2 := GetAttributeValue(TestUtilsDevice, testValidValPorts, 0)
131 assert.Equal(t, wantResultPorts, result1)
132 assert.Equal(t, reflect.ValueOf(TestUtilsPort).Index(0), result2.Index(0))
133
134 result3, _ := GetAttributeValue(TestUtilsDevice, testValidValItems, 1)
135 assert.Equal(t, wantResultItems, result3)
136
137 result4, _ := GetAttributeValue(TestUtilsDevice, testInvalidVal, 1)
138 assert.Empty(t, result4)
139}
140
141func TestGetAttributeStructure(t *testing.T) {
142 result := GetAttributeStructure(TestUtilsDevice, testValidValPorts, 0)
143 assert.Equal(t, wantResultPorts, result.Name)
144 assert.Equal(t, reflect.TypeOf(TestUtilsPort), result.Type)
145
146 result1 := GetAttributeStructure(TestUtilsDevice, testValidValItems, 1)
147 assert.Equal(t, wantResultItems, result1.Name)
148 assert.Equal(t, reflect.TypeOf(TestUtilsFlows.Items), result1.Type)
149
150 result2 := GetAttributeStructure(TestUtilsDevice, testInvalidVal, 1)
151 assert.Empty(t, result2.Name)
152 assert.Nil(t, result2.Type)
153}