Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 1 | /* |
| 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 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 20 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 21 | "github.com/stretchr/testify/assert" |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 22 | "reflect" |
| 23 | "testing" |
| 24 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/db/model" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type fields struct { |
| 32 | rootProxy *model.Proxy |
| 33 | basePath string |
| 34 | } |
| 35 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 36 | func getModelProxyPathNotFound(t *testing.T) *fields { |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 37 | var modelProxy fields |
| 38 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 39 | TestProxyRoot := model.NewRoot(&voltha.Voltha{}, nil) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 40 | 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 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 45 | modelProxy.rootProxy = TestProxyRootProxy |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 46 | modelProxy.basePath = "base_path" |
| 47 | |
| 48 | return &modelProxy |
| 49 | } |
| 50 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 51 | func getModelProxyPathFound(t *testing.T) *fields { |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 52 | var modelProxy fields |
| 53 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 54 | TestProxyRoot := model.NewRoot(&voltha.Voltha{}, nil) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 55 | 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 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 60 | modelProxy.rootProxy = TestProxyRootProxy |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 61 | modelProxy.basePath = "devices" |
| 62 | |
| 63 | return &modelProxy |
| 64 | } |
| 65 | |
| 66 | func testModelProxyObject(testModelProxy *fields) *ModelProxy { |
| 67 | return &ModelProxy{ |
| 68 | rootProxy: testModelProxy.rootProxy, |
| 69 | basePath: testModelProxy.basePath, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func 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 | |
| 95 | func TestModelProxy_Get(t *testing.T) { |
| 96 | tests := []struct { |
| 97 | name string |
| 98 | fields *fields |
| 99 | wantErr error |
| 100 | }{ |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 101 | {"Get-PathNotFound", getModelProxyPathNotFound(t), status.Errorf(codes.NotFound, "data-path: base_path")}, |
| 102 | {"Get-PathFound", getModelProxyPathFound(t), nil}, |
Hardik Windlass | dbdea88 | 2019-11-05 15:07:32 +0000 | [diff] [blame] | 103 | } |
| 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 | } |