blob: 4cb4218e95c404d99056deda1f8b61cb532ae2c6 [file] [log] [blame]
cbabu28f80602019-10-16 11:55:09 +02001/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
cbabu28f80602019-10-16 11:55:09 +02003
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 Bakerdbd960e2020-02-28 08:57:51 -080017package core
cbabu28f80602019-10-16 11:55:09 +020018
19import (
npujarec5762e2020-01-01 14:08:48 +053020 "context"
cbabu28f80602019-10-16 11:55:09 +020021 "reflect"
22 "testing"
npujarec5762e2020-01-01 14:08:48 +053023 "time"
Akash Kankanala041a2122024-10-16 15:49:22 +053024
25 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
cbabu28f80602019-10-16 11:55:09 +020026)
27
Joey Armstrong3f0e2422023-07-05 18:25:41 -040028/*
29*
cbabu28f80602019-10-16 11:55:09 +020030Get's the transition Map with current state of the device.
31*/
32func getTranisitions() map[Trigger]Transition {
33 transitions := make(map[Trigger]Transition)
34 transition := Transition{
35 previousState: []DeviceState{deviceStateConnected},
36 currentState: deviceStateConnected,
37 }
38 transitions[DeviceInit] = transition
39 return transitions
40}
41
Joey Armstrong3f0e2422023-07-05 18:25:41 -040042/*
43*
cbabu28f80602019-10-16 11:55:09 +020044Get's the transition Map with after Transition func added.
45*/
46func getTranisitionsAfter() map[Trigger]Transition {
47 transitions := make(map[Trigger]Transition)
48 transition := Transition{
49 previousState: []DeviceState{deviceStateConnected},
50 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053051 after: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020052 return nil
npujarec5762e2020-01-01 14:08:48 +053053 }, func(ctx context.Context) error {
Thomas Lee S94109f12020-03-03 16:39:29 +053054 return olterrors.ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020055 }},
56 }
57 transitions[GrpcConnected] = transition
58 return transitions
59}
60
Joey Armstrong3f0e2422023-07-05 18:25:41 -040061/*
62*
cbabu28f80602019-10-16 11:55:09 +020063Get's the transition Map with before Transition func added.
64*/
65func getTranisitionsBefore() map[Trigger]Transition {
66 transitions := make(map[Trigger]Transition)
67 transition := Transition{
68 previousState: []DeviceState{deviceStateConnected},
69 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053070 before: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020071 return nil
npujarec5762e2020-01-01 14:08:48 +053072 }, func(ctx context.Context) error {
Thomas Lee S94109f12020-03-03 16:39:29 +053073 return olterrors.ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020074 }},
75 }
76 transitions[GrpcConnected] = transition
77 return transitions
78}
79
Joey Armstrong3f0e2422023-07-05 18:25:41 -040080/*
81*
cbabu28f80602019-10-16 11:55:09 +020082Check's Creation of transition Map, return's NewTransitionMap.
83*/
84func TestNewTransitionMap(t *testing.T) {
85 type args struct {
86 dh *DeviceHandler
87 }
88 tests := []struct {
89 name string
90 args args
91 want *TransitionMap
92 }{
93 {"NewTransitionMap-1", args{newMockDeviceHandler()}, &TransitionMap{}},
94 }
95 for _, tt := range tests {
96 t.Run(tt.name, func(t *testing.T) {
97 if got := NewTransitionMap(tt.args.dh); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
98 t.Errorf("NewTransitionMap() = %v, want %v", got, tt.want)
99 }
100 })
101 }
102}
103
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400104/*
105*
cbabu28f80602019-10-16 11:55:09 +0200106Checks the different transition of the device handled properly.
107*/
108func TestTransitionMap_Handle(t *testing.T) {
109 type fields struct {
110 transitions map[Trigger]Transition
111 currentDeviceState DeviceState
112 }
113 type args struct {
114 trigger Trigger
115 }
116 tests := []struct {
117 name string
118 fields fields
119 args args
120 }{
121 {"Handle-1", fields{getTranisitions(), deviceStateDown}, args{GrpcConnected}},
122 {"Handle-2", fields{getTranisitions(), deviceStateConnected}, args{GrpcConnected}},
123 {"Handle-3", fields{getTranisitionsBefore(), deviceStateConnected}, args{GrpcConnected}},
124 {"Handle-4", fields{getTranisitionsAfter(), deviceStateConnected}, args{GrpcConnected}},
125 }
126 for _, tt := range tests {
127 t.Run(tt.name, func(t *testing.T) {
128 tMap := &TransitionMap{
129 transitions: tt.fields.transitions,
130 currentDeviceState: tt.fields.currentDeviceState,
131 }
npujarec5762e2020-01-01 14:08:48 +0530132 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
133 defer cancel()
134 tMap.Handle(ctx, tt.args.trigger)
cbabu28f80602019-10-16 11:55:09 +0200135 })
136 }
137}
138
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400139/*
140*
cbabu28f80602019-10-16 11:55:09 +0200141Check's if the transition is valid or not.
142*/
143func TestTransitionMap_isValidTransition(t *testing.T) {
144 type fields struct {
145 transitions map[Trigger]Transition
146 currentDeviceState DeviceState
147 }
148 type args struct {
149 trigger Trigger
150 }
151 tests := []struct {
152 name string
153 fields fields
154 args args
155 want bool
156 }{
157 {"isValidTransition-1", fields{getTranisitions(), deviceStateConnected}, args{DeviceInit},
158 true},
159 {"isValidTransition-2", fields{getTranisitions(), deviceStateDown}, args{GrpcConnected},
160 false},
Girish Gowdra852ad912021-05-04 00:05:50 -0700161 {"isValidTransition-3", fields{getTranisitions(), deviceStateConnected}, args{DeviceDownInd},
162 false},
cbabu28f80602019-10-16 11:55:09 +0200163 }
164 for _, tt := range tests {
165 t.Run(tt.name, func(t *testing.T) {
166 tMap := &TransitionMap{
167 transitions: tt.fields.transitions,
168 currentDeviceState: tt.fields.currentDeviceState,
169 }
170 if got := tMap.isValidTransition(tt.args.trigger); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
171 t.Errorf("isValidTransition() = %v, want %v", got, tt.want)
172 }
173 })
174 }
175}
176
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400177/*
178*
cbabu28f80602019-10-16 11:55:09 +0200179Get's the After/Before transition method's function name.
180*/
181func Test_funcName(t *testing.T) {
182 type args struct {
183 f interface{}
184 }
185 tests := []struct {
186 name string
187 args args
188 want string
189 }{
190 {"FuncName-1", args{newMockDeviceHandler()}, ""},
191 }
192 for _, tt := range tests {
193 t.Run(tt.name, func(t *testing.T) {
194 if got := funcName(tt.args.f); got != tt.want {
195 t.Errorf("funcName() = %v, want %v", got, tt.want)
196 }
197 })
198 }
199}