-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: #279 Stored setting up Default write controller
* Added interface for event store * Setup GORM based event store * Changed the resourced repository to have the event dispatcher injected (instead of making it implement the event dispatcher)
- Loading branch information
1 parent
d2cdb83
commit 56454cf
Showing
13 changed files
with
1,231 additions
and
327 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
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,64 @@ | ||
package rest_test | ||
|
||
import ( | ||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/labstack/echo/v4" | ||
"github.com/wepala/weos/v2/rest" | ||
"golang.org/x/net/context" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestDefaultWriteController(t *testing.T) { | ||
schema, err := openapi3.NewLoader().LoadFromFile("fixtures/blog.yaml") | ||
if err != nil { | ||
t.Fatalf("error encountered loading schema '%s'", err) | ||
} | ||
logger := &LogMock{ | ||
DebugfFunc: func(format string, args ...interface{}) { | ||
|
||
}, | ||
DebugFunc: func(args ...interface{}) { | ||
|
||
}, | ||
ErrorfFunc: func(format string, args ...interface{}) { | ||
|
||
}, | ||
ErrorFunc: func(args ...interface{}) { | ||
|
||
}, | ||
} | ||
t.Run("create a simple resource", func(t *testing.T) { | ||
repository := &RepositoryMock{ | ||
PersistFunc: func(ctxt context.Context, logger rest.Log, resources []rest.Resource) []error { | ||
return nil | ||
}, | ||
} | ||
commandDispatcher := &CommandDispatcherMock{ | ||
DispatchFunc: func(ctx context.Context, command *rest.Command, repository rest.Repository, logger rest.Log) (interface{}, error) { | ||
return nil, nil | ||
}, | ||
} | ||
controller := rest.DefaultWriteController(logger, commandDispatcher, repository, schema, nil, nil) | ||
e := echo.New() | ||
e.POST("/*", controller) | ||
resp := httptest.NewRecorder() | ||
req := httptest.NewRequest(echo.POST, "/blogs/test", strings.NewReader(`{ | ||
"@id": "/blogs/test", | ||
"@type": "http://schema.org/Blog", | ||
"title":"test" | ||
}`)) | ||
req.Header.Set(echo.HeaderContentType, "application/ld+json") | ||
e.ServeHTTP(resp, req) | ||
if resp.Code != http.StatusCreated { | ||
t.Errorf("expected status code %d, got %d", http.StatusCreated, resp.Code) | ||
} | ||
|
||
if len(commandDispatcher.DispatchCalls()) != 1 { | ||
t.Errorf("expected dispatch to be called once, got %d", len(commandDispatcher.DispatchCalls())) | ||
} | ||
|
||
}) | ||
} |
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,124 @@ | ||
package rest_test | ||
|
||
import ( | ||
"github.com/wepala/weos/v2/rest" | ||
"golang.org/x/net/context" | ||
"testing" | ||
) | ||
|
||
func TestDefaultEventDisptacher_AddSubscriber(t *testing.T) { | ||
t.Run("add subscriber for event type only", func(t *testing.T) { | ||
eventDispatcher := new(rest.GORMEventStore) | ||
err := eventDispatcher.AddSubscriber(rest.EventHandlerConfig{ | ||
Type: "create", | ||
Handler: func(ctx context.Context, logger rest.Log, event rest.Event) error { | ||
return nil | ||
}, | ||
}) | ||
if err != nil { | ||
t.Errorf("expected no error, got %s", err) | ||
} | ||
handlers := eventDispatcher.GetSubscribers("") | ||
if len(handlers) != 1 { | ||
t.Errorf("expected 1 handler, got %d", len(handlers)) | ||
} | ||
if handler, ok := handlers["create"]; !ok { | ||
t.Errorf("expected handler for create event type") | ||
} else { | ||
if handler == nil { | ||
t.Errorf("expected handler for create event type") | ||
} | ||
} | ||
}) | ||
t.Run("add subscriber for resource type and event", func(t *testing.T) { | ||
eventDispatcher := new(rest.GORMEventStore) | ||
err := eventDispatcher.AddSubscriber(rest.EventHandlerConfig{ | ||
ResourceType: "Article", | ||
Type: "create", | ||
Handler: func(ctx context.Context, logger rest.Log, event rest.Event) error { | ||
return nil | ||
}, | ||
}) | ||
if err != nil { | ||
t.Errorf("expected no error, got %s", err) | ||
} | ||
handlers := eventDispatcher.GetSubscribers("Article") | ||
if len(handlers) != 1 { | ||
t.Errorf("expected 1 handler, got %d", len(handlers)) | ||
} | ||
if handler, ok := handlers["create"]; !ok { | ||
t.Errorf("expected handler for create event type") | ||
} else { | ||
if handler == nil { | ||
t.Errorf("expected handler for create event type") | ||
} | ||
} | ||
}) | ||
t.Run("adding subscriber without handler should throw error", func(t *testing.T) { | ||
eventDispatcher := new(rest.GORMEventStore) | ||
err := eventDispatcher.AddSubscriber(rest.EventHandlerConfig{ | ||
Type: "create", | ||
}) | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
}) | ||
} | ||
|
||
func TestResourceRepository_Dispatch(t *testing.T) { | ||
logger := &LogMock{ | ||
DebugfFunc: func(format string, args ...interface{}) { | ||
|
||
}, | ||
DebugFunc: func(args ...interface{}) { | ||
|
||
}, | ||
ErrorfFunc: func(format string, args ...interface{}) { | ||
|
||
}, | ||
ErrorFunc: func(args ...interface{}) { | ||
|
||
}, | ||
} | ||
t.Run("should trigger resource specific handler and generic event type handler", func(t *testing.T) { | ||
createHandlerHit := false | ||
articleCreateHandlerHit := false | ||
eventDispatcher := new(rest.GORMEventStore) | ||
err := eventDispatcher.AddSubscriber(rest.EventHandlerConfig{ | ||
Type: "create", | ||
Handler: func(ctx context.Context, logger rest.Log, event rest.Event) error { | ||
createHandlerHit = true | ||
return nil | ||
}, | ||
}) | ||
if err != nil { | ||
t.Errorf("expected no error, got %s", err) | ||
} | ||
err = eventDispatcher.AddSubscriber(rest.EventHandlerConfig{ | ||
Type: "create", | ||
ResourceType: "Article", | ||
Handler: func(ctx context.Context, logger rest.Log, event rest.Event) error { | ||
articleCreateHandlerHit = true | ||
return nil | ||
}, | ||
}) | ||
if err != nil { | ||
t.Errorf("expected no error, got %s", err) | ||
} | ||
errors := eventDispatcher.Dispatch(context.Background(), rest.Event{ | ||
Type: "create", | ||
Meta: rest.EventMeta{ | ||
ResourceType: "Article", | ||
}, | ||
}, logger) | ||
if len(errors) != 0 { | ||
t.Errorf("expected no errors, got %d", len(errors)) | ||
} | ||
if !createHandlerHit { | ||
t.Errorf("expected create handler to be hit") | ||
} | ||
if !articleCreateHandlerHit { | ||
t.Errorf("expected article create handler to be hit") | ||
} | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -875,3 +875,4 @@ paths: | |
responses: | ||
200: | ||
description: Delete author | ||
|
Oops, something went wrong.