Skip to content

Commit 8a2b94a

Browse files
fix(notifications): add Retrofit2SyncCall.execute to BearychatService in BearychatNotificationService (#1482)
* test(notifications): add a test to demonstrate the missing Retrofit2SyncCall.execute calls in BearychatNotificationService * fix(notifications): add Retrofit2SyncCall.execute to BearychatService in BearychatNotificationService
1 parent d3c1833 commit 8a2b94a

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-5
lines changed

echo-notifications/echo-notifications.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
testImplementation "org.springframework:spring-test"
4444
testImplementation "com.squareup.retrofit2:retrofit-mock"
4545
testImplementation "com.github.tomakehurst:wiremock-jre8"
46+
testImplementation "org.assertj:assertj-core"
4647
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
4748
}
4849

echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatNotificationService.groovy

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.netflix.spinnaker.echo.api.Notification
2020
import com.netflix.spinnaker.echo.controller.EchoResponse
2121
import com.netflix.spinnaker.echo.notification.NotificationService
2222
import com.netflix.spinnaker.echo.notification.NotificationTemplateEngine
23+
import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall
2324
import org.springframework.beans.factory.annotation.Autowired
2425
import org.springframework.beans.factory.annotation.Value
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
@@ -47,16 +48,17 @@ class BearychatNotificationService implements NotificationService {
4748
EchoResponse.Void handle(Notification notification) {
4849
//TODO: add body templates
4950
def body = notificationTemplateEngine.build(notification, NotificationTemplateEngine.Type.BODY)
50-
List<BearychatUserInfo> userList = bearychatService.getUserList(token)
51+
List<BearychatUserInfo> userList = Retrofit2SyncCall.execute(bearychatService.getUserList(token))
5152
notification.to.each {
5253
String userid = getUseridByEmail(userList, it)
53-
CreateP2PChannelResponse channelInfo = bearychatService.createp2pchannel(token, new CreateP2PChannelPara(user_id: userid))
54+
CreateP2PChannelResponse channelInfo = Retrofit2SyncCall.execute(bearychatService.createp2pchannel(token, new CreateP2PChannelPara(user_id: userid)))
5455
String channelId = getVChannelid(channelInfo)
5556
//TODO:add text msg
56-
bearychatService.sendMessage(token, new SendMessagePara(vchannel_id: channelId,
57+
Retrofit2SyncCall.execute(bearychatService.sendMessage(token, new SendMessagePara(vchannel_id: channelId,
5758
text: body,
58-
attachments: " " ))
59+
attachments: " " )))
5960
}
61+
return
6062
}
6163

6264
private static String getUseridByEmail(List<BearychatUserInfo> userList,String targetEmail) {

echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/bearychat/BearychatService.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ interface BearychatService {
3434
@Body CreateP2PChannelPara para)
3535

3636
@POST("v1/message.create")
37-
Call<Response<ResponseBody>> sendMessage(@Query("token") String token,
37+
Call<ResponseBody> sendMessage(@Query("token") String token,
3838
@Body SendMessagePara para)
3939
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2025 OpsMx, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.netflix.spinnaker.echo.bearychat;
18+
19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
20+
21+
import com.netflix.spinnaker.echo.api.Notification;
22+
import com.netflix.spinnaker.echo.notification.NotificationTemplateEngine;
23+
import java.util.List;
24+
import java.util.Map;
25+
import okhttp3.MediaType;
26+
import okhttp3.ResponseBody;
27+
import org.junit.jupiter.api.Test;
28+
import org.mockito.Mockito;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.boot.test.mock.mockito.MockBean;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import retrofit2.mock.Calls;
35+
36+
@SpringBootTest(
37+
properties = {"bearychat.enabled=true", "bearychat.token=abc"},
38+
classes = {
39+
BearychatNotificationService.class,
40+
NotificationTemplateEngine.class,
41+
freemarker.template.Configuration.class,
42+
BearychatNotificationServiceTest.TestConfig.class
43+
},
44+
webEnvironment = SpringBootTest.WebEnvironment.NONE)
45+
public class BearychatNotificationServiceTest {
46+
47+
@Autowired BearychatNotificationService bearychatNotificationService;
48+
49+
@Autowired BearychatService bearychatService;
50+
51+
@Test
52+
void testBearychatNotificationService() {
53+
BearychatUserInfo userInfo = new BearychatUserInfo();
54+
userInfo.setId("uid1");
55+
userInfo.setEmail("[email protected]");
56+
Mockito.when(bearychatService.getUserList("abc")).thenReturn(Calls.response(List.of(userInfo)));
57+
58+
CreateP2PChannelPara para = new CreateP2PChannelPara();
59+
para.setUser_id("uid1");
60+
CreateP2PChannelResponse channelResponse = new CreateP2PChannelResponse();
61+
channelResponse.setId("cid1");
62+
channelResponse.setVchannel_id("cid1");
63+
Mockito.when(
64+
bearychatService.createp2pchannel(
65+
Mockito.anyString(), Mockito.any(CreateP2PChannelPara.class)))
66+
.thenReturn(Calls.response(channelResponse));
67+
68+
Mockito.when(
69+
bearychatService.sendMessage(Mockito.anyString(), Mockito.any(SendMessagePara.class)))
70+
.thenReturn(Calls.response(ResponseBody.create("", MediaType.parse("application/json"))));
71+
72+
Notification notification = new Notification();
73+
notification.setNotificationType("bearychat");
74+
notification.setTo(List.of("[email protected]"));
75+
notification.setAdditionalContext(Map.of("body", "notification Body"));
76+
77+
assertDoesNotThrow(() -> bearychatNotificationService.handle(notification));
78+
}
79+
80+
@Configuration
81+
public static class TestConfig {
82+
@MockBean BearychatService bearychatService;
83+
84+
@Bean
85+
public BearychatService bearychatService() {
86+
return bearychatService;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)