diff --git a/echo-notifications/echo-notifications.gradle b/echo-notifications/echo-notifications.gradle index 2eb195600..b76d151d3 100644 --- a/echo-notifications/echo-notifications.gradle +++ b/echo-notifications/echo-notifications.gradle @@ -43,6 +43,7 @@ dependencies { testImplementation "org.springframework:spring-test" testImplementation "com.squareup.retrofit2:retrofit-mock" testImplementation "com.github.tomakehurst:wiremock-jre8" + testImplementation "org.assertj:assertj-core" annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" } diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatNotificationService.groovy b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatNotificationService.groovy index e8f6cc29a..8e8a2bb16 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatNotificationService.groovy +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatNotificationService.groovy @@ -20,6 +20,7 @@ import com.netflix.spinnaker.echo.api.Notification import com.netflix.spinnaker.echo.controller.EchoResponse import com.netflix.spinnaker.echo.notification.NotificationService import com.netflix.spinnaker.echo.notification.NotificationTemplateEngine +import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty @@ -47,16 +48,17 @@ class BearychatNotificationService implements NotificationService { EchoResponse.Void handle(Notification notification) { //TODO: add body templates def body = notificationTemplateEngine.build(notification, NotificationTemplateEngine.Type.BODY) - List userList = bearychatService.getUserList(token) + List userList = Retrofit2SyncCall.execute(bearychatService.getUserList(token)) notification.to.each { String userid = getUseridByEmail(userList, it) - CreateP2PChannelResponse channelInfo = bearychatService.createp2pchannel(token, new CreateP2PChannelPara(user_id: userid)) + CreateP2PChannelResponse channelInfo = Retrofit2SyncCall.execute(bearychatService.createp2pchannel(token, new CreateP2PChannelPara(user_id: userid))) String channelId = getVChannelid(channelInfo) //TODO:add text msg - bearychatService.sendMessage(token, new SendMessagePara(vchannel_id: channelId, + Retrofit2SyncCall.execute(bearychatService.sendMessage(token, new SendMessagePara(vchannel_id: channelId, text: body, - attachments: " " )) + attachments: " " ))) } + return } private static String getUseridByEmail(List userList,String targetEmail) { diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatService.groovy b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatService.groovy index a662616b0..7b7fc5bf4 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatService.groovy +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatService.groovy @@ -34,6 +34,6 @@ interface BearychatService { @Body CreateP2PChannelPara para) @POST("v1/message.create") - Call> sendMessage(@Query("token") String token, + Call sendMessage(@Query("token") String token, @Body SendMessagePara para) } diff --git a/echo-notifications/src/test/java/com/netflix/spinnaker/echo/bearychat/BearychatNotificationServiceTest.java b/echo-notifications/src/test/java/com/netflix/spinnaker/echo/bearychat/BearychatNotificationServiceTest.java new file mode 100644 index 000000000..2c8b7c4b0 --- /dev/null +++ b/echo-notifications/src/test/java/com/netflix/spinnaker/echo/bearychat/BearychatNotificationServiceTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2025 OpsMx, Inc. + * + * 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. + */ + +package com.netflix.spinnaker.echo.bearychat; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import com.netflix.spinnaker.echo.api.Notification; +import com.netflix.spinnaker.echo.notification.NotificationTemplateEngine; +import java.util.List; +import java.util.Map; +import okhttp3.MediaType; +import okhttp3.ResponseBody; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import retrofit2.mock.Calls; + +@SpringBootTest( + properties = {"bearychat.enabled=true", "bearychat.token=abc"}, + classes = { + BearychatNotificationService.class, + NotificationTemplateEngine.class, + freemarker.template.Configuration.class, + BearychatNotificationServiceTest.TestConfig.class + }, + webEnvironment = SpringBootTest.WebEnvironment.NONE) +public class BearychatNotificationServiceTest { + + @Autowired BearychatNotificationService bearychatNotificationService; + + @Autowired BearychatService bearychatService; + + @Test + void testBearychatNotificationService() { + BearychatUserInfo userInfo = new BearychatUserInfo(); + userInfo.setId("uid1"); + userInfo.setEmail("foo@bar.com"); + Mockito.when(bearychatService.getUserList("abc")).thenReturn(Calls.response(List.of(userInfo))); + + CreateP2PChannelPara para = new CreateP2PChannelPara(); + para.setUser_id("uid1"); + CreateP2PChannelResponse channelResponse = new CreateP2PChannelResponse(); + channelResponse.setId("cid1"); + channelResponse.setVchannel_id("cid1"); + Mockito.when( + bearychatService.createp2pchannel( + Mockito.anyString(), Mockito.any(CreateP2PChannelPara.class))) + .thenReturn(Calls.response(channelResponse)); + + Mockito.when( + bearychatService.sendMessage(Mockito.anyString(), Mockito.any(SendMessagePara.class))) + .thenReturn(Calls.response(ResponseBody.create("", MediaType.parse("application/json")))); + + Notification notification = new Notification(); + notification.setNotificationType("bearychat"); + notification.setTo(List.of("foo@bar.com")); + notification.setAdditionalContext(Map.of("body", "notification Body")); + + assertDoesNotThrow(() -> bearychatNotificationService.handle(notification)); + } + + @Configuration + public static class TestConfig { + @MockBean BearychatService bearychatService; + + @Bean + public BearychatService bearychatService() { + return bearychatService; + } + } +}