blob: c79aeb082bcef991b5dc22acceb7237a44ee9d45 [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
17package adaptercore
18
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"
cbabu28f80602019-10-16 11:55:09 +020024)
25
26/**
27Get's the transition Map with current state of the device.
28*/
29func 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/**
40Get's the transition Map with after Transition func added.
41*/
42func getTranisitionsAfter() map[Trigger]Transition {
43 transitions := make(map[Trigger]Transition)
44 transition := Transition{
45 previousState: []DeviceState{deviceStateConnected},
46 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053047 after: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020048 return nil
npujarec5762e2020-01-01 14:08:48 +053049 }, func(ctx context.Context) error {
David K. Bainbridge794735f2020-02-11 21:01:37 -080050 return ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020051 }},
52 }
53 transitions[GrpcConnected] = transition
54 return transitions
55}
56
57/**
58Get's the transition Map with before Transition func added.
59*/
60func getTranisitionsBefore() map[Trigger]Transition {
61 transitions := make(map[Trigger]Transition)
62 transition := Transition{
63 previousState: []DeviceState{deviceStateConnected},
64 currentState: deviceStateConnected,
npujarec5762e2020-01-01 14:08:48 +053065 before: []TransitionHandler{func(ctx context.Context) error {
cbabu28f80602019-10-16 11:55:09 +020066 return nil
npujarec5762e2020-01-01 14:08:48 +053067 }, func(ctx context.Context) error {
David K. Bainbridge794735f2020-02-11 21:01:37 -080068 return ErrStateTransition
cbabu28f80602019-10-16 11:55:09 +020069 }},
70 }
71 transitions[GrpcConnected] = transition
72 return transitions
73}
74
75/**
76Check's Creation of transition Map, return's NewTransitionMap.
77*/
78func 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/**
99Checks the different transition of the device handled properly.
100*/
101func 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 }
npujarec5762e2020-01-01 14:08:48 +0530125 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
126 defer cancel()
127 tMap.Handle(ctx, tt.args.trigger)
cbabu28f80602019-10-16 11:55:09 +0200128 })
129 }
130}
131
132/**
133Check's if the transition is valid or not.
134*/
135func 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/**
168Get's the After/Before transition method's function name.
169*/
170func 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}