From 4ff453e70838af2ca3de96a2ff0143678d5e6c05 Mon Sep 17 00:00:00 2001 From: damon92 Date: Thu, 28 Mar 2019 16:49:01 +0900 Subject: [PATCH] add REST setServiceNames w/ test --- src/discoverymgr/discovery_init.go | 8 +++++--- src/restapi/v1/restapi.go | 20 ++++++++++++++++++++ src/restapi/v1/restapi_test.go | 17 +++++++++++++++-- src/restapi/v1/routers.go | 7 +++++++ 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/discoverymgr/discovery_init.go b/src/discoverymgr/discovery_init.go index fe4ca1e..6b74142 100644 --- a/src/discoverymgr/discovery_init.go +++ b/src/discoverymgr/discovery_init.go @@ -2,6 +2,7 @@ package discoverymgr import ( "context" + "errors" "fmt" "log" "time" @@ -12,17 +13,18 @@ import ( var gServer *zeroconf.Server //SetServiceNames set txt of mdns message -func SetServiceNames(ServiceNames []string) { +func SetServiceNames(ServiceNames []string) error { var TXTSize int for _, str := range ServiceNames { TXTSize += len(str) } if TXTSize > maxTXTSize { - fmt.Println("TXT Size is Too much for mDNS TXT - 400B") - return + return errors.New("TXT Size is Too much for mDNS TXT - 400B") } gServer.SetText(ServiceNames) + + return nil } func registerDevice(ServiceNames []string, ret chan error) { diff --git a/src/restapi/v1/restapi.go b/src/restapi/v1/restapi.go index 3b57d19..5794fec 100644 --- a/src/restapi/v1/restapi.go +++ b/src/restapi/v1/restapi.go @@ -71,6 +71,7 @@ func APIV1DiscoverymgrDevicesGet(w http.ResponseWriter, r *http.Request) { ret, err := discoverymgr.DeviceList() if err != nil { writeJSONResponse(w, nil, http.StatusBadRequest) + return } json, err := json.Marshal(ret) @@ -81,6 +82,25 @@ func APIV1DiscoverymgrDevicesGet(w http.ResponseWriter, r *http.Request) { } } +// APIV1DiscoverymgrDevicesGet function +func APIV1DiscoverymgrDevicesPost(w http.ResponseWriter, r *http.Request) { + log.Printf("[%s] APIV1DiscoverymgrDevicesPost", logPrefix) + decoder := json.NewDecoder(r.Body) + var serviceNames map[string][]string + err := decoder.Decode(&serviceNames) + if err != nil { + writeJSONResponse(w, nil, http.StatusBadRequest) + return + } + + err = discoverymgr.SetServiceNames(serviceNames["servicenames"]) + if err == nil { + writeJSONResponse(w, nil, http.StatusOK) + } else { + writeJSONResponse(w, nil, http.StatusBadRequest) + } +} + // APIV1ServicemgrServicesDelete function func APIV1ServicemgrServicesDelete(w http.ResponseWriter, r *http.Request) { log.Printf("[%s] APIV1ServicemgrServicesDelete", logPrefix) diff --git a/src/restapi/v1/restapi_test.go b/src/restapi/v1/restapi_test.go index 595fc25..5746ea7 100644 --- a/src/restapi/v1/restapi_test.go +++ b/src/restapi/v1/restapi_test.go @@ -29,7 +29,8 @@ const ( ConstDeviceResourceUsageNetworkGet = "/api/v1/device/resource/usage/network" // @TODO discovery mgr URI - ConstDiscoverymgrDevices = "/api/v1/discoverymgr/devices" + ConstDiscoverymgrDevicesGet = "/api/v1/discoverymgr/devices" + ConstDiscoverymgrDevicesPost = "/api/v1/discoverymgr/devices" // Service mgr URI ConstServicemgrServices = "/api/v1/servicemgr/services" @@ -243,10 +244,22 @@ func TestAPIV1DeviceResourceUsageNetworkGet(t *testing.T) { } func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) { - targetURI := ConstDiscoverymgrDevices + targetURI := ConstDiscoverymgrDevicesGet testGet(t, targetURI, http.StatusOK) } +func TestAPIV1DiscoverymgrDevicesPost(t *testing.T) { + targetURI := ConstDiscoverymgrDevicesPost + ServiceNames := make(map[string][]string) + ServiceNames["servicenames"] = []string{"Distributed Web Engine", "Television", "Soundbox"} + + bdbytes, err := json.Marshal(ServiceNames) + if err != nil { + t.Log(err.Error()) + t.Error() + } + testPost(t, targetURI, bdbytes, http.StatusOK) +} func TestAPIV1ServicemgrServicesPost(t *testing.T) { registerService(t) } diff --git a/src/restapi/v1/routers.go b/src/restapi/v1/routers.go index d80d7d5..76f8989 100644 --- a/src/restapi/v1/routers.go +++ b/src/restapi/v1/routers.go @@ -102,6 +102,13 @@ var routes = Routes{ }, Route{ + "APIV1DiscoverymgrDevicesPost", + strings.ToUpper("Post"), + "/api/v1/discoverymgr/devices", + APIV1DiscoverymgrDevicesPost, + }, + + Route{ "APIV1ServicemgrServicesAppnameGet", strings.ToUpper("Get"), "/api/v1/servicemgr/services/{appname}", -- 2.7.4