blob: 492c80a3f8ddcabdf07e97663944fefd683d9f53 [file] [log] [blame]
cbabu28f80602019-10-16 11:55:09 +02001/*
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
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
27/**
28Get's the transition Map with current state of the device.
29*/
30func getTranisitions() map[Trigger]Transition {
31 transitions := make(map[Trigger]Transition)
32 transition := Transition{
33 previousState: []DeviceState{deviceStateConnected},
34 currentState: deviceStateConnected,
35 }
36 transitions[DeviceInit] = transition
37 return transitions
38}
39
40/**
41Get's the transition Map with after Transition func added.
42*/
43func getTranisitionsAfter() map[Trigger]Transition {
44 transitions := make(map[Trigger]Transition)
45 transition := Transition{
46 previousState: []DeviceState{deviceStateConnected},
47 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053048 after: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020049 return nil
npujarec5762e2020-01-01 14:08:48 +053050 }, func(ctx context.Context) error {
Thomas Lee S94109f12020-03-03 16:39:29 +053051 return olterrors.ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020052 }},
53 }
54 transitions[GrpcConnected] = transition
55 return transitions
56}
57
58/**
59Get's the transition Map with before Transition func added.
60*/
61func getTranisitionsBefore() map[Trigger]Transition {
62 transitions := make(map[Trigger]Transition)
63 transition := Transition{
64 previousState: []DeviceState{deviceStateConnected},
65 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053066 before: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020067 return nil
npujarec5762e2020-01-01 14:08:48 +053068 }, func(ctx context.Context) error {
Thomas Lee S94109f12020-03-03 16:39:29 +053069 return olterrors.ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020070 }},
71 }
72 transitions[GrpcConnected] = transition
73 return transitions
74}
75
76/**
77Check's Creation of transition Map, return's NewTransitionMap.
78*/
79func TestNewTransitionMap(t *testing.T) {
80 type args struct {
81 dh *DeviceHandler
82 }
83 tests := []struct {
84 name string
85 args args
86 want *TransitionMap
87 }{
88 {"NewTransitionMap-1", args{newMockDeviceHandler()}, &TransitionMap{}},
89 }
90 for _, tt := range tests {
91 t.Run(tt.name, func(t *testing.T) {
92 if got := NewTransitionMap(tt.args.dh); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
93 t.Errorf("NewTransitionMap() = %v, want %v", got, tt.want)
94 }
95 })
96 }
97}
98
99/**
100Checks the different transition of the device handled properly.
101*/
102func TestTransitionMap_Handle(t *testing.T) {
103 type fields struct {
104 transitions map[Trigger]Transition
105 currentDeviceState DeviceState
106 }
107 type args struct {
108 trigger Trigger
109 }
110 tests := []struct {
111 name string
112 fields fields
113 args args
114 }{
115 {"Handle-1", fields{getTranisitions(), deviceStateDown}, args{GrpcConnected}},
116 {"Handle-2", fields{getTranisitions(), deviceStateConnected}, args{GrpcConnected}},
117 {"Handle-3", fields{getTranisitionsBefore(), deviceStateConnected}, args{GrpcConnected}},
118 {"Handle-4", fields{getTranisitionsAfter(), deviceStateConnected}, args{GrpcConnected}},
119 }
120 for _, tt := range tests {
121 t.Run(tt.name, func(t *testing.T) {
122 tMap := &TransitionMap{
123 transitions: tt.fields.transitions,
124 currentDeviceState: tt.fields.currentDeviceState,
125 }
npujarec5762e2020-01-01 14:08:48 +0530126 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
127 defer cancel()
128 tMap.Handle(ctx, tt.args.trigger)
cbabu28f80602019-10-16 11:55:09 +0200129 })
130 }
131}
132
133/**
134Check's if the transition is valid or not.
135*/
136func TestTransitionMap_isValidTransition(t *testing.T) {
137 type fields struct {
138 transitions map[Trigger]Transition
139 currentDeviceState DeviceState
140 }
141 type args struct {
142 trigger Trigger
143 }
144 tests := []struct {
145 name string
146 fields fields
147 args args
148 want bool
149 }{
150 {"isValidTransition-1", fields{getTranisitions(), deviceStateConnected}, args{DeviceInit},
151 true},
152 {"isValidTransition-2", fields{getTranisitions(), deviceStateDown}, args{GrpcConnected},
153 false},
Girish Gowdra852ad912021-05-04 00:05:50 -0700154 {"isValidTransition-3", fields{getTranisitions(), deviceStateConnected}, args{DeviceDownInd},
155 false},
cbabu28f80602019-10-16 11:55:09 +0200156 }
157 for _, tt := range tests {
158 t.Run(tt.name, func(t *testing.T) {
159 tMap := &TransitionMap{
160 transitions: tt.fields.transitions,
161 currentDeviceState: tt.fields.currentDeviceState,
162 }
163 if got := tMap.isValidTransition(tt.args.trigger); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
164 t.Errorf("isValidTransition() = %v, want %v", got, tt.want)
165 }
166 })
167 }
168}
169
170/**
171Get's the After/Before transition method's function name.
172*/
173func Test_funcName(t *testing.T) {
174 type args struct {
175 f interface{}
176 }
177 tests := []struct {
178 name string
179 args args
180 want string
181 }{
182 {"FuncName-1", args{newMockDeviceHandler()}, ""},
183 }
184 for _, tt := range tests {
185 t.Run(tt.name, func(t *testing.T) {
186 if got := funcName(tt.args.f); got != tt.want {
187 t.Errorf("funcName() = %v, want %v", got, tt.want)
188 }
189 })
190 }
191}