-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c21e21
commit 5dec7b7
Showing
23 changed files
with
1,047 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/baetyl/baetyl-cloud/common" | ||
"github.com/baetyl/baetyl-cloud/models" | ||
) | ||
|
||
func (api *API) CreateProperty(c *common.Context) (interface{}, error) { | ||
property := &models.Property{} | ||
err := c.LoadBody(property) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, api.propertyService.CreateProperty(property) | ||
} | ||
|
||
func (api *API) DeleteProperty(c *common.Context) (interface{}, error) { | ||
return nil, api.propertyService.DeleteProperty(c.Param("key")) | ||
} | ||
|
||
func (api *API) ListProperty(c *common.Context) (interface{}, error) { | ||
params := &models.Filter{} | ||
if err := c.Bind(params); err != nil { | ||
return nil, err | ||
} | ||
params.Format() | ||
properties, err := api.propertyService.ListProperty(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
count, err := api.propertyService.CountProperty(params.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return models.MisData{ | ||
Count: count, | ||
Rows: properties, | ||
}, nil | ||
} | ||
|
||
func (api *API) UpdateProperty(c *common.Context) (interface{}, error) { | ||
property := &models.Property{ | ||
Key: c.Param("key"), | ||
} | ||
err := c.LoadBody(property) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, api.propertyService.UpdateProperty(property) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/baetyl/baetyl-cloud/common" | ||
plugin "github.com/baetyl/baetyl-cloud/mock/service" | ||
"github.com/baetyl/baetyl-cloud/models" | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func initPropertyAPI(t *testing.T) (*API, *gin.Engine, *gomock.Controller) { | ||
api := &API{} | ||
router := gin.Default() | ||
mockCtl := gomock.NewController(t) | ||
mockIM := func(c *gin.Context) { common.NewContext(c).SetNamespace("default") } | ||
|
||
v1 := router.Group("v1") | ||
{ | ||
property := v1.Group("/properties") | ||
|
||
property.GET("", mockIM, common.WrapperMis(api.ListProperty)) | ||
property.POST("", mockIM, common.WrapperMis(api.CreateProperty)) | ||
property.DELETE("/:key", mockIM, common.WrapperMis(api.DeleteProperty)) | ||
property.PUT("/:key", mockIM, common.WrapperMis(api.UpdateProperty)) | ||
} | ||
return api, router, mockCtl | ||
} | ||
|
||
func genProperty() *models.Property { | ||
return &models.Property{ | ||
Key: "bae", | ||
Value: "http://test", | ||
} | ||
} | ||
|
||
func TestCreateProperty(t *testing.T) { | ||
api, router, ctl := initPropertyAPI(t) | ||
rs := plugin.NewMockPropertyService(ctl) | ||
api.propertyService = rs | ||
|
||
property := genProperty() | ||
|
||
rs.EXPECT().CreateProperty(property).Return(nil).Times(1) | ||
// good case | ||
body, _ := json.Marshal(property) | ||
req, _ := http.NewRequest(http.MethodPost, "/v1/properties", bytes.NewReader(body)) | ||
w := httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
// empty body | ||
req, _ = http.NewRequest(http.MethodPost, "/v1/properties", nil) | ||
w = httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusBadRequest, w.Code) | ||
|
||
} | ||
|
||
func TestDeleteProperty(t *testing.T) { | ||
api, router, ctl := initPropertyAPI(t) | ||
rs := plugin.NewMockPropertyService(ctl) | ||
api.propertyService = rs | ||
|
||
property := genProperty() | ||
|
||
rs.EXPECT().DeleteProperty(gomock.Any()).Return(nil).Times(1) | ||
|
||
req, _ := http.NewRequest(http.MethodDelete, "/v1/properties/"+property.Key, nil) | ||
w := httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
} | ||
|
||
func TestListProperty(t *testing.T) { | ||
api, router, ctl := initPropertyAPI(t) | ||
rs := plugin.NewMockPropertyService(ctl) | ||
api.propertyService = rs | ||
|
||
mConf := genProperty() | ||
page := &models.Filter{ | ||
PageNo: 1, | ||
PageSize: 2, | ||
Name: "%", | ||
} | ||
// good case | ||
rs.EXPECT().ListProperty(page).Return([]models.Property{*mConf}, nil).Times(1) | ||
rs.EXPECT().CountProperty(page.Name).Return(1, nil).Times(1) | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "/v1/properties?pageNo=1&pageSize=2", nil) | ||
w := httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
|
||
page = &models.Filter{ | ||
PageNo: 1, | ||
PageSize: 20, | ||
Name: "%", | ||
} | ||
// List error case | ||
rs.EXPECT().ListProperty(page).Return([]models.Property{*mConf}, fmt.Errorf("GetResource error")).Times(1) | ||
req, _ = http.NewRequest(http.MethodGet, "/v1/properties", nil) | ||
w = httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
// Count error case | ||
rs.EXPECT().ListProperty(page).Return([]models.Property{*mConf}, nil).Times(1) | ||
rs.EXPECT().CountProperty(page.Name).Return(0, fmt.Errorf("GetResource error")).Times(1) | ||
req, _ = http.NewRequest(http.MethodGet, "/v1/properties", nil) | ||
w = httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
} | ||
|
||
func TestUpdateProperty(t *testing.T) { | ||
api, router, ctl := initPropertyAPI(t) | ||
rs := plugin.NewMockPropertyService(ctl) | ||
api.propertyService = rs | ||
|
||
property := genProperty() | ||
rs.EXPECT().UpdateProperty(property).Return(nil).Times(1) | ||
// good case | ||
body, _ := json.Marshal(property) | ||
req, _ := http.NewRequest(http.MethodPut, "/v1/properties/"+property.Key, bytes.NewReader(body)) | ||
w := httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
// empty body | ||
req, _ = http.NewRequest(http.MethodPut, "/v1/properties/"+property.Key, nil) | ||
w = httptest.NewRecorder() | ||
router.ServeHTTP(w, req) | ||
assert.Equal(t, http.StatusBadRequest, w.Code) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.