|
| 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