|
| 1 | +/* |
| 2 | + * Copyright 2000-2026 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.server.webpush; |
| 17 | + |
| 18 | +import java.lang.reflect.Field; |
| 19 | + |
| 20 | +import com.interaso.webpush.WebPush.SubscriptionState; |
| 21 | +import com.interaso.webpush.WebPushService; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertThrows; |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | +import static org.mockito.Mockito.when; |
| 30 | + |
| 31 | +public class WebPushTest { |
| 32 | + |
| 33 | + private static final String PUBLIC_KEY = "BPXZkCj3rxN6a1v21aCyMQHmTaAn1QZyWRDeBfwQ4qperQNszSD9JhnZv9b45vHLQLxnK3zsCvCl1r8EDpPDjoM"; |
| 34 | + private static final String PRIVATE_KEY = "W-J0f4QwsjrEwDnJJTky5waIX9xNaM87-Dfd42_SEDM"; |
| 35 | + |
| 36 | + private WebPush webPush; |
| 37 | + private WebPushService pushServiceMock; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() throws Exception { |
| 41 | + webPush = new WebPush(PUBLIC_KEY, PRIVATE_KEY, |
| 42 | + "mailto:test@example.com"); |
| 43 | + pushServiceMock = mock(WebPushService.class); |
| 44 | + Field field = WebPush.class.getDeclaredField("pushService"); |
| 45 | + field.setAccessible(true); |
| 46 | + field.set(webPush, pushServiceMock); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void sendNotification_subscriptionExpired_throwsWebPushException() { |
| 51 | + when(pushServiceMock.send(any(String.class), any(String.class), |
| 52 | + any(String.class), any(String.class), any(), any(), any())) |
| 53 | + .thenReturn(SubscriptionState.EXPIRED); |
| 54 | + |
| 55 | + WebPushSubscription subscription = new WebPushSubscription( |
| 56 | + "https://push.example/endpoint", |
| 57 | + new WebPushKeys("p256dhKey", "authKey")); |
| 58 | + WebPushMessage message = new WebPushMessage("title", "body"); |
| 59 | + |
| 60 | + WebPushException ex = assertThrows(WebPushException.class, |
| 61 | + () -> webPush.sendNotification(subscription, message)); |
| 62 | + assertEquals( |
| 63 | + "Sending of web push notification failed with status code 404 or 410", |
| 64 | + ex.getMessage()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void sendNotification_subscriptionActive_doesNotThrow() { |
| 69 | + when(pushServiceMock.send(any(String.class), any(String.class), |
| 70 | + any(String.class), any(String.class), any(), any(), any())) |
| 71 | + .thenReturn(SubscriptionState.ACTIVE); |
| 72 | + |
| 73 | + WebPushSubscription subscription = new WebPushSubscription( |
| 74 | + "https://push.example/endpoint", |
| 75 | + new WebPushKeys("p256dhKey", "authKey")); |
| 76 | + WebPushMessage message = new WebPushMessage("title", "body"); |
| 77 | + |
| 78 | + webPush.sendNotification(subscription, message); |
| 79 | + } |
| 80 | +} |
0 commit comments