--- /dev/null
+package httpclient
+
+/*******************************************************************************
+ * Copyright 2019 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *******************************************************************************/
+
+import (
+ "common"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "log"
+ "net/http"
+ "strconv"
+)
+
+// DoExecuteRemoteDevice for executing at remote device
+func DoExecuteRemoteDevice(appInfo map[string]interface{}, target string) (err error) {
+ targetURL := ConstPrefixHTTP + target + ":" +
+ strconv.Itoa(ConstWellknownPort) + ConstServiceExecuteURI
+
+ reqBytes, err := json.Marshal(appInfo)
+ if common.CheckError(err) == true {
+ return
+ }
+
+ respBytes, statusCode, err := DoPost(targetURL, reqBytes)
+ if common.CheckError(err) == true {
+ return
+ }
+
+ if statusCode != http.StatusOK {
+ log.Println(statusCode)
+ err = fmt.Errorf("Status Code : %d", statusCode)
+ return
+ }
+
+ var responseMsg map[string]interface{}
+ err = json.Unmarshal(respBytes, &responseMsg)
+ if err != nil {
+ return
+ }
+
+ log.Println("[JSON] : ", responseMsg)
+
+ str := responseMsg["Status"].(string)
+ if str == "Failed" {
+ err = errors.New("Failed")
+ }
+
+ return
+}
+
+// DoGetScoreRemoteDevice for getting score in remote device
+func DoGetScoreRemoteDevice(appName string, target string) (scoreValue float64, err error) {
+ targetURL := ConstPrefixHTTP + target + ":" +
+ strconv.Itoa(ConstWellknownPort) + ConstGetScoreURI + appName
+
+ respBytes, statusCode, err := DoGet(targetURL)
+ if common.CheckError(err) == true {
+ return scoreValue, err
+ }
+
+ if statusCode != http.StatusOK {
+ log.Println(statusCode)
+ err = fmt.Errorf("Status Code : %d", statusCode)
+ return
+ }
+
+ var responseMsg map[string]interface{}
+ err = json.Unmarshal(respBytes, &responseMsg)
+ if err == nil {
+ scoreValue = responseMsg["ScoreValue"].(float64)
+ }
+
+ return
+}
+
+// DoNotifyAppStatusRemoteDevice for notify app status to remote device
+func DoNotifyAppStatusRemoteDevice(statusNotificationInfo map[string]interface{}, appID uint64, target string) (err error) {
+ targetURL := ConstPrefixHTTP + target + ":" +
+ strconv.Itoa(ConstWellknownPort) + ConstServiceStatusNotiURI + strconv.FormatUint(appID, 10)
+
+ reqbytes, err := json.Marshal(statusNotificationInfo)
+ if common.CheckError(err) == true {
+ return
+ }
+
+ _, statusCode, err := DoPost(targetURL, reqbytes)
+ if common.CheckError(err) == true {
+ return
+ }
+
+ if statusCode != http.StatusOK {
+ log.Println(statusCode)
+ err = fmt.Errorf("Status Code : %d", statusCode)
+ }
+
+ return
+}
--- /dev/null
+package httpclient_test
+
+/*******************************************************************************
+* Copyright 2019 Samsung Electronics All Rights Reserved.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*******************************************************************************/
+
+import (
+ "fmt"
+ "log"
+ "net"
+ "net/http"
+ "net/http/httptest"
+ "restapi/httpclient"
+ "scoringmgr"
+ "securemgr"
+ "servicemgr"
+ "strconv"
+ "strings"
+ "testing"
+ "time"
+
+ confdescription "configuremgr/description"
+ restapi "restapi/v1"
+
+ "gopkg.in/sconf/ini.v0"
+ "gopkg.in/sconf/sconf.v0"
+)
+
+var (
+ server *httptest.Server
+ serviceID uint64
+ iscoringmgr *scoringmgr.Handlers
+
+ targetCustomURL = fmt.Sprintf("127.0.0.1:%d", httpclient.ConstWellknownPort)
+ targetRemoteDeviceAddr = "127.0.0.1"
+ notExistFileName = "NotExistFile"
+ keyFilePath = "../../securemgr/test/key.txt"
+
+ serviceName = "ls"
+)
+
+func init() {
+ servicemgr.Init()
+ securemgr.Init(keyFilePath)
+
+ iscoringmgr = scoringmgr.Init()
+}
+
+func setUp() {
+ router := restapi.NewRouter()
+ server = httptest.NewUnstartedServer(router)
+
+ l, _ := net.Listen("tcp", targetCustomURL)
+
+ server.Listener = l
+ server.Start()
+}
+
+func tearDown() {
+ defer server.Close()
+}
+
+func TestDoExecuteRemoteDevice(t *testing.T) {
+ setUp()
+
+ appInfo := make(map[string]interface{})
+ appInfo["ServiceName"] = serviceName
+ appInfo["NotificationTargetURL"] = httpclient.ConstPrefixHTTP +
+ targetRemoteDeviceAddr + ":" + strconv.Itoa(httpclient.ConstWellknownPort)
+
+ err := httpclient.DoExecuteRemoteDevice(appInfo, targetRemoteDeviceAddr)
+
+ if err != nil {
+ t.Error()
+ }
+
+ tearDown()
+}
+
+func TestDoExecuteRemoteDeviceWithInvalidApp(t *testing.T) {
+ setUp()
+
+ appInfo := make(map[string]interface{})
+ appInfo["ServiceName"] = notExistFileName
+ err := httpclient.DoExecuteRemoteDevice(appInfo, targetRemoteDeviceAddr)
+
+ log.Println("[err]", err)
+
+ if strings.Contains(err.Error(), fmt.Sprintf("%d", http.StatusBadRequest)) != true {
+ t.Error()
+ }
+
+ tearDown()
+}
+
+func TestDoGetScoreRemoteDevice(t *testing.T) {
+ setUp()
+ //interface
+ iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
+ iscoringmgr.IGetScore = scoringmgr.GetScore
+ iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
+ iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
+ iscoringmgr.Ch = make(chan interface{}, 1024)
+
+ //start
+ iscoringmgr.Listening()
+ iscoringmgr.IStartResourceService()
+
+ //setting
+ libName := "myscoring"
+ libPath := fmt.Sprintf("../../scoringmgr/mock/%s/lib%s.so", libName, libName)
+ confPath := fmt.Sprintf("../../scoringmgr/mock/%s/%s.conf", libName, libName)
+ cfg := new(confdescription.Doc)
+ sconf.Must(cfg).Read(ini.File(confPath))
+ log.Println(cfg)
+ scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
+
+ //working on
+ time.Sleep(time.Duration(3) * time.Second)
+
+ scoreValue, err := httpclient.DoGetScoreRemoteDevice("ls", targetRemoteDeviceAddr)
+ log.Printf("scorevalue : %f\n", scoreValue)
+
+ if err != nil {
+ t.Error()
+ }
+
+ //release
+ iscoringmgr.IStopResourceService()
+ iscoringmgr.RemoveAll()
+
+ time.Sleep(time.Duration(100) * time.Millisecond)
+ tearDown()
+}
+
+func TestDoNotifyAppStatusRemoteDevice(t *testing.T) {
+ setUp()
+
+ statusNotificationInfo := make(map[string]interface{})
+ statusNotificationInfo["ServiceID"] = 1
+ statusNotificationInfo["Status"] = "Failed"
+
+ err := httpclient.DoNotifyAppStatusRemoteDevice(statusNotificationInfo, uint64(1), targetRemoteDeviceAddr)
+
+ if err != nil {
+ t.Error()
+ }
+
+ tearDown()
+}