diff --git a/bridge/broker_connector_test.go b/bridge/broker_connector_test.go index e6206e7..e48232d 100644 --- a/bridge/broker_connector_test.go +++ b/bridge/broker_connector_test.go @@ -114,14 +114,6 @@ func TestBrokerConnector_BadConfig(t *testing.T) { "Missing address from config", &BrokerConnectorConfig{Username: "guest", Password: "guest"}, fmt.Errorf("config invalid, config missing server address")}, - { - "Missing username from config", - &BrokerConnectorConfig{ServerAddr: "somewhere:000"}, - fmt.Errorf("config invalid, config missing username")}, - { - "Missing password from config", - &BrokerConnectorConfig{Username: "hi", ServerAddr: "somewhere:000"}, - fmt.Errorf("config invalid, config missing password")}, } for _, tc := range tt { diff --git a/bus/channel_test.go b/bus/channel_test.go index bf16147..9ccad4c 100644 --- a/bus/channel_test.go +++ b/bus/channel_test.go @@ -4,6 +4,7 @@ package bus import ( + "context" "github.com/go-stomp/stomp/v3/frame" "github.com/google/uuid" "github.com/pb33f/ranch/bridge" @@ -247,6 +248,16 @@ type MockBridgeConnection struct { Id *uuid.UUID } +func (c *MockBridgeConnection) Conversation(destination string, payload []byte, opts ...func(*frame.Frame) error) (bridge.Subscription, error) { + //TODO implement me + panic("implement me") +} + +func (c *MockBridgeConnection) RequestResponse(ctx context.Context, payload []byte, opts ...func(*frame.Frame) error) (*model.Message, error) { + //TODO implement me + panic("implement me") +} + func (c *MockBridgeConnection) GetId() *uuid.UUID { return c.Id } diff --git a/bus/eventbus.go b/bus/eventbus.go index fced418..0015867 100644 --- a/bus/eventbus.go +++ b/bus/eventbus.go @@ -462,8 +462,8 @@ func (bus *transportEventBus) StartFabricEndpoint( connectionListener stompserver.RawConnectionListener, config EndpointConfig) error { if bus.fabEndpoint != nil { - return nil - // return fmt.Errorf("unable to start: fabric endpoint is already running") + //return nil + return fmt.Errorf("unable to start: fabric endpoint is already running") } if configErr := config.validate(); configErr != nil { return configErr diff --git a/bus/fabric_endpoint_test.go b/bus/fabric_endpoint_test.go index 75bace7..e1ef9b0 100644 --- a/bus/fabric_endpoint_test.go +++ b/bus/fabric_endpoint_test.go @@ -347,9 +347,9 @@ func TestFabricEndpoint_BridgeMessage(t *testing.T) { id1 := uuid.New() req1, _ := json.Marshal(model.Request{ - Request: "test-request", - Payload: "test-rq", - Id: &id1, + RequestCommand: "test-request", + Payload: "test-rq", + Id: &id1, }) wg.Add(1) @@ -363,9 +363,9 @@ func TestFabricEndpoint_BridgeMessage(t *testing.T) { id2 := uuid.New() req2, _ := json.Marshal(model.Request{ - Request: "test-request2", - Payload: "test-rq2", - Id: &id2, + RequestCommand: "test-request2", + Payload: "test-rq2", + Id: &id2, }) wg.Wait() @@ -378,14 +378,14 @@ func TestFabricEndpoint_BridgeMessage(t *testing.T) { receivedReq := messages[0].Payload.(*model.Request) - assert.Equal(t, receivedReq.Request, "test-request") + assert.Equal(t, receivedReq.RequestCommand, "test-request") assert.Equal(t, receivedReq.Payload, "test-rq") assert.Equal(t, *receivedReq.Id, id1) assert.Nil(t, receivedReq.BrokerDestination) receivedReq2 := messages[1].Payload.(*model.Request) - assert.Equal(t, receivedReq2.Request, "test-request2") + assert.Equal(t, receivedReq2.RequestCommand, "test-request2") assert.Equal(t, receivedReq2.Payload, "test-rq2") assert.Equal(t, *receivedReq2.Id, id2) assert.Equal(t, receivedReq2.BrokerDestination.ConnectionId, "con2") diff --git a/bus/store_sync_service_test.go b/bus/store_sync_service_test.go index d5381f2..bff337b 100644 --- a/bus/store_sync_service_test.go +++ b/bus/store_sync_service_test.go @@ -53,16 +53,16 @@ func TestStoreSyncService_OpenStoreErrors(t *testing.T) { id := uuid.New() bus.SendRequestMessage(syncChan, "invalid-request", nil) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: "invalid-payload", - Id: &id, + RequestCommand: openStoreRequest, + Payload: "invalid-payload", + Id: &id, }, nil) wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: make(map[string]interface{}), - Id: &id, + RequestCommand: openStoreRequest, + Payload: make(map[string]interface{}), + Id: &id, }, nil) wg.Wait() @@ -72,9 +72,9 @@ func TestStoreSyncService_OpenStoreErrors(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "non-existing-store"}, - Id: &id, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "non-existing-store"}, + Id: &id, }, nil) wg.Wait() @@ -111,8 +111,8 @@ func TestStoreSyncService_OpenStore(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) wg.Wait() @@ -135,8 +135,8 @@ func TestStoreSyncService_OpenStore(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) wg.Wait() @@ -157,8 +157,8 @@ func TestStoreSyncService_OpenStore(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan2, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) wg.Wait() @@ -230,33 +230,33 @@ func TestStoreSyncService_CloseStore(t *testing.T) { wg.Add(2) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) bus.SendRequestMessage(syncChan2, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) wg.Wait() assert.Equal(t, len(service.syncStoreListeners["test-store"].clientSyncChannels), 2) bus.SendRequestMessage(syncChan, &model.Request{ - Request: closeStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, - Id: &id, + RequestCommand: closeStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, + Id: &id, }, nil) wg.Add(2) bus.SendRequestMessage(syncChan, &model.Request{ - Request: closeStoreRequest, - Payload: make(map[string]interface{}), - Id: &id, + RequestCommand: closeStoreRequest, + Payload: make(map[string]interface{}), + Id: &id, }, nil) bus.SendRequestMessage(syncChan2, &model.Request{ - Request: closeStoreRequest, - Payload: map[string]interface{}{"storeId": ""}, - Id: &id, + RequestCommand: closeStoreRequest, + Payload: map[string]interface{}{"storeId": ""}, + Id: &id, }, nil) wg.Wait() @@ -272,21 +272,21 @@ func TestStoreSyncService_CloseStore(t *testing.T) { service.lock.Unlock() bus.SendRequestMessage(syncChan2, &model.Request{ - Request: closeStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, - Id: &id, + RequestCommand: closeStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, + Id: &id, }, nil) wg.Add(2) bus.SendRequestMessage(syncChan2, &model.Request{ - Request: closeStoreRequest, - Payload: make(map[string]interface{}), - Id: &id, + RequestCommand: closeStoreRequest, + Payload: make(map[string]interface{}), + Id: &id, }, nil) bus.SendRequestMessage(syncChan, &model.Request{ - Request: closeStoreRequest, - Payload: map[string]interface{}{"storeId": ""}, - Id: &id, + RequestCommand: closeStoreRequest, + Payload: map[string]interface{}{"storeId": ""}, + Id: &id, }, nil) wg.Wait() @@ -323,9 +323,9 @@ func TestStoreSyncService_UpdateStoreErrors(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, - Payload: map[string]interface{}{}, - Id: &id, + RequestCommand: updateStoreRequest, + Payload: map[string]interface{}{}, + Id: &id, }, nil) wg.Wait() @@ -333,9 +333,9 @@ func TestStoreSyncService_UpdateStoreErrors(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, - Id: &id, + RequestCommand: updateStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, + Id: &id, }, nil) wg.Wait() @@ -343,9 +343,9 @@ func TestStoreSyncService_UpdateStoreErrors(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store", "itemId": "item1"}, - Id: &id, + RequestCommand: updateStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store", "itemId": "item1"}, + Id: &id, }, nil) wg.Wait() @@ -392,12 +392,12 @@ func TestStoreSyncService_UpdateStore(t *testing.T) { wg.Add(2) bus.SendRequestMessage(syncChan, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) bus.SendRequestMessage(syncChan2, &model.Request{ - Request: openStoreRequest, - Payload: map[string]interface{}{"storeId": "test-store"}, + RequestCommand: openStoreRequest, + Payload: map[string]interface{}{"storeId": "test-store"}, }, nil) wg.Wait() @@ -407,7 +407,7 @@ func TestStoreSyncService_UpdateStore(t *testing.T) { wg.Add(2) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, + RequestCommand: updateStoreRequest, Payload: map[string]interface{}{ "storeId": "test-store", "itemId": "item3", @@ -469,7 +469,7 @@ func TestStoreSyncService_UpdateStore(t *testing.T) { assert.Equal(t, syncResp1[3], syncResp2[3]) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, + RequestCommand: updateStoreRequest, Payload: map[string]interface{}{ "storeId": "test-store", "itemId": "item4", @@ -478,7 +478,7 @@ func TestStoreSyncService_UpdateStore(t *testing.T) { wg.Add(2) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, + RequestCommand: updateStoreRequest, Payload: map[string]interface{}{ "storeId": "test-store", "itemId": "item3", @@ -501,7 +501,7 @@ func TestStoreSyncService_UpdateStore(t *testing.T) { wg.Add(1) bus.SendRequestMessage(syncChan, &model.Request{ - Request: updateStoreRequest, + RequestCommand: updateStoreRequest, Payload: map[string]interface{}{ "storeId": "test-store", "itemId": "item3", diff --git a/model/message.go b/model/message.go index 502456b..f6e9eed 100644 --- a/model/message.go +++ b/model/message.go @@ -74,5 +74,7 @@ func decodeResponsePaylod(resp *Response, typ interface{}) error { if resp.Error { return errors.New(resp.ErrorMessage) } - return mapstructure.Decode(resp.Payload, typ) + err := mapstructure.Decode(resp.Payload, typ) + + return err } diff --git a/model/message_test.go b/model/message_test.go index 70d5781..ca931a0 100644 --- a/model/message_test.go +++ b/model/message_test.go @@ -18,7 +18,7 @@ func TestMessage_CastPayloadToType_HappyPath(t *testing.T) { // assert assert.Nil(t, err) - assert.EqualValues(t, "dummy-value-coming-through", dest.Request) + assert.EqualValues(t, "dummy-value-coming-through", dest.RequestCommand) } func TestMessage_CastPayloadToType_BadPayload(t *testing.T) { @@ -35,7 +35,7 @@ func TestMessage_CastPayloadToType_BadPayload(t *testing.T) { // assert assert.NotNil(t, err) assert.Contains(t, err.Error(), "failed to unmarshal payload") - assert.NotEqual(t, "dummy-value-coming-through", dest.Request) + assert.NotEqual(t, "dummy-value-coming-through", dest.RequestCommand) } func TestMessage_CastPayloadToType_NonPointer(t *testing.T) { @@ -48,7 +48,7 @@ func TestMessage_CastPayloadToType_NonPointer(t *testing.T) { // assert assert.NotNil(t, err) - assert.NotEqual(t, "dummy-value-coming-through", dest.Request) + assert.NotEqual(t, "dummy-value-coming-through", dest.RequestCommand) } func TestMessage_CastPayloadToType_NilPointer(t *testing.T) { @@ -108,7 +108,7 @@ func TestMessage_CastPayloadToType_ErrorResponse(t *testing.T) { func getNewTestMessage() *Message { rspPayload := &Response{ Id: &uuid.UUID{}, - Payload: Request{Request: "dummy-value-coming-through"}, + Payload: Request{RequestCommand: "dummy-value-coming-through"}, } jsonEncoded, _ := json.Marshal(rspPayload) diff --git a/model/request.go b/model/request.go index 0c34cd6..73f48b8 100644 --- a/model/request.go +++ b/model/request.go @@ -13,7 +13,7 @@ type Request struct { Id *uuid.UUID `json:"id,omitempty"` Destination string `json:"channel,omitempty"` Payload interface{} `json:"payload,omitempty"` - RequestCommand string `json:"request,omitempty"` + RequestCommand string `json:"request,omitempty" mapstructure:"request"` HttpRequest *http.Request `json:"-"` HttpResponseWriter http.ResponseWriter `json:"-"` // Populated if the request was sent on a "private" channel and diff --git a/plank/pkg/server/endpointer_handler_factory_test.go b/plank/pkg/server/endpointer_handler_factory_test.go index e476bab..653a8e7 100644 --- a/plank/pkg/server/endpointer_handler_factory_test.go +++ b/plank/pkg/server/endpointer_handler_factory_test.go @@ -1,7 +1,6 @@ package server import ( - "encoding/json" "fmt" "github.com/google/uuid" "github.com/pb33f/ranch/bus" @@ -64,7 +63,7 @@ func TestBuildEndpointHandler_SuccessResponse(t *testing.T) { uId := &uuid.UUID{} msgChan <- &model.Message{Payload: &model.Response{ Id: uId, - Payload: []byte("{\"error\": false}"), + Payload: "{\"error\": false}", }} return model.Request{ Id: uId, @@ -83,15 +82,16 @@ func TestBuildEndpointHandler_ErrorResponse(t *testing.T) { ps := NewPlatformServer(config).(*platformServer) ps.eventbus = b + expected := `{"error": true}` + msgChan := make(chan *model.Message, 1) uId := &uuid.UUID{} rsp := &model.Response{ Id: uId, - Payload: "{\"error\": true}", + Payload: expected, ErrorCode: 500, Error: true, } - expected, _ := json.Marshal(rsp.Payload) assert.HTTPBodyContains(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request { msgChan <- &model.Message{Payload: rsp} @@ -101,7 +101,7 @@ func TestBuildEndpointHandler_ErrorResponse(t *testing.T) { RequestCommand: "test-request", } - }, 5*time.Second, msgChan), "GET", "http://localhost", nil, string(expected)) + }, 5*time.Second, msgChan), "GET", "http://localhost", nil, expected) } func TestBuildEndpointHandler_ErrorResponseAlternative(t *testing.T) { diff --git a/plank/pkg/server/helpers_test.go b/plank/pkg/server/helpers_test.go index 6266163..c0116ff 100644 --- a/plank/pkg/server/helpers_test.go +++ b/plank/pkg/server/helpers_test.go @@ -29,10 +29,6 @@ func TestGeneratePlatformServerConfig_Default(t *testing.T) { assert.EqualValues(t, wd, config.RootDir) assert.Empty(t, config.StaticDir) assert.EqualValues(t, 5*time.Minute, config.ShutdownTimeout) - assert.EqualValues(t, "stdout", config.LogConfig.OutputLog) - assert.EqualValues(t, "stdout", config.LogConfig.AccessLog) - assert.EqualValues(t, "stderr", config.LogConfig.ErrorLog) - assert.EqualValues(t, wd, config.LogConfig.Root) assert.False(t, config.Debug) assert.False(t, config.NoBanner) assert.EqualValues(t, time.Minute, config.RestBridgeTimeout) @@ -104,10 +100,6 @@ func TestGeneratePlatformServerConfig_ConfigFile(t *testing.T) { assert.EqualValues(t, "./", config.RootDir) assert.Empty(t, config.StaticDir) assert.EqualValues(t, 5*time.Minute, config.ShutdownTimeout) - assert.EqualValues(t, "stdout", config.LogConfig.OutputLog) - assert.EqualValues(t, "access.log", config.LogConfig.AccessLog) - assert.EqualValues(t, "errors.log", config.LogConfig.ErrorLog) - assert.EqualValues(t, ".", config.LogConfig.Root) assert.True(t, config.Debug) assert.True(t, config.NoBanner) assert.EqualValues(t, time.Minute, config.RestBridgeTimeout) diff --git a/plank/pkg/server/initialize_rest_bridge_override_test.go b/plank/pkg/server/initialize_rest_bridge_override_test.go index 7c159e3..9602e00 100644 --- a/plank/pkg/server/initialize_rest_bridge_override_test.go +++ b/plank/pkg/server/initialize_rest_bridge_override_test.go @@ -6,9 +6,7 @@ import ( "github.com/pb33f/ranch/bus" "github.com/pb33f/ranch/model" "github.com/pb33f/ranch/plank/services" - "github.com/pb33f/ranch/plank/utils" "github.com/pb33f/ranch/service" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "net/http" "os" @@ -31,8 +29,6 @@ func TestInitialize_DebugLogging(t *testing.T) { // act _, _, _ = CreateTestServer(cfg) - // assert - assert.EqualValues(t, logrus.DebugLevel, utils.Log.GetLevel()) } func TestInitialize_RestBridgeOverride(t *testing.T) { diff --git a/plank/pkg/server/server_test.go b/plank/pkg/server/server_test.go index 5448a44..e37d3fe 100644 --- a/plank/pkg/server/server_test.go +++ b/plank/pkg/server/server_test.go @@ -16,7 +16,6 @@ import ( "io/ioutil" "net/http" "os" - "path/filepath" "strings" "sync" "testing" @@ -41,20 +40,6 @@ func TestNewPlatformServer_EmptyRootDir(t *testing.T) { assert.Equal(t, wd, newConfig.RootDir) } -func TestNewPlatformServer_FileLog(t *testing.T) { - defer func() { - _ = os.Remove(filepath.Join(os.TempDir(), "testlog.log")) - }() - - newBus := bus.ResetBus() - service.ResetServiceRegistry() - port := GetTestPort() - newConfig := GetBasicTestServerConfig(os.TempDir(), filepath.Join(os.TempDir(), "testlog.log"), "stdout", "stderr", port, true) - ps := NewPlatformServer(newConfig) - ps.(*platformServer).eventbus = newBus - assert.FileExists(t, filepath.Join(os.TempDir(), "testlog.log")) -} - func TestPlatformServer_StartServer(t *testing.T) { newBus := bus.ResetBus() service.ResetServiceRegistry() diff --git a/service/fabric_core_test.go b/service/fabric_core_test.go index 285f2da..6018d79 100644 --- a/service/fabric_core_test.go +++ b/service/fabric_core_test.go @@ -47,8 +47,8 @@ func TestFabricCore_SendMethods(t *testing.T) { id := uuid.New() req := model.Request{ - Id: &id, - Request: "test-request", + Id: &id, + RequestCommand: "test-request", BrokerDestination: &model.BrokerDestinationConfig{ Destination: "test", }, @@ -68,7 +68,7 @@ func TestFabricCore_SendMethods(t *testing.T) { assert.Equal(t, response.BrokerDestination.Destination, "test") wg.Add(1) - h := make(map[string]string) + h := make(map[string]any) h["hello"] = "there" core.SendResponseWithHeaders(&req, "test-response-with-headers", h) wg.Wait() @@ -98,7 +98,7 @@ func TestFabricCore_SendMethods(t *testing.T) { wg.Add(1) - h = make(map[string]string) + h = make(map[string]any) h["chicken"] = "nugget" core.SendErrorResponseWithHeaders(&req, 422, "test-header-error", h) wg.Wait() @@ -115,7 +115,7 @@ func TestFabricCore_SendMethods(t *testing.T) { wg.Add(1) - h = make(map[string]string) + h = make(map[string]any) h["potato"] = "dog" core.SendErrorResponseWithHeadersAndPayload(&req, 500, "test-header-payload-error", "oh my!", h) wg.Wait() @@ -313,7 +313,7 @@ func TestFabricCore_SetDefaultJSONHeadersEmpty(t *testing.T) { } wg.Add(1) - core.SendResponseWithHeaders(&req, "test-response", map[string]string{"Content-Type": "pizza/cake"}) + core.SendResponseWithHeaders(&req, "test-response", map[string]any{"Content-Type": "pizza/cake"}) wg.Wait() response := lastMessage.Payload.(*model.Response)