blob: 2d23a705478fb70ae3d32921191bd8d8e63abef4 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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 devices
18
19import (
Shrey Baid688b4242020-07-10 20:40:10 +053020 "testing"
21
Matteo Scandolo11006992019-08-28 11:29:46 -070022 "github.com/looplab/fsm"
23 "gotest.tools/assert"
Matteo Scandolo11006992019-08-28 11:29:46 -070024)
25
26var (
27 originalNewFSM func(initial string, events []fsm.EventDesc, callbacks map[string]fsm.Callback) *fsm.FSM
28)
29
Matteo Scandolo10f965c2019-09-24 10:40:46 -070030func setUpHelpers() {
Matteo Scandolo11006992019-08-28 11:29:46 -070031 originalNewFSM = newFSM
32}
33
Matteo Scandolo10f965c2019-09-24 10:40:46 -070034func tearDownHelpers() {
Matteo Scandolo11006992019-08-28 11:29:46 -070035 newFSM = originalNewFSM
36}
37
Matteo Scandolo11006992019-08-28 11:29:46 -070038func Test_Helpers(t *testing.T) {
39
Matteo Scandolo10f965c2019-09-24 10:40:46 -070040 setUpHelpers()
41
Matteo Scandolo11006992019-08-28 11:29:46 -070042 // feedback values for the mock
43 called := 0
44 args := struct {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070045 initial string
46 events []fsm.EventDesc
Matteo Scandolo11006992019-08-28 11:29:46 -070047 callbacks map[string]fsm.Callback
48 }{}
49
50 // creating the mock function
Matteo Scandolo10f965c2019-09-24 10:40:46 -070051 mockFSM := func(initial string, events []fsm.EventDesc, callbacks map[string]fsm.Callback) *fsm.FSM {
Matteo Scandolo11006992019-08-28 11:29:46 -070052 called++
53 args.initial = initial
54 args.events = events
55 args.callbacks = callbacks
56 return fsm.NewFSM(initial, events, callbacks)
57 }
58 newFSM = mockFSM
59
60 // params for the method under test
61 cb_called := 0
62 cb := func(e *fsm.Event) {
63 cb_called++
Matteo Scandolo11006992019-08-28 11:29:46 -070064 }
65
66 // calling the method under test
67 sm := getOperStateFSM(cb)
68
69 // verify
70 assert.Equal(t, called, 1, "Expected fsm.NewFSM to have been called once, instead it was called %d", called)
71 assert.Equal(t, args.initial, "down")
72
73 assert.Equal(t, args.events[0].Name, "enable")
74 assert.Equal(t, args.events[0].Src[0], "down")
75 assert.Equal(t, args.events[0].Dst, "up")
76
77 assert.Equal(t, args.events[1].Name, "disable")
78 assert.Equal(t, args.events[1].Src[0], "up")
79 assert.Equal(t, args.events[1].Dst, "down")
80
81 // this is to test that the callback is called when the state change
Shrey Baid688b4242020-07-10 20:40:10 +053082 _ = sm.Event("enable")
Matteo Scandolo11006992019-08-28 11:29:46 -070083 assert.Equal(t, cb_called, 1)
84
Matteo Scandolo10f965c2019-09-24 10:40:46 -070085 tearDownHelpers()
86
87}