|
17 | 17 | import org.ays.auth.util.exception.AysUserAlreadyExistsByEmailAddressException;
|
18 | 18 | import org.ays.auth.util.exception.AysUserAlreadyExistsByPhoneNumberException;
|
19 | 19 | import org.ays.auth.util.exception.AysUserIsNotActiveOrPassiveException;
|
| 20 | +import org.ays.auth.util.exception.AysUserNotActiveException; |
20 | 21 | import org.ays.auth.util.exception.AysUserNotExistByIdException;
|
21 | 22 | import org.ays.auth.util.exception.AysUserNotPassiveException;
|
22 | 23 | import org.ays.common.model.AysPhoneNumber;
|
@@ -820,4 +821,157 @@ void givenValidId_whenUserNotMatchedWithInstitution_thenThrowAysUserNotExistById
|
820 | 821 | .save(Mockito.any(AysUser.class));
|
821 | 822 | }
|
822 | 823 |
|
| 824 | + |
| 825 | + @Test |
| 826 | + void givenValidId_whenUserIsActive_thenPassivateUser() { |
| 827 | + |
| 828 | + // Given |
| 829 | + String mockId = "21a0ab5a-c0e9-4789-9704-a6b5c02e2325"; |
| 830 | + |
| 831 | + // When |
| 832 | + Institution mockInstitution = new InstitutionBuilder() |
| 833 | + .withValidValues() |
| 834 | + .build(); |
| 835 | + Mockito.when(identity.getInstitutionId()) |
| 836 | + .thenReturn(mockInstitution.getId()); |
| 837 | + |
| 838 | + AysUser mockUser = new AysUserBuilder() |
| 839 | + .withValidValues() |
| 840 | + .withId(mockId) |
| 841 | + .withInstitution(mockInstitution) |
| 842 | + .withStatus(AysUserStatus.ACTIVE) |
| 843 | + .build(); |
| 844 | + |
| 845 | + Mockito.when(userReadPort.findById(Mockito.anyString())) |
| 846 | + .thenReturn(Optional.of(mockUser)); |
| 847 | + |
| 848 | + Mockito.when(userSavePort.save(Mockito.any(AysUser.class))) |
| 849 | + .thenReturn(Mockito.mock(AysUser.class)); |
| 850 | + |
| 851 | + // Then |
| 852 | + userUpdateService.passivate(mockId); |
| 853 | + |
| 854 | + // Verify |
| 855 | + Mockito.verify(identity, Mockito.times(1)) |
| 856 | + .getInstitutionId(); |
| 857 | + |
| 858 | + Mockito.verify(userReadPort, Mockito.times(1)) |
| 859 | + .findById(Mockito.anyString()); |
| 860 | + |
| 861 | + Mockito.verify(userSavePort, Mockito.times(1)) |
| 862 | + .save(Mockito.any(AysUser.class)); |
| 863 | + } |
| 864 | + |
| 865 | + @Test |
| 866 | + void givenValidUserId_whenUserNotFound_thenThrowAysUserNotExistByIdException(){ |
| 867 | + |
| 868 | + // Given |
| 869 | + String mockId = "9f1eb072-7830-4c43-9a32-d77b62ccddd3"; |
| 870 | + |
| 871 | + // When |
| 872 | + Mockito.when(userReadPort.findById(Mockito.anyString())) |
| 873 | + .thenReturn(Optional.empty()); |
| 874 | + |
| 875 | + // Then |
| 876 | + Assertions.assertThrows( |
| 877 | + AysUserNotExistByIdException.class, |
| 878 | + () -> userUpdateService.passivate(mockId) |
| 879 | + ); |
| 880 | + |
| 881 | + // Verify |
| 882 | + Mockito.verify(userReadPort, Mockito.times(1)) |
| 883 | + .findById(Mockito.anyString()); |
| 884 | + |
| 885 | + Mockito.verify(identity, Mockito.never()) |
| 886 | + .getInstitutionId(); |
| 887 | + |
| 888 | + Mockito.verify(userSavePort, Mockito.never()) |
| 889 | + .save(Mockito.any(AysUser.class)); |
| 890 | + } |
| 891 | + |
| 892 | + @Test |
| 893 | + void givenValidId_whenUserIsNotActive_thenThrowAysUserNotActiveException(){ |
| 894 | + |
| 895 | + // Given |
| 896 | + String mockId = "bf7cc8d4-eab7-487d-8564-19be0f439b4a"; |
| 897 | + |
| 898 | + // When |
| 899 | + Institution mockInstitution = new InstitutionBuilder() |
| 900 | + .withValidValues() |
| 901 | + .build(); |
| 902 | + Mockito.when(identity.getInstitutionId()) |
| 903 | + .thenReturn(mockInstitution.getId()); |
| 904 | + |
| 905 | + AysUser mockUser = new AysUserBuilder() |
| 906 | + .withValidValues() |
| 907 | + .withId(mockId) |
| 908 | + .withInstitution(mockInstitution) |
| 909 | + .withStatus(AysUserStatus.PASSIVE) // Not active |
| 910 | + .build(); |
| 911 | + |
| 912 | + Mockito.when(userReadPort.findById(Mockito.anyString())) |
| 913 | + .thenReturn(Optional.of(mockUser)); |
| 914 | + |
| 915 | + // Then |
| 916 | + Assertions.assertThrows( |
| 917 | + AysUserNotActiveException.class, |
| 918 | + () -> userUpdateService.passivate(mockId) |
| 919 | + ); |
| 920 | + |
| 921 | + // Verify |
| 922 | + Mockito.verify(identity, Mockito.times(1)) |
| 923 | + .getInstitutionId(); |
| 924 | + |
| 925 | + Mockito.verify(userReadPort, Mockito.times(1)) |
| 926 | + .findById(Mockito.anyString()); |
| 927 | + |
| 928 | + Mockito.verify(userSavePort, Mockito.never()) |
| 929 | + .save(Mockito.any(AysUser.class)); |
| 930 | + } |
| 931 | + |
| 932 | + @Test |
| 933 | + void givenValidUserId_whenInstitutionIdDoesNotMatch_thenThrowAysUserNotExistByIdException() { |
| 934 | + |
| 935 | + // Given |
| 936 | + String mockId = "a785c6a2-229f-4a73-8e3a-3ff49bd16a07"; |
| 937 | + |
| 938 | + // When |
| 939 | + Institution mockInstitution = new InstitutionBuilder() |
| 940 | + .withValidValues() |
| 941 | + .build(); |
| 942 | + |
| 943 | + Institution differentInstitution = new InstitutionBuilder() |
| 944 | + .withValidValues() |
| 945 | + .build(); |
| 946 | + |
| 947 | + AysUser mockUser = new AysUserBuilder() |
| 948 | + .withValidValues() |
| 949 | + .withId(mockId) |
| 950 | + .withInstitution(differentInstitution) |
| 951 | + .withStatus(AysUserStatus.ACTIVE) |
| 952 | + .build(); |
| 953 | + |
| 954 | + Mockito.when(identity.getInstitutionId()) |
| 955 | + .thenReturn(mockInstitution.getId()); |
| 956 | + |
| 957 | + Mockito.when(userReadPort.findById(mockId)) |
| 958 | + .thenReturn(Optional.of(mockUser)); |
| 959 | + |
| 960 | + // Then |
| 961 | + Assertions.assertThrows( |
| 962 | + AysUserNotExistByIdException.class, |
| 963 | + () -> userUpdateService.passivate(mockId) |
| 964 | + ); |
| 965 | + |
| 966 | + // Verify |
| 967 | + Mockito.verify(identity, Mockito.times(1)) |
| 968 | + .getInstitutionId(); |
| 969 | + |
| 970 | + Mockito.verify(userReadPort, Mockito.times(1)) |
| 971 | + .findById(mockId); |
| 972 | + |
| 973 | + Mockito.verify(userSavePort, Mockito.never()) |
| 974 | + .save(mockUser); |
| 975 | + } |
| 976 | + |
823 | 977 | }
|
0 commit comments