Skip to content

Commit

Permalink
Apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kojisaikiAtSony committed Jun 10, 2024
1 parent 0aa2a14 commit 15500d0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions app/gosns/create_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func CreateTopicV1(req *http.Request) (int, interfaces.AbstractResponseBody) {
} else {
topicArn = "arn:aws:sns:" + app.CurrentEnvironment.Region + ":" + app.CurrentEnvironment.AccountID + ":" + topicName

log.Println("Creating Topic:", topicName)
log.Debug("Creating Topic:", topicName)
topic := &app.Topic{Name: topicName, Arn: topicArn}
topic.Subscriptions = make([]*app.Subscription, 0, 0)
app.SyncTopics.Lock()
Expand All @@ -36,7 +36,7 @@ func CreateTopicV1(req *http.Request) (int, interfaces.AbstractResponseBody) {

uuid, _ := common.NewUUID()
respStruct := models.CreateTopicResponse{
Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/",
Xmlns: models.BASE_XMLNS,
Result: models.CreateTopicResult{
TopicArn: topicArn,
},
Expand Down
19 changes: 12 additions & 7 deletions app/gosns/create_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func TestCreateTopicV1_success(t *testing.T) {
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

targetTopicName := "new-topic-1"
request_success := models.CreateTopicRequest{
Name: "new-topic-1",
Name: targetTopicName,
}
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
v := resultingStruct.(*models.CreateTopicRequest)
Expand All @@ -40,20 +41,23 @@ func TestCreateTopicV1_success(t *testing.T) {
createTopicResponse, ok := response.(models.CreateTopicResponse)
assert.True(t, ok)
assert.Contains(t, createTopicResponse.Result.TopicArn, "arn:aws:sns:")
assert.Contains(t, createTopicResponse.Result.TopicArn, "new-topic-1")
assert.Contains(t, createTopicResponse.Result.TopicArn, targetTopicName)
// 1 topic there
assert.Equal(t, 1, len(app.SyncTopics.Topics))
}

func TestCreateTopicV1_existant_topic(t *testing.T) {
app.CurrentEnvironment = fixtures.LOCAL_ENVIRONMENT
defer func() {
utils.ResetApp()
utils.REQUEST_TRANSFORMER = utils.TransformRequest
}()

targetTopicName := "new-topic-1"

// Same topic name with existant topic
request_success := models.CreateTopicRequest{
Name: "new-topic-1",
Name: targetTopicName,
}
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
v := resultingStruct.(*models.CreateTopicRequest)
Expand All @@ -62,11 +66,12 @@ func TestCreateTopicV1_existant_topic(t *testing.T) {
}

// Prepare existant topic
targetTopicArn := "arn:aws:sns:us-east-1:123456789012:" + targetTopicName
topic := &app.Topic{
Name: "new-topic-1",
Arn: "arn:aws:sns:us-east-1:123456789012:new-topic-1",
Name: targetTopicName,
Arn: targetTopicArn,
}
app.SyncTopics.Topics["new-topic-1"] = topic
app.SyncTopics.Topics[targetTopicName] = topic
assert.Equal(t, 1, len(app.SyncTopics.Topics))

// Reques
Expand All @@ -77,7 +82,7 @@ func TestCreateTopicV1_existant_topic(t *testing.T) {
assert.Equal(t, http.StatusOK, status)
createTopicResponse, ok := response.(models.CreateTopicResponse)
assert.True(t, ok)
assert.Equal(t, "arn:aws:sns:us-east-1:123456789012:new-topic-1", createTopicResponse.Result.TopicArn) // Same with existant topic
assert.Equal(t, targetTopicArn, createTopicResponse.Result.TopicArn) // Same with existant topic
// No additional topic
assert.Equal(t, 1, len(app.SyncTopics.Topics))
}
Expand Down
35 changes: 20 additions & 15 deletions app/gosns/gosns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func TestPublishhandler_POST_SendMessage(t *testing.T) {
Arn: "arn:aws:sns:local:000000000000:UnitTestTopic1",
}
app.SyncTopics.Topics["UnitTestTopic1"] = topic
defer func() {
// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}()

// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
Expand All @@ -85,9 +89,6 @@ func TestPublishhandler_POST_SendMessage(t *testing.T) {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}

// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}

func TestPublishHandler_POST_FilterPolicyRejectsTheMessage(t *testing.T) {
Expand Down Expand Up @@ -253,6 +254,10 @@ func TestSubscribehandler_POST_Success(t *testing.T) {
Arn: "arn:aws:sns:local:000000000000:UnitTestTopic1",
}
app.SyncTopics.Topics["UnitTestTopic1"] = topic
defer func() {
// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}()

// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
Expand All @@ -274,9 +279,6 @@ func TestSubscribehandler_POST_Success(t *testing.T) {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}

// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}

func TestSubscribehandler_HTTP_POST_Success(t *testing.T) {
Expand Down Expand Up @@ -311,6 +313,10 @@ func TestSubscribehandler_HTTP_POST_Success(t *testing.T) {
Arn: "arn:aws:sns:local:000000000000:UnitTestTopic1",
}
app.SyncTopics.Topics["UnitTestTopic1"] = topic
defer func() {
// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}()

// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
Expand Down Expand Up @@ -341,9 +347,6 @@ func TestSubscribehandler_HTTP_POST_Success(t *testing.T) {
case <-time.After(2 * time.Second):
t.Fatal("http sns handler must be called")
}

// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}

func TestPublish_No_Queue_Error_handler_POST_Success(t *testing.T) {
Expand All @@ -365,6 +368,10 @@ func TestPublish_No_Queue_Error_handler_POST_Success(t *testing.T) {
Arn: "arn:aws:sns:local:000000000000:UnitTestTopic1",
}
app.SyncTopics.Topics["UnitTestTopic1"] = topic
defer func() {
// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}()

// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
Expand All @@ -386,9 +393,6 @@ func TestPublish_No_Queue_Error_handler_POST_Success(t *testing.T) {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}

// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}

func TestListSubscriptionByTopicResponse_No_Owner(t *testing.T) {
Expand Down Expand Up @@ -423,6 +427,10 @@ func TestListSubscriptionByTopicResponse_No_Owner(t *testing.T) {
},
}
app.SyncTopics.Topics["UnitTestTopic1"] = topic
defer func() {
// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}()

// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
Expand All @@ -444,9 +452,6 @@ func TestListSubscriptionByTopicResponse_No_Owner(t *testing.T) {
t.Errorf("handler returned empty owner for subscription member: got %v want %v",
rr.Body.String(), expected)
}

// Clear topic
delete(app.SyncTopics.Topics, "UnitTestTopic1")
}

func TestListSubscriptionsResponse_No_Owner(t *testing.T) {
Expand Down
31 changes: 17 additions & 14 deletions smoke_tests/sns_create_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_CreateTopicV1_json_success(t *testing.T) {
})

// Should success
assert.Contains(t, *sdkResponse.TopicArn, "new-topic-1")
assert.Contains(t, *sdkResponse.TopicArn, topicName)
assert.Nil(t, err)

// Get created topic
Expand All @@ -53,7 +53,7 @@ func Test_CreateTopicV1_json_success(t *testing.T) {
r2 := app.ListTopicsResponse{}
xml.Unmarshal([]byte(r), &r2)
assert.Equal(t, 1, len(r2.Result.Topics.Member))
assert.Contains(t, r2.Result.Topics.Member[0].TopicArn, "new-topic-1")
assert.Contains(t, r2.Result.Topics.Member[0].TopicArn, topicName)
}

func Test_CreateTopicV1_json_existant_topic(t *testing.T) {
Expand All @@ -71,7 +71,7 @@ func Test_CreateTopicV1_json_existant_topic(t *testing.T) {
sdkResponse, _ := snsClient.CreateTopic(context.TODO(), &sns.CreateTopicInput{
Name: &topicName,
})
assert.Contains(t, *sdkResponse.TopicArn, "new-topic-1")
assert.Contains(t, *sdkResponse.TopicArn, topicName)
assert.Nil(t, err)

// Target test: create topic with same name
Expand All @@ -80,7 +80,7 @@ func Test_CreateTopicV1_json_existant_topic(t *testing.T) {
})

// Should success
assert.Contains(t, *sdkResponse.TopicArn, "new-topic-1")
assert.Contains(t, *sdkResponse.TopicArn, topicName)
assert.Nil(t, err)

// Topic should not be duplicated
Expand All @@ -100,7 +100,7 @@ func Test_CreateTopicV1_json_existant_topic(t *testing.T) {
r2 := app.ListTopicsResponse{}
xml.Unmarshal([]byte(r), &r2)
assert.Equal(t, 1, len(r2.Result.Topics.Member))
assert.Contains(t, r2.Result.Topics.Member[0].TopicArn, "new-topic-1")
assert.Contains(t, r2.Result.Topics.Member[0].TopicArn, topicName)
}

func Test_CreateTopicV1_json_add_new_topic(t *testing.T) {
Expand All @@ -118,7 +118,7 @@ func Test_CreateTopicV1_json_add_new_topic(t *testing.T) {
sdkResponse, _ := snsClient.CreateTopic(context.TODO(), &sns.CreateTopicInput{
Name: &topicName,
})
assert.Contains(t, *sdkResponse.TopicArn, "new-topic-1")
assert.Contains(t, *sdkResponse.TopicArn, topicName)
assert.Nil(t, err)

// Target test: create topic with same name
Expand All @@ -128,7 +128,7 @@ func Test_CreateTopicV1_json_add_new_topic(t *testing.T) {
})

// Should success
assert.Contains(t, *sdkResponse.TopicArn, "new-topic-2")
assert.Contains(t, *sdkResponse.TopicArn, topicName2)
assert.Nil(t, err)

// Number of topic should be 2
Expand Down Expand Up @@ -158,14 +158,15 @@ func Test_CreateTopicV1_xml_success(t *testing.T) {
}()

// Target test
topicName := "new-topic-1"
createTopicsXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
Name string `xml:"Name"`
}{
Action: "CreateTopic",
Version: "2012-11-05",
Name: "new-topic-1",
Name: topicName,
}
e := httpexpect.Default(t, server.URL)
r := e.POST("/").
Expand All @@ -175,7 +176,7 @@ func Test_CreateTopicV1_xml_success(t *testing.T) {
Body().Raw()
r2 := models.CreateTopicResponse{}
xml.Unmarshal([]byte(r), &r2)
assert.Contains(t, r2.Result.TopicArn, "new-topic-1")
assert.Contains(t, r2.Result.TopicArn, topicName)

// Get created topic
listTopicsXML := struct {
Expand All @@ -193,7 +194,7 @@ func Test_CreateTopicV1_xml_success(t *testing.T) {
r3 := app.ListTopicsResponse{}
xml.Unmarshal([]byte(r), &r3)
assert.Equal(t, 1, len(r3.Result.Topics.Member))
assert.Contains(t, r3.Result.Topics.Member[0].TopicArn, "new-topic-1")
assert.Contains(t, r3.Result.Topics.Member[0].TopicArn, topicName)
}

func Test_CreateTopicV1_xml_existant_topic(t *testing.T) {
Expand All @@ -203,6 +204,8 @@ func Test_CreateTopicV1_xml_existant_topic(t *testing.T) {
utils.ResetResources()
}()

topicName := "new-topic-1"

// Prepare existant topic
createTopicsXML := struct {
Action string `xml:"Action"`
Expand All @@ -211,7 +214,7 @@ func Test_CreateTopicV1_xml_existant_topic(t *testing.T) {
}{
Action: "CreateTopic",
Version: "2012-11-05",
Name: "new-topic-1",
Name: topicName,
}
e := httpexpect.Default(t, server.URL)
r := e.POST("/").
Expand All @@ -221,7 +224,7 @@ func Test_CreateTopicV1_xml_existant_topic(t *testing.T) {
Body().Raw()
r2 := models.CreateTopicResponse{}
xml.Unmarshal([]byte(r), &r2)
assert.Contains(t, r2.Result.TopicArn, "new-topic-1")
assert.Contains(t, r2.Result.TopicArn, topicName)

// Target test: create topic with same name
r = e.POST("/").
Expand All @@ -231,7 +234,7 @@ func Test_CreateTopicV1_xml_existant_topic(t *testing.T) {
Body().Raw()
r2 = models.CreateTopicResponse{}
xml.Unmarshal([]byte(r), &r2)
assert.Contains(t, r2.Result.TopicArn, "new-topic-1")
assert.Contains(t, r2.Result.TopicArn, topicName)

// Topic should not be duplicated
listTopicsXML := struct {
Expand All @@ -249,5 +252,5 @@ func Test_CreateTopicV1_xml_existant_topic(t *testing.T) {
r3 := app.ListTopicsResponse{}
xml.Unmarshal([]byte(r), &r3)
assert.Equal(t, 1, len(r3.Result.Topics.Member))
assert.Contains(t, r3.Result.Topics.Member[0].TopicArn, "new-topic-1")
assert.Contains(t, r3.Result.Topics.Member[0].TopicArn, topicName)
}

0 comments on commit 15500d0

Please sign in to comment.