improve coverage in restapi/v1 pkg 54/203154/2
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Wed, 10 Apr 2019 07:31:00 +0000 (16:31 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Wed, 10 Apr 2019 07:42:01 +0000 (16:42 +0900)
- delete not used restapi server
- add testcase of discoverymgr in restapi_test

Change-Id: Ifaf9506edecf25b755c619bb0ad0fffba7a2e50b
Signed-off-by: jw_wonny.cha <jw_wonny.cha@samsung.com>
src/restapi/v1/restapi.go
src/restapi/v1/restapi_test.go
src/restapi/v1/routers.go

index be5a74ec5f7b499e51f262e0f30e0fb2c7f53c38..2ef1438803ee10e58a6bdca3ce8aac0c8453c3cc 100755 (executable)
@@ -65,11 +65,6 @@ func APIV1DeviceResourceUsageNetworkGet(w http.ResponseWriter, r *http.Request)
        writeJSONResponse(w, data, http.StatusOK)
 }
 
-// APIV1DiscoverymgrDevicesDeviceIDGet function
-func APIV1DiscoverymgrDevicesDeviceIDGet(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1DiscoverymgrDevicesDeviceIDGet", logPrefix)
-}
-
 // APIV1DiscoverymgrDevicesGet function
 func APIV1DiscoverymgrDevicesGet(w http.ResponseWriter, r *http.Request) {
        log.Printf("[%s] APIV1DiscoverymgrDevicesGet", logPrefix)
@@ -128,27 +123,24 @@ func APIV1DiscoverymgrDevicesTXTPost(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       // var distService map[string]string
-       // decoder := json.NewDecoder(r.Body)
-       // err := decoder.Decode(&distService)
-       // if err != nil {
-       //      return
-       // }
        target := distService["ServiceName"]
 
+       ret := make(map[string]interface{})
+
        err = discoverymgr.SetServiceNames(target.(string))
        if err != nil {
-               writeJSONResponse(w, nil, http.StatusBadRequest)
+               ret["Return"] = err.Error()
+               bdbytes, _ := json.Marshal(ret)
+               w.WriteHeader(http.StatusBadRequest)
+               writeJSONResponse(w, bdbytes, http.StatusBadRequest)
        } else {
-               writeJSONResponse(w, nil, http.StatusOK)
+               ret["Return"] = "OK"
+               w.WriteHeader(http.StatusOK)
+               bdbytes, _ := json.Marshal(ret)
+               writeJSONResponse(w, bdbytes, http.StatusOK)
        }
 }
 
-// APIV1ServicemgrServicesDelete function
-func APIV1ServicemgrServicesDelete(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1ServicemgrServicesDelete", logPrefix)
-}
-
 // APIV1ServicemgrServicesPost function
 func APIV1ServicemgrServicesPost(w http.ResponseWriter, r *http.Request) {
        log.Printf("[%s] APIV1ServicemgrServicesPost", logPrefix)
@@ -165,12 +157,6 @@ func APIV1ServicemgrServicesPost(w http.ResponseWriter, r *http.Request) {
        }
 }
 
-// APIV1ServicemgrEventServiceIDPost function
-func APIV1ServicemgrEventServiceIDPost(w http.ResponseWriter, r *http.Request) {
-       log.Printf("[%s] APIV1ServicemgrEventServiceIDPost", logPrefix)
-       writeJSONResponse(w, nil, http.StatusNotImplemented)
-}
-
 // APIV1ServicemgrServicesNotificationServiceIDPost function
 func APIV1ServicemgrServicesNotificationServiceIDPost(w http.ResponseWriter, r *http.Request) {
        log.Printf("[%s] APIV1ServicemgrServicesNotificationServiceIDPost", logPrefix)
@@ -230,12 +216,6 @@ func executeServiceHandler(w http.ResponseWriter, req *http.Request) (distServic
                return
        }
 
-       // decoder := json.NewDecoder(req.Body)
-       // err = decoder.Decode(&distService)
-       // if err != nil {
-       //      return
-       // }
-
        _, err = exec.LookPath(distService["ServiceName"].(string))
        if err != nil {
                err = errors.New("It is Invalid Service")
index 747cbacfea3e3a2d4dbe9eada4734d6d491b0e73..0e680927209ff837ca23508b798d9705c9245b71 100755 (executable)
@@ -17,7 +17,8 @@ import (
 )
 
 var (
-       testURL string
+       testURL     string
+       keyFilePath = "./../../securemgr/test/key.txt"
 )
 
 const (
@@ -28,7 +29,8 @@ const (
        ConstDeviceResourceUsageNetworkGet = "/api/v1/device/resource/usage/network"
 
        // discovery mgr URI
-       ConstDiscoverymgrDevicesGet = "/api/v1/discoverymgr/devices"
+       ConstDiscoverymgrDevices    = "/api/v1/discoverymgr/devices"
+       ConstDiscoverymgrDevicesTXT = "/api/v1/discoverymgr/devices/TXT"
 
        // Service mgr URI
        ConstServicemgrServices     = "/api/v1/servicemgr/services"
@@ -37,8 +39,13 @@ const (
        // Scoring mgr URI
        ConstScoremgrScore = "/api/v1/scoringmgr/score"
 
-       ConstAppName     = "AppName"
-       ConstServiceName = "ls"
+       ConstAppName         = "ls"
+       ConstNotExistAppName = "NotExistFile"
+       ConstTooLongAppName  = `00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+                                                       00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+                                                       00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+                                                       00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+                                                       00000000000000000000000000000000000000000000000000000000000000000000000000000000000`
 )
 
 type Response struct {
@@ -46,6 +53,89 @@ type Response struct {
        Code    int
 }
 
+func init() {
+       devicemgr.InitDeviceMgr()
+       servicemgr.Init()
+       discoverymgr.InitDiscovery()
+       scoringmgr.Init()
+       securemgr.Init(keyFilePath)
+
+       router := NewRouter()
+       server := httptest.NewServer(router)
+
+       testURL = server.URL
+
+       log.Println("testURL:", testURL)
+}
+
+func TestAPIV1DeviceResourceUsageCPUGet(t *testing.T) {
+       targetURI := ConstDeviceResourceUsageCPUGet
+       testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DeviceResourceUsageDiskGet(t *testing.T) {
+       targetURI := ConstDeviceResourceUsageDiskGet
+       testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DeviceResourceUsageMemoryGet(t *testing.T) {
+       targetURI := ConstDeviceResourceUsageMemoryGet
+       testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DeviceResourceUsageNetworkGet(t *testing.T) {
+       targetURI := ConstDeviceResourceUsageNetworkGet
+       testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) {
+       targetURI := ConstDiscoverymgrDevices
+       testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesPost(t *testing.T) {
+       getDevice(t, ConstDiscoverymgrDevices, ConstAppName, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesTXTPost(t *testing.T) {
+       getDevice(t, ConstDiscoverymgrDevicesTXT, ConstAppName, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesTXTPostWithInvalidAppName(t *testing.T) {
+       getDevice(t, ConstDiscoverymgrDevicesTXT, ConstTooLongAppName, http.StatusBadRequest)
+}
+
+func TestAPIV1ServicemgrServicesPost(t *testing.T) {
+       executeService(t, ConstAppName, http.StatusOK)
+}
+
+func TestAPIV1ServicemgrServicesPostWithNotExistExecutionFile(t *testing.T) {
+       executeService(t, ConstNotExistAppName, http.StatusBadRequest)
+}
+
+func TestAPIV1ScoringmgrScoreLibnameGet(t *testing.T) {
+       targetURI := ConstScoremgrScore
+       testGet(t, targetURI+"/mysum", http.StatusOK)
+}
+
+func TestAPIV1ServicemgrServicesNotificationServiceIDPost(t *testing.T) {
+       statusNotificationRequest := make(map[string]interface{})
+       statusNotificationRequest["ServiceID"] = 1
+       statusNotificationRequest["Status"] = "Failed"
+
+       snbytes, _ := json.Marshal(statusNotificationRequest)
+
+       targetURI := ConstServicemgrServicesNoti
+
+       _, resStatusCode, err := httpclient.DoPost(testURL+targetURI+"1", snbytes)
+       if err != nil {
+               t.Log(err.Error())
+               t.Error()
+       }
+
+       AssertEqualInt(t, resStatusCode, http.StatusOK)
+}
+
 func AssertEqualStr(t *testing.T, a, b string) {
        t.Helper()
        if strings.Compare(a, b) != 0 {
@@ -99,94 +189,42 @@ func testPost(t *testing.T, targetURI string, bodyContents []byte, statusCode in
        return
 }
 
-func executeService(t *testing.T) {
+func executeService(t *testing.T, appName string, expectedStatus int) {
        t.Helper()
        targetURI := ConstServicemgrServices
 
        appInfo := make(map[string]interface{})
 
-       appInfo[servicemgr.ConstKeyServiceID] = 1
-       appInfo[servicemgr.ConstKeyServiceName] = ConstServiceName
-       appInfo[servicemgr.ConstKeyNotiTargetURL] = "URL"
+       appInfo["ServiceID"] = 1
+       appInfo["ServiceName"] = appName
+       appInfo["NotificationTargetURL"] = "URL"
 
        userArgs := []string{"-ail"}
-       appInfo[servicemgr.ConstKeyUserArgs] = userArgs
+       appInfo["UserArgs"] = userArgs
 
        bdbytes, err := json.Marshal(appInfo)
 
        if err != nil {
                t.Log(err.Error())
-               t.Error()
+               t.Fatal()
        }
 
-       testPost(t, targetURI, bdbytes, http.StatusOK)
-}
-
-var keyFilePath string = "./../../securemgr/test/key.txt"
-
-func init() {
-       devicemgr.InitDeviceMgr()
-       servicemgr.Init()
-       discoverymgr.InitDiscovery()
-       scoringmgr.Init()
-       securemgr.Init(keyFilePath)
-
-       router := NewRouter()
-       server := httptest.NewServer(router)
-
-       testURL = server.URL
-
-       log.Println("testURL:", testURL)
-}
-
-func TestAPIV1DeviceResourceUsageCPUGet(t *testing.T) {
-       targetURI := ConstDeviceResourceUsageCPUGet
-       testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DeviceResourceUsageDiskGet(t *testing.T) {
-       targetURI := ConstDeviceResourceUsageDiskGet
-       testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DeviceResourceUsageMemoryGet(t *testing.T) {
-       targetURI := ConstDeviceResourceUsageMemoryGet
-       testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DeviceResourceUsageNetworkGet(t *testing.T) {
-       targetURI := ConstDeviceResourceUsageNetworkGet
-       testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) {
-       targetURI := ConstDiscoverymgrDevicesGet
-       testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1ServicemgrServicesPost(t *testing.T) {
-       executeService(t)
+       testPost(t, targetURI, bdbytes, expectedStatus)
 }
 
-func TestAPIV1ScoringmgrScoreLibnameGet(t *testing.T) {
-       targetURI := ConstScoremgrScore
-       testGet(t, targetURI+"/mysum", http.StatusOK)
-}
+func getDevice(t *testing.T, targetURI string, appName string, expectedStatus int) {
+       t.Helper()
 
-func TestAPIV1ServicemgrServicesNotificationServiceIDPost(t *testing.T) {
-       statusNotificationRequest := make(map[string]interface{})
-       statusNotificationRequest[servicemgr.ConstKeyServiceID] = 1
-       statusNotificationRequest[servicemgr.ConstKeyStatus] = servicemgr.ConstServiceStatusFailed
+       appInfo := make(map[string]interface{})
 
-       snbytes, _ := json.Marshal(statusNotificationRequest)
+       appInfo["ServiceName"] = appName
 
-       targetURI := ConstServicemgrServicesNoti
+       bdbytes, err := json.Marshal(appInfo)
 
-       _, resStatusCode, err := httpclient.DoPost(testURL+targetURI+"1", snbytes)
        if err != nil {
                t.Log(err.Error())
-               t.Error()
+               t.Fatal()
        }
 
-       AssertEqualInt(t, resStatusCode, http.StatusOK)
+       testPost(t, targetURI, bdbytes, expectedStatus)
 }
index c81bacc1104bf1db5137ace9d670ea9cb1c59bc2..3b1c0d8ad14b11523914050c58e4111c2f1bdfcd 100644 (file)
@@ -87,13 +87,6 @@ var routes = Routes{
                APIV1DeviceResourceUsageNetworkGet,
        },
 
-       Route{
-               "APIV1DiscoverymgrDevicesDeviceIDGet",
-               strings.ToUpper("Get"),
-               "/api/v1/discoverymgr/devices/{deviceid}",
-               APIV1DiscoverymgrDevicesDeviceIDGet,
-       },
-
        Route{
                "APIV1DiscoverymgrDevicesGet",
                strings.ToUpper("Get"),