|
| 1 | +/* |
| 2 | + * Copyright 2018-Present Entando Inc. (http://www.entando.com) All rights reserved. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU Lesser General Public License as published by the Free |
| 6 | + * Software Foundation; either version 2.1 of the License, or (at your option) |
| 7 | + * any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 | + * details. |
| 13 | + */ |
| 14 | +package org.entando.entando.aps.system.services.userprofile; |
| 15 | + |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +import com.agiletec.aps.system.SystemConstants; |
| 19 | +import com.agiletec.aps.system.common.entity.model.attribute.MonoTextAttribute; |
| 20 | +import com.agiletec.aps.system.common.entity.parse.attribute.MonoTextAttributeHandler; |
| 21 | +import com.agiletec.aps.system.services.user.User; |
| 22 | +import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager; |
| 23 | +import org.entando.entando.aps.system.services.userprofile.model.IUserProfile; |
| 24 | +import org.entando.entando.aps.system.services.userprofile.model.UserProfile; |
| 25 | +import org.entando.entando.ent.exception.EntException; |
| 26 | +import org.junit.jupiter.api.Assertions; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.mockito.InjectMocks; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.Mockito; |
| 32 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 33 | + |
| 34 | +@ExtendWith(MockitoExtension.class) |
| 35 | +class UserManagementAspectTest { |
| 36 | + |
| 37 | + @InjectMocks |
| 38 | + private UserManagementAspect userManagementAspect; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private UserProfileManager userProfileManager; |
| 42 | + @Mock |
| 43 | + private IAvatarService avatarService; |
| 44 | + @Mock |
| 45 | + private IUserPreferencesManager userPreferencesManager; |
| 46 | + |
| 47 | + @Test |
| 48 | + void testInjectProfile() throws EntException { |
| 49 | + IUserProfile returned = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE); |
| 50 | + when(userProfileManager.getProfile(Mockito.anyString())).thenReturn(returned); |
| 51 | + |
| 52 | + User user = new User(); |
| 53 | + user.setUsername("test"); |
| 54 | + Assertions.assertNull(user.getProfile()); |
| 55 | + |
| 56 | + userManagementAspect.injectProfile(user); |
| 57 | + Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test"); |
| 58 | + IUserProfile profile = (IUserProfile) user.getProfile(); |
| 59 | + Assertions.assertNotNull(profile); |
| 60 | + Assertions.assertEquals("test", profile.getUsername()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testInjectProfileWithError() throws EntException { |
| 65 | + when(userProfileManager.getProfile(Mockito.anyString())).thenThrow(EntException.class); |
| 66 | + |
| 67 | + User user = new User(); |
| 68 | + user.setUsername("test"); |
| 69 | + Assertions.assertNull(user.getProfile()); |
| 70 | + |
| 71 | + userManagementAspect.injectProfile(user); |
| 72 | + Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test"); |
| 73 | + Assertions.assertNull(user.getProfile()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testInjectProfileWithNullUser() throws EntException { |
| 78 | + userManagementAspect.injectProfile(null); |
| 79 | + Mockito.verifyNoInteractions(userProfileManager); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testMissingInjectProfile() throws EntException { |
| 84 | + IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE); |
| 85 | + User user = new User(); |
| 86 | + user.setUsername("test"); |
| 87 | + user.setProfile(profile); |
| 88 | + |
| 89 | + userManagementAspect.injectProfile(user); |
| 90 | + Mockito.verify(userProfileManager, Mockito.times(0)).getProfile("test"); |
| 91 | + Assertions.assertNotNull(user.getProfile()); |
| 92 | + Assertions.assertSame(profile, user.getProfile()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testAddProfile() throws EntException { |
| 97 | + IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE); |
| 98 | + User user = new User(); |
| 99 | + user.setUsername("test"); |
| 100 | + user.setProfile(profile); |
| 101 | + userManagementAspect.addProfile(user); |
| 102 | + Mockito.verify(userProfileManager, Mockito.times(1)).addProfile("test", profile); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testAddProfileWithNullUser() throws EntException { |
| 107 | + userManagementAspect.addProfile(null); |
| 108 | + Mockito.verifyNoInteractions(userProfileManager); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testInvokeAddProfileWithUserWithNullProfile() throws EntException { |
| 113 | + User user = new User(); |
| 114 | + user.setUsername("test"); |
| 115 | + userManagementAspect.addProfile(user); |
| 116 | + Mockito.verifyNoInteractions(userProfileManager); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testInvokeAddProfileWithError() { |
| 121 | + try { |
| 122 | + Mockito.doThrow(EntException.class).when(userProfileManager).addProfile(Mockito.anyString(), Mockito.any()); |
| 123 | + User user = new User(); |
| 124 | + user.setUsername("test"); |
| 125 | + user.setProfile(Mockito.mock(IUserProfile.class)); |
| 126 | + userManagementAspect.addProfile(user); |
| 127 | + Mockito.verify(userProfileManager, Mockito.times(1)).addProfile(Mockito.anyString(), Mockito.any(IUserProfile.class)); |
| 128 | + } catch (Exception e) { |
| 129 | + Assertions.fail(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void testUpdateProfile() throws EntException { |
| 135 | + IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE); |
| 136 | + User user = new User(); |
| 137 | + user.setUsername("test"); |
| 138 | + user.setProfile(profile); |
| 139 | + userManagementAspect.updateProfile(user); |
| 140 | + Mockito.verify(userProfileManager, Mockito.times(1)).updateProfile("test", profile); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testUpdateProfileWithNullUSer() throws EntException { |
| 145 | + userManagementAspect.updateProfile(null); |
| 146 | + Mockito.verifyNoInteractions(userProfileManager); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testInvokeUpdateProfileWithUserWithNullProfile() throws EntException { |
| 151 | + User user = new User(); |
| 152 | + user.setUsername("test"); |
| 153 | + userManagementAspect.updateProfile(user); |
| 154 | + Mockito.verifyNoInteractions(userProfileManager); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testInvokeUpdateProfileWithError() { |
| 159 | + try { |
| 160 | + Mockito.doThrow(EntException.class).when(userProfileManager).updateProfile(Mockito.anyString(), Mockito.any()); |
| 161 | + User user = new User(); |
| 162 | + user.setUsername("test"); |
| 163 | + user.setProfile(Mockito.mock(IUserProfile.class)); |
| 164 | + userManagementAspect.updateProfile(user); |
| 165 | + Mockito.verify(userProfileManager, Mockito.times(1)).updateProfile(Mockito.anyString(), Mockito.any(IUserProfile.class)); |
| 166 | + } catch (Exception e) { |
| 167 | + Assertions.fail(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void testDeleteUserDataFromUser() throws EntException { |
| 173 | + User user = new User(); |
| 174 | + user.setUsername("test"); |
| 175 | + userManagementAspect.deleteUserData(user); |
| 176 | + Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("test"); |
| 177 | + Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("test"); |
| 178 | + Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("test"); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void testDeleteUserDataFromUsername() throws EntException { |
| 183 | + userManagementAspect.deleteUserData("test"); |
| 184 | + Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("test"); |
| 185 | + Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("test"); |
| 186 | + Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("test"); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + void testDeleteUserDataWithNullUsername() throws EntException { |
| 191 | + userManagementAspect.deleteUserData(null); |
| 192 | + Mockito.verifyNoInteractions(userProfileManager); |
| 193 | + Mockito.verifyNoInteractions(userPreferencesManager); |
| 194 | + Mockito.verifyNoInteractions(avatarService); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + void testDeleteUserDataWithErrorOnUserProfileManager() { |
| 199 | + try { |
| 200 | + Mockito.doThrow(EntException.class).when(userProfileManager).deleteProfile(Mockito.anyString()); |
| 201 | + userManagementAspect.deleteUserData("username_error1"); |
| 202 | + Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error1"); |
| 203 | + Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error1"); |
| 204 | + Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error1"); |
| 205 | + } catch (Exception e) { |
| 206 | + Assertions.fail(); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + void testDeleteUserDataWithErrorOnUserAvatarService() { |
| 212 | + try { |
| 213 | + Mockito.doThrow(EntException.class).when(avatarService).deleteAvatar(Mockito.anyString()); |
| 214 | + userManagementAspect.deleteUserData("username_error2"); |
| 215 | + Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error2"); |
| 216 | + Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error2"); |
| 217 | + Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error2"); |
| 218 | + } catch (Exception e) { |
| 219 | + Assertions.fail(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + void testDeleteUserDataWithErrorOnUserPreference() { |
| 225 | + try { |
| 226 | + Mockito.doThrow(EntException.class).when(userPreferencesManager).deleteUserPreferences(Mockito.anyString()); |
| 227 | + userManagementAspect.deleteUserData("username_error3"); |
| 228 | + Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error3"); |
| 229 | + Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error3"); |
| 230 | + Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error3"); |
| 231 | + } catch (Exception e) { |
| 232 | + Assertions.fail(); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private IUserProfile createFakeProfile(String username, String defaultProfileTypeCode) { |
| 237 | + UserProfile userProfile = new UserProfile(); |
| 238 | + userProfile.setId(username); |
| 239 | + MonoTextAttribute monoTextAttribute = new MonoTextAttribute(); |
| 240 | + monoTextAttribute.setName("Name"); |
| 241 | + monoTextAttribute.setHandler(new MonoTextAttributeHandler()); |
| 242 | + userProfile.addAttribute(monoTextAttribute); |
| 243 | + userProfile.setTypeCode(defaultProfileTypeCode); |
| 244 | + return userProfile; |
| 245 | + } |
| 246 | + |
| 247 | +} |
0 commit comments