Skip to content

Commit

Permalink
Add PublishBatch for AWS JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dhumphreys01 authored and Admiral-Piett committed Oct 28, 2024
1 parent 4e06d3d commit 3e612da
Show file tree
Hide file tree
Showing 32 changed files with 3,406 additions and 1,809 deletions.
94 changes: 47 additions & 47 deletions app/gosns/delete_topic.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
package gosns

import (
"net/http"
"strings"

"github.com/Admiral-Piett/goaws/app"
"github.com/Admiral-Piett/goaws/app/common"
"github.com/Admiral-Piett/goaws/app/interfaces"
"github.com/Admiral-Piett/goaws/app/models"
"github.com/Admiral-Piett/goaws/app/utils"

log "github.com/sirupsen/logrus"
)

func DeleteTopicV1(req *http.Request) (int, interfaces.AbstractResponseBody) {
requestBody := models.NewDeleteTopicRequest()
ok := utils.REQUEST_TRANSFORMER(requestBody, req, false)
if !ok {
log.Error("Invalid Request - DeleteTopicV1")
return utils.CreateErrorResponseV1("InvalidParameterValue", false)
}

topicArn := requestBody.TopicArn
uriSegments := strings.Split(topicArn, ":")
topicName := uriSegments[len(uriSegments)-1]

log.Info("Delete Topic - TopicName:", topicName)

_, ok = app.SyncTopics.Topics[topicName]

if !ok {
return utils.CreateErrorResponseV1("TopicNotFound", false)
}

app.SyncTopics.Lock()
delete(app.SyncTopics.Topics, topicName)
app.SyncTopics.Unlock()
uuid, _ := common.NewUUID()
respStruct := models.DeleteTopicResponse{
Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/",
Metadata: app.ResponseMetadata{RequestId: uuid},
}

return http.StatusOK, respStruct

}
package gosns

import (
"net/http"
"strings"

"github.com/Admiral-Piett/goaws/app"
"github.com/Admiral-Piett/goaws/app/common"
"github.com/Admiral-Piett/goaws/app/interfaces"
"github.com/Admiral-Piett/goaws/app/models"
"github.com/Admiral-Piett/goaws/app/utils"

log "github.com/sirupsen/logrus"
)

func DeleteTopicV1(req *http.Request) (int, interfaces.AbstractResponseBody) {
requestBody := models.NewDeleteTopicRequest()
ok := utils.REQUEST_TRANSFORMER(requestBody, req, false)
if !ok {
log.Error("Invalid Request - DeleteTopicV1")
return utils.CreateErrorResponseV1("InvalidParameterValue", false)
}

topicArn := requestBody.TopicArn
uriSegments := strings.Split(topicArn, ":")
topicName := uriSegments[len(uriSegments)-1]

log.Info("Delete Topic - TopicName:", topicName)

_, ok = app.SyncTopics.Topics[topicName]

if !ok {
return utils.CreateErrorResponseV1("TopicNotFound", false)
}

app.SyncTopics.Lock()
delete(app.SyncTopics.Topics, topicName)
app.SyncTopics.Unlock()
uuid, _ := common.NewUUID()
respStruct := models.DeleteTopicResponse{
Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/",
Metadata: app.ResponseMetadata{RequestId: uuid},
}

return http.StatusOK, respStruct

}
176 changes: 88 additions & 88 deletions app/gosns/delete_topic_test.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
package gosns

import (
"net/http"
"testing"

"github.com/Admiral-Piett/goaws/app"
"github.com/Admiral-Piett/goaws/app/conf"
"github.com/Admiral-Piett/goaws/app/interfaces"
"github.com/Admiral-Piett/goaws/app/models"
"github.com/Admiral-Piett/goaws/app/test"
"github.com/Admiral-Piett/goaws/app/utils"
"github.com/stretchr/testify/assert"
)

func TestDeleteTopicV1_Success(t *testing.T) {
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
defer func() {
test.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

initial_num_topics := len(app.SyncTopics.Topics)

topicName1 := "unit-topic1"

utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
v := resultingStruct.(*models.DeleteTopicRequest)
*v = models.DeleteTopicRequest{
TopicArn: "arn:aws:sns:region:accountID:" + topicName1,
}
return true
}

_, r := test.GenerateRequestInfo("POST", "/", nil, true)
code, res := DeleteTopicV1(r)

response, _ := res.(models.DeleteTopicResponse)

assert.Equal(t, http.StatusOK, code)
assert.Equal(t, models.BASE_XMLNS, response.Xmlns)
assert.NotEqual(t, "", response.Metadata)

topics := app.SyncTopics.Topics
assert.Equal(t, initial_num_topics-1, len(topics))
_, ok := topics[topicName1]
assert.False(t, ok)
}

func TestDeleteTopicV1_NotFound(t *testing.T) {
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "NoQueuesOrTopics")
defer func() {
test.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
v := resultingStruct.(*models.DeleteTopicRequest)
*v = models.DeleteTopicRequest{
TopicArn: "asdf",
}
return true
}

_, r := test.GenerateRequestInfo("POST", "/", nil, true)
code, res := DeleteTopicV1(r)
resp := res.(models.ErrorResponse)

assert.Equal(t, http.StatusBadRequest, code)
assert.Equal(t, resp.Result.Type, "Not Found")
}

func TestDeleteTopicV1_request_transformer_error(t *testing.T) {
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
defer func() {
test.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
return false
}

_, r := test.GenerateRequestInfo("POST", "/", nil, true)
code, _ := DeleteTopicV1(r)

assert.Equal(t, http.StatusBadRequest, code)
}
package gosns

import (
"net/http"
"testing"

"github.com/Admiral-Piett/goaws/app"
"github.com/Admiral-Piett/goaws/app/conf"
"github.com/Admiral-Piett/goaws/app/interfaces"
"github.com/Admiral-Piett/goaws/app/models"
"github.com/Admiral-Piett/goaws/app/test"
"github.com/Admiral-Piett/goaws/app/utils"
"github.com/stretchr/testify/assert"
)

func TestDeleteTopicV1_Success(t *testing.T) {
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
defer func() {
test.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

initial_num_topics := len(app.SyncTopics.Topics)

topicName1 := "unit-topic1"

utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
v := resultingStruct.(*models.DeleteTopicRequest)
*v = models.DeleteTopicRequest{
TopicArn: "arn:aws:sns:region:accountID:" + topicName1,
}
return true
}

_, r := test.GenerateRequestInfo("POST", "/", nil, true)
code, res := DeleteTopicV1(r)

response, _ := res.(models.DeleteTopicResponse)

assert.Equal(t, http.StatusOK, code)
assert.Equal(t, models.BASE_XMLNS, response.Xmlns)
assert.NotEqual(t, "", response.Metadata)

topics := app.SyncTopics.Topics
assert.Equal(t, initial_num_topics-1, len(topics))
_, ok := topics[topicName1]
assert.False(t, ok)
}

func TestDeleteTopicV1_NotFound(t *testing.T) {
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "NoQueuesOrTopics")
defer func() {
test.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
v := resultingStruct.(*models.DeleteTopicRequest)
*v = models.DeleteTopicRequest{
TopicArn: "asdf",
}
return true
}

_, r := test.GenerateRequestInfo("POST", "/", nil, true)
code, res := DeleteTopicV1(r)
resp := res.(models.ErrorResponse)

assert.Equal(t, http.StatusBadRequest, code)
assert.Equal(t, resp.Result.Type, "Not Found")
}

func TestDeleteTopicV1_request_transformer_error(t *testing.T) {
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
defer func() {
test.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
return false
}

_, r := test.GenerateRequestInfo("POST", "/", nil, true)
code, _ := DeleteTopicV1(r)

assert.Equal(t, http.StatusBadRequest, code)
}
Loading

0 comments on commit 3e612da

Please sign in to comment.