Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / servicemgr_test.patch
1 diff --git a/src/controller/servicemgr/servicemgr_test.go b/src/controller/servicemgr/servicemgr_test.go
2 index 00d5ac2..caad3ac 100644
3 --- a/src/controller/servicemgr/servicemgr_test.go
4 +++ b/src/controller/servicemgr/servicemgr_test.go
5 @@ -18,6 +18,7 @@
6  package servicemgr
7  
8  import (
9 +       "errors"
10         "fmt"
11         "strings"
12         "testing"
13 @@ -26,6 +27,13 @@ import (
14         "common/networkhelper"
15         executorMock "controller/servicemgr/executor/mocks"
16         clientApiMock "restinterface/client/mocks"
17 +       serviceDBMock "db/bolt/service/mocks"
18 +       networkDBMock "db/bolt/network/mocks"
19 +       configureDBMock "db/bolt/configuration/mocks"
20 +       configureDB "db/bolt/configuration"
21 +       networkDB "db/bolt/network"
22 +       serviceDB "db/bolt/service"
23 +       utilMock "common/utils/mocks"
24  
25         "github.com/golang/mock/gomock"
26         //      "bytes"
27 @@ -43,6 +51,10 @@ const (
28  var (
29         paramStr         = []interface{}{"ls"}
30         paramStrWithArgs = []interface{}{"ls", "-ail"}
31 +       mockServiceDB *serviceDBMock.MockDBInterface
32 +       mockNetworkDB *networkDBMock.MockDBInterface
33 +       mockConfDB *configureDBMock.MockDBInterface
34 +       mockUtil *utilMock.MockUtilsHelper
35  )
36  
37  var (
38 @@ -101,6 +113,84 @@ func TestExecuteAppOnRemote(t *testing.T) {
39         checkError(t, err)
40  }
41  
42 +func TestGetAvailableDevices(t *testing.T) {
43 +       serviceIns := GetInstance()
44 +
45 +       ctrl := gomock.NewController(t)
46 +       defer ctrl.Finish()
47 +
48 +       createMockIns(ctrl)
49 +
50 +       t.Run("Error", func(t *testing.T){
51 +               var defaultServiceInfo []serviceDB.ServiceInfo
52 +               mockServiceDB.EXPECT().GetList().Return(defaultServiceInfo, errors.New(""))
53 +               
54 +               _, err := serviceIns.GetAuthenticatedDevices("service", "extype")
55 +               if err == nil{
56 +                       t.Error("Error should not be nil")
57 +                       return
58 +               }
59 +       })
60 +
61 +       t.Run("LengthZero", func(t *testing.T){
62 +               var defaultServiceInfo []serviceDB.ServiceInfo
63 +               mockServiceDB.EXPECT().GetList().Return(defaultServiceInfo, nil)
64 +
65 +               _, err := serviceIns.GetAuthenticatedDevices("service", "extype")
66 +               if err == nil{
67 +                       t.Error("Error should not be nil")
68 +                       return
69 +               }
70 +       })
71 +
72 +       t.Run("ServiceInfoSuccess", func(t *testing.T){
73 +               servInfo := serviceDB.ServiceInfo{
74 +                       ID:"defaultID",
75 +                       Services: []string{"defaultService"},
76 +               }
77 +               var defaultServiceInfo []serviceDB.ServiceInfo
78 +               defaultServiceInfo = append(defaultServiceInfo, servInfo)
79 +
80 +               confInfo := configureDB.Configuration{
81 +                       ID: "defaultID",
82 +                       ExecType: "defaultExecutionType",
83 +                       Platform : "defaultPlatform",
84 +               }
85 +
86 +               netInfo := networkDB.NetworkInfo{
87 +                       ID : "defaultID",
88 +                       IPv4 : []string{"192.168.1.10"},
89 +                       RTT: 2.2,
90 +               }
91 +
92 +               gomock.InOrder(
93 +                       mockServiceDB.EXPECT().GetList().Return(defaultServiceInfo, nil),
94 +                       mockConfDB.EXPECT().Get(gomock.Any()).Return(confInfo, nil),
95 +                       mockNetworkDB.EXPECT().Get(gomock.Any()).Return(netInfo, nil),
96 +                       mockUtil.EXPECT().PingDevice(gomock.Any()).Return(true),
97 +                       )
98 +
99 +               _, err := serviceIns.GetAuthenticatedDevices("defaultService", "defaultExecutionType")
100 +               if err != nil{
101 +                       t.Error("Error should not be there, received:", err.Error())
102 +                       return
103 +               }
104 +       })
105 +
106 +
107 +}
108 +
109 +func createMockIns(ctrl *gomock.Controller){
110 +       mockServiceDB = serviceDBMock.NewMockDBInterface(ctrl)
111 +       mockNetworkDB = networkDBMock.NewMockDBInterface(ctrl)
112 +       mockConfDB = configureDBMock.NewMockDBInterface(ctrl)
113 +       mockUtil = utilMock.NewMockUtilsHelper(ctrl)
114 +       util = mockUtil
115 +       confDBExecutor = mockConfDB
116 +       netDBExecutor = mockNetworkDB
117 +       servDBExecutor = mockServiceDB
118 +}
119 +
120  /**************** SERVICEMGR REST INIT TEST ***********************/
121  //func TestRestInit(t *testing.T) {
122  //     //for coverage