blob: 41f814c785bf258c4f806676bfdfecdac257fc23 [file] [log] [blame]
Hardik Windlassdbdea882019-11-05 15:07:32 +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 */
16package core
17
18import (
19 "context"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080020 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Thomas Lee Se5a44012019-11-07 20:32:24 +053021 "github.com/stretchr/testify/assert"
npujar03b018e2019-11-13 15:29:36 +053022 "reflect"
23 "testing"
24
sbarbari17d7e222019-11-05 10:02:29 -050025 "github.com/opencord/voltha-go/db/model"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 "github.com/opencord/voltha-protos/v3/go/voltha"
Hardik Windlassdbdea882019-11-05 15:07:32 +000027 "google.golang.org/grpc/codes"
28 "google.golang.org/grpc/status"
Hardik Windlassdbdea882019-11-05 15:07:32 +000029)
30
31type fields struct {
32 rootProxy *model.Proxy
33 basePath string
34}
35
Thomas Lee Se5a44012019-11-07 20:32:24 +053036func getModelProxyPathNotFound(t *testing.T) *fields {
Hardik Windlassdbdea882019-11-05 15:07:32 +000037 var modelProxy fields
38
npujar03b018e2019-11-13 15:29:36 +053039 TestProxyRoot := model.NewRoot(&voltha.Voltha{}, nil)
Thomas Lee Se5a44012019-11-07 20:32:24 +053040 TestProxyRootProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/", false)
41 if err != nil {
42 log.Errorw("failed-to-create-test-proxy", log.Fields{"error": err})
43 assert.NotNil(t, err)
44 }
npujar03b018e2019-11-13 15:29:36 +053045 modelProxy.rootProxy = TestProxyRootProxy
Hardik Windlassdbdea882019-11-05 15:07:32 +000046 modelProxy.basePath = "base_path"
47
48 return &modelProxy
49}
50
Thomas Lee Se5a44012019-11-07 20:32:24 +053051func getModelProxyPathFound(t *testing.T) *fields {
Hardik Windlassdbdea882019-11-05 15:07:32 +000052 var modelProxy fields
53
npujar03b018e2019-11-13 15:29:36 +053054 TestProxyRoot := model.NewRoot(&voltha.Voltha{}, nil)
Thomas Lee Se5a44012019-11-07 20:32:24 +053055 TestProxyRootProxy, err := TestProxyRoot.CreateProxy(context.Background(), "/", false)
56 if err != nil {
57 log.Errorw("failed-to-create-test-proxy", log.Fields{"error": err})
58 assert.NotNil(t, err)
59 }
npujar03b018e2019-11-13 15:29:36 +053060 modelProxy.rootProxy = TestProxyRootProxy
Hardik Windlassdbdea882019-11-05 15:07:32 +000061 modelProxy.basePath = "devices"
62
63 return &modelProxy
64}
65
66func testModelProxyObject(testModelProxy *fields) *ModelProxy {
67 return &ModelProxy{
68 rootProxy: testModelProxy.rootProxy,
69 basePath: testModelProxy.basePath,
70 }
71}
72
73func TestNewModelProxy(t *testing.T) {
74 type args struct {
75 basePath string
76 rootProxy *model.Proxy
77 }
78 tests := []struct {
79 name string
80 args args
81 want *ModelProxy
82 }{
83 {"NewModelProxy-1", args{"base_path", &model.Proxy{}}, &ModelProxy{}},
84 {"NewModelProxy-2", args{"/base_path", &model.Proxy{}}, &ModelProxy{}},
85 }
86 for _, tt := range tests {
87 t.Run(tt.name, func(t *testing.T) {
88 if got := newModelProxy(tt.args.basePath, tt.args.rootProxy); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
89 t.Errorf("newModelProxy() = %v, want %v", got, tt.want)
90 }
91 })
92 }
93}
94
95func TestModelProxy_Get(t *testing.T) {
96 tests := []struct {
97 name string
98 fields *fields
99 wantErr error
100 }{
Thomas Lee Se5a44012019-11-07 20:32:24 +0530101 {"Get-PathNotFound", getModelProxyPathNotFound(t), status.Errorf(codes.NotFound, "data-path: base_path")},
102 {"Get-PathFound", getModelProxyPathFound(t), nil},
Hardik Windlassdbdea882019-11-05 15:07:32 +0000103 }
104 for _, tt := range tests {
105 t.Run(tt.name, func(t *testing.T) {
106 ModelProxyObj := testModelProxyObject(tt.fields)
107 _, err := ModelProxyObj.Get()
108 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
109 t.Errorf("Get() error = %t, wantErr %t", err, tt.wantErr)
110 }
111 })
112 }
113}