Move business logic to httpclient pkg from each module 84/203284/1
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 11 Apr 2019 08:31:58 +0000 (17:31 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 11 Apr 2019 08:31:58 +0000 (17:31 +0900)
Change-Id: Ia9d6a2c5acb49407ea6db20ab6ed10864ad3178b
Signed-off-by: jw_wonny.cha <jw_wonny.cha@samsung.com>
src/restapi/httpclient/internal.go [new file with mode: 0644]
src/restapi/httpclient/internal_test.go [new file with mode: 0644]

diff --git a/src/restapi/httpclient/internal.go b/src/restapi/httpclient/internal.go
new file mode 100644 (file)
index 0000000..abc9d08
--- /dev/null
@@ -0,0 +1,113 @@
+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
+}
diff --git a/src/restapi/httpclient/internal_test.go b/src/restapi/httpclient/internal_test.go
new file mode 100644 (file)
index 0000000..42ed975
--- /dev/null
@@ -0,0 +1,163 @@
+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()
+}