Skip to content

Commit

Permalink
fix(notifications): add Retrofit2SyncCall.execute to BearychatService…
Browse files Browse the repository at this point in the history
… in BearychatNotificationService (#1482) (#1483)

* test(notifications): add a test to demonstrate the missing Retrofit2SyncCall.execute calls in BearychatNotificationService

* fix(notifications): add Retrofit2SyncCall.execute to BearychatService in BearychatNotificationService

(cherry picked from commit 8a2b94a)

Co-authored-by: Kiran Godishala <[email protected]>
  • Loading branch information
mergify[bot] and kirangodishala authored Mar 3, 2025
1 parent 9d3a7dc commit 6762b0a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
1 change: 1 addition & 0 deletions echo-notifications/echo-notifications.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<BearychatUserInfo> userList = bearychatService.getUserList(token)
List<BearychatUserInfo> 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<BearychatUserInfo> userList,String targetEmail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ interface BearychatService {
@Body CreateP2PChannelPara para)

@POST("v1/message.create")
Call<Response<ResponseBody>> sendMessage(@Query("token") String token,
Call<ResponseBody> sendMessage(@Query("token") String token,
@Body SendMessagePara para)
}
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
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("[email protected]"));
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;
}
}
}

0 comments on commit 6762b0a

Please sign in to comment.