Skip to content

Commit

Permalink
feature:WEOS-1131
Browse files Browse the repository at this point in the history
- Added 2 controller test, 1 operation initialize test
  • Loading branch information
RandyDeo committed Feb 7, 2022
1 parent 2071a20 commit da45dd5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
108 changes: 108 additions & 0 deletions controllers/rest/controller_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
Expand Down Expand Up @@ -1610,4 +1611,111 @@ func TestStandardControllers_DeleteID(t *testing.T) {
t.Errorf("expected response code to be %d, got %d", 200, response.StatusCode)
}
})
t.Run("basic delete based on simple content type id parameter in path. (No weosID)", func(t *testing.T) {
mockEntity1 := &model.ContentEntity{}
mockEntity1.Property = mockBlog

mockInterface1 := map[string]interface{}{"title": "Test Blog", "description": "testing description", "id": "12", "sequence_no": "1"}

eventMock1 := &EventRepositoryMock{
GetAggregateSequenceNumberFunc: func(ID string) (int64, error) {
return 2, nil
},
}

projection1 := &ProjectionMock{
GetByKeyFunc: func(ctxt context.Context, entityFactory model.EntityFactory, identifiers map[string]interface{}) (map[string]interface{}, error) {
return mockInterface1, nil
},
GetByEntityIDFunc: func(ctxt context.Context, entityFactory model.EntityFactory, id string) (map[string]interface{}, error) {
return mockInterface1, nil
},
GetContentEntityFunc: func(ctx context.Context, entityFactory model.EntityFactory, weosID string) (*model.ContentEntity, error) {
return mockEntity1, nil
},
}

paramName := "id"

accountID := "Delete Blog"
path := swagger.Paths.Find("/blogs/:" + paramName)
entityFactory := &EntityFactoryMock{
NameFunc: func() string {
return "Blog"
},
SchemaFunc: func() *openapi3.Schema {
return swagger.Components.Schemas["Blog"].Value
},
}
resp := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodDelete, "/blogs/12", nil)
req.Header.Set(weoscontext.HeaderXAccountID, accountID)
mw := rest.Context(restAPI, projection, dispatcher, eventMock, entityFactory, path, path.Delete)
deleteMw := rest.DeleteMiddleware(restAPI, projection1, dispatcher, eventMock1, entityFactory, path, path.Delete)
controller := rest.DeleteController(restAPI, projection1, dispatcher, eventMock1, entityFactory)
e.DELETE("/blogs/:"+paramName, controller, mw, deleteMw)
e.ServeHTTP(resp, req)

response := resp.Result()
defer response.Body.Close()

if response.StatusCode != 404 {
t.Errorf("expected response code to be %d, got %d", 404, response.StatusCode)
}
})

t.Run("basic delete based on simple content type id parameter in path. (No weosID)", func(t *testing.T) {
mockEntity1 := &model.ContentEntity{}
mockEntity1.Property = mockBlog

mockInterface1 := map[string]interface{}{"title": "Test Blog", "description": "testing description", "weos_id": "123456qwerty", "id": "12", "sequence_no": "1"}

eventMock1 := &EventRepositoryMock{
GetAggregateSequenceNumberFunc: func(ID string) (int64, error) {
return 2, nil
},
}

err1 := fmt.Errorf("this is an error")

projection1 := &ProjectionMock{
GetByKeyFunc: func(ctxt context.Context, entityFactory model.EntityFactory, identifiers map[string]interface{}) (map[string]interface{}, error) {
return nil, err1
},
GetByEntityIDFunc: func(ctxt context.Context, entityFactory model.EntityFactory, id string) (map[string]interface{}, error) {
return mockInterface1, nil
},
GetContentEntityFunc: func(ctx context.Context, entityFactory model.EntityFactory, weosID string) (*model.ContentEntity, error) {
return mockEntity1, nil
},
}

paramName := "id"

accountID := "Delete Blog"
path := swagger.Paths.Find("/blogs/:" + paramName)
entityFactory := &EntityFactoryMock{
NameFunc: func() string {
return "Blog"
},
SchemaFunc: func() *openapi3.Schema {
return swagger.Components.Schemas["Blog"].Value
},
}
resp := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodDelete, "/blogs/12", nil)
req.Header.Set(weoscontext.HeaderXAccountID, accountID)
mw := rest.Context(restAPI, projection, dispatcher, eventMock, entityFactory, path, path.Delete)
deleteMw := rest.DeleteMiddleware(restAPI, projection1, dispatcher, eventMock1, entityFactory, path, path.Delete)
controller := rest.DeleteController(restAPI, projection1, dispatcher, eventMock1, entityFactory)
e.DELETE("/blogs/:"+paramName, controller, mw, deleteMw)
e.ServeHTTP(resp, req)

response := resp.Result()
defer response.Body.Close()

if response.StatusCode != 500 {
t.Errorf("expected response code to be %d, got %d", 404, response.StatusCode)
}
})
}
11 changes: 11 additions & 0 deletions controllers/rest/operation_initializers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func TestStandardInitializer(t *testing.T) {
api.RegisterController("ListController", rest.ListController)
api.RegisterController("UpdateController", rest.UpdateController)
api.RegisterController("ViewController", rest.ViewController)
api.RegisterController("DeleteController", rest.DeleteController)
t.Run("attach standard create", func(t *testing.T) {
ctxt, err := rest.StandardInitializer(baseCtxt, api, "/blogs", http.MethodPost, api.Swagger, api.Swagger.Paths["/blogs"], api.Swagger.Paths["/blogs"].Post)
if err != nil {
Expand Down Expand Up @@ -215,4 +216,14 @@ func TestStandardInitializer(t *testing.T) {
t.Fatalf("expected controller to be in the context")
}
})
t.Run("attach standard delete", func(t *testing.T) {
ctxt, err := rest.StandardInitializer(baseCtxt, api, "/blogs/{id}", http.MethodDelete, api.Swagger, api.Swagger.Paths["/blogs/{id}"], api.Swagger.Paths["/blogs/{id}"].Delete)
if err != nil {
t.Fatalf("unexpected error loading api '%s'", err)
}
controller := rest.GetOperationController(ctxt)
if controller == nil {
t.Fatalf("expected controller to be in the context")
}
})
}

0 comments on commit da45dd5

Please sign in to comment.