From 2257ad8dd7706ac555a122f60d618eb2e63c716c Mon Sep 17 00:00:00 2001 From: william Date: Tue, 10 Dec 2024 15:18:45 +0000 Subject: [PATCH 1/3] fix: Add hello API with GET and POST endpoints and tests --- go.mod | 3 +++ internal/sbi/api_hello.go | 33 +++++++++++++++++++++++++++++++ internal/sbi/api_hello_test.go | 36 ++++++++++++++++++++++++++++++++++ internal/sbi/router.go | 3 +++ 4 files changed, 75 insertions(+) create mode 100644 internal/sbi/api_hello.go create mode 100644 internal/sbi/api_hello_test.go diff --git a/go.mod b/go.mod index d36d9ba..a313925 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/google/uuid v1.6.0 github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.15 go.uber.org/mock v0.4.0 gopkg.in/yaml.v2 v2.4.0 @@ -18,6 +19,7 @@ require ( github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -33,6 +35,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/internal/sbi/api_hello.go b/internal/sbi/api_hello.go new file mode 100644 index 0000000..75366fa --- /dev/null +++ b/internal/sbi/api_hello.go @@ -0,0 +1,33 @@ +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) getHelloRoute() []Route { + return []Route{ + { + Name: "GetHello", + Method: "GET", + Pattern: "/", + APIFunc: s.GetHello, + }, + { + Name: "PostHello", + Method: "POST", + Pattern: "/:message", + APIFunc: s.PostHello, + }, + } +} + +func (s *Server) GetHello(c *gin.Context) { + c.String(http.StatusOK, "Hello from feature/hello!") +} + +func (s *Server) PostHello(c *gin.Context) { + message := c.Param("message") + c.String(http.StatusOK, "Received message: %s", message) +} \ No newline at end of file diff --git a/internal/sbi/api_hello_test.go b/internal/sbi/api_hello_test.go new file mode 100644 index 0000000..199a00c --- /dev/null +++ b/internal/sbi/api_hello_test.go @@ -0,0 +1,36 @@ +package sbi + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +func TestGetHello(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + server := &Server{} + server.GetHello(c) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "Hello from feature/hello!", w.Body.String()) +} + +func TestPostHello(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Params = []gin.Param{{Key: "message", Value: "test"}} + + server := &Server{} + server.PostHello(c) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "Received message: test", w.Body.String()) +} \ No newline at end of file diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 34a902d..1d16794 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -60,6 +60,9 @@ func newRouter(s *Server) *gin.Engine { ChiikawaGroup := router.Group("/chiikawa") applyRoutes(ChiikawaGroup, s.getChiikawaRoute()) + helloGroup := router.Group("/hello") + applyRoutes(helloGroup, s.getHelloRoute()) + return router } From 04c825b5a8154fe834ed2e980e6003793fd2ad9a Mon Sep 17 00:00:00 2001 From: william Date: Sun, 29 Dec 2024 18:33:34 +0000 Subject: [PATCH 2/3] style: fix code formatting and import ordering --- internal/sbi/api_hello.go | 66 +++++++++++++++--------------- internal/sbi/api_hello_test.go | 73 +++++++++++++++++----------------- 2 files changed, 70 insertions(+), 69 deletions(-) diff --git a/internal/sbi/api_hello.go b/internal/sbi/api_hello.go index 75366fa..51f8669 100644 --- a/internal/sbi/api_hello.go +++ b/internal/sbi/api_hello.go @@ -1,33 +1,33 @@ -package sbi - -import ( - "net/http" - - "github.com/gin-gonic/gin" -) - -func (s *Server) getHelloRoute() []Route { - return []Route{ - { - Name: "GetHello", - Method: "GET", - Pattern: "/", - APIFunc: s.GetHello, - }, - { - Name: "PostHello", - Method: "POST", - Pattern: "/:message", - APIFunc: s.PostHello, - }, - } -} - -func (s *Server) GetHello(c *gin.Context) { - c.String(http.StatusOK, "Hello from feature/hello!") -} - -func (s *Server) PostHello(c *gin.Context) { - message := c.Param("message") - c.String(http.StatusOK, "Received message: %s", message) -} \ No newline at end of file +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) getHelloRoute() []Route { + return []Route{ + { + Name: "GetHello", + Method: "GET", + Pattern: "/", + APIFunc: s.GetHello, + }, + { + Name: "PostHello", + Method: "POST", + Pattern: "/:message", + APIFunc: s.PostHello, + }, + } +} + +func (s *Server) GetHello(c *gin.Context) { + c.String(http.StatusOK, "Hello from feature/hello!") +} + +func (s *Server) PostHello(c *gin.Context) { + message := c.Param("message") + c.String(http.StatusOK, "Received message: %s", message) +} diff --git a/internal/sbi/api_hello_test.go b/internal/sbi/api_hello_test.go index 199a00c..68ea620 100644 --- a/internal/sbi/api_hello_test.go +++ b/internal/sbi/api_hello_test.go @@ -1,36 +1,37 @@ -package sbi - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/stretchr/testify/assert" -) - -func TestGetHello(t *testing.T) { - gin.SetMode(gin.TestMode) - w := httptest.NewRecorder() - c, _ := gin.CreateTestContext(w) - - server := &Server{} - server.GetHello(c) - - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "Hello from feature/hello!", w.Body.String()) -} - -func TestPostHello(t *testing.T) { - gin.SetMode(gin.TestMode) - w := httptest.NewRecorder() - c, _ := gin.CreateTestContext(w) - - c.Params = []gin.Param{{Key: "message", Value: "test"}} - - server := &Server{} - server.PostHello(c) - - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "Received message: test", w.Body.String()) -} \ No newline at end of file +package sbi + +//nolint:dupl +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +func TestGetHello(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + server := &Server{} + server.GetHello(c) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "Hello from feature/hello!", w.Body.String()) +} + +func TestPostHello(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Params = []gin.Param{{Key: "message", Value: "test"}} + + server := &Server{} + server.PostHello(c) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "Received message: test", w.Body.String()) +} From ce5fa7963ff7e46f003c3918041d87d0b07e97e2 Mon Sep 17 00:00:00 2001 From: william Date: Sun, 29 Dec 2024 18:57:45 +0000 Subject: [PATCH 3/3] fix: fix linting and test issues --- internal/sbi/api_hello_test.go | 82 +++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/internal/sbi/api_hello_test.go b/internal/sbi/api_hello_test.go index 68ea620..fca1386 100644 --- a/internal/sbi/api_hello_test.go +++ b/internal/sbi/api_hello_test.go @@ -1,37 +1,79 @@ -package sbi +package sbi_test -//nolint:dupl import ( "net/http" "net/http/httptest" "testing" + "github.com/andy89923/nf-example/internal/sbi" + "github.com/andy89923/nf-example/pkg/factory" "github.com/gin-gonic/gin" - "github.com/stretchr/testify/assert" + "go.uber.org/mock/gomock" ) -func TestGetHello(t *testing.T) { +func Test_getHelloRoutes(t *testing.T) { gin.SetMode(gin.TestMode) - w := httptest.NewRecorder() - c, _ := gin.CreateTestContext(w) - server := &Server{} - server.GetHello(c) + mockCtrl := gomock.NewController(t) + nfApp := sbi.NewMocknfApp(mockCtrl) + nfApp.EXPECT().Config().Return(&factory.Config{ + Configuration: &factory.Configuration{ + Sbi: &factory.Sbi{ + Port: 8000, + }, + }, + }).AnyTimes() + server := sbi.NewServer(nfApp, "") - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "Hello from feature/hello!", w.Body.String()) -} + t.Run("GetHello", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const EXPECTED_BODY = "Hello from feature/hello!" -func TestPostHello(t *testing.T) { - gin.SetMode(gin.TestMode) - w := httptest.NewRecorder() - c, _ := gin.CreateTestContext(w) + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + ginCtx.Request, err = http.NewRequest("GET", "/", nil) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.GetHello(ginCtx) + + if httpRecorder.Code != EXPECTED_STATUS { + t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) + } + + if httpRecorder.Body.String() != EXPECTED_BODY { + t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) + } + }) + + t.Run("PostHello", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const TEST_MESSAGE = "test" + const EXPECTED_BODY = "Received message: test" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + ginCtx.Request, err = http.NewRequest("POST", "/"+TEST_MESSAGE, nil) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + ginCtx.Params = []gin.Param{{Key: "message", Value: TEST_MESSAGE}} - c.Params = []gin.Param{{Key: "message", Value: "test"}} + server.PostHello(ginCtx) - server := &Server{} - server.PostHello(c) + if httpRecorder.Code != EXPECTED_STATUS { + t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) + } - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "Received message: test", w.Body.String()) + if httpRecorder.Body.String() != EXPECTED_BODY { + t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) + } + }) }