diff --git a/src/main/java/com/wellmeet/config/WebPushConfig.java b/src/main/java/com/wellmeet/config/WebPushConfig.java new file mode 100644 index 0000000..f858949 --- /dev/null +++ b/src/main/java/com/wellmeet/config/WebPushConfig.java @@ -0,0 +1,28 @@ +package com.wellmeet.config; + +import java.security.GeneralSecurityException; +import java.security.Security; +import lombok.RequiredArgsConstructor; +import nl.martijndwars.webpush.PushService; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +@Configuration +@Profile("!test") +@RequiredArgsConstructor +public class WebPushConfig { + + private final VapidConfig vapidConfig; + + @Bean + public PushService pushService() throws GeneralSecurityException { + Security.addProvider(new BouncyCastleProvider()); + PushService pushService = new PushService(); + pushService.setPublicKey(vapidConfig.getPublicKey()); + pushService.setPrivateKey(vapidConfig.getPrivateKey()); + pushService.setSubject(vapidConfig.getSubject()); + return pushService; + } +} diff --git a/src/main/java/com/wellmeet/notification/consumer/NotificationSender.java b/src/main/java/com/wellmeet/notification/consumer/NotificationSender.java index 17dff12..baa8717 100644 --- a/src/main/java/com/wellmeet/notification/consumer/NotificationSender.java +++ b/src/main/java/com/wellmeet/notification/consumer/NotificationSender.java @@ -21,8 +21,8 @@ public class NotificationSender { public void send(NotificationMessage message, List enables) { NotificationHistory history = new NotificationHistory(message.getRecipient(), message.getPayload().toString()); + notificationHistoryRepository.save(history); for (NotificationEnabled enabled : enables) { - notificationHistoryRepository.save(history); Sender sender = senders.stream() .filter(low -> low.isEnabled(enabled.getChannel())) .findFirst() diff --git a/src/main/java/com/wellmeet/notification/consumer/dto/MessageHeader.java b/src/main/java/com/wellmeet/notification/consumer/dto/MessageHeader.java index 2ac3a81..8ac986d 100644 --- a/src/main/java/com/wellmeet/notification/consumer/dto/MessageHeader.java +++ b/src/main/java/com/wellmeet/notification/consumer/dto/MessageHeader.java @@ -10,5 +10,4 @@ public class MessageHeader { private String messageId; private LocalDateTime timestamp; - private String source; } diff --git a/src/main/java/com/wellmeet/notification/consumer/dto/NotificationType.java b/src/main/java/com/wellmeet/notification/consumer/dto/NotificationType.java index fd4f05f..387effb 100644 --- a/src/main/java/com/wellmeet/notification/consumer/dto/NotificationType.java +++ b/src/main/java/com/wellmeet/notification/consumer/dto/NotificationType.java @@ -5,5 +5,8 @@ @Getter public enum NotificationType { - RESERVATION_CREATED + RESERVATION_CREATED, + RESERVATION_CONFIRMED, + RESERVATION_UPDATED, + RESERVATION_CANCELED, } diff --git a/src/main/java/com/wellmeet/notification/webpush/WebPushService.java b/src/main/java/com/wellmeet/notification/webpush/WebPushService.java index 6785a06..1b405c6 100644 --- a/src/main/java/com/wellmeet/notification/webpush/WebPushService.java +++ b/src/main/java/com/wellmeet/notification/webpush/WebPushService.java @@ -7,8 +7,8 @@ import com.wellmeet.notification.webpush.dto.SubscribeResponse; import com.wellmeet.notification.webpush.dto.TestPushRequest; import com.wellmeet.notification.webpush.dto.UnsubscribeRequest; -import com.wellmeet.notification.webpush.infrastructure.WebPushSender; import com.wellmeet.notification.webpush.repository.PushSubscriptionRepository; +import com.wellmeet.notification.webpush.sender.WebPushSender; import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/wellmeet/notification/webpush/infrastructure/WebPushSender.java b/src/main/java/com/wellmeet/notification/webpush/sender/WebPushSender.java similarity index 85% rename from src/main/java/com/wellmeet/notification/webpush/infrastructure/WebPushSender.java rename to src/main/java/com/wellmeet/notification/webpush/sender/WebPushSender.java index 42c09f9..4bd5665 100644 --- a/src/main/java/com/wellmeet/notification/webpush/infrastructure/WebPushSender.java +++ b/src/main/java/com/wellmeet/notification/webpush/sender/WebPushSender.java @@ -1,7 +1,6 @@ -package com.wellmeet.notification.webpush.infrastructure; +package com.wellmeet.notification.webpush.sender; import com.fasterxml.jackson.databind.ObjectMapper; -import com.wellmeet.config.VapidConfig; import com.wellmeet.exception.ErrorCode; import com.wellmeet.exception.WellMeetNotificationException; import com.wellmeet.notification.Sender; @@ -10,10 +9,8 @@ import com.wellmeet.notification.webpush.domain.PushSubscription; import com.wellmeet.notification.webpush.dto.TestPushRequest; import com.wellmeet.notification.webpush.repository.PushSubscriptionRepository; -import jakarta.annotation.PostConstruct; import java.io.IOException; import java.security.GeneralSecurityException; -import java.security.Security; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,7 +20,6 @@ import nl.martijndwars.webpush.PushService; import nl.martijndwars.webpush.Subscription; import nl.martijndwars.webpush.Subscription.Keys; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.jose4j.lang.JoseException; import org.springframework.stereotype.Service; @@ -32,22 +28,8 @@ public class WebPushSender implements Sender { private final PushSubscriptionRepository pushSubscriptionRepository; - private final VapidConfig vapidConfig; + private final PushService pushService; private final ObjectMapper objectMapper = new ObjectMapper(); - private PushService pushService; - - @PostConstruct - public void init() { - Security.addProvider(new BouncyCastleProvider()); - try { - pushService = new PushService(); - pushService.setPublicKey(vapidConfig.getPublicKey()); - pushService.setPrivateKey(vapidConfig.getPrivateKey()); - pushService.setSubject(vapidConfig.getSubject()); - } catch (Exception e) { - throw new WellMeetNotificationException(ErrorCode.INTERNAL_SERVER_ERROR); - } - } @Override public boolean isEnabled(NotificationChannel channel) { diff --git a/src/test/java/com/wellmeet/BaseControllerTest.java b/src/test/java/com/wellmeet/BaseControllerTest.java index e669f4d..3371b5f 100644 --- a/src/test/java/com/wellmeet/BaseControllerTest.java +++ b/src/test/java/com/wellmeet/BaseControllerTest.java @@ -1,5 +1,6 @@ package com.wellmeet; +import com.wellmeet.config.WebPushTestConfig; import com.wellmeet.notification.webpush.repository.PushSubscriptionRepository; import io.restassured.RestAssured; import io.restassured.builder.RequestSpecBuilder; @@ -11,11 +12,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; @ExtendWith(DataBaseCleaner.class) @ActiveProfiles("test") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Import(WebPushTestConfig.class) public abstract class BaseControllerTest { @Autowired diff --git a/src/test/java/com/wellmeet/BaseServiceTest.java b/src/test/java/com/wellmeet/BaseServiceTest.java index dbe7792..38e8b0a 100644 --- a/src/test/java/com/wellmeet/BaseServiceTest.java +++ b/src/test/java/com/wellmeet/BaseServiceTest.java @@ -1,14 +1,17 @@ package com.wellmeet; +import com.wellmeet.config.WebPushTestConfig; import com.wellmeet.notification.webpush.repository.PushSubscriptionRepository; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; @ExtendWith(DataBaseCleaner.class) @ActiveProfiles("test") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +@Import(WebPushTestConfig.class) public abstract class BaseServiceTest { @Autowired diff --git a/src/test/java/com/wellmeet/WellmeetNotificationApplicationTests.java b/src/test/java/com/wellmeet/WellmeetNotificationApplicationTests.java index 23debab..40ac6ac 100644 --- a/src/test/java/com/wellmeet/WellmeetNotificationApplicationTests.java +++ b/src/test/java/com/wellmeet/WellmeetNotificationApplicationTests.java @@ -1,11 +1,14 @@ package com.wellmeet; +import com.wellmeet.config.WebPushTestConfig; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @ActiveProfiles("test") +@Import(WebPushTestConfig.class) class WellmeetNotificationApplicationTests { @Test diff --git a/src/test/java/com/wellmeet/config/WebPushTestConfig.java b/src/test/java/com/wellmeet/config/WebPushTestConfig.java new file mode 100644 index 0000000..fb82a9a --- /dev/null +++ b/src/test/java/com/wellmeet/config/WebPushTestConfig.java @@ -0,0 +1,18 @@ +package com.wellmeet.config; + +import static org.mockito.Mockito.mock; + +import nl.martijndwars.webpush.PushService; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; + +@TestConfiguration +public class WebPushTestConfig { + + @Bean + @Primary + public PushService pushService() { + return mock(PushService.class); + } +}