Skip to content

Commit

Permalink
[TEST] - added more test validations
Browse files Browse the repository at this point in the history
  • Loading branch information
brienze1 committed Aug 31, 2022
1 parent d58f6d2 commit 0579638
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 12 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ Example of how the line should look like:
"operation": "BUY",
"symbol": "BTC",
"start_time": "2007-12-03 10:15:30",
"available_amount": 1233.32
}
```

Expand Down Expand Up @@ -398,7 +397,6 @@ using GitHub actions. Local environment is created using localstack for testing
- [x] Implement Behaviour tests (BDD)
- [x] Implement Unit tests
- [x] Implement application logic
- [ ] Fix payload sent to SNS to include operation variables
- [x] Create Dockerfile
- [x] Create Docker compose for local infrastructure
- [x] Document everything in Readme
Expand Down
9 changes: 9 additions & 0 deletions internal/operation-hub/domain/enum/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ var values = map[Summary]int{
func (s Summary) Value() int {
return values[s]
}

func (s Summary) Name() string {
return string(s)
}

func (s Summary) Summary(summary string) Summary {
switch summary {
case string(StrongBuy):
Expand All @@ -47,3 +52,7 @@ func (s Summary) OperationType() operation_type.OperationType {
}
return operation_type.None
}

func (s Summary) OperationTypeString() string {
return string(s.OperationType())
}
22 changes: 22 additions & 0 deletions internal/operation-hub/domain/model/operation_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package model

import (
"github.com/brienze1/crypto-robot-operation-hub/internal/operation-hub/domain/enum/summary"
"time"
)

type OperationRequest struct {
ClientId string `json:"client_id"`
Operation string `json:"operation"`
Symbol string `json:"symbol"`
StartTime string `json:"start_time"`
}

func NewOperationRequest(client Client, summary summary.Summary) *OperationRequest {
return &OperationRequest{
ClientId: client.Id,
Operation: summary.OperationTypeString(),
Symbol: summary.Name(),
StartTime: time.Now().String(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o *operationUseCase) TriggerOperations(operationSummary summary.Summary) e
}

for _, client := range *clients {
if err := o.eventService.Send(client); err != nil {
if err := o.eventService.Send(model.NewOperationRequest(client, operationSummary)); err != nil {
return o.abort(err, "Error while trying to send event")
}
}
Expand Down
8 changes: 7 additions & 1 deletion test/integrated/features/trigger_operations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Feature: Trigger operations
And sns service is "up"
When I receive message with summary equals "STRONG_BUY"
Then there should be 1 messages sent via sns
And sns messages payload should have all client_id's got from clients table
And sns messages payload symbol should be equal "STRONG_BUY"
And sns messages payload operation should be equal "BUY"
And process should exit with 0

Scenario: Trigger operation for six clients
Expand All @@ -19,4 +22,7 @@ Feature: Trigger operations
And sns service is "up"
When I receive message with summary equals "STRONG_BUY"
Then there should be 6 messages sent via sns
And process should exit with 0
And sns messages payload should have all client_id's got from clients table
And sns messages payload symbol should be equal "STRONG_BUY"
And sns messages payload operation should be equal "BUY"
And process should exit with 0
76 changes: 68 additions & 8 deletions test/integrated/step_definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"strconv"
"time"
)

Expand All @@ -33,6 +32,9 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^sns service is "([^"]*)"$`, snsServiceIs)
ctx.Step(`^I receive message with summary equals "([^"]*)"$`, iReceiveMessageWithSummaryEquals)
ctx.Step(`^there should be (\d+) messages sent via sns$`, thereShouldBeMessagesSentViaSns)
ctx.Step(`^sns messages payload should have all client_id\'s got from clients table$`, snsMessagesPayloadShouldHaveAllClientIdsGotFromClientsTable)
ctx.Step(`^sns messages payload symbol should be equal "([^"]*)"$`, snsMessagesPayloadSymbolShouldBeEqual)
ctx.Step(`^sns messages payload operation should be equal "([^"]*)"$`, snsMessagesPayloadOperationShouldBeEqual)
ctx.Step(`^process should exit with (\d+)$`, processShouldExitWith)
}

Expand All @@ -53,6 +55,7 @@ var (
dynamoDBClients []model.Client
snsClientError error
snsClientPublishCounter = 0
snsClientPublishInputs []model.OperationRequest
handlerError error
)

Expand All @@ -77,8 +80,11 @@ func (d *dynamoDBMock) Scan(_ context.Context, _ *dynamodb.ScanInput, _ ...func(
}, dynamoDBError
}

func (s *snsClientMock) Publish(_ context.Context, _ *sns.PublishInput, _ ...func(*sns.Options)) (*sns.PublishOutput, error) {
func (s *snsClientMock) Publish(_ context.Context, input *sns.PublishInput, _ ...func(*sns.Options)) (*sns.PublishOutput, error) {
snsClientPublishCounter++
request := model.OperationRequest{}
_ = json.Unmarshal([]byte(*input.Message), &request)
snsClientPublishInputs = append(snsClientPublishInputs, request)
return nil, snsClientError
}

Expand Down Expand Up @@ -127,6 +133,7 @@ func binanceApiIs(status string) error {

func snsServiceIs(status string) error {
snsClientPublishCounter = 0
snsClientPublishInputs = []model.OperationRequest{}
config.DependencyInjector().SNSClient = &snsClientMock{}

if status != "up" {
Expand All @@ -149,13 +156,57 @@ func iReceiveMessageWithSummaryEquals(value string) error {
}

func thereShouldBeMessagesSentViaSns(numberOfMessages int) error {
if snsClientPublishCounter != numberOfMessages {
return errors.New("sns should have sent " +
strconv.Itoa(numberOfMessages) +
", but sent " +
strconv.Itoa(snsClientPublishCounter) +
"instead")
err := assertEqual(snsClientPublishCounter, numberOfMessages)
if err != nil {
return err
}

err = assertEqual(len(snsClientPublishInputs), numberOfMessages)
if err != nil {
return err
}

return err
}

func snsMessagesPayloadShouldHaveAllClientIdsGotFromClientsTable() error {
for _, client := range dynamoDBClients {
found := false

for _, request := range snsClientPublishInputs {
err := assertEqual(request.ClientId, client.Id)
if err == nil {
found = true
}
}

if !found {
return errors.New("client id should have been sent to sns")
}
}

return nil
}

func snsMessagesPayloadSymbolShouldBeEqual(value string) error {
for _, request := range snsClientPublishInputs {
err := assertEqual(request.Symbol, value)
if err != nil {
return err
}
}

return nil
}

func snsMessagesPayloadOperationShouldBeEqual(value string) error {
for _, request := range snsClientPublishInputs {
err := assertEqual(request.Operation, value)
if err != nil {
return err
}
}

return nil
}

Expand All @@ -168,6 +219,15 @@ func processShouldExitWith(status int) error {
return nil
}

func assertEqual(val1, val2 interface{}) error {
if val1 == val2 {
return nil
}
val1String, _ := json.Marshal(val1)
val2String, _ := json.Marshal(val2)
return errors.New(string(val1String) + " should be equal to " + string(val2String))
}

func createSQSEvent(summary summary.Summary) *events.SQSEvent {
analysisDto := dto2.AnalysisDto{
Summary: summary,
Expand Down

0 comments on commit 0579638

Please sign in to comment.