4040import org .orcid .core .manager .v3 .OrcidSecurityManager ;
4141import org .orcid .core .manager .v3 .ProfileEntityManager ;
4242import org .orcid .core .manager .v3 .RecordNameManager ;
43- import org .orcid .core .manager .v3 .read_only .GivenPermissionToManagerReadOnly ;
44- import org .orcid .core .manager .v3 .read_only .ProfileEntityManagerReadOnly ;
45- import org .orcid .core .manager .v3 .read_only .RecordNameManagerReadOnly ;
43+ import org .orcid .core .manager .v3 .read_only .*;
4644import org .orcid .core .oauth .OrcidProfileUserDetails ;
4745import org .orcid .core .security .OrcidWebRole ;
46+ import org .orcid .jaxb .model .v3 .release .common .*;
4847import org .orcid .utils .DateUtils ;
4948import org .orcid .core .utils .v3 .OrcidIdentifierUtils ;
5049import org .orcid .frontend .email .RecordEmailSender ;
51- import org .orcid .jaxb .model .v3 .release .common .CreditName ;
52- import org .orcid .jaxb .model .v3 .release .common .OrcidIdentifier ;
53- import org .orcid .jaxb .model .v3 .release .common .Visibility ;
5450import org .orcid .jaxb .model .v3 .release .record .Biography ;
5551import org .orcid .jaxb .model .v3 .release .record .Email ;
5652import org .orcid .jaxb .model .v3 .release .record .Emails ;
@@ -84,6 +80,7 @@ public class ManageProfileControllerTest {
8480 private static final String USER_ORCID = "0000-0000-0000-0001" ;
8581 private static final String DEPRECATED_USER_ORCID = "0000-0000-0000-0002" ;
8682 private static final String DEPRECATED_USER_ORCID_URL = "https://localhost:8443/0000-0000-0000-0002" ;
83+ private static final String USER_CREDIT_NAME = "Credit Name" ;
8784
8885 @ Mock
8986 private ProfileEntityCacheManager mockProfileEntityCacheManager ;
@@ -94,6 +91,9 @@ public class ManageProfileControllerTest {
9491 @ Mock
9592 private EmailManager mockEmailManager ;
9693
94+ @ Mock
95+ private ProfileEmailDomainManagerReadOnly mockProfileEmailDomainManagerReadOnly ;
96+
9797 @ Mock
9898 private LocaleManager mockLocaleManager ;
9999
@@ -127,6 +127,21 @@ public class ManageProfileControllerTest {
127127 @ Mock (name ="profileEntityManagerReadOnlyV3" )
128128 private ProfileEntityManagerReadOnly mockProfileEntityManagerReadOnly ;
129129
130+ @ Mock
131+ private PersonalDetailsManagerReadOnly mockPersonalDetailsManagerReadOnly ;
132+
133+ @ Mock
134+ private AddressManagerReadOnly mockAddressManagerReadOnly ;
135+
136+ @ Mock
137+ private ProfileKeywordManagerReadOnly mockKeywordManagerReadOnly ;
138+
139+ @ Mock
140+ private ResearcherUrlManagerReadOnly mockResearcherUrlManagerReadOnly ;
141+
142+ @ Mock
143+ private ExternalIdentifierManagerReadOnly mockExternalIdentifierManagerReadOnly ;
144+
130145 @ Before
131146 public void initMocks () throws Exception {
132147 controller = new ManageProfileController ();
@@ -136,6 +151,7 @@ public void initMocks() throws Exception {
136151 TargetProxyHelper .injectIntoProxy (controller , "encryptionManager" , mockEncryptionManager );
137152 TargetProxyHelper .injectIntoProxy (controller , "emailManager" , mockEmailManager );
138153 TargetProxyHelper .injectIntoProxy (controller , "emailManagerReadOnly" , mockEmailManager );
154+ TargetProxyHelper .injectIntoProxy (controller , "profileEmailDomainManagerReadOnly" , mockProfileEmailDomainManagerReadOnly );
139155 TargetProxyHelper .injectIntoProxy (controller , "localeManager" , mockLocaleManager );
140156 TargetProxyHelper .injectIntoProxy (controller , "profileEntityManager" , mockProfileEntityManager );
141157 TargetProxyHelper .injectIntoProxy (controller , "givenPermissionToManager" , mockGivenPermissionToManager );
@@ -147,6 +163,12 @@ public void initMocks() throws Exception {
147163 TargetProxyHelper .injectIntoProxy (controller , "twoFactorAuthenticationManager" , twoFactorAuthenticationManager );
148164 TargetProxyHelper .injectIntoProxy (controller , "recordEmailSender" , mockRecordEmailSender );
149165 TargetProxyHelper .injectIntoProxy (controller , "profileEntityManagerReadOnly" , mockProfileEntityManagerReadOnly );
166+ TargetProxyHelper .injectIntoProxy (controller , "personalDetailsManagerReadOnly" , mockPersonalDetailsManagerReadOnly );
167+ TargetProxyHelper .injectIntoProxy (controller , "addressManagerReadOnly" , mockAddressManagerReadOnly );
168+ TargetProxyHelper .injectIntoProxy (controller , "keywordManagerReadOnly" , mockKeywordManagerReadOnly );
169+ TargetProxyHelper .injectIntoProxy (controller , "researcherUrlManagerReadOnly" , mockResearcherUrlManagerReadOnly );
170+ TargetProxyHelper .injectIntoProxy (controller , "externalIdentifierManagerReadOnly" , mockExternalIdentifierManagerReadOnly );
171+
150172
151173 when (mockOrcidSecurityManager .isPasswordConfirmationRequired ()).thenReturn (true );
152174 when (mockEncryptionManager .hashMatches (Mockito .anyString (), Mockito .anyString ())).thenReturn (true );
@@ -192,13 +214,23 @@ public Emails answer(InvocationOnMock invocation) throws Throwable {
192214 Emails emails = new Emails ();
193215 Email email1 = new Email ();
194216 email1 .setEmail (invocation .getArgument (0 ) + "_1@test.orcid.org" );
217+ email1 .setSource (new Source ());
195218 email1 .setVisibility (Visibility .PUBLIC );
196219 emails .getEmails ().add (email1 );
197220
198221 Email email2 = new Email ();
199222 email2 .setEmail (invocation .getArgument (0 ) + "_2@test.orcid.org" );
223+ email2 .setSource (new Source ());
224+ email2 .getSource ().setSourceName (new SourceName (USER_CREDIT_NAME ));
200225 email2 .setVisibility (Visibility .PUBLIC );
201226 emails .getEmails ().add (email2 );
227+
228+ Email email3 = new Email ();
229+ email3 .setEmail (invocation .getArgument (0 ) + "_3@test.orcid.org" );
230+ email3 .setSource (new Source ());
231+ email3 .getSource ().setSourceClientId (new SourceClientId (USER_ORCID ));
232+ email3 .setVisibility (Visibility .PUBLIC );
233+ emails .getEmails ().add (email3 );
202234 return emails ;
203235 }
204236
@@ -387,16 +419,18 @@ public void testValidateDeprecateProfileWithValidData() {
387419 assertNotNull (deprecateProfile .getDeprecatingEmails ());
388420 assertEquals ("0000-0000-0000-0002" , deprecateProfile .getDeprecatingOrcid ());
389421 assertEquals ("0000-0000-0000-0002 Given Names 0000-0000-0000-0002 Family Name" , deprecateProfile .getDeprecatingAccountName ());
390- assertEquals (2 , deprecateProfile .getDeprecatingEmails ().size ());
422+ assertEquals (3 , deprecateProfile .getDeprecatingEmails ().size ());
391423 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_1@test.orcid.org" ));
392424 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_2@test.orcid.org" ));
425+ assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_3@test.orcid.org" ));
393426
394427 assertEquals ("0000-0000-0000-0001" , deprecateProfile .getPrimaryOrcid ());
395428 assertEquals ("0000-0000-0000-0001 Given Names 0000-0000-0000-0001 Family Name" , deprecateProfile .getPrimaryAccountName ());
396429 assertNotNull (deprecateProfile .getPrimaryEmails ());
397- assertEquals (2 , deprecateProfile .getPrimaryEmails ().size ());
430+ assertEquals (3 , deprecateProfile .getPrimaryEmails ().size ());
398431 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_1@test.orcid.org" ));
399432 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_2@test.orcid.org" ));
433+ assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_3@test.orcid.org" ));
400434 assertTrue (deprecateProfile .getErrors ().isEmpty ());
401435
402436 // Using orcid
@@ -409,16 +443,19 @@ public void testValidateDeprecateProfileWithValidData() {
409443 assertNotNull (deprecateProfile .getDeprecatingEmails ());
410444 assertEquals ("0000-0000-0000-0002" , deprecateProfile .getDeprecatingOrcid ());
411445 assertEquals ("0000-0000-0000-0002 Given Names 0000-0000-0000-0002 Family Name" , deprecateProfile .getDeprecatingAccountName ());
412- assertEquals (2 , deprecateProfile .getDeprecatingEmails ().size ());
446+ assertEquals (3 , deprecateProfile .getDeprecatingEmails ().size ());
413447 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_1@test.orcid.org" ));
414448 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_2@test.orcid.org" ));
449+ assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_3@test.orcid.org" ));
450+
415451
416452 assertEquals ("0000-0000-0000-0001" , deprecateProfile .getPrimaryOrcid ());
417453 assertEquals ("0000-0000-0000-0001 Given Names 0000-0000-0000-0001 Family Name" , deprecateProfile .getPrimaryAccountName ());
418454 assertNotNull (deprecateProfile .getPrimaryEmails ());
419- assertEquals (2 , deprecateProfile .getPrimaryEmails ().size ());
455+ assertEquals (3 , deprecateProfile .getPrimaryEmails ().size ());
420456 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_1@test.orcid.org" ));
421457 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_2@test.orcid.org" ));
458+ assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_3@test.orcid.org" ));
422459 assertTrue (deprecateProfile .getErrors ().isEmpty ());
423460
424461 // Using orcid URL
@@ -431,16 +468,19 @@ public void testValidateDeprecateProfileWithValidData() {
431468 assertNotNull (deprecateProfile .getDeprecatingEmails ());
432469 assertEquals ("0000-0000-0000-0002" , deprecateProfile .getDeprecatingOrcid ());
433470 assertEquals ("0000-0000-0000-0002 Given Names 0000-0000-0000-0002 Family Name" , deprecateProfile .getDeprecatingAccountName ());
434- assertEquals (2 , deprecateProfile .getDeprecatingEmails ().size ());
471+ assertEquals (3 , deprecateProfile .getDeprecatingEmails ().size ());
435472 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_1@test.orcid.org" ));
436473 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_2@test.orcid.org" ));
474+ assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_3@test.orcid.org" ));
437475
438476 assertEquals ("0000-0000-0000-0001" , deprecateProfile .getPrimaryOrcid ());
439477 assertEquals ("0000-0000-0000-0001 Given Names 0000-0000-0000-0001 Family Name" , deprecateProfile .getPrimaryAccountName ());
440478 assertNotNull (deprecateProfile .getPrimaryEmails ());
441- assertEquals (2 , deprecateProfile .getPrimaryEmails ().size ());
479+ assertEquals (3 , deprecateProfile .getPrimaryEmails ().size ());
442480 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_1@test.orcid.org" ));
443481 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_2@test.orcid.org" ));
482+ assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_3@test.orcid.org" ));
483+
444484 assertTrue (deprecateProfile .getErrors ().isEmpty ());
445485
446486 // Using orcid trim space
@@ -454,16 +494,19 @@ public void testValidateDeprecateProfileWithValidData() {
454494 assertEquals ("0000-0000-0000-0002" , deprecateProfile .getDeprecatingOrcid ());
455495 assertEquals ("0000-0000-0000-0002 Given Names 0000-0000-0000-0002 Family Name" , deprecateProfile .getDeprecatingAccountName ());
456496
457- assertEquals (2 , deprecateProfile .getDeprecatingEmails ().size ());
497+ assertEquals (3 , deprecateProfile .getDeprecatingEmails ().size ());
458498 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_1@test.orcid.org" ));
459499 assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_2@test.orcid.org" ));
500+ assertTrue (deprecateProfile .getDeprecatingEmails ().contains ("0000-0000-0000-0002_3@test.orcid.org" ));
501+
460502
461503 assertEquals ("0000-0000-0000-0001" , deprecateProfile .getPrimaryOrcid ());
462504 assertEquals ("0000-0000-0000-0001 Given Names 0000-0000-0000-0001 Family Name" , deprecateProfile .getPrimaryAccountName ());
463505 assertNotNull (deprecateProfile .getPrimaryEmails ());
464- assertEquals (2 , deprecateProfile .getPrimaryEmails ().size ());
506+ assertEquals (3 , deprecateProfile .getPrimaryEmails ().size ());
465507 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_1@test.orcid.org" ));
466508 assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_2@test.orcid.org" ));
509+ assertTrue (deprecateProfile .getPrimaryEmails ().contains ("0000-0000-0000-0001_3@test.orcid.org" ));
467510 assertTrue (deprecateProfile .getErrors ().isEmpty ());
468511 }
469512
@@ -1116,7 +1159,68 @@ public void testEditEmail_primaryEmailChange() {
11161159
11171160 verify (mockRecordEmailSender , Mockito .times (1 )).sendVerificationEmail (eq (USER_ORCID ), eq ("email@orcid.org" ), eq (true ));
11181161 }
1119-
1162+
1163+ @ Test
1164+ public void testEmptyEmailSource () {
1165+ SecurityContextHolder .getContext ().setAuthentication (getAuthentication (USER_ORCID ));
1166+ when (mockProfileEmailDomainManagerReadOnly .getEmailDomains (eq (USER_ORCID ))).thenReturn (null );
1167+ when (mockEmailManager .getPublicEmails (eq (USER_ORCID ))).thenReturn (new Emails ());
1168+ MockHttpServletRequest mockRequest = new MockHttpServletRequest ();
1169+ MockHttpSession mockSession = new MockHttpSession ();
1170+ mockRequest .setSession (mockSession );
1171+ org .orcid .pojo .ajaxForm .Emails emails = controller .getEmails (mockRequest );
1172+
1173+ assertEquals (3 , emails .getEmails ().size ());
1174+
1175+ org .orcid .pojo .ajaxForm .Email email1 = emails .getEmails ().get (0 );
1176+ assertEquals (email1 .getValue (), USER_ORCID + "_1@test.orcid.org" );
1177+ assertEquals (email1 .getSource (), USER_ORCID );
1178+ assertNull (email1 .getSourceName ());
1179+
1180+ org .orcid .pojo .ajaxForm .Email email2 = emails .getEmails ().get (1 );
1181+ assertEquals (email2 .getValue (), USER_ORCID + "_2@test.orcid.org" );
1182+ assertNull (email2 .getSource ());
1183+ assertEquals (email2 .getSourceName (), USER_CREDIT_NAME );
1184+ }
1185+
1186+ @ Test
1187+ public void testEmailSourceWithSourceName () {
1188+ SecurityContextHolder .getContext ().setAuthentication (getAuthentication (USER_ORCID ));
1189+ when (mockProfileEmailDomainManagerReadOnly .getEmailDomains (eq (USER_ORCID ))).thenReturn (null );
1190+ when (mockEmailManager .getPublicEmails (eq (USER_ORCID ))).thenReturn (new Emails ());
1191+ MockHttpServletRequest mockRequest = new MockHttpServletRequest ();
1192+ MockHttpSession mockSession = new MockHttpSession ();
1193+ mockRequest .setSession (mockSession );
1194+ org .orcid .pojo .ajaxForm .Emails emails = controller .getEmails (mockRequest );
1195+
1196+ assertEquals (3 , emails .getEmails ().size ());
1197+
1198+ org .orcid .pojo .ajaxForm .Email email2 = emails .getEmails ().get (1 );
1199+ assertEquals (email2 .getValue (), USER_ORCID + "_2@test.orcid.org" );
1200+ assertNull (email2 .getSource ());
1201+ assertEquals (email2 .getSourceName (), USER_CREDIT_NAME );
1202+ }
1203+
1204+ @ Test
1205+ public void testEmailSourceWithSourceId () {
1206+ SecurityContextHolder .getContext ().setAuthentication (getAuthentication (USER_ORCID ));
1207+ when (mockProfileEmailDomainManagerReadOnly .getEmailDomains (eq (USER_ORCID ))).thenReturn (null );
1208+ when (mockEmailManager .getPublicEmails (eq (USER_ORCID ))).thenReturn (new Emails ());
1209+ MockHttpServletRequest mockRequest = new MockHttpServletRequest ();
1210+ MockHttpSession mockSession = new MockHttpSession ();
1211+ mockRequest .setSession (mockSession );
1212+ org .orcid .pojo .ajaxForm .Emails emails = controller .getEmails (mockRequest );
1213+
1214+ assertEquals (3 , emails .getEmails ().size ());
1215+
1216+ org .orcid .pojo .ajaxForm .Email email3 = emails .getEmails ().get (2 );
1217+ assertEquals (email3 .getValue (), USER_ORCID + "_3@test.orcid.org" );
1218+ assertNull (email3 .getSourceName ());
1219+ assertEquals (email3 .getSource (), USER_ORCID );
1220+ }
1221+
1222+
1223+
11201224 protected Authentication getAuthentication (String orcid ) {
11211225 List <OrcidWebRole > roles = Arrays .asList (OrcidWebRole .ROLE_USER );
11221226 OrcidProfileUserDetails details = new OrcidProfileUserDetails (orcid , "user_1@test.orcid.org" , null , roles );
0 commit comments