blob: df4e8c6bb5b4b1f1950274154573876a15e68459 [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"
Thomas Lee S94109f12020-03-03 16:39:29 +053021 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
cbabu28f80602019-10-16 11:55:09 +020022 "reflect"
23 "testing"
npujarec5762e2020-01-01 14:08:48 +053024 "time"
cbabu28f80602019-10-16 11:55:09 +020025)
26
Joey Armstrong3f0e2422023-07-05 18:25:41 -040027/*
28*
cbabu28f80602019-10-16 11:55:09 +020029Get's the transition Map with current state of the device.
30*/
31func 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 Armstrong3f0e2422023-07-05 18:25:41 -040041/*
42*
cbabu28f80602019-10-16 11:55:09 +020043Get's the transition Map with after Transition func added.
44*/
45func getTranisitionsAfter() map[Trigger]Transition {
46 transitions := make(map[Trigger]Transition)
47 transition := Transition{
48 previousState: []DeviceState{deviceStateConnected},
49 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053050 after: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020051 return nil
npujarec5762e2020-01-01 14:08:48 +053052 }, func(ctx context.Context) error {
Thomas Lee S94109f12020-03-03 16:39:29 +053053 return olterrors.ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020054 }},
55 }
56 transitions[GrpcConnected] = transition
57 return transitions
58}
59
Joey Armstrong3f0e2422023-07-05 18:25:41 -040060/*
61*
cbabu28f80602019-10-16 11:55:09 +020062Get's the transition Map with before Transition func added.
63*/
64func getTranisitionsBefore() map[Trigger]Transition {
65 transitions := make(map[Trigger]Transition)
66 transition := Transition{
67 previousState: []DeviceState{deviceStateConnected},
68 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053069 before: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020070 return nil
npujarec5762e2020-01-01 14:08:48 +053071 }, func(ctx context.Context) error {
Thomas Lee S94109f12020-03-03 16:39:29 +053072 return olterrors.ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020073 }},
74 }
75 transitions[GrpcConnected] = transition
76 return transitions
77}
78
Joey Armstrong3f0e2422023-07-05 18:25:41 -040079/*
80*
cbabu28f80602019-10-16 11:55:09 +020081Check's Creation of transition Map, return's NewTransitionMap.
82*/
83func 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 Armstrong3f0e2422023-07-05 18:25:41 -0400103/*
104*
cbabu28f80602019-10-16 11:55:09 +0200105Checks the different transition of the device handled properly.
106*/
107func 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 }
npujarec5762e2020-01-01 14:08:48 +0530131 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
132 defer cancel()
133 tMap.Handle(ctx, tt.args.trigger)
cbabu28f80602019-10-16 11:55:09 +0200134 })
135 }
136}
137
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400138/*
139*
cbabu28f80602019-10-16 11:55:09 +0200140Check's if the transition is valid or not.
141*/
142func 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 Gowdra852ad912021-05-04 00:05:50 -0700160 {"isValidTransition-3", fields{getTranisitions(), deviceStateConnected}, args{DeviceDownInd},
161 false},
cbabu28f80602019-10-16 11:55:09 +0200162 }
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 Armstrong3f0e2422023-07-05 18:25:41 -0400176/*
177*
cbabu28f80602019-10-16 11:55:09 +0200178Get's the After/Before transition method's function name.
179*/
180func 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}