Skip to content

Commit

Permalink
move model into same model files + update a test
Browse files Browse the repository at this point in the history
  • Loading branch information
kojisaikiAtSony committed Apr 26, 2024
1 parent 06db0ee commit 29b45a3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 104 deletions.
56 changes: 56 additions & 0 deletions app/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,62 @@ func (r *ListQueueRequest) SetAttributesFromForm(values url.Values) {
r.QueueNamePrefix = values.Get("QueueNamePrefix")
}

/*** Send Message Request */

func NewSendMessageRequest() *SendMessageRequest {
return &SendMessageRequest{
MessageAttributes: make(map[string]MessageAttributes),
MessageSystemAttributes: make(map[string]MessageAttributes),
}
}

type SendMessageRequest struct {
DelaySeconds int `json:"Del1aySeconds" schema:"DelaySeconds"`
MessageAttributes map[string]MessageAttributes `json:"MessageAttributes" schema:"MessageAttributes"`
MessageBody string `json:"MessageBody" schema:"MessageBody"`
MessageDeduplicationId string `json:"MessageDeduplicationId" schema:"MessageDeduplicationId"`
MessageGroupId string `json:"MessageGroupId" schema:"MessageGroupId"`
MessageSystemAttributes map[string]MessageAttributes `json:"MessageSystemAttributes" schema:"MessageSystemAttributes"`
QueueUrl string `json:"QueueUrl" schema:"QueueUrl"`
}
type MessageAttributes struct {
BinaryListValues []string `json:"BinaryListValues"` // goaws does not supported yet
BinaryValue string `json:"BinaryValue"`
DataType string `json:"DataType"`
StringListValues []string `json:"StringListValues"` // goaws does not supported yet
StringValue string `json:"StringValue"`
}

func (r *SendMessageRequest) SetAttributesFromForm(values url.Values) {
for i := 1; true; i++ {
nameKey := fmt.Sprintf("MessageAttribute.%d.Name", i)
name := values.Get(nameKey)
if name == "" {
break
}

dataTypeKey := fmt.Sprintf("MessageAttribute.%d.Value.DataType", i)
dataType := values.Get(dataTypeKey)
if dataType == "" {
log.Warnf("DataType of MessageAttribute %s is missing, MD5 checksum will most probably be wrong!\n", name)
continue
}

stringValue := values.Get(fmt.Sprintf("MessageAttribute.%d.Value.StringValue", i))
binaryValue := values.Get(fmt.Sprintf("MessageAttribute.%d.Value.BinaryValue", i))

r.MessageAttributes[name] = MessageAttributes{
DataType: dataType,
StringValue: stringValue,
BinaryValue: binaryValue,
}

if _, ok := r.MessageAttributes[name]; !ok {
log.Warnf("StringValue or BinaryValue of MessageAttribute %s is missing, MD5 checksum will most probably be wrong!\n", name)
}
}
}

// TODO - copy Attributes for SNS

// TODO - there are FIFO attributes and things too
Expand Down
22 changes: 22 additions & 0 deletions app/models/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,25 @@ func (r ListQueuesResponse) GetResult() interface{} {
func (r ListQueuesResponse) GetRequestId() string {
return r.Metadata.RequestId
}

/*** Send Message Response */
type SendMessageResult struct {
MD5OfMessageAttributes string `xml:"MD5OfMessageAttributes"`
MD5OfMessageBody string `xml:"MD5OfMessageBody"`
MessageId string `xml:"MessageId"`
SequenceNumber string `xml:"SequenceNumber"`
}

type SendMessageResponse struct {
Xmlns string `xml:"xmlns,attr"`
Result SendMessageResult `xml:"SendMessageResult"`
Metadata app.ResponseMetadata `xml:"ResponseMetadata"`
}

func (r SendMessageResponse) GetResult() interface{} {
return r.Result
}

func (r SendMessageResponse) GetRequestId() string {
return r.Metadata.RequestId
}
88 changes: 0 additions & 88 deletions app/models/send_message_model.go

This file was deleted.

59 changes: 43 additions & 16 deletions smoke_tests/sqs_send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package smoke_tests

import (
"context"
"encoding/xml"
"net/http"
"testing"

"github.com/Admiral-Piett/goaws/app"
af "github.com/Admiral-Piett/goaws/app/fixtures"
"github.com/Admiral-Piett/goaws/app/utils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/gavv/httpexpect/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,28 +31,51 @@ func Test_SendMessageV1_json_no_attributes(t *testing.T) {
})
targetQueueUrl := sdkResponse.QueueUrl

// TODO: I tried invoke without awssdk but there was an unexpected/unknown error on Xml.Decode on CreateQueue API logic...
// e := httpexpect.Default(t, server.URL)
// r := e.POST("/").
// WithForm(sf.CreateQueuesRequestBodyXML).
// Expect().
// Status(http.StatusOK).
// Body().Raw()
// r2 := models.CreateQueueResponse{}
// xml.Unmarshal([]byte(r), &r2)
// targetQueueUrl := r2.Result.QueueUrl
// Assert no messages in the queue before sending message
getQueueAttributesBodyXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
Attribute1 string `xml:"AttributeName.1"`
QueueUrl string `xml:"QueueUrl"`
}{
Action: "GetQueueAttributes",
Version: "2012-11-05",
Attribute1: "All",
QueueUrl: *targetQueueUrl,
}
e := httpexpect.Default(t, server.URL)
r := e.POST("/").
WithForm(getQueueAttributesBodyXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r2 := app.GetQueueAttributesResponse{}
xml.Unmarshal([]byte(r), &r2)
for _, attr := range r2.Result.Attrs {
if attr.Name == "ApproximateNumberOfMessages" {
assert.Equal(t, "0", attr.Value)
}
}

// Target test
// Target test: send a message
targetMessageBody := "Test_SendMessageV1_json_no_attributes"
// sdkConfig, _ := config.LoadDefaultConfig(context.TODO())
// sdkConfig.BaseEndpoint = aws.String(server.URL)
// sqsClient := sqs.NewFromConfig(sdkConfig)
sendMessageOutput, _ := sqsClient.SendMessage(context.TODO(), &sqs.SendMessageInput{
QueueUrl: targetQueueUrl,
MessageBody: &targetMessageBody,
})

assert.NotNil(t, sendMessageOutput.MessageId)

// TODO: Assert message count in queue with GetQueueAttributes API
// Assert 1 message in the queue
r = e.POST("/").
WithForm(getQueueAttributesBodyXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r2 = app.GetQueueAttributesResponse{}
xml.Unmarshal([]byte(r), &r2)
for _, attr := range r2.Result.Attrs {
if attr.Name == "ApproximateNumberOfMessages" {
assert.Equal(t, "1", attr.Value)
}
}
}

0 comments on commit 29b45a3

Please sign in to comment.