Skip to content

Commit 8937b73

Browse files
committed
add delete topic v1 support
add request transform test to delete topic revise comments - change deletetopicv1 println to log.Info - remove unnecessary deletetopicv1 request setAttributesFromForm
1 parent 1a9d01b commit 8937b73

File tree

10 files changed

+338
-91
lines changed

10 files changed

+338
-91
lines changed

app/gosns/delete_topic.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package gosns
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/Admiral-Piett/goaws/app"
8+
"github.com/Admiral-Piett/goaws/app/common"
9+
"github.com/Admiral-Piett/goaws/app/interfaces"
10+
"github.com/Admiral-Piett/goaws/app/models"
11+
"github.com/Admiral-Piett/goaws/app/utils"
12+
13+
log "github.com/sirupsen/logrus"
14+
)
15+
16+
func DeleteTopicV1(req *http.Request) (int, interfaces.AbstractResponseBody) {
17+
requestBody := models.NewDeleteTopicRequest()
18+
ok := utils.REQUEST_TRANSFORMER(requestBody, req, false)
19+
if !ok {
20+
log.Error("Invalid Request - DeleteTopicV1")
21+
return utils.CreateErrorResponseV1("InvalidParameterValue", false)
22+
}
23+
24+
topicArn := requestBody.TopicArn
25+
uriSegments := strings.Split(topicArn, ":")
26+
topicName := uriSegments[len(uriSegments)-1]
27+
28+
log.Info("Delete Topic - TopicName:", topicName)
29+
30+
_, ok = app.SyncTopics.Topics[topicName]
31+
32+
if !ok {
33+
return utils.CreateErrorResponseV1("TopicNotFound", false)
34+
}
35+
36+
app.SyncTopics.Lock()
37+
delete(app.SyncTopics.Topics, topicName)
38+
app.SyncTopics.Unlock()
39+
uuid, _ := common.NewUUID()
40+
respStruct := models.DeleteTopicResponse{
41+
Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/",
42+
Metadata: app.ResponseMetadata{RequestId: uuid},
43+
}
44+
45+
return http.StatusOK, respStruct
46+
47+
}

app/gosns/delete_topic_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package gosns
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/Admiral-Piett/goaws/app"
8+
"github.com/Admiral-Piett/goaws/app/conf"
9+
"github.com/Admiral-Piett/goaws/app/interfaces"
10+
"github.com/Admiral-Piett/goaws/app/models"
11+
"github.com/Admiral-Piett/goaws/app/test"
12+
"github.com/Admiral-Piett/goaws/app/utils"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestDeleteTopicV1_Success(t *testing.T) {
17+
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
18+
defer func() {
19+
test.ResetApp()
20+
utils.REQUEST_TRANSFORMER = utils.TransformRequest
21+
}()
22+
23+
topicName1 := "unit-topic1"
24+
25+
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
26+
v := resultingStruct.(*models.DeleteTopicRequest)
27+
*v = models.DeleteTopicRequest{
28+
TopicArn: "arn:aws:sns:region:accountID:" + topicName1,
29+
}
30+
return true
31+
}
32+
33+
_, r := test.GenerateRequestInfo("POST", "/", nil, true)
34+
code, res := DeleteTopicV1(r)
35+
36+
response, _ := res.(models.DeleteTopicResponse)
37+
38+
assert.Equal(t, http.StatusOK, code)
39+
assert.Equal(t, models.BASE_XMLNS, response.Xmlns)
40+
assert.NotEqual(t, "", response.Metadata)
41+
42+
topics := app.SyncTopics.Topics
43+
assert.Len(t, topics, 1)
44+
_, ok := topics[topicName1]
45+
assert.False(t, ok)
46+
}
47+
48+
func TestDeleteTopicV1_NotFound(t *testing.T) {
49+
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "NoQueuesOrTopics")
50+
defer func() {
51+
test.ResetApp()
52+
utils.REQUEST_TRANSFORMER = utils.TransformRequest
53+
}()
54+
55+
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
56+
v := resultingStruct.(*models.DeleteTopicRequest)
57+
*v = models.DeleteTopicRequest{
58+
TopicArn: "asdf",
59+
}
60+
return true
61+
}
62+
63+
_, r := test.GenerateRequestInfo("POST", "/", nil, true)
64+
code, res := DeleteTopicV1(r)
65+
resp := res.(models.ErrorResponse)
66+
67+
assert.Equal(t, http.StatusBadRequest, code)
68+
assert.Equal(t, resp.Result.Type, "Not Found")
69+
}
70+
71+
func TestDeleteTopicV1_request_transformer_error(t *testing.T) {
72+
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "BaseUnitTests")
73+
defer func() {
74+
test.ResetApp()
75+
utils.REQUEST_TRANSFORMER = utils.TransformRequest
76+
}()
77+
78+
utils.REQUEST_TRANSFORMER = func(resultingStruct interfaces.AbstractRequestBody, req *http.Request, emptyRequestValid bool) (success bool) {
79+
return false
80+
}
81+
82+
_, r := test.GenerateRequestInfo("POST", "/", nil, true)
83+
code, _ := DeleteTopicV1(r)
84+
85+
assert.Equal(t, http.StatusBadRequest, code)
86+
}

app/gosns/gosns.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -306,29 +306,6 @@ func GetSubscriptionAttributes(w http.ResponseWriter, req *http.Request) {
306306
createErrorResponse(w, req, "SubscriptionNotFound")
307307
}
308308

309-
func DeleteTopic(w http.ResponseWriter, req *http.Request) {
310-
content := req.FormValue("ContentType")
311-
topicArn := req.FormValue("TopicArn")
312-
313-
uriSegments := strings.Split(topicArn, ":")
314-
topicName := uriSegments[len(uriSegments)-1]
315-
316-
log.Println("Delete Topic - TopicName:", topicName)
317-
318-
_, ok := app.SyncTopics.Topics[topicName]
319-
if ok {
320-
app.SyncTopics.Lock()
321-
delete(app.SyncTopics.Topics, topicName)
322-
app.SyncTopics.Unlock()
323-
uuid, _ := common.NewUUID()
324-
respStruct := app.DeleteTopicResponse{"http://queue.amazonaws.com/doc/2012-11-05/", app.ResponseMetadata{RequestId: uuid}}
325-
SendResponseBack(w, req, respStruct, content)
326-
} else {
327-
createErrorResponse(w, req, "TopicNotFound")
328-
}
329-
330-
}
331-
332309
// NOTE: The use case for this is to use GoAWS to call some external system with the message payload. Essentially
333310
// it is a localized subscription to some non-AWS endpoint.
334311
func callEndpoint(endpoint string, subArn string, msg app.SNSMessage, raw bool) error {

app/gosns/gosns_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/Admiral-Piett/goaws/app/conf"
1111
"github.com/Admiral-Piett/goaws/app/test"
12-
"github.com/stretchr/testify/assert"
1312

1413
"github.com/Admiral-Piett/goaws/app"
1514
"github.com/Admiral-Piett/goaws/app/common"
@@ -164,63 +163,6 @@ func TestListSubscriptionsResponse_No_Owner(t *testing.T) {
164163
}
165164
}
166165

167-
func TestDeleteTopichandler_POST_Success(t *testing.T) {
168-
conf.LoadYamlConfig("../conf/mock-data/mock-config.yaml", "Local")
169-
defer func() {
170-
test.ResetApp()
171-
}()
172-
173-
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
174-
// pass 'nil' as the third parameter.
175-
req, err := http.NewRequest("POST", "/", nil)
176-
if err != nil {
177-
t.Fatal(err)
178-
}
179-
180-
form := url.Values{}
181-
form.Add("TopicArn", "arn:aws:sns:local:000000000000:local-topic1")
182-
form.Add("Message", "TestMessage1")
183-
req.PostForm = form
184-
185-
// Prepare existant topic
186-
topic := &app.Topic{
187-
Name: "local-topic1",
188-
Arn: "arn:aws:sns:local:000000000000:local-topic1",
189-
}
190-
app.SyncTopics.Topics["local-topic1"] = topic
191-
192-
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
193-
rr := httptest.NewRecorder()
194-
handler := http.HandlerFunc(DeleteTopic)
195-
196-
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
197-
// directly and pass in our Request and ResponseRecorder.
198-
handler.ServeHTTP(rr, req)
199-
200-
// Check the status code is what we expect.
201-
if status := rr.Code; status != http.StatusOK {
202-
t.Errorf("handler returned wrong status code: got %v want %v",
203-
status, http.StatusOK)
204-
}
205-
206-
// Check the response body is what we expect.
207-
expected := "</DeleteTopicResponse>"
208-
if !strings.Contains(rr.Body.String(), expected) {
209-
t.Errorf("handler returned unexpected body: got %v want %v",
210-
rr.Body.String(), expected)
211-
}
212-
// Check the response body is what we expect.
213-
expected = "</ResponseMetadata>"
214-
if !strings.Contains(rr.Body.String(), expected) {
215-
t.Errorf("handler returned unexpected body: got %v want %v",
216-
rr.Body.String(), expected)
217-
}
218-
219-
// Target topic should be disappeared
220-
_, ok := app.SyncTopics.Topics["local-topic1"]
221-
assert.False(t, ok)
222-
}
223-
224166
func TestGetSubscriptionAttributesHandler_POST_Success(t *testing.T) {
225167
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
226168
// pass 'nil' as the third parameter.

app/models/responses.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,17 @@ func (r PublishResponse) GetResult() interface{} {
420420
func (r PublishResponse) GetRequestId() string {
421421
return r.Metadata.RequestId
422422
}
423+
424+
/*** Delete Topic ***/
425+
type DeleteTopicResponse struct {
426+
Xmlns string `xml:"xmlns,attr"`
427+
Metadata app.ResponseMetadata `xml:"ResponseMetadata"`
428+
}
429+
430+
func (r DeleteTopicResponse) GetResult() interface{} {
431+
return nil
432+
}
433+
434+
func (r DeleteTopicResponse) GetRequestId() string {
435+
return r.Metadata.RequestId
436+
}

app/models/sns.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,15 @@ func (r *PublishRequest) SetAttributesFromForm(values url.Values) {
224224
}
225225
}
226226
}
227+
228+
// DeleteTopicV1
229+
230+
func NewDeleteTopicRequest() *DeleteTopicRequest {
231+
return &DeleteTopicRequest{}
232+
}
233+
234+
type DeleteTopicRequest struct {
235+
TopicArn string `json:"TopicArn" schema:"TopicArn"`
236+
}
237+
238+
func (r *DeleteTopicRequest) SetAttributesFromForm(values url.Values) {}

app/router/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ var routingTableV1 = map[string]func(r *http.Request) (int, interfaces.AbstractR
7979
"DeleteMessageBatch": sqs.DeleteMessageBatchV1,
8080

8181
// SNS
82-
"CreateTopic": sns.CreateTopicV1,
8382
"Subscribe": sns.SubscribeV1,
8483
"Unsubscribe": sns.UnsubscribeV1,
8584
"Publish": sns.PublishV1,
85+
"CreateTopic": sns.CreateTopicV1,
86+
"DeleteTopic": sns.DeleteTopicV1,
8687
}
8788

8889
var routingTable = map[string]http.HandlerFunc{
8990
// SNS
9091
"ListTopics": sns.ListTopics,
91-
"DeleteTopic": sns.DeleteTopic,
9292
"SetSubscriptionAttributes": sns.SetSubscriptionAttributes,
9393
"GetSubscriptionAttributes": sns.GetSubscriptionAttributes,
9494
"ListSubscriptionsByTopic": sns.ListSubscriptionsByTopic,

app/router/router_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,16 @@ func TestActionHandler_v0_xml(t *testing.T) {
271271
"DeleteMessageBatch": sqs.DeleteMessageBatchV1,
272272

273273
// SNS
274-
"CreateTopic": sns.CreateTopicV1,
275274
"Subscribe": sns.SubscribeV1,
276275
"Unsubscribe": sns.UnsubscribeV1,
277276
"Publish": sns.PublishV1,
277+
"CreateTopic": sns.CreateTopicV1,
278+
"DeleteTopic": sns.DeleteTopicV1,
278279
}
279280

280281
routingTable = map[string]http.HandlerFunc{
281282
// SNS
282283
"ListTopics": sns.ListTopics,
283-
"DeleteTopic": sns.DeleteTopic,
284284
"SetSubscriptionAttributes": sns.SetSubscriptionAttributes,
285285
"GetSubscriptionAttributes": sns.GetSubscriptionAttributes,
286286
"ListSubscriptionsByTopic": sns.ListSubscriptionsByTopic,

app/sns_messages.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,3 @@ type ListSubscriptionsByTopicResponse struct {
8080
Result ListSubscriptionsByTopicResult `xml:"ListSubscriptionsByTopicResult"`
8181
Metadata ResponseMetadata `xml:"ResponseMetadata"`
8282
}
83-
84-
/*** Delete Topic ***/
85-
type DeleteTopicResponse struct {
86-
Xmlns string `xml:"xmlns,attr"`
87-
Metadata ResponseMetadata `xml:"ResponseMetadata"`
88-
}

0 commit comments

Comments
 (0)