blob: a9caa00f2a599b2e401acb80582c1b678773428f [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"
npujar03b018e2019-11-13 15:29:36 +053020 "reflect"
21 "testing"
22
sbarbari17d7e222019-11-05 10:02:29 -050023 "github.com/opencord/voltha-go/db/model"
Hardik Windlassdbdea882019-11-05 15:07:32 +000024 "github.com/opencord/voltha-protos/v2/go/voltha"
25 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
Hardik Windlassdbdea882019-11-05 15:07:32 +000027)
28
29type fields struct {
30 rootProxy *model.Proxy
31 basePath string
32}
33
34func getModelProxyPathNotFound() *fields {
35 var modelProxy fields
36
npujar03b018e2019-11-13 15:29:36 +053037 TestProxyRoot := model.NewRoot(&voltha.Voltha{}, nil)
38 TestProxyRootProxy := TestProxyRoot.CreateProxy(context.Background(), "/", false)
39 modelProxy.rootProxy = TestProxyRootProxy
Hardik Windlassdbdea882019-11-05 15:07:32 +000040 modelProxy.basePath = "base_path"
41
42 return &modelProxy
43}
44
45func getModelProxyPathFound() *fields {
46 var modelProxy fields
47
npujar03b018e2019-11-13 15:29:36 +053048 TestProxyRoot := model.NewRoot(&voltha.Voltha{}, nil)
49 TestProxyRootProxy := TestProxyRoot.CreateProxy(context.Background(), "/", false)
50 modelProxy.rootProxy = TestProxyRootProxy
Hardik Windlassdbdea882019-11-05 15:07:32 +000051 modelProxy.basePath = "devices"
52
53 return &modelProxy
54}
55
56func testModelProxyObject(testModelProxy *fields) *ModelProxy {
57 return &ModelProxy{
58 rootProxy: testModelProxy.rootProxy,
59 basePath: testModelProxy.basePath,
60 }
61}
62
63func TestNewModelProxy(t *testing.T) {
64 type args struct {
65 basePath string
66 rootProxy *model.Proxy
67 }
68 tests := []struct {
69 name string
70 args args
71 want *ModelProxy
72 }{
73 {"NewModelProxy-1", args{"base_path", &model.Proxy{}}, &ModelProxy{}},
74 {"NewModelProxy-2", args{"/base_path", &model.Proxy{}}, &ModelProxy{}},
75 }
76 for _, tt := range tests {
77 t.Run(tt.name, func(t *testing.T) {
78 if got := newModelProxy(tt.args.basePath, tt.args.rootProxy); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
79 t.Errorf("newModelProxy() = %v, want %v", got, tt.want)
80 }
81 })
82 }
83}
84
85func TestModelProxy_Get(t *testing.T) {
86 tests := []struct {
87 name string
88 fields *fields
89 wantErr error
90 }{
91 {"Get-PathNotFound", getModelProxyPathNotFound(), status.Errorf(codes.NotFound, "data-path: base_path")},
92 {"Get-PathFound", getModelProxyPathFound(), nil},
93 }
94 for _, tt := range tests {
95 t.Run(tt.name, func(t *testing.T) {
96 ModelProxyObj := testModelProxyObject(tt.fields)
97 _, err := ModelProxyObj.Get()
98 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
99 t.Errorf("Get() error = %t, wantErr %t", err, tt.wantErr)
100 }
101 })
102 }
103}