You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
3.0 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "context"
  17. "testing"
  18. "cloud.google.com/go/internal/testutil"
  19. raw "google.golang.org/api/storage/v1"
  20. )
  21. func TestParseNotificationTopic(t *testing.T) {
  22. for _, test := range []struct {
  23. in string
  24. wantProjectID string
  25. wantTopicID string
  26. }{
  27. {"", "?", "?"},
  28. {"foobar", "?", "?"},
  29. {"//pubsub.googleapis.com/projects/foo", "?", "?"},
  30. {"//pubsub.googleapis.com/projects/my-project/topics/my-topic",
  31. "my-project", "my-topic"},
  32. } {
  33. gotProjectID, gotTopicID := parseNotificationTopic(test.in)
  34. if gotProjectID != test.wantProjectID || gotTopicID != test.wantTopicID {
  35. t.Errorf("%q: got (%q, %q), want (%q, %q)",
  36. test.in, gotProjectID, gotTopicID, test.wantProjectID, test.wantTopicID)
  37. }
  38. }
  39. }
  40. func TestConvertNotification(t *testing.T) {
  41. want := &Notification{
  42. ID: "id",
  43. TopicProjectID: "my-project",
  44. TopicID: "my-topic",
  45. EventTypes: []string{ObjectFinalizeEvent},
  46. ObjectNamePrefix: "prefix",
  47. CustomAttributes: map[string]string{"a": "b"},
  48. PayloadFormat: JSONPayload,
  49. }
  50. got := toNotification(toRawNotification(want))
  51. if diff := testutil.Diff(got, want); diff != "" {
  52. t.Errorf("got=-, want=+:\n%s", diff)
  53. }
  54. }
  55. func TestNotificationsToMap(t *testing.T) {
  56. got := notificationsToMap(nil)
  57. want := map[string]*Notification{}
  58. if !testutil.Equal(got, want) {
  59. t.Errorf("got %+v, want %+v", got, want)
  60. }
  61. in := []*raw.Notification{
  62. {Id: "a", Topic: "//pubsub.googleapis.com/projects/P1/topics/T1"},
  63. {Id: "b", Topic: "//pubsub.googleapis.com/projects/P2/topics/T2"},
  64. {Id: "c", Topic: "//pubsub.googleapis.com/projects/P3/topics/T3"},
  65. }
  66. got = notificationsToMap(in)
  67. want = map[string]*Notification{
  68. "a": {ID: "a", TopicProjectID: "P1", TopicID: "T1"},
  69. "b": {ID: "b", TopicProjectID: "P2", TopicID: "T2"},
  70. "c": {ID: "c", TopicProjectID: "P3", TopicID: "T3"},
  71. }
  72. if diff := testutil.Diff(got, want); diff != "" {
  73. t.Errorf("got=-, want=+:\n%s", diff)
  74. }
  75. }
  76. func TestAddNotificationsErrors(t *testing.T) {
  77. c := &Client{}
  78. b := c.Bucket("b")
  79. for _, n := range []*Notification{
  80. {ID: "foo", TopicProjectID: "p", TopicID: "t"}, // has ID
  81. {TopicProjectID: "p"}, // missing TopicID
  82. {TopicID: "t"}, // missing TopicProjectID
  83. } {
  84. _, err := b.AddNotification(context.Background(), n)
  85. if err == nil {
  86. t.Errorf("%+v: got nil, want error", n)
  87. }
  88. }
  89. }