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