|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package notification |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/apache/answer/internal/base/constant" |
| 29 | + basedata "github.com/apache/answer/internal/base/data" |
| 30 | + "github.com/apache/answer/internal/entity" |
| 31 | + "github.com/apache/answer/internal/schema" |
| 32 | + "github.com/apache/answer/internal/service/noticequeue" |
| 33 | + notficationcommon "github.com/apache/answer/internal/service/notification_common" |
| 34 | + "github.com/segmentfault/pacman/cache" |
| 35 | + "github.com/stretchr/testify/assert" |
| 36 | + "github.com/stretchr/testify/require" |
| 37 | +) |
| 38 | + |
| 39 | +// redDotTestNotificationRepo is an in-memory notification repo. Its ClearIDUnRead only changes |
| 40 | +// the notification while it is still unread and reports the changed rows, the same way the |
| 41 | +// database repo does. |
| 42 | +type redDotTestNotificationRepo struct { |
| 43 | + notifications map[string]*entity.Notification |
| 44 | +} |
| 45 | + |
| 46 | +func (f *redDotTestNotificationRepo) AddNotification(_ context.Context, notification *entity.Notification) error { |
| 47 | + f.notifications[notification.ID] = notification |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (f *redDotTestNotificationRepo) GetNotificationPage(_ context.Context, _ *schema.NotificationSearch) ( |
| 52 | + []*entity.Notification, int64, error) { |
| 53 | + return nil, 0, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (f *redDotTestNotificationRepo) ClearUnRead(_ context.Context, _ string, _ int) error { |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (f *redDotTestNotificationRepo) ClearIDUnRead(_ context.Context, userID string, id string) (int64, error) { |
| 61 | + notification, exist := f.notifications[id] |
| 62 | + if !exist || notification.UserID != userID || notification.IsRead != schema.NotificationNotRead { |
| 63 | + return 0, nil |
| 64 | + } |
| 65 | + notification.IsRead = schema.NotificationRead |
| 66 | + return 1, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (f *redDotTestNotificationRepo) GetByUserIdObjectIdTypeId(_ context.Context, _, _ string, _ int) ( |
| 70 | + *entity.Notification, bool, error) { |
| 71 | + return nil, false, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (f *redDotTestNotificationRepo) UpdateNotificationContent(_ context.Context, _ *entity.Notification) error { |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (f *redDotTestNotificationRepo) GetById(_ context.Context, id string) (*entity.Notification, bool, error) { |
| 79 | + notification, exist := f.notifications[id] |
| 80 | + return notification, exist, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (f *redDotTestNotificationRepo) CountNotificationByUser(_ context.Context, _ *entity.Notification) (int64, error) { |
| 84 | + return 0, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (f *redDotTestNotificationRepo) DeleteNotification(_ context.Context, _ string) error { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (f *redDotTestNotificationRepo) DeleteUserNotificationConfig(_ context.Context, _ string) error { |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// TestNotificationService_ClearIDUnReadDecreaseRedDotOnce checks that reading the same |
| 96 | +// notification more than once only decreases the red dot cache once, because only the request |
| 97 | +// that really changes the notification from unread to read may decrease it. |
| 98 | +func TestNotificationService_ClearIDUnReadDecreaseRedDotOnce(t *testing.T) { |
| 99 | + ctx := context.Background() |
| 100 | + const ( |
| 101 | + userID = "red-dot-user" |
| 102 | + notificationID = "100" |
| 103 | + ) |
| 104 | + |
| 105 | + cacheStore, cleanup, err := basedata.NewCache(&basedata.CacheConf{}) |
| 106 | + require.NoError(t, err) |
| 107 | + t.Cleanup(cleanup) |
| 108 | + |
| 109 | + testData := &basedata.Data{Cache: cacheStore} |
| 110 | + repo := &redDotTestNotificationRepo{notifications: map[string]*entity.Notification{}} |
| 111 | + info := &entity.Notification{ |
| 112 | + ID: notificationID, |
| 113 | + UserID: userID, |
| 114 | + ObjectID: "1", |
| 115 | + Content: "{}", |
| 116 | + Type: schema.NotificationTypeInbox, |
| 117 | + IsRead: schema.NotificationNotRead, |
| 118 | + Status: schema.NotificationStatusNormal, |
| 119 | + } |
| 120 | + require.NoError(t, repo.AddNotification(ctx, info)) |
| 121 | + |
| 122 | + service := &NotificationService{ |
| 123 | + data: testData, |
| 124 | + notificationRepo: repo, |
| 125 | + notificationCommon: notficationcommon.NewNotificationCommon( |
| 126 | + testData, repo, nil, nil, nil, nil, noticequeue.NewService(), nil, nil), |
| 127 | + } |
| 128 | + |
| 129 | + // the user has 3 unread notifications and reads one of them |
| 130 | + redDotKey := fmt.Sprintf(constant.RedDotCacheKey, constant.NotificationTypeInbox, userID) |
| 131 | + require.NoError(t, cacheStore.SetInt64(ctx, redDotKey, 3, time.Hour)) |
| 132 | + |
| 133 | + require.NoError(t, service.ClearIDUnRead(ctx, userID, notificationID)) |
| 134 | + assertRedDotCount(t, ctx, cacheStore, redDotKey, 2) |
| 135 | + |
| 136 | + // a duplicate request must not decrease the red dot cache again |
| 137 | + require.NoError(t, service.ClearIDUnRead(ctx, userID, notificationID)) |
| 138 | + assertRedDotCount(t, ctx, cacheStore, redDotKey, 2) |
| 139 | + |
| 140 | + // another user can neither read the notification nor change the red dot cache |
| 141 | + require.NoError(t, service.ClearIDUnRead(ctx, "another-user", notificationID)) |
| 142 | + assertRedDotCount(t, ctx, cacheStore, redDotKey, 2) |
| 143 | +} |
| 144 | + |
| 145 | +func assertRedDotCount(t *testing.T, ctx context.Context, c cache.Cache, key string, expected int64) { |
| 146 | + count, exist, err := c.GetInt64(ctx, key) |
| 147 | + require.NoError(t, err) |
| 148 | + require.True(t, exist, "red dot cache key should exist") |
| 149 | + assert.Equal(t, expected, count) |
| 150 | +} |
0 commit comments