Apply moving internal restapi logic in scoringmgr 90/203290/2
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 11 Apr 2019 08:45:51 +0000 (17:45 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Fri, 12 Apr 2019 01:10:25 +0000 (10:10 +0900)
Change-Id: I038019a60d6af00ae676c580c9c5f026b994307f
Signed-off-by: jw_wonny.cha <jw_wonny.cha@samsung.com>
src/scoringmgr/score.go
src/scoringmgr/scoringmgr_test.go

index 099591d..9f1978c 100755 (executable)
@@ -1,29 +1,27 @@
 /*******************************************************************************
- * 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.
- *
- *******************************************************************************/
+* 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.
+*
+*******************************************************************************/
+
 package scoringmgr
 
 import (
        "common"
-       "encoding/json"
        "errors"
        "log"
-       "net/http"
        "restapi/httpclient"
-       "strconv"
        "strings"
 )
 
@@ -37,7 +35,7 @@ func GetScore(target string, name string) (scoreValue float64, err error) {
        if strings.Compare(target, outboundIP) == 0 || strings.Compare(target, httpclient.ConstLocalTarget) == 0 {
                scoreValue, err = getScoreLocalEnv(name)
        } else {
-               scoreValue, err = getScoreRemoteEnv(target, name)
+               scoreValue, err = httpclient.DoGetScoreRemoteDevice(name, target)
        }
 
        log.Println("[scoringmgr] scoreValue", scoreValue)
@@ -57,30 +55,3 @@ func getScoreLocalEnv(name string) (scoreValue float64, err error) {
 
        return
 }
-
-func getScoreRemoteEnv(target string, name string) (scoreValue float64, err error) {
-       targetURL := httpclient.ConstPrefixHTTP + target + ":" + strconv.Itoa(httpclient.ConstWellknownPort) + "/api/v1/scoringmgr/score/" + name
-       respBytes, statusCode, err := httpclient.DoGet(targetURL)
-       if checkError(err) == true {
-               return scoreValue, err
-       }
-
-       var responseMsg map[string]interface{}
-       err = json.Unmarshal(respBytes, &responseMsg)
-       if err == nil {
-               scoreValue = responseMsg["ScoreValue"].(float64)
-       }
-       if statusCode != http.StatusOK {
-               log.Println(statusCode)
-       }
-
-       return
-}
-
-func checkError(err error) bool {
-       if err != nil {
-               log.Println(err.Error())
-               return true
-       }
-       return false
-}
index efe85c4..4e63c6b 100755 (executable)
 /*******************************************************************************
- * 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.
- *
- *******************************************************************************/
- package scoringmgr_test
-
- //$ touch resourceservice/resourceservice.c
- import (
-        "context"
-        "fmt"
-        "log"
-        "net/http"
-        "testing"
-        "time"
-        "gopkg.in/sconf/ini.v0"
-        "gopkg.in/sconf/sconf.v0"
-        confdescription "configuremgr/description"
-        restapi "restapi/v1"
-        scoringmgr "scoringmgr"
-        mockscoringmgr "scoringmgr/mock"
- )
- func TestScoringMgrMyScoringLibRemoveApp(t *testing.T) {
-        //interface
-        iscoringmgr := scoringmgr.Init()
-        iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
-        iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
-        iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
-        iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
-        iscoringmgr.Ch = make(chan interface{}, 1024)
-        //start
-        iscoringmgr.Listening()
-        iscoringmgr.IStartResourceService()
-        time.Sleep(time.Duration(100) * time.Millisecond)
-        //setting
-        libName := "myscoring"
-        libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
-        confPath := fmt.Sprintf("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, _ := iscoringmgr.IGetScore("mock", "mock")
-        log.Printf("scorevalue : %f\n", scoreValue)
-        //release
-        iscoringmgr.IStopResourceService()
-        iscoringmgr.RemoveApp(cfg.ServiceInfo.ServiceName)
-        time.Sleep(time.Duration(100) * time.Millisecond)
- }
- func TestScoringMgrMyScoringLib(t *testing.T) {
-        //interface
-        iscoringmgr := scoringmgr.Init()
-        iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
-        iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
-        iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
-        iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
-        iscoringmgr.Ch = make(chan interface{}, 1024)
-        //start
-        iscoringmgr.Listening()
-        iscoringmgr.IStartResourceService()
-        time.Sleep(time.Duration(100) * time.Millisecond)
-        //setting
-        libName := "myscoring"
-        libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
-        confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
-        cfg := new(confdescription.Doc)
-        sconf.Must(cfg).Read(ini.File(confPath))
-        log.Println("[scoringmgr]", cfg)
-        scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
-        //working on
-        time.Sleep(time.Duration(3) * time.Second)
-        scoreValue, _ := iscoringmgr.IGetScore("mock", "mock")
-        log.Printf("[scoringmgr] scorevalue : %f\n", scoreValue)
-        //release
-        iscoringmgr.IStopResourceService()
-        iscoringmgr.RemoveAll()
-        time.Sleep(time.Duration(100) * time.Millisecond)
- }
- func TestScoringMgrMyScoringLibGetScoreLocal(t *testing.T) {
-        //interface
-        iscoringmgr := scoringmgr.Init()
-        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()
-        time.Sleep(time.Duration(100) * time.Millisecond)
-        //setting
-        libName := "myscoring"
-        libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
-        confPath := fmt.Sprintf("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, _ := iscoringmgr.IGetScore("localhost", "myscoring")
-        log.Printf("scorevalue : %f\n", scoreValue)
-        //release
-        iscoringmgr.IStopResourceService()
-        iscoringmgr.RemoveAll()
-        time.Sleep(time.Duration(100) * time.Millisecond)
- }
- func TestScoringMgrMyScoringLibGetScoreRemote(t *testing.T) {
-        //interface
-        iscoringmgr := scoringmgr.Init()
-        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()
-        router := restapi.NewRouter()
-        server := &http.Server{Addr: fmt.Sprintf(":%d", 56001), Handler: router}
-        go server.ListenAndServe()
-        time.Sleep(time.Duration(100) * time.Millisecond)
-        //setting
-        libName := "myscoring"
-        libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
-        confPath := fmt.Sprintf("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, _ := iscoringmgr.IGetScore("127.0.0.1", "myscoring")
-        log.Printf("scorevalue : %f\n", scoreValue)
-        //release
-        iscoringmgr.IStopResourceService()
-        iscoringmgr.RemoveAll()
-        if err := server.Shutdown(context.TODO()); err != nil {
-                panic(err)
-        }
-        time.Sleep(time.Duration(100) * time.Millisecond)
-}
\ No newline at end of file
+* 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.
+*
+*******************************************************************************/
+
+package scoringmgr_test
+
+//$ touch resourceservice/resourceservice.c
+
+import (
+       "fmt"
+       "log"
+       "testing"
+       "time"
+
+       "gopkg.in/sconf/ini.v0"
+       "gopkg.in/sconf/sconf.v0"
+
+       confdescription "configuremgr/description"
+       scoringmgr "scoringmgr"
+       mockscoringmgr "scoringmgr/mock"
+)
+
+func TestScoringMgrMyScoringLibRemoveApp(t *testing.T) {
+
+       //interface
+       iscoringmgr := scoringmgr.Init()
+       iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
+       iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
+       iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
+       iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
+       iscoringmgr.Ch = make(chan interface{}, 1024)
+
+       //start
+       iscoringmgr.Listening()
+       iscoringmgr.IStartResourceService()
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+       //setting
+       libName := "myscoring"
+       libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
+       confPath := fmt.Sprintf("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, _ := iscoringmgr.IGetScore("mock", "mock")
+       log.Printf("scorevalue : %f\n", scoreValue)
+
+       //release
+       iscoringmgr.IStopResourceService()
+       iscoringmgr.RemoveApp(cfg.ServiceInfo.ServiceName)
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+}
+
+func TestScoringMgrMyScoringLib(t *testing.T) {
+
+       //interface
+       iscoringmgr := scoringmgr.Init()
+       iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
+       iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
+       iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
+       iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
+       iscoringmgr.Ch = make(chan interface{}, 1024)
+
+       //start
+       iscoringmgr.Listening()
+       iscoringmgr.IStartResourceService()
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+       //setting
+       libName := "myscoring"
+       libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
+       confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
+       cfg := new(confdescription.Doc)
+       sconf.Must(cfg).Read(ini.File(confPath))
+       log.Println("[scoringmgr]", cfg)
+       scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
+
+       //working on
+       time.Sleep(time.Duration(3) * time.Second)
+       scoreValue, _ := iscoringmgr.IGetScore("mock", "mock")
+       log.Printf("[scoringmgr] scorevalue : %f\n", scoreValue)
+
+       //release
+       iscoringmgr.IStopResourceService()
+       iscoringmgr.RemoveAll()
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+}
+
+func TestScoringMgrMyScoringLibGetScoreLocal(t *testing.T) {
+
+       //interface
+       iscoringmgr := scoringmgr.Init()
+       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()
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+       //setting
+       libName := "myscoring"
+       libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
+       confPath := fmt.Sprintf("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, _ := iscoringmgr.IGetScore("localhost", "myscoring")
+       log.Printf("scorevalue : %f\n", scoreValue)
+
+       //release
+       iscoringmgr.IStopResourceService()
+       iscoringmgr.RemoveAll()
+
+       time.Sleep(time.Duration(100) * time.Millisecond)
+
+}