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