Skip to content

Commit b064549

Browse files
[SELC-7814] feat: update requesterName in buildCreateEmailDataModel after call from APIM (#308)
1 parent df02350 commit b064549

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserNotificationServiceImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class UserNotificationServiceImpl implements UserNotificationService {
5050
public static final String INSTITUTION_NAME = "institutionName";
5151
public static final String NO_ROLE_FOUND = "no_role_found";
5252
public static final String PRODUCT_ROLE = "productRole";
53+
public static final String DEFAULT_NAME = "Operatore PagoPa";
54+
5355
@Inject
5456
@RestClient
5557
EventHubRestClient eventHubRestClient;
@@ -138,7 +140,13 @@ public Uni<Void> sendOtpNotification(String institutionalEmail, String name, Str
138140

139141
private Map<String, String> buildCreateEmailDataModel(LoggedUser loggedUser, Product product, String institutionDescription, List<String> productRoleCodes) {
140142
Map<String, String> dataModel = new HashMap<>();
141-
dataModel.put(REQUESTER_NAME, Optional.ofNullable(loggedUser.getName()).orElse(""));
143+
144+
if(Objects.equals(loggedUser.getName(), "apim")){
145+
dataModel.put(REQUESTER_NAME, DEFAULT_NAME);
146+
} else {
147+
dataModel.put(REQUESTER_NAME, Optional.ofNullable(loggedUser.getName()).orElse(""));
148+
}
149+
142150
dataModel.put(REQUESTER_SURNAME, Optional.ofNullable(loggedUser.getFamilyName()).orElse(""));
143151

144152
dataModel.put(PRODUCT_NAME, Optional.ofNullable(product.getTitle()).orElse(""));

apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserNotificationServiceImplTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.microsoft.applicationinsights.TelemetryClient;
44
import freemarker.template.Configuration;
5+
import freemarker.template.Template;
6+
import freemarker.template.TemplateException;
57
import io.quarkus.test.InjectMock;
68
import io.quarkus.test.junit.QuarkusTest;
79
import io.smallrye.mutiny.Uni;
@@ -30,6 +32,7 @@
3032
import org.openapi.quarkus.user_registry_json.model.WorkContactResource;
3133

3234
import java.io.IOException;
35+
import java.io.Writer;
3336
import java.util.HashMap;
3437
import java.util.List;
3538
import java.util.Map;
@@ -351,6 +354,53 @@ void testSendCreateUserNotification() throws IOException {
351354
verify(mailService, times(1)).sendMail(anyString(), anyString(), anyString());
352355
}
353356

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+
354404
@Test
355405
void testSendCreateUserNotificationWith2RoleLabel() throws IOException {
356406
String loggedUserName = "loggedUserName";

0 commit comments

Comments
 (0)