Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "testing" |
| 22 | |
| 23 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 24 | "github.com/stretchr/testify/assert" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | testDeviceId = "123145abcdef" |
| 29 | ) |
| 30 | |
| 31 | func getItemWithPath(items []YangItem, path string) (value string, ok bool) { |
| 32 | for _, item := range items { |
| 33 | if item.Path == path { |
| 34 | return item.Value, true |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return "", false |
| 39 | } |
| 40 | |
| 41 | func TestDevicePath(t *testing.T) { |
| 42 | path := getDevicePath(testDeviceId) |
| 43 | assert.Equal(t, fmt.Sprintf("/bbf-device-aggregation:devices/device[name='%s']", testDeviceId), path) |
| 44 | } |
| 45 | |
| 46 | func TestTranslateDevice(t *testing.T) { |
| 47 | olt := voltha.Device{ |
| 48 | Id: testDeviceId, |
| 49 | Root: true, |
| 50 | } |
| 51 | items := translateDevice(olt) |
| 52 | |
| 53 | val, ok := getItemWithPath(items, fmt.Sprintf("%s/type", getDevicePath(testDeviceId))) |
| 54 | assert.True(t, ok, "No type item for olt") |
| 55 | assert.Equal(t, DeviceTypeOlt, val) |
| 56 | |
| 57 | onu := voltha.Device{ |
| 58 | Id: testDeviceId, |
| 59 | Root: false, |
| 60 | } |
| 61 | items = translateDevice(onu) |
| 62 | |
| 63 | val, ok = getItemWithPath(items, fmt.Sprintf("%s/type", getDevicePath(testDeviceId))) |
| 64 | assert.True(t, ok, "No type item for onu") |
| 65 | assert.Equal(t, DeviceTypeOnu, val) |
| 66 | } |
| 67 | |
| 68 | func TestTranslateDevices(t *testing.T) { |
| 69 | devicesNum := 10 |
| 70 | |
| 71 | //Create test devices |
| 72 | devices := voltha.Devices{ |
| 73 | Items: []*voltha.Device{}, |
| 74 | } |
| 75 | |
| 76 | for i := 0; i < devicesNum; i++ { |
| 77 | devices.Items = append(devices.Items, &voltha.Device{ |
| 78 | Id: fmt.Sprintf("%d", i), |
| 79 | Root: i%2 == 0, |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | //Translate them to items |
| 84 | items := translateDevices(devices) |
| 85 | |
| 86 | //Check if the number of generated items is correct |
| 87 | singleDeviceItemsNum := len(translateDevice(*devices.Items[0])) |
| 88 | assert.Equal(t, singleDeviceItemsNum*devicesNum, len(items)) |
| 89 | |
| 90 | //Check if the content is right |
| 91 | for i := 0; i < devicesNum; i++ { |
| 92 | val, ok := getItemWithPath(items, fmt.Sprintf("%s/type", getDevicePath(devices.Items[i].Id))) |
| 93 | assert.True(t, ok, fmt.Sprintf("No type item for device %d", i)) |
| 94 | |
| 95 | if devices.Items[i].Root { |
| 96 | assert.Equal(t, DeviceTypeOlt, val) |
| 97 | } else { |
| 98 | assert.Equal(t, DeviceTypeOnu, val) |
| 99 | } |
| 100 | } |
| 101 | } |