File size: 23,058 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import mock

from google.cloud.storage.retry import DEFAULT_RETRY


class TestBucketNotification(unittest.TestCase):
    BUCKET_NAME = "test-bucket"
    BUCKET_PROJECT = "bucket-project-123"
    TOPIC_NAME = "test-topic"
    TOPIC_ALT_PROJECT = "topic-project-456"
    TOPIC_REF_FMT = "//pubsub.googleapis.com/projects/{}/topics/{}"
    TOPIC_REF = TOPIC_REF_FMT.format(BUCKET_PROJECT, TOPIC_NAME)
    TOPIC_ALT_REF = TOPIC_REF_FMT.format(TOPIC_ALT_PROJECT, TOPIC_NAME)
    CUSTOM_ATTRIBUTES = {"attr1": "value1", "attr2": "value2"}
    BLOB_NAME_PREFIX = "blob-name-prefix/"
    NOTIFICATION_ID = "123"
    SELF_LINK = "https://example.com/notification/123"
    ETAG = "DEADBEEF"
    CREATE_PATH = f"/b/{BUCKET_NAME}/notificationConfigs"
    NOTIFICATION_PATH = f"/b/{BUCKET_NAME}/notificationConfigs/{NOTIFICATION_ID}"

    @staticmethod
    def event_types():
        from google.cloud.storage.notification import (
            OBJECT_FINALIZE_EVENT_TYPE,
            OBJECT_DELETE_EVENT_TYPE,
        )

        return [OBJECT_FINALIZE_EVENT_TYPE, OBJECT_DELETE_EVENT_TYPE]

    @staticmethod
    def payload_format():
        from google.cloud.storage.notification import JSON_API_V1_PAYLOAD_FORMAT

        return JSON_API_V1_PAYLOAD_FORMAT

    @staticmethod
    def _get_default_timeout():
        from google.cloud.storage.constants import _DEFAULT_TIMEOUT

        return _DEFAULT_TIMEOUT

    @staticmethod
    def _get_target_class():
        from google.cloud.storage.notification import BucketNotification

        return BucketNotification

    def _make_one(self, *args, **kw):
        return self._get_target_class()(*args, **kw)

    def _make_client(self, project=BUCKET_PROJECT):
        from google.cloud.storage.client import Client

        return mock.Mock(project=project, spec=Client)

    def _make_bucket(self, client, name=BUCKET_NAME, user_project=None):
        bucket = mock.Mock(spec=["client", "name", "user_project"])
        bucket.client = client
        bucket.name = name
        bucket.user_project = user_project
        return bucket

    def test_ctor_w_missing_project(self):
        client = self._make_client(project=None)
        bucket = self._make_bucket(client)

        with self.assertRaises(ValueError):
            self._make_one(bucket, self.TOPIC_NAME)

    def test_ctor_defaults(self):
        from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT

        client = self._make_client()
        bucket = self._make_bucket(client)

        notification = self._make_one(bucket, self.TOPIC_NAME)

        self.assertIs(notification.bucket, bucket)
        self.assertEqual(notification.topic_name, self.TOPIC_NAME)
        self.assertEqual(notification.topic_project, self.BUCKET_PROJECT)
        self.assertIsNone(notification.custom_attributes)
        self.assertIsNone(notification.event_types)
        self.assertIsNone(notification.blob_name_prefix)
        self.assertEqual(notification.payload_format, NONE_PAYLOAD_FORMAT)

    def test_ctor_explicit(self):
        client = self._make_client()
        bucket = self._make_bucket(client)

        notification = self._make_one(
            bucket,
            self.TOPIC_NAME,
            topic_project=self.TOPIC_ALT_PROJECT,
            custom_attributes=self.CUSTOM_ATTRIBUTES,
            event_types=self.event_types(),
            blob_name_prefix=self.BLOB_NAME_PREFIX,
            payload_format=self.payload_format(),
        )

        self.assertIs(notification.bucket, bucket)
        self.assertEqual(notification.topic_name, self.TOPIC_NAME)
        self.assertEqual(notification.topic_project, self.TOPIC_ALT_PROJECT)
        self.assertEqual(notification.custom_attributes, self.CUSTOM_ATTRIBUTES)
        self.assertEqual(notification.event_types, self.event_types())
        self.assertEqual(notification.blob_name_prefix, self.BLOB_NAME_PREFIX)
        self.assertEqual(notification.payload_format, self.payload_format())

    def test_from_api_repr_no_topic(self):
        klass = self._get_target_class()
        client = self._make_client()
        bucket = self._make_bucket(client)
        resource = {}

        with self.assertRaises(ValueError):
            klass.from_api_repr(resource, bucket=bucket)

    def test_from_api_repr_invalid_topic(self):
        klass = self._get_target_class()
        client = self._make_client()
        bucket = self._make_bucket(client)
        resource = {"topic": "@#$%"}

        with self.assertRaises(ValueError):
            klass.from_api_repr(resource, bucket=bucket)

    def test_from_api_repr_minimal(self):
        from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT

        klass = self._get_target_class()
        client = self._make_client()
        bucket = self._make_bucket(client)
        resource = {
            "topic": self.TOPIC_REF,
            "id": self.NOTIFICATION_ID,
            "etag": self.ETAG,
            "selfLink": self.SELF_LINK,
            "payload_format": NONE_PAYLOAD_FORMAT,
        }

        notification = klass.from_api_repr(resource, bucket=bucket)

        self.assertIs(notification.bucket, bucket)
        self.assertEqual(notification.topic_name, self.TOPIC_NAME)
        self.assertEqual(notification.topic_project, self.BUCKET_PROJECT)
        self.assertIsNone(notification.custom_attributes)
        self.assertIsNone(notification.event_types)
        self.assertIsNone(notification.blob_name_prefix)
        self.assertEqual(notification.payload_format, NONE_PAYLOAD_FORMAT)
        self.assertEqual(notification.etag, self.ETAG)
        self.assertEqual(notification.self_link, self.SELF_LINK)

    def test_from_api_repr_explicit(self):
        klass = self._get_target_class()
        client = self._make_client()
        bucket = self._make_bucket(client)
        resource = {
            "topic": self.TOPIC_ALT_REF,
            "custom_attributes": self.CUSTOM_ATTRIBUTES,
            "event_types": self.event_types(),
            "object_name_prefix": self.BLOB_NAME_PREFIX,
            "payload_format": self.payload_format(),
            "id": self.NOTIFICATION_ID,
            "etag": self.ETAG,
            "selfLink": self.SELF_LINK,
        }

        notification = klass.from_api_repr(resource, bucket=bucket)

        self.assertIs(notification.bucket, bucket)
        self.assertEqual(notification.topic_name, self.TOPIC_NAME)
        self.assertEqual(notification.topic_project, self.TOPIC_ALT_PROJECT)
        self.assertEqual(notification.custom_attributes, self.CUSTOM_ATTRIBUTES)
        self.assertEqual(notification.event_types, self.event_types())
        self.assertEqual(notification.blob_name_prefix, self.BLOB_NAME_PREFIX)
        self.assertEqual(notification.payload_format, self.payload_format())
        self.assertEqual(notification.notification_id, self.NOTIFICATION_ID)
        self.assertEqual(notification.etag, self.ETAG)
        self.assertEqual(notification.self_link, self.SELF_LINK)

    def test_notification_id(self):
        client = self._make_client()
        bucket = self._make_bucket(client)

        notification = self._make_one(bucket, self.TOPIC_NAME)

        self.assertIsNone(notification.notification_id)

        notification._properties["id"] = self.NOTIFICATION_ID
        self.assertEqual(notification.notification_id, self.NOTIFICATION_ID)

    def test_etag(self):
        client = self._make_client()
        bucket = self._make_bucket(client)

        notification = self._make_one(bucket, self.TOPIC_NAME)

        self.assertIsNone(notification.etag)

        notification._properties["etag"] = self.ETAG
        self.assertEqual(notification.etag, self.ETAG)

    def test_self_link(self):
        client = self._make_client()
        bucket = self._make_bucket(client)

        notification = self._make_one(bucket, self.TOPIC_NAME)

        self.assertIsNone(notification.self_link)

        notification._properties["selfLink"] = self.SELF_LINK
        self.assertEqual(notification.self_link, self.SELF_LINK)

    def test_create_w_existing_notification_id(self):
        client = mock.Mock(spec=["_post_resource", "project"])
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID

        with self.assertRaises(ValueError):
            notification.create()

        client._post_resource.assert_not_called()

    def test_create_wo_topic_name(self):
        from google.cloud.exceptions import BadRequest
        from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT

        client = mock.Mock(spec=["_post_resource", "project"])
        client.project = self.BUCKET_PROJECT
        client._post_resource.side_effect = BadRequest(
            "Invalid Google Cloud Pub/Sub topic."
        )
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, None)

        with self.assertRaises(BadRequest):
            notification.create()

        expected_topic = self.TOPIC_REF_FMT.format(self.BUCKET_PROJECT, "")
        expected_data = {
            "topic": expected_topic,
            "payload_format": NONE_PAYLOAD_FORMAT,
        }
        expected_query_params = {}
        client._post_resource.assert_called_once_with(
            self.CREATE_PATH,
            expected_data,
            query_params=expected_query_params,
            timeout=self._get_default_timeout(),
            retry=None,
        )

    def test_create_w_defaults(self):
        from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT

        api_response = {
            "topic": self.TOPIC_REF,
            "id": self.NOTIFICATION_ID,
            "etag": self.ETAG,
            "selfLink": self.SELF_LINK,
            "payload_format": NONE_PAYLOAD_FORMAT,
        }
        client = mock.Mock(spec=["_post_resource", "project"])
        client.project = self.BUCKET_PROJECT
        client._post_resource.return_value = api_response
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)

        notification.create()

        self.assertEqual(notification.notification_id, self.NOTIFICATION_ID)
        self.assertEqual(notification.etag, self.ETAG)
        self.assertEqual(notification.self_link, self.SELF_LINK)
        self.assertIsNone(notification.custom_attributes)
        self.assertIsNone(notification.event_types)
        self.assertIsNone(notification.blob_name_prefix)
        self.assertEqual(notification.payload_format, NONE_PAYLOAD_FORMAT)

        expected_data = {
            "topic": self.TOPIC_REF,
            "payload_format": NONE_PAYLOAD_FORMAT,
        }
        expected_query_params = {}
        client._post_resource.assert_called_once_with(
            self.CREATE_PATH,
            expected_data,
            query_params=expected_query_params,
            timeout=self._get_default_timeout(),
            retry=None,
        )

    def test_create_w_explicit_client_w_timeout_w_retry(self):
        user_project = "user-project-123"
        api_response = {
            "topic": self.TOPIC_ALT_REF,
            "custom_attributes": self.CUSTOM_ATTRIBUTES,
            "event_types": self.event_types(),
            "object_name_prefix": self.BLOB_NAME_PREFIX,
            "payload_format": self.payload_format(),
            "id": self.NOTIFICATION_ID,
            "etag": self.ETAG,
            "selfLink": self.SELF_LINK,
        }
        bucket = self._make_bucket(client=None, user_project=user_project)
        notification = self._make_one(
            bucket,
            self.TOPIC_NAME,
            topic_project=self.TOPIC_ALT_PROJECT,
            custom_attributes=self.CUSTOM_ATTRIBUTES,
            event_types=self.event_types(),
            blob_name_prefix=self.BLOB_NAME_PREFIX,
            payload_format=self.payload_format(),
        )
        client = mock.Mock(spec=["_post_resource", "project"])
        client.project = self.BUCKET_PROJECT
        client._post_resource.return_value = api_response
        timeout = 42
        retry = mock.Mock(spec=[])

        notification.create(client=client, timeout=timeout, retry=retry)

        self.assertEqual(notification.custom_attributes, self.CUSTOM_ATTRIBUTES)
        self.assertEqual(notification.event_types, self.event_types())
        self.assertEqual(notification.blob_name_prefix, self.BLOB_NAME_PREFIX)
        self.assertEqual(notification.payload_format, self.payload_format())
        self.assertEqual(notification.notification_id, self.NOTIFICATION_ID)
        self.assertEqual(notification.etag, self.ETAG)
        self.assertEqual(notification.self_link, self.SELF_LINK)

        expected_data = {
            "topic": self.TOPIC_ALT_REF,
            "custom_attributes": self.CUSTOM_ATTRIBUTES,
            "event_types": self.event_types(),
            "object_name_prefix": self.BLOB_NAME_PREFIX,
            "payload_format": self.payload_format(),
        }
        expected_query_params = {"userProject": user_project}
        client._post_resource.assert_called_once_with(
            self.CREATE_PATH,
            expected_data,
            query_params=expected_query_params,
            timeout=timeout,
            retry=retry,
        )

    def test_exists_wo_notification_id(self):
        client = mock.Mock(spec=["_get_resource", "project"])
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)

        with self.assertRaises(ValueError):
            notification.exists()

        client._get_resource.assert_not_called()

    def test_exists_miss_w_defaults(self):
        from google.cloud.exceptions import NotFound

        client = mock.Mock(spec=["_get_resource", "project"])
        client._get_resource.side_effect = NotFound("testing")
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID

        self.assertFalse(notification.exists())

        expected_query_params = {}
        client._get_resource.assert_called_once_with(
            self.NOTIFICATION_PATH,
            query_params=expected_query_params,
            timeout=self._get_default_timeout(),
            retry=DEFAULT_RETRY,
        )

    def test_exists_hit_w_explicit_w_user_project(self):
        user_project = "user-project-123"
        api_response = {
            "topic": self.TOPIC_REF,
            "id": self.NOTIFICATION_ID,
            "etag": self.ETAG,
            "selfLink": self.SELF_LINK,
        }
        client = mock.Mock(spec=["_get_resource", "project"])
        client._get_resource.return_vale = api_response
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client, user_project=user_project)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID
        timeout = 42
        retry = mock.Mock(spec=[])

        self.assertTrue(
            notification.exists(client=client, timeout=timeout, retry=retry)
        )

        expected_query_params = {"userProject": user_project}
        client._get_resource.assert_called_once_with(
            self.NOTIFICATION_PATH,
            query_params=expected_query_params,
            timeout=timeout,
            retry=retry,
        )

    def test_reload_wo_notification_id(self):
        client = mock.Mock(spec=["_get_resource", "project"])
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)

        with self.assertRaises(ValueError):
            notification.reload()

        client._get_resource.assert_not_called()

    def test_reload_miss_w_defaults(self):
        from google.cloud.exceptions import NotFound

        client = mock.Mock(spec=["_get_resource", "project"])
        client._get_resource.side_effect = NotFound("testing")
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID

        with self.assertRaises(NotFound):
            notification.reload()

        expected_query_params = {}
        client._get_resource.assert_called_once_with(
            self.NOTIFICATION_PATH,
            query_params=expected_query_params,
            timeout=self._get_default_timeout(),
            retry=DEFAULT_RETRY,
        )

    def test_reload_hit_w_explicit_w_user_project(self):
        from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT

        user_project = "user-project-123"
        api_response = {
            "topic": self.TOPIC_REF,
            "id": self.NOTIFICATION_ID,
            "etag": self.ETAG,
            "selfLink": self.SELF_LINK,
            "payload_format": NONE_PAYLOAD_FORMAT,
        }
        client = mock.Mock(spec=["_get_resource", "project"])
        client._get_resource.return_value = api_response
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client, user_project=user_project)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID
        timeout = 42
        retry = mock.Mock(spec=[])

        notification.reload(client=client, timeout=timeout, retry=retry)

        self.assertEqual(notification.etag, self.ETAG)
        self.assertEqual(notification.self_link, self.SELF_LINK)
        self.assertIsNone(notification.custom_attributes)
        self.assertIsNone(notification.event_types)
        self.assertIsNone(notification.blob_name_prefix)
        self.assertEqual(notification.payload_format, NONE_PAYLOAD_FORMAT)

        expected_query_params = {"userProject": user_project}
        client._get_resource.assert_called_once_with(
            self.NOTIFICATION_PATH,
            query_params=expected_query_params,
            timeout=timeout,
            retry=retry,
        )

    def test_delete_wo_notification_id(self):
        client = mock.Mock(spec=["_delete_resource", "project"])
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)

        with self.assertRaises(ValueError):
            notification.delete()

        client._delete_resource.assert_not_called()

    def test_delete_miss_w_defaults(self):
        from google.cloud.exceptions import NotFound

        client = mock.Mock(spec=["_delete_resource", "project"])
        client._delete_resource.side_effect = NotFound("testing")
        client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(client)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID

        with self.assertRaises(NotFound):
            notification.delete()

        client._delete_resource.assert_called_once_with(
            self.NOTIFICATION_PATH,
            query_params={},
            timeout=self._get_default_timeout(),
            retry=DEFAULT_RETRY,
        )

    def test_delete_hit_w_explicit_client_timeout_retry(self):
        user_project = "user-project-123"
        client = mock.Mock(spec=["_delete_resource"])
        client._delete_resource.return_value = None
        bucket_client = mock.Mock(spec=["project"])
        bucket_client.project = self.BUCKET_PROJECT
        bucket = self._make_bucket(bucket_client, user_project=user_project)
        notification = self._make_one(bucket, self.TOPIC_NAME)
        notification._properties["id"] = self.NOTIFICATION_ID
        timeout = 42
        retry = mock.Mock(spec=[])

        notification.delete(client=client, timeout=timeout, retry=retry)

        client._delete_resource.assert_called_once_with(
            self.NOTIFICATION_PATH,
            query_params={"userProject": user_project},
            timeout=timeout,
            retry=retry,
        )


class Test__parse_topic_path(unittest.TestCase):
    @staticmethod
    def _call_fut(*args, **kwargs):
        from google.cloud.storage import notification

        return notification._parse_topic_path(*args, **kwargs)

    @staticmethod
    def _make_topic_path(project, topic_name):
        from google.cloud.storage import notification

        return notification._TOPIC_REF_FMT.format(project, topic_name)

    def test_project_name_too_long(self):
        project = "a" * 31
        topic_path = self._make_topic_path(project, "topic-name")
        with self.assertRaises(ValueError):
            self._call_fut(topic_path)

    def test_project_name_uppercase(self):
        project = "aaaAaa"
        topic_path = self._make_topic_path(project, "topic-name")
        with self.assertRaises(ValueError):
            self._call_fut(topic_path)

    def test_leading_digit(self):
        project = "1aaaaa"
        topic_path = self._make_topic_path(project, "topic-name")
        with self.assertRaises(ValueError):
            self._call_fut(topic_path)

    def test_leading_hyphen(self):
        project = "-aaaaa"
        topic_path = self._make_topic_path(project, "topic-name")
        with self.assertRaises(ValueError):
            self._call_fut(topic_path)

    def test_trailing_hyphen(self):
        project = "aaaaa-"
        topic_path = self._make_topic_path(project, "topic-name")
        with self.assertRaises(ValueError):
            self._call_fut(topic_path)

    def test_invalid_format(self):
        topic_path = "@#$%"
        with self.assertRaises(ValueError):
            self._call_fut(topic_path)

    def test_success(self):
        topic_name = "tah-pic-nehm"
        project_choices = (
            "a" * 30,  # Max length.
            "a-b--c---d",  # Valid hyphen usage.
            "abcdefghijklmnopqrstuvwxyz",  # Valid letters.
            "z0123456789",  # Valid digits (non-leading).
            "a-bcdefghijklmn12opqrstuv0wxyz",
        )
        for project in project_choices:
            topic_path = self._make_topic_path(project, topic_name)
            result = self._call_fut(topic_path)
            self.assertEqual(result, (topic_name, project))