13
13
import io .smallrye .mutiny .Uni ;
14
14
import io .smallrye .mutiny .helpers .test .AssertSubscriber ;
15
15
import io .smallrye .mutiny .helpers .test .UniAssertSubscriber ;
16
+ import it .pagopa .selfcare .onboarding .common .PartyRole ;
16
17
import it .pagopa .selfcare .product .entity .Product ;
17
18
import it .pagopa .selfcare .product .service .ProductService ;
18
19
import it .pagopa .selfcare .user .controller .request .AddUserRoleDto ;
58
59
import java .util .*;
59
60
60
61
import static it .pagopa .selfcare .onboarding .common .PartyRole .MANAGER ;
62
+ import static it .pagopa .selfcare .onboarding .common .PartyRole .OPERATOR ;
61
63
import static it .pagopa .selfcare .user .constant .CustomError .*;
62
64
import static it .pagopa .selfcare .user .model .constants .EventsMetric .EVENTS_USER_INSTITUTION_SUCCESS ;
63
65
import static it .pagopa .selfcare .user .model .constants .EventsName .EVENT_USER_MS_NAME ;
64
66
import static it .pagopa .selfcare .user .model .constants .OnboardedProductState .ACTIVE ;
67
+ import static it .pagopa .selfcare .user .model .constants .OnboardedProductState .DELETED ;
65
68
import static it .pagopa .selfcare .user .service .UserServiceImpl .USERS_FIELD_LIST_WITHOUT_FISCAL_CODE ;
66
69
import static it .pagopa .selfcare .user .service .UserServiceImpl .USERS_WORKS_FIELD_LIST ;
67
70
import static org .junit .jupiter .api .Assertions .*;
@@ -141,6 +144,7 @@ private UserInstitution createUserInstitution(){
141
144
OnboardedProduct productTest = new OnboardedProduct ();
142
145
productTest .setProductId ("prod-test" );
143
146
productTest .setProductRole ("operator" );
147
+ productTest .setRole (PartyRole .OPERATOR );
144
148
productTest .setStatus (OnboardedProductState .DELETED );
145
149
List <OnboardedProduct > products = new ArrayList <>();
146
150
products .add (product );
@@ -807,7 +811,8 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() {
807
811
CreateUserDto .User user = new CreateUserDto .User ();
808
812
user .setFiscalCode ("fiscalCode" );
809
813
CreateUserDto .Product createUserProduct = new CreateUserDto .Product ();
810
- createUserProduct .setProductId ("prod-io" );
814
+ createUserProduct .setProductId ("test" );
815
+ createUserProduct .setRole (MANAGER .name ());
811
816
createUserProduct .setProductRoles (List .of ("admin2" ,"admin3" ));
812
817
createUserDto .setUser (user );
813
818
createUserDto .setProduct (createUserProduct );
@@ -847,6 +852,96 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() {
847
852
verify (userNotificationService ).sendCreateUserNotification (any (), any (), any (), any (), any (),any ());
848
853
}
849
854
855
+ @ Test
856
+ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role_oneAlreadyOnboarded () {
857
+ UserInstitution userInstitution = createUserInstitution ();
858
+ // Prepare test data
859
+ CreateUserDto createUserDto = new CreateUserDto ();
860
+ CreateUserDto .User user = new CreateUserDto .User ();
861
+ user .setFiscalCode ("fiscalCode" );
862
+ CreateUserDto .Product createUserProduct = new CreateUserDto .Product ();
863
+ createUserProduct .setProductId ("test" );
864
+ createUserProduct .setRole (MANAGER .name ());
865
+ createUserProduct .setProductRoles (List .of ("admin" ,"admin3" ));
866
+ createUserDto .setUser (user );
867
+ createUserDto .setProduct (createUserProduct );
868
+ LoggedUser loggedUser = LoggedUser .builder ().build ();
869
+
870
+ Product product = new Product ();
871
+ product .setDescription ("description" );
872
+
873
+ UserToNotify userToNotify = new UserToNotify ();
874
+ userToNotify .setUserId (userId .toString ());
875
+
876
+ UserNotificationToSend userNotificationToSend = new UserNotificationToSend ();
877
+ userNotificationToSend .setUser (userToNotify );
878
+
879
+ // Mock external dependencies
880
+ when (userRegistryApi .searchUsingPOST (any (), any ())).thenReturn (Uni .createFrom ().item (userResource ));
881
+ when (userInstitutionService .findByUserIdAndInstitutionId (any (), any ())).thenReturn (Uni .createFrom ().item (userInstitution ));
882
+ when (userRegistryApi .updateUsingPATCH (any (), any ())).thenReturn (Uni .createFrom ().item (Response .ok ().build ()));
883
+ when (userInstitutionService .persistOrUpdate (any ())).thenReturn (Uni .createFrom ().item (userInstitution ));
884
+ when (productService .getProduct (any ())).thenReturn (product );
885
+ when (userNotificationService .sendCreateUserNotification (any (), any (), any (), any (), any (),any ())).thenReturn (Uni .createFrom ().voidItem ());
886
+ when (userUtils .buildUsersNotificationResponse (any (), any ())).thenReturn (List .of (userNotificationToSend ));
887
+
888
+ // Call the method
889
+ UniAssertSubscriber <CreateOrUpdateUserByFiscalCodeResponse > subscriber = userService .createOrUpdateUserByFiscalCode (createUserDto , loggedUser )
890
+ .subscribe ().withSubscriber (UniAssertSubscriber .create ());
891
+
892
+ Assertions .assertEquals (3 , userInstitution .getProducts ().size ());
893
+ // Verify the result
894
+
895
+ CreateOrUpdateUserByFiscalCodeResponse response = subscriber .awaitItem ().getItem ();
896
+
897
+ assertEquals (userId .toString (),response .getUserId ());
898
+ verify (userRegistryApi ).updateUsingPATCH (any (), any ());
899
+ verify (userInstitutionService ).persistOrUpdate (any ());
900
+ verify (userInstitutionService ).findByUserIdAndInstitutionId (any (), any ());
901
+ verify (userNotificationService ).sendCreateUserNotification (any (), any (), any (), any (), any (),any ());
902
+ }
903
+
904
+ @ Test
905
+ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role_oneAlreadyOnboarded_withDifferentSelcRole () {
906
+ UserInstitution userInstitution = createUserInstitution ();
907
+ // Prepare test data
908
+ CreateUserDto createUserDto = new CreateUserDto ();
909
+ CreateUserDto .User user = new CreateUserDto .User ();
910
+ user .setFiscalCode ("fiscalCode" );
911
+ CreateUserDto .Product createUserProduct = new CreateUserDto .Product ();
912
+ createUserProduct .setProductId ("test" );
913
+ createUserProduct .setRole (PartyRole .OPERATOR .name ());
914
+ createUserProduct .setProductRoles (List .of ("admin" ,"admin3" ));
915
+ createUserDto .setUser (user );
916
+ createUserDto .setProduct (createUserProduct );
917
+ LoggedUser loggedUser = LoggedUser .builder ().build ();
918
+
919
+ Product product = new Product ();
920
+ product .setDescription ("description" );
921
+
922
+ UserToNotify userToNotify = new UserToNotify ();
923
+ userToNotify .setUserId (userId .toString ());
924
+
925
+ UserNotificationToSend userNotificationToSend = new UserNotificationToSend ();
926
+ userNotificationToSend .setUser (userToNotify );
927
+
928
+ // Mock external dependencies
929
+ when (userRegistryApi .searchUsingPOST (any (), any ())).thenReturn (Uni .createFrom ().item (userResource ));
930
+ when (userInstitutionService .findByUserIdAndInstitutionId (any (), any ())).thenReturn (Uni .createFrom ().item (userInstitution ));
931
+ when (userRegistryApi .updateUsingPATCH (any (), any ())).thenReturn (Uni .createFrom ().item (Response .ok ().build ()));
932
+ when (userInstitutionService .persistOrUpdate (any ())).thenReturn (Uni .createFrom ().item (userInstitution ));
933
+ when (productService .getProduct (any ())).thenReturn (product );
934
+ when (userNotificationService .sendCreateUserNotification (any (), any (), any (), any (), any (),any ())).thenReturn (Uni .createFrom ().voidItem ());
935
+ when (userUtils .buildUsersNotificationResponse (any (), any ())).thenReturn (List .of (userNotificationToSend ));
936
+
937
+ userService .createOrUpdateUserByFiscalCode (createUserDto , loggedUser )
938
+ .subscribe ().withSubscriber (UniAssertSubscriber .create ())
939
+ .assertFailedWith (InvalidRequestException .class , "User already has different role on Product test" );
940
+
941
+ Assertions .assertEquals (2 , userInstitution .getProducts ().size ());
942
+ }
943
+
944
+
850
945
@ Test
851
946
void testCreateOrUpdateUser_CreateUser_SuccessByFiscalCode () {
852
947
// Prepare test data
@@ -959,20 +1054,87 @@ void testCreateOrUpdateUser_UpdateUser_ProductAlreadyOnboarded() {
959
1054
AddUserRoleDto .Product addUserRoleProduct = new AddUserRoleDto .Product ();
960
1055
addUserRoleProduct .setProductId ("test" );
961
1056
addUserRoleProduct .setDelegationId ("delegationId" );
962
- addUserRoleDto . setProduct ( addUserRoleProduct );
1057
+ addUserRoleProduct . setRole ( MANAGER . name () );
963
1058
addUserRoleProduct .setProductRoles (List .of ("admin" ));
1059
+ addUserRoleDto .setProduct (addUserRoleProduct );
964
1060
LoggedUser loggedUser = LoggedUser .builder ().build ();
965
1061
966
1062
when (userRegistryApi .findByIdUsingGET (any (), eq ("userId" ))).thenReturn (Uni .createFrom ().item (userResource ));
967
1063
when (userInstitutionService .findByUserIdAndInstitutionId (userResource .getId ().toString (), addUserRoleDto .getInstitutionId ())).thenReturn (Uni .createFrom ().item (createUserInstitution ()));
968
1064
1065
+ userService .createOrUpdateUserByUserId (addUserRoleDto , "userId" , loggedUser )
1066
+ .subscribe ().withSubscriber (UniAssertSubscriber .create ())
1067
+ .assertFailedWith (InvalidRequestException .class , "User already has roles on Product test" );
1068
+ }
1069
+
1070
+ @ Test
1071
+ void testCreateOrUpdateUser_UpdateUser_ProductAlreadyOnboardedWithAnotherRole () {
1072
+ // Prepare test data
1073
+ AddUserRoleDto addUserRoleDto = new AddUserRoleDto ();
1074
+ addUserRoleDto .setInstitutionId ("institutionId" );
1075
+ AddUserRoleDto .Product addUserRoleProduct = new AddUserRoleDto .Product ();
1076
+ addUserRoleProduct .setProductId ("test" );
1077
+ addUserRoleProduct .setRole (OPERATOR .name ());
1078
+ addUserRoleProduct .setDelegationId ("delegationId" );
1079
+ addUserRoleDto .setProduct (addUserRoleProduct );
1080
+ addUserRoleProduct .setProductRoles (List .of ("operatore api" ));
1081
+ LoggedUser loggedUser = LoggedUser .builder ().build ();
1082
+
1083
+ when (userRegistryApi .findByIdUsingGET (any (), eq ("userId" ))).thenReturn (Uni .createFrom ().item (userResource ));
1084
+ when (userInstitutionService .findByUserIdAndInstitutionId (userResource .getId ().toString (), addUserRoleDto .getInstitutionId ())).thenReturn (Uni .createFrom ().item (createUserInstitution ()));
1085
+
1086
+
1087
+ userService .createOrUpdateUserByUserId (addUserRoleDto , "userId" , loggedUser )
1088
+ .subscribe ().withSubscriber (UniAssertSubscriber .create ())
1089
+ .assertFailedWith (InvalidRequestException .class , "User already has different role on Product test" );
1090
+
1091
+ }
1092
+
1093
+ @ Test
1094
+ void testCreateOrUpdateUser_UpdateUser_ProductAlreadyOnboardedWithAnotherProductRole () {
1095
+ // Prepare test data
1096
+ AddUserRoleDto addUserRoleDto = new AddUserRoleDto ();
1097
+ addUserRoleDto .setInstitutionId ("institutionId" );
1098
+ AddUserRoleDto .Product addUserRoleProduct = new AddUserRoleDto .Product ();
1099
+ addUserRoleProduct .setProductId ("test" );
1100
+ addUserRoleProduct .setRole (OPERATOR .name ());
1101
+ addUserRoleProduct .setDelegationId ("delegationId" );
1102
+ addUserRoleDto .setProduct (addUserRoleProduct );
1103
+ addUserRoleProduct .setProductRoles (List .of ("security" ));
1104
+ LoggedUser loggedUser = LoggedUser .builder ().build ();
1105
+ UserInstitution userInstitution = createUserInstitution ();
1106
+ userInstitution .getProducts ().get (0 ).setStatus (DELETED );
1107
+ userInstitution .getProducts ().get (1 ).setStatus (ACTIVE );
1108
+ userInstitution .getProducts ().get (1 ).setProductId ("test" );
1109
+
1110
+ UserToNotify userToNotify = new UserToNotify ();
1111
+ userToNotify .setUserId (userId .toString ());
1112
+
1113
+ UserNotificationToSend userNotificationToSend = new UserNotificationToSend ();
1114
+ userNotificationToSend .setUser (userToNotify );
1115
+
1116
+ Product product = new Product ();
1117
+ product .setDescription ("description" );
1118
+
1119
+ // Mock external dependencies
1120
+ when (userRegistryApi .findByIdUsingGET (any (), eq ("userId" ))).thenReturn (Uni .createFrom ().item (userResource ));
1121
+ when (userInstitutionService .findByUserIdAndInstitutionId (userResource .getId ().toString (), addUserRoleDto .getInstitutionId ())).thenReturn (Uni .createFrom ().item ((userInstitution )));
1122
+ when (userInstitutionService .persistOrUpdate (any ())).thenReturn (Uni .createFrom ().item (createUserInstitution ()));
1123
+ when (productService .getProduct (any ())).thenReturn (product );
1124
+ when (userNotificationService .sendCreateUserNotification (any (), any (), any (), any (), any (),any ())).thenReturn (Uni .createFrom ().voidItem ());
1125
+ when (userUtils .buildUsersNotificationResponse (any (), any ())).thenReturn (List .of (userNotificationToSend ));
1126
+ when (userNotificationService .sendKafkaNotification (any ())).thenReturn (Uni .createFrom ().item (userNotificationToSend ));
1127
+
969
1128
970
1129
// Call the method
971
1130
UniAssertSubscriber <String > subscriber = userService .createOrUpdateUserByUserId (addUserRoleDto , "userId" , loggedUser )
972
1131
.subscribe ().withSubscriber (UniAssertSubscriber .create ());
973
1132
974
1133
// Verify the result
975
- assertNull (subscriber .awaitItem ().getItem ());
1134
+ subscriber .awaitItem ().assertCompleted ();
1135
+ verify (userRegistryApi ).findByIdUsingGET (any (), eq ("userId" ));
1136
+ verify (userInstitutionService ).persistOrUpdate (any ());
1137
+
976
1138
}
977
1139
978
1140
@ Test
@@ -982,6 +1144,7 @@ void testCreateOrUpdateUser_UpdateUser_OneOfProductRoleAlreadyOnboarded() {
982
1144
addUserRoleDto .setInstitutionId ("institutionId" );
983
1145
AddUserRoleDto .Product addUserRoleProduct = new AddUserRoleDto .Product ();
984
1146
addUserRoleProduct .setProductId ("test" );
1147
+ addUserRoleProduct .setRole (MANAGER .name ());
985
1148
addUserRoleDto .setProduct (addUserRoleProduct );
986
1149
addUserRoleProduct .setProductRoles (List .of ("admin" , "admin2" ));
987
1150
LoggedUser loggedUser = LoggedUser .builder ().build ();
0 commit comments