cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 1 | /* |
Joey Armstrong | a6af152 | 2023-01-17 16:06:16 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 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 | |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 17 | package core |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 18 | |
| 19 | import ( |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 20 | "context" |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 21 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors" |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 22 | "reflect" |
| 23 | "testing" |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 24 | "time" |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 27 | /* |
| 28 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 29 | Get's the transition Map with current state of the device. |
| 30 | */ |
| 31 | func getTranisitions() map[Trigger]Transition { |
| 32 | transitions := make(map[Trigger]Transition) |
| 33 | transition := Transition{ |
| 34 | previousState: []DeviceState{deviceStateConnected}, |
| 35 | currentState: deviceStateConnected, |
| 36 | } |
| 37 | transitions[DeviceInit] = transition |
| 38 | return transitions |
| 39 | } |
| 40 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 41 | /* |
| 42 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 43 | Get's the transition Map with after Transition func added. |
| 44 | */ |
| 45 | func getTranisitionsAfter() map[Trigger]Transition { |
| 46 | transitions := make(map[Trigger]Transition) |
| 47 | transition := Transition{ |
| 48 | previousState: []DeviceState{deviceStateConnected}, |
| 49 | currentState: deviceStateConnected, |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 50 | after: []TransitionHandler{func(ctx context.Context) error { |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 51 | return nil |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 52 | }, func(ctx context.Context) error { |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 53 | return olterrors.ErrStateTransition |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 54 | }}, |
| 55 | } |
| 56 | transitions[GrpcConnected] = transition |
| 57 | return transitions |
| 58 | } |
| 59 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 60 | /* |
| 61 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 62 | Get's the transition Map with before Transition func added. |
| 63 | */ |
| 64 | func getTranisitionsBefore() map[Trigger]Transition { |
| 65 | transitions := make(map[Trigger]Transition) |
| 66 | transition := Transition{ |
| 67 | previousState: []DeviceState{deviceStateConnected}, |
| 68 | currentState: deviceStateConnected, |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 69 | before: []TransitionHandler{func(ctx context.Context) error { |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 70 | return nil |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 71 | }, func(ctx context.Context) error { |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 72 | return olterrors.ErrStateTransition |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 73 | }}, |
| 74 | } |
| 75 | transitions[GrpcConnected] = transition |
| 76 | return transitions |
| 77 | } |
| 78 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 79 | /* |
| 80 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 81 | Check's Creation of transition Map, return's NewTransitionMap. |
| 82 | */ |
| 83 | func TestNewTransitionMap(t *testing.T) { |
| 84 | type args struct { |
| 85 | dh *DeviceHandler |
| 86 | } |
| 87 | tests := []struct { |
| 88 | name string |
| 89 | args args |
| 90 | want *TransitionMap |
| 91 | }{ |
| 92 | {"NewTransitionMap-1", args{newMockDeviceHandler()}, &TransitionMap{}}, |
| 93 | } |
| 94 | for _, tt := range tests { |
| 95 | t.Run(tt.name, func(t *testing.T) { |
| 96 | if got := NewTransitionMap(tt.args.dh); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 97 | t.Errorf("NewTransitionMap() = %v, want %v", got, tt.want) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 103 | /* |
| 104 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 105 | Checks the different transition of the device handled properly. |
| 106 | */ |
| 107 | func TestTransitionMap_Handle(t *testing.T) { |
| 108 | type fields struct { |
| 109 | transitions map[Trigger]Transition |
| 110 | currentDeviceState DeviceState |
| 111 | } |
| 112 | type args struct { |
| 113 | trigger Trigger |
| 114 | } |
| 115 | tests := []struct { |
| 116 | name string |
| 117 | fields fields |
| 118 | args args |
| 119 | }{ |
| 120 | {"Handle-1", fields{getTranisitions(), deviceStateDown}, args{GrpcConnected}}, |
| 121 | {"Handle-2", fields{getTranisitions(), deviceStateConnected}, args{GrpcConnected}}, |
| 122 | {"Handle-3", fields{getTranisitionsBefore(), deviceStateConnected}, args{GrpcConnected}}, |
| 123 | {"Handle-4", fields{getTranisitionsAfter(), deviceStateConnected}, args{GrpcConnected}}, |
| 124 | } |
| 125 | for _, tt := range tests { |
| 126 | t.Run(tt.name, func(t *testing.T) { |
| 127 | tMap := &TransitionMap{ |
| 128 | transitions: tt.fields.transitions, |
| 129 | currentDeviceState: tt.fields.currentDeviceState, |
| 130 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 131 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 132 | defer cancel() |
| 133 | tMap.Handle(ctx, tt.args.trigger) |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 134 | }) |
| 135 | } |
| 136 | } |
| 137 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 138 | /* |
| 139 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 140 | Check's if the transition is valid or not. |
| 141 | */ |
| 142 | func TestTransitionMap_isValidTransition(t *testing.T) { |
| 143 | type fields struct { |
| 144 | transitions map[Trigger]Transition |
| 145 | currentDeviceState DeviceState |
| 146 | } |
| 147 | type args struct { |
| 148 | trigger Trigger |
| 149 | } |
| 150 | tests := []struct { |
| 151 | name string |
| 152 | fields fields |
| 153 | args args |
| 154 | want bool |
| 155 | }{ |
| 156 | {"isValidTransition-1", fields{getTranisitions(), deviceStateConnected}, args{DeviceInit}, |
| 157 | true}, |
| 158 | {"isValidTransition-2", fields{getTranisitions(), deviceStateDown}, args{GrpcConnected}, |
| 159 | false}, |
Girish Gowdra | 852ad91 | 2021-05-04 00:05:50 -0700 | [diff] [blame] | 160 | {"isValidTransition-3", fields{getTranisitions(), deviceStateConnected}, args{DeviceDownInd}, |
| 161 | false}, |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 162 | } |
| 163 | for _, tt := range tests { |
| 164 | t.Run(tt.name, func(t *testing.T) { |
| 165 | tMap := &TransitionMap{ |
| 166 | transitions: tt.fields.transitions, |
| 167 | currentDeviceState: tt.fields.currentDeviceState, |
| 168 | } |
| 169 | if got := tMap.isValidTransition(tt.args.trigger); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 170 | t.Errorf("isValidTransition() = %v, want %v", got, tt.want) |
| 171 | } |
| 172 | }) |
| 173 | } |
| 174 | } |
| 175 | |
Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 176 | /* |
| 177 | * |
cbabu | 28f8060 | 2019-10-16 11:55:09 +0200 | [diff] [blame] | 178 | Get's the After/Before transition method's function name. |
| 179 | */ |
| 180 | func Test_funcName(t *testing.T) { |
| 181 | type args struct { |
| 182 | f interface{} |
| 183 | } |
| 184 | tests := []struct { |
| 185 | name string |
| 186 | args args |
| 187 | want string |
| 188 | }{ |
| 189 | {"FuncName-1", args{newMockDeviceHandler()}, ""}, |
| 190 | } |
| 191 | for _, tt := range tests { |
| 192 | t.Run(tt.name, func(t *testing.T) { |
| 193 | if got := funcName(tt.args.f); got != tt.want { |
| 194 | t.Errorf("funcName() = %v, want %v", got, tt.want) |
| 195 | } |
| 196 | }) |
| 197 | } |
| 198 | } |