Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ping v2 api #387

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ require (
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.1.1
)

replace github.com/edgexfoundry/go-mod-core-contracts => ../go-mod-core-contracts
25 changes: 25 additions & 0 deletions internal/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand All @@ -29,6 +30,7 @@ import (
"github.com/edgexfoundry/app-functions-sdk-go/internal/telemetry"
"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
gmcccommon "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about contracts for the alias?


"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -83,6 +85,28 @@ func (webserver *WebServer) pingHandler(writer http.ResponseWriter, _ *http.Requ
writer.Header().Set("Content-Type", "text/plain")
writer.Write([]byte("pong"))
}
func (webserver *WebServer) pingHandler_V2(writer http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var pingRequest gmcccommon.PingRequest

err := decoder.Decode(&pingRequest)
if err != nil {
msg := fmt.Sprintf("Failed to parse json payload: %v", err)
webserver.writeResponse(writer, msg, http.StatusBadRequest)
return
}

response := gmcccommon.PingResponse{
BaseResponse: gmcccommon.BaseResponse{
RequestID: pingRequest.RequestID,
Message: "pong",
StatusCode: 200,
},
Timestamp: strconv.FormatInt(time.Now().UnixNano(), 10),
}
webserver.encode(response, writer)
return
}

// swagger:operation GET /config System_Management_Agent Config
//
Expand Down Expand Up @@ -281,6 +305,7 @@ func (webserver *WebServer) ConfigureStandardRoutes() {

// Ping Resource
webserver.router.HandleFunc(clients.ApiPingRoute, webserver.pingHandler).Methods(http.MethodGet)
webserver.router.HandleFunc("/api/v2/ping", webserver.pingHandler_V2).Methods(http.MethodPost)

// Configuration
webserver.router.HandleFunc(clients.ApiConfigRoute, webserver.configHandler).Methods(http.MethodGet)
Expand Down
22 changes: 22 additions & 0 deletions internal/webserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"math"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"

"github.com/edgexfoundry/app-functions-sdk-go/internal/security"

Expand All @@ -31,6 +33,7 @@ import (
"github.com/edgexfoundry/app-functions-sdk-go/internal/telemetry"
"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
gmcccommon "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -83,6 +86,25 @@ func TestConfigureAndPingRoute(t *testing.T) {
body := rr.Body.String()
assert.Equal(t, "pong", body)

}
func TestConfigureAndPingV2Route(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Configure in the name?


sp := security.NewSecretProvider(logClient, config)
webserver := NewWebServer(config, sp, logClient, mux.NewRouter())
webserver.ConfigureStandardRoutes()
requestID := "123"
payload := []byte("{\"requestId\":\"" + requestID + "\"}")
req, _ := http.NewRequest(http.MethodPost, "/api/v2/ping", bytes.NewReader(payload))
rr := httptest.NewRecorder()
webserver.router.ServeHTTP(rr, req)
decoder := json.NewDecoder(rr.Body)
var pingResponse gmcccommon.PingResponse
assert.NoError(t, decoder.Decode(&pingResponse))
assert.Equal(t, requestID, pingResponse.RequestID)
assert.Equal(t, "pong", pingResponse.Message)
assert.Equal(t, 200, pingResponse.StatusCode)
assert.GreaterOrEqual(t, strconv.FormatInt(time.Now().UnixNano(), 10), pingResponse.Timestamp)

}

func TestConfigureAndVersionRoute(t *testing.T) {
Expand Down