|
2 | 2 |
|
3 | 3 | import com.microsoft.applicationinsights.TelemetryClient; |
4 | 4 | import freemarker.template.Configuration; |
| 5 | +import freemarker.template.Template; |
| 6 | +import freemarker.template.TemplateException; |
5 | 7 | import io.quarkus.test.InjectMock; |
6 | 8 | import io.quarkus.test.junit.QuarkusTest; |
7 | 9 | import io.smallrye.mutiny.Uni; |
|
30 | 32 | import org.openapi.quarkus.user_registry_json.model.WorkContactResource; |
31 | 33 |
|
32 | 34 | import java.io.IOException; |
| 35 | +import java.io.Writer; |
33 | 36 | import java.util.HashMap; |
34 | 37 | import java.util.List; |
35 | 38 | import java.util.Map; |
@@ -351,6 +354,53 @@ void testSendCreateUserNotification() throws IOException { |
351 | 354 | verify(mailService, times(1)).sendMail(anyString(), anyString(), anyString()); |
352 | 355 | } |
353 | 356 |
|
| 357 | + @Test |
| 358 | + void testSendCreateUserNotification_whenLoggedUserIsFromApim_shouldUseDefaultName() throws IOException, TemplateException { |
| 359 | + // Arrange |
| 360 | + LoggedUser loggedUser = LoggedUser.builder() |
| 361 | + .name("apim") |
| 362 | + .build(); |
| 363 | + |
| 364 | + Configuration freemarkerConfig = mock(Configuration.class); |
| 365 | + CloudTemplateLoader cloudTemplateLoader = mock(CloudTemplateLoader.class); |
| 366 | + Template mockTemplate = mock(freemarker.template.Template.class); |
| 367 | + |
| 368 | + when(freemarkerConfig.getTemplate(anyString())).thenReturn(mockTemplate); |
| 369 | + when(freemarkerConfig.getTemplateLoader()).thenReturn(cloudTemplateLoader); |
| 370 | + |
| 371 | + ArgumentCaptor<Map<String, String>> dataModelCaptor = ArgumentCaptor.forClass(Map.class); |
| 372 | + doNothing().when(mockTemplate).process(dataModelCaptor.capture(), any(Writer.class)); |
| 373 | + |
| 374 | + UserNotificationServiceImpl userNotificationService = new UserNotificationServiceImpl( |
| 375 | + freemarkerConfig, cloudTemplateLoader, mailService, true, telemetryClient |
| 376 | + ); |
| 377 | + |
| 378 | + when(mailService.sendMail(anyString(), anyString(), anyString())) |
| 379 | + .thenReturn(Uni.createFrom().voidItem()); |
| 380 | + |
| 381 | + List<String> roleLabels = List.of("code2", "code3"); |
| 382 | + |
| 383 | + // Act |
| 384 | + userNotificationService.sendCreateUserNotification( |
| 385 | + userInstitution.getInstitutionDescription(), |
| 386 | + roleLabels, |
| 387 | + userResource, |
| 388 | + userInstitution, |
| 389 | + product, |
| 390 | + loggedUser |
| 391 | + ) |
| 392 | + .subscribe() |
| 393 | + .withSubscriber(UniAssertSubscriber.create()) |
| 394 | + .awaitItem() |
| 395 | + .assertCompleted(); |
| 396 | + |
| 397 | + // Assert |
| 398 | + verify(mockTemplate, times(1)).process(any(Map.class), any(Writer.class)); |
| 399 | + Map<String, String> capturedDataModel = dataModelCaptor.getValue(); |
| 400 | + assertEquals(UserNotificationServiceImpl.DEFAULT_NAME, capturedDataModel.get(UserNotificationServiceImpl.REQUESTER_NAME)); |
| 401 | + } |
| 402 | + |
| 403 | + |
354 | 404 | @Test |
355 | 405 | void testSendCreateUserNotificationWith2RoleLabel() throws IOException { |
356 | 406 | String loggedUserName = "loggedUserName"; |
|
0 commit comments