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
15package storage
16
17import (
18	"context"
19	"testing"
20
21	"cloud.google.com/go/internal/testutil"
22	raw "google.golang.org/api/storage/v1"
23)
24
25func TestParseNotificationTopic(t *testing.T) {
26	for _, test := range []struct {
27		in            string
28		wantProjectID string
29		wantTopicID   string
30	}{
31		{"", "?", "?"},
32		{"foobar", "?", "?"},
33		{"//pubsub.googleapis.com/projects/foo", "?", "?"},
34		{"//pubsub.googleapis.com/projects/my-project/topics/my-topic",
35			"my-project", "my-topic"},
36	} {
37		gotProjectID, gotTopicID := parseNotificationTopic(test.in)
38		if gotProjectID != test.wantProjectID || gotTopicID != test.wantTopicID {
39			t.Errorf("%q: got (%q, %q), want (%q, %q)",
40				test.in, gotProjectID, gotTopicID, test.wantProjectID, test.wantTopicID)
41		}
42	}
43
44}
45
46func TestConvertNotification(t *testing.T) {
47	want := &Notification{
48		ID:               "id",
49		TopicProjectID:   "my-project",
50		TopicID:          "my-topic",
51		EventTypes:       []string{ObjectFinalizeEvent},
52		ObjectNamePrefix: "prefix",
53		CustomAttributes: map[string]string{"a": "b"},
54		PayloadFormat:    JSONPayload,
55	}
56	got := toNotification(toRawNotification(want))
57	if diff := testutil.Diff(got, want); diff != "" {
58		t.Errorf("got=-, want=+:\n%s", diff)
59	}
60}
61
62func TestNotificationsToMap(t *testing.T) {
63	got := notificationsToMap(nil)
64	want := map[string]*Notification{}
65	if !testutil.Equal(got, want) {
66		t.Errorf("got %+v, want %+v", got, want)
67	}
68
69	in := []*raw.Notification{
70		{Id: "a", Topic: "//pubsub.googleapis.com/projects/P1/topics/T1"},
71		{Id: "b", Topic: "//pubsub.googleapis.com/projects/P2/topics/T2"},
72		{Id: "c", Topic: "//pubsub.googleapis.com/projects/P3/topics/T3"},
73	}
74	got = notificationsToMap(in)
75	want = map[string]*Notification{
76		"a": {ID: "a", TopicProjectID: "P1", TopicID: "T1"},
77		"b": {ID: "b", TopicProjectID: "P2", TopicID: "T2"},
78		"c": {ID: "c", TopicProjectID: "P3", TopicID: "T3"},
79	}
80	if diff := testutil.Diff(got, want); diff != "" {
81		t.Errorf("got=-, want=+:\n%s", diff)
82	}
83}
84
85func TestAddNotificationsErrors(t *testing.T) {
86	c := &Client{}
87	b := c.Bucket("b")
88	for _, n := range []*Notification{
89		{ID: "foo", TopicProjectID: "p", TopicID: "t"}, // has ID
90		{TopicProjectID: "p"},                          // missing TopicID
91		{TopicID: "t"},                                 // missing TopicProjectID
92	} {
93		_, err := b.AddNotification(context.Background(), n)
94		if err == nil {
95			t.Errorf("%+v: got nil, want error", n)
96		}
97	}
98}
99