Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / patches / orchestrationapi_test.patch
1 diff --git a/src/orchestrationapi/orchestrationapi_test.go b/src/orchestrationapi/orchestrationapi_test.go
2 index e1eda62..c875f8a 100644
3 --- a/src/orchestrationapi/orchestrationapi_test.go
4 +++ b/src/orchestrationapi/orchestrationapi_test.go
5 @@ -141,3 +141,316 @@ func TestRequestService(t *testing.T) {
6                 })
7         })
8  }
9 +
10 +func TestRequestServiceOnDevice(t *testing.T) {
11 +       ctrl := gomock.NewController(t)
12 +       defer ctrl.Finish()
13 +
14 +       createMockIns(ctrl)
15 +
16 +       appName := "MyApp"
17 +       args := []string{"a", "-b", "-c"}
18 +
19 +       var requestServiceInfo ReqeustService
20 +       requestServiceInfo.ServiceName = appName
21 +       requestServiceInfo.SelfSelection = true
22 +       requestServiceInfo.ServiceRequester = "my"
23 +       requestServiceInfo.ServiceInfo = []RequestServiceInfo{
24 +               {
25 +                       ExecutionType: "platform",
26 +                       ExeCmd:        args,
27 +               },
28 +       }
29 +
30 +       requestervalidator.RequesterValidator{}.StoreRequesterInfo(appName, []string{"my"})
31 +
32 +       candidateInfos := make([]dbhelper.ExecutionCandidate, 0)
33 +       candidateInfos = append(candidateInfos, dbhelper.ExecutionCandidate{
34 +               Id:       "ID1",
35 +               ExecType: "platform",
36 +               Endpoint: []string{"endpoint1"},
37 +       })
38 +       candidateInfos = append(candidateInfos, dbhelper.ExecutionCandidate{
39 +               Id:       "ID2",
40 +               ExecType: "platform",
41 +               Endpoint: []string{"endpoint2"},
42 +       })
43 +       candidateInfos = append(candidateInfos, dbhelper.ExecutionCandidate{
44 +               Id:       "ID3",
45 +               ExecType: "platform",
46 +               Endpoint: []string{"endpoint3"},
47 +       })
48 +
49 +
50 +       t.Run("Success", func(t *testing.T) {
51 +
52 +               gomock.InOrder(
53 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
54 +                       mockNetwork.EXPECT().GetIPs().Return([]string{""}, nil),
55 +                       mockService.EXPECT().Execute(gomock.Any(), appName, gomock.Any(), gomock.Any(), gomock.Any()),
56 +               )
57 +
58 +               o := getOcheIns(ctrl)
59 +               if o == nil {
60 +                       t.Error("ochestration object is nil, expected is not nil")
61 +               }
62 +
63 +               oche := getOrcheImple()
64 +
65 +               oche.Ready = true
66 +
67 +               res := oche.RequestServiceOnDevice(requestServiceInfo,"192.168.1.1")
68 +               if res.Message != ERROR_NONE {
69 +                       t.Error("unexpected handle")
70 +               }
71 +       })
72 +
73 +       t.Run("Localhost", func(t *testing.T) {
74 +
75 +               gomock.InOrder(
76 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
77 +                       mockNetwork.EXPECT().GetIPs().Return([]string{"192.168.1.1"}, nil),
78 +                       mockService.EXPECT().Execute(gomock.Any(), appName, gomock.Any(), gomock.Any(), gomock.Any()),
79 +               )
80 +
81 +               o := getOcheIns(ctrl)
82 +               if o == nil {
83 +                       t.Error("ochestration object is nil, expected is not nil")
84 +               }
85 +
86 +               oche := getOrcheImple()
87 +
88 +               oche.Ready = true
89 +
90 +               res := oche.RequestServiceOnDevice(requestServiceInfo,"192.168.1.1")
91 +               if res.Message != ERROR_NONE {
92 +                       t.Error("unexpected handle")
93 +               }
94 +       })
95 +
96 +       t.Run("Error", func(t *testing.T) {
97 +               t.Run("NotReady", func(t *testing.T) {
98 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor)
99 +                       o := getOcheIns(ctrl)
100 +                       if o == nil {
101 +                               t.Error("ochestration object is nil, expected is not nil")
102 +                       }
103 +
104 +                       oche := getOrcheImple()
105 +                       res := oche.RequestService(requestServiceInfo)
106 +                       if res.Message != INTERNAL_SERVER_ERROR {
107 +                               t.Error("unexpected Error")
108 +                       }
109 +               })
110 +               t.Run("DiscoveryFail", func(t *testing.T) {
111 +                       gomock.InOrder(
112 +                               mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
113 +                               mockDBHelper.EXPECT().GetDeviceInfoWithService(gomock.Eq(appName), gomock.Any()).Return(nil, errors.New("-3")),
114 +                       )
115 +                       o := getOcheIns(ctrl)
116 +                       if o == nil {
117 +                               t.Error("ochestration object is nil, expected is not nil")
118 +                       }
119 +
120 +                       oche := getOrcheImple()
121 +
122 +                       oche.Ready = true
123 +
124 +                       res := oche.RequestService(requestServiceInfo)
125 +                       if res.Message == ERROR_NONE {
126 +                               t.Error("unexpected Error")
127 +                       }
128 +               })
129 +       })
130 +}
131 +
132 +func TestGetAuthenticatedDeviceList(t *testing.T) {
133 +       ctrl := gomock.NewController(t)
134 +       defer ctrl.Finish()
135 +
136 +       createMockIns(ctrl)
137 +
138 +       t.Run("Success", func(t *testing.T){
139 +               var dummy[] string
140 +               gomock.InOrder(
141 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
142 +                       mockService.EXPECT().GetAuthenticatedDevices(gomock.Any(),gomock.Any()).Return(dummy,nil),
143 +               )
144 +               o := getOcheIns(ctrl)
145 +               if o == nil {
146 +                       t.Error("ochestration object is nil, expected is not nil")
147 +               }
148 +
149 +               oche := getOrcheImple()
150 +
151 +               oche.Ready = true
152 +               _, err := oche.GetAuthenticatedDeviceList("ServiceName","ExType")
153 +               if err != nil {
154 +                       t.Error("Err should be null")
155 +               }
156 +       })
157 +
158 +       t.Run("Faliure", func(t *testing.T){
159 +               var dummy[] string
160 +               gomock.InOrder(
161 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
162 +                       mockService.EXPECT().GetAuthenticatedDevices(gomock.Any(),gomock.Any()).Return(dummy,errors.New("")),
163 +               )
164 +               o := getOcheIns(ctrl)
165 +               if o == nil {
166 +                       t.Error("ochestration object is nil, expected is not nil")
167 +               }
168 +
169 +               oche := getOrcheImple()
170 +
171 +               oche.Ready = true
172 +               _, err := oche.GetAuthenticatedDeviceList("ServiceName","ExType")
173 +               if err == nil {
174 +                       t.Error("Err should be null")
175 +               }
176 +       })
177 +       t.Run("OrcFaliure", func(t *testing.T){
178 +               gomock.InOrder(
179 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
180 +               )
181 +               o := getOcheIns(ctrl)
182 +               if o == nil {
183 +                       t.Error("ochestration object is nil, expected is not nil")
184 +               }
185 +
186 +               oche := getOrcheImple()
187 +
188 +               oche.Ready = false
189 +               _, err := oche.GetAuthenticatedDeviceList("ServiceName","ExType")
190 +               if err == nil {
191 +                       t.Error("Err should be null")
192 +               }
193 +       })
194 +}
195 +
196 +func TestWriteCapability(t *testing.T) {
197 +       ctrl := gomock.NewController(t)
198 +       defer ctrl.Finish()
199 +
200 +       createMockIns(ctrl)
201 +       t.Run("Success", func(t *testing.T){
202 +               gomock.InOrder(
203 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
204 +                       mockCapability.EXPECT().WriteCapability(gomock.Any()).Return(nil),
205 +               )
206 +               o := getOcheIns(ctrl)
207 +               if o == nil {
208 +                       t.Error("ochestration object is nil, expected is not nil")
209 +               }
210 +
211 +               oche := getOrcheImple()
212 +
213 +               oche.Ready = true
214 +               err := oche.WriteCapability("DefaultCapability")
215 +               if err != nil {
216 +                       t.Error("Err should be null")
217 +               }
218 +       })
219 +
220 +       t.Run("Faliure", func(t *testing.T){
221 +               gomock.InOrder(
222 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
223 +                       mockCapability.EXPECT().WriteCapability(gomock.Any()).Return(errors.New("")),
224 +               )
225 +               o := getOcheIns(ctrl)
226 +               if o == nil {
227 +                       t.Error("ochestration object is nil, expected is not nil")
228 +               }
229 +
230 +               oche := getOrcheImple()
231 +
232 +               oche.Ready = true
233 +               err := oche.WriteCapability("DefaultCapability")
234 +               if err == nil {
235 +                       t.Error("Err should be null")
236 +               }
237 +
238 +               t.Run("OrchFaliure", func(t *testing.T){
239 +                       gomock.InOrder(
240 +                               mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
241 +                       )
242 +                       o := getOcheIns(ctrl)
243 +                       if o == nil {
244 +                               t.Error("ochestration object is nil, expected is not nil")
245 +                       }
246 +
247 +                       oche := getOrcheImple()
248 +
249 +                       oche.Ready = false
250 +                       err := oche.WriteCapability("DefaultCapability")
251 +                       if err == nil {
252 +                               t.Error("Err should be null")
253 +                       }
254 +               })
255 +       })
256 +}
257 +
258 +func TestReadCapability(t *testing.T) {
259 +       ctrl := gomock.NewController(t)
260 +       defer ctrl.Finish()
261 +
262 +       createMockIns(ctrl)
263 +       t.Run("Success", func(t *testing.T){
264 +               var dummy string
265 +               gomock.InOrder(
266 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
267 +                       mockCapability.EXPECT().ReadCapability(gomock.Any()).Return(dummy,nil),
268 +               )
269 +               o := getOcheIns(ctrl)
270 +               if o == nil {
271 +                       t.Error("ochestration object is nil, expected is not nil")
272 +               }
273 +
274 +               oche := getOrcheImple()
275 +
276 +               oche.Ready = true
277 +               _,err := oche.ReadCapability("DefaultIP")
278 +               if err != nil {
279 +                       t.Error("Err should be null")
280 +               }
281 +       })
282 +
283 +       t.Run("Faliure", func(t *testing.T){
284 +               var dummy string
285 +               gomock.InOrder(
286 +                       mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
287 +                       mockCapability.EXPECT().ReadCapability(gomock.Any()).Return(dummy,errors.New("")),
288 +               )
289 +               o := getOcheIns(ctrl)
290 +               if o == nil {
291 +                       t.Error("ochestration object is nil, expected is not nil")
292 +               }
293 +
294 +               oche := getOrcheImple()
295 +
296 +               oche.Ready = true
297 +               _,err := oche.ReadCapability("DefaultIP")
298 +               if err == nil {
299 +                       t.Error("Err should be null")
300 +               }
301 +
302 +               t.Run("OrchFaliure", func(t *testing.T){
303 +                       gomock.InOrder(
304 +                               mockService.EXPECT().SetLocalServiceExecutor(mockExecutor),
305 +                       )
306 +                       o := getOcheIns(ctrl)
307 +                       if o == nil {
308 +                               t.Error("ochestration object is nil, expected is not nil")
309 +                       }
310 +
311 +                       oche := getOrcheImple()
312 +
313 +                       oche.Ready = false
314 +                       _,err := oche.ReadCapability("DefaultIP")
315 +                       if err == nil {
316 +                               t.Error("Err should be null")
317 +                       }
318 +               })
319 +       })
320 +}
321 +