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