|
| 1 | +package org.restcomm.connect.core.service.profile; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | + |
| 6 | +import java.sql.SQLException; |
| 7 | + |
| 8 | +import javax.servlet.ServletContext; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | +import org.mockito.Mockito; |
| 12 | +import org.restcomm.connect.commons.dao.Sid; |
| 13 | +import org.restcomm.connect.core.service.api.ProfileService; |
| 14 | +import org.restcomm.connect.dao.AccountsDao; |
| 15 | +import org.restcomm.connect.dao.DaoManager; |
| 16 | +import org.restcomm.connect.dao.OrganizationsDao; |
| 17 | +import org.restcomm.connect.dao.ProfileAssociationsDao; |
| 18 | +import org.restcomm.connect.dao.ProfilesDao; |
| 19 | +import org.restcomm.connect.dao.entities.Account; |
| 20 | +import org.restcomm.connect.dao.entities.Organization; |
| 21 | +import org.restcomm.connect.dao.entities.Profile; |
| 22 | +import org.restcomm.connect.dao.entities.ProfileAssociation; |
| 23 | + |
| 24 | +public class ProfileServiceTest { |
| 25 | + |
| 26 | + /** |
| 27 | + * case 1: given: a profile is explicitly assigned to an account |
| 28 | + * calling retrieveExplicitlyAssociatedProfile by account sid will return that profile |
| 29 | + * @throws SQLException |
| 30 | + */ |
| 31 | + @Test |
| 32 | + public void retrieveExplicitlyAssociatedProfileCase1() throws SQLException { |
| 33 | + MockingService mocks = new MockingService(); |
| 34 | + Account account = returnValidAccount(mocks); |
| 35 | + Profile expectedProfile = returnProfile(mocks); |
| 36 | + returnProfileAssociation(new Sid(expectedProfile.getSid()), account.getSid(), mocks); |
| 37 | + Profile resultantProfile = mocks.profileService.retrieveExplicitlyAssociatedProfile(account.getSid()); |
| 38 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * case 2: given: a profile is explicitly assigned to an organization |
| 43 | + * calling retrieveExplicitlyAssociatedProfile by organization sid will return that profile |
| 44 | + * @throws SQLException |
| 45 | + */ |
| 46 | + @Test |
| 47 | + public void retrieveExplicitlyAssociatedProfileCase2() throws SQLException { |
| 48 | + MockingService mocks = new MockingService(); |
| 49 | + Organization organization = returnValidOrganization(mocks); |
| 50 | + Profile expectedProfile = returnProfile(mocks); |
| 51 | + returnProfileAssociation(new Sid(expectedProfile.getSid()), organization.getSid(), mocks); |
| 52 | + Profile resultantProfile = mocks.profileService.retrieveExplicitlyAssociatedProfile(organization.getSid()); |
| 53 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * case 3: given: a profile is not explicitly assigned to an account |
| 58 | + * calling retrieveExplicitlyAssociatedProfile by account sid will return NULL |
| 59 | + * @throws SQLException |
| 60 | + */ |
| 61 | + @Test |
| 62 | + public void retrieveExplicitlyAssociatedProfileCase3() throws SQLException { |
| 63 | + MockingService mocks = new MockingService(); |
| 64 | + Account account = returnValidAccount(mocks); |
| 65 | + Profile resultantProfile = mocks.profileService.retrieveExplicitlyAssociatedProfile(account.getSid()); |
| 66 | + assertNull(resultantProfile); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * case 4: given: a profile is not explicitly assigned to an organization |
| 71 | + * calling retrieveExplicitlyAssociatedProfile by organization sid will return NULL |
| 72 | + * @throws SQLException |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void retrieveExplicitlyAssociatedProfileCase4() throws SQLException { |
| 76 | + MockingService mocks = new MockingService(); |
| 77 | + Organization organization = returnValidOrganization(mocks); |
| 78 | + Profile resultantProfile = mocks.profileService.retrieveExplicitlyAssociatedProfile(organization.getSid()); |
| 79 | + assertNull(resultantProfile); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * case 1: given: a profile is explicitly assigned to an organization |
| 84 | + * calling retrieveEffectiveProfileByOrganizationSid by organization sid will return that profile |
| 85 | + * @throws SQLException |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void retrieveEffectiveProfileByOrganizationSidCase1() throws SQLException { |
| 89 | + MockingService mocks = new MockingService(); |
| 90 | + Organization organization = returnValidOrganization(mocks); |
| 91 | + Profile expectedProfile = returnProfile(mocks); |
| 92 | + returnProfileAssociation(new Sid(expectedProfile.getSid()), organization.getSid(), mocks); |
| 93 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByOrganizationSid(organization.getSid()); |
| 94 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * case 2: given: a profile is NOT explicitly assigned to an organization |
| 99 | + * calling retrieveEffectiveProfileByOrganizationSid by organization sid will return DEFAULT profile |
| 100 | + * @throws SQLException |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void retrieveEffectiveProfileByOrganizationSidCase2() throws SQLException { |
| 104 | + MockingService mocks = new MockingService(); |
| 105 | + Organization organization = returnValidOrganization(mocks); |
| 106 | + returnDefaultProfile(mocks); |
| 107 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByOrganizationSid(organization.getSid()); |
| 108 | + assertEquals(Profile.DEFAULT_PROFILE_SID, resultantProfile.getSid()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * case 1: given: a profile is explicitly assigned to an account |
| 113 | + * calling retrieveEffectiveProfileByAccountSid by account sid will return that profile |
| 114 | + * @throws SQLException |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void retrieveEffectiveProfileByAccountSidCase1() throws SQLException { |
| 118 | + MockingService mocks = new MockingService(); |
| 119 | + Account account = returnValidAccount(mocks); |
| 120 | + Profile expectedProfile = returnProfile(mocks); |
| 121 | + returnProfileAssociation(new Sid(expectedProfile.getSid()), account.getSid(), mocks); |
| 122 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByAccountSid(account.getSid()); |
| 123 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * case 2: given: a profile is NOT explicitly assigned to an account, but its associated to its parent |
| 128 | + * calling retrieveEffectiveProfileByAccountSid by account sid will return that profile |
| 129 | + * @throws SQLException |
| 130 | + */ |
| 131 | + @Test |
| 132 | + public void retrieveEffectiveProfileByAccountSidCase2() throws SQLException { |
| 133 | + MockingService mocks = new MockingService(); |
| 134 | + Profile expectedProfile = returnProfile(mocks); |
| 135 | + Account account = returnValidAccountWithOnlyParentAssignedProfile(new Sid(expectedProfile.getSid()), mocks); |
| 136 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByAccountSid(account.getSid()); |
| 137 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * case 3: given: a profile is NOT explicitly assigned to an account nor to its parent, but its associated to its grand parent |
| 142 | + * calling retrieveEffectiveProfileByAccountSid by account sid will return that profile |
| 143 | + * @throws SQLException |
| 144 | + */ |
| 145 | + @Test |
| 146 | + public void retrieveEffectiveProfileByAccountSidCase3() throws SQLException { |
| 147 | + MockingService mocks = new MockingService(); |
| 148 | + Profile expectedProfile = returnProfile(mocks); |
| 149 | + Account account = returnValidAccountWithOnlyGrandParentAssignedProfile(new Sid(expectedProfile.getSid()), mocks); |
| 150 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByAccountSid(account.getSid()); |
| 151 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * case 4: given: a profile is NOT explicitly assigned to an account nor to its parent or grand parent, but its associated to its organization |
| 156 | + * calling retrieveEffectiveProfileByAccountSid by account sid SHOULD return DEFAULT profile |
| 157 | + * @throws SQLException |
| 158 | + */ |
| 159 | + @Test |
| 160 | + public void retrieveEffectiveProfileByAccountSidCase4() throws SQLException { |
| 161 | + MockingService mocks = new MockingService(); |
| 162 | + Profile expectedProfile = returnProfile(mocks); |
| 163 | + Account account = returnValidAccountWitOrganizationAssignedProfile(new Sid(expectedProfile.getSid()), mocks); |
| 164 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByAccountSid(account.getSid()); |
| 165 | + assertEquals(expectedProfile.getSid(), resultantProfile.getSid()); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * case 4: given: a profile is NOT explicitly assigned to an account nor to its parent, grand parent or organization: |
| 170 | + * calling retrieveEffectiveProfileByAccountSid by account sid SHOULD return DEFAULT profile |
| 171 | + * @throws SQLException |
| 172 | + */ |
| 173 | + @Test |
| 174 | + public void retrieveEffectiveProfileByAccountSidCase5() throws SQLException { |
| 175 | + MockingService mocks = new MockingService(); |
| 176 | + Account account = returnValidAccount(mocks); |
| 177 | + returnDefaultProfile(mocks); |
| 178 | + Profile resultantProfile = mocks.profileService.retrieveEffectiveProfileByAccountSid(account.getSid()); |
| 179 | + assertEquals(Profile.DEFAULT_PROFILE_SID, resultantProfile.getSid()); |
| 180 | + } |
| 181 | + |
| 182 | + private Account returnValidAccount(MockingService mocks ) { |
| 183 | + Sid organizationSid = Sid.generate(Sid.Type.ORGANIZATION); |
| 184 | + |
| 185 | + Sid acctSidGrandParent = Sid.generate(Sid.Type.ACCOUNT); |
| 186 | + Account.Builder builderGParent = Account.builder(); |
| 187 | + builderGParent.setSid(acctSidGrandParent); |
| 188 | + builderGParent.setOrganizationSid(organizationSid); |
| 189 | + Account accountGrandParent = builderGParent.build(); |
| 190 | + |
| 191 | + Sid acctSidParent = Sid.generate(Sid.Type.ACCOUNT); |
| 192 | + Account.Builder builderParent = Account.builder(); |
| 193 | + builderParent.setSid(acctSidParent); |
| 194 | + builderParent.setOrganizationSid(organizationSid); |
| 195 | + builderParent.setParentSid(acctSidGrandParent); |
| 196 | + Account accountParent = builderParent.build(); |
| 197 | + |
| 198 | + Sid acctSid = Sid.generate(Sid.Type.ACCOUNT); |
| 199 | + Account.Builder builder = Account.builder(); |
| 200 | + builder.setSid(acctSid); |
| 201 | + builder.setOrganizationSid(organizationSid); |
| 202 | + builder.setParentSid(acctSidParent); |
| 203 | + Account account = builder.build(); |
| 204 | + |
| 205 | + when (mocks.mockedAccountsDao.getAccount(acctSidParent)).thenReturn(accountParent); |
| 206 | + when (mocks.mockedAccountsDao.getAccount(acctSidGrandParent)).thenReturn(accountGrandParent); |
| 207 | + when (mocks.mockedAccountsDao.getAccount(acctSid)).thenReturn(account); |
| 208 | + |
| 209 | + return account; |
| 210 | + } |
| 211 | + |
| 212 | + private Account returnValidAccountWitOrganizationAssignedProfile(Sid profileSid, MockingService mocks ) throws SQLException { |
| 213 | + Sid organizationSid = Sid.generate(Sid.Type.ORGANIZATION); |
| 214 | + Sid acctSidGrandParent = Sid.generate(Sid.Type.ACCOUNT); |
| 215 | + Account.Builder builderGParent = Account.builder(); |
| 216 | + builderGParent.setSid(acctSidGrandParent); |
| 217 | + builderGParent.setOrganizationSid(organizationSid); |
| 218 | + Account accountGrandParent = builderGParent.build(); |
| 219 | + |
| 220 | + Sid acctSidParent = Sid.generate(Sid.Type.ACCOUNT); |
| 221 | + Account.Builder builderParent = Account.builder(); |
| 222 | + builderParent.setSid(acctSidParent); |
| 223 | + builderParent.setOrganizationSid(organizationSid); |
| 224 | + builderParent.setParentSid(acctSidGrandParent); |
| 225 | + Account accountParent = builderParent.build(); |
| 226 | + |
| 227 | + Sid acctSid = Sid.generate(Sid.Type.ACCOUNT); |
| 228 | + Account.Builder builder = Account.builder(); |
| 229 | + builder.setSid(acctSid); |
| 230 | + builder.setOrganizationSid(organizationSid); |
| 231 | + builder.setParentSid(acctSidParent); |
| 232 | + Account account = builder.build(); |
| 233 | + |
| 234 | + when (mocks.mockedAccountsDao.getAccount(acctSidGrandParent)).thenReturn(accountGrandParent); |
| 235 | + when (mocks.mockedAccountsDao.getAccount(acctSidParent)).thenReturn(accountParent); |
| 236 | + when (mocks.mockedAccountsDao.getAccount(acctSid)).thenReturn(account); |
| 237 | + returnProfileAssociation(profileSid, organizationSid, mocks); |
| 238 | + |
| 239 | + return account; |
| 240 | + } |
| 241 | + |
| 242 | + private Account returnValidAccountWithOnlyGrandParentAssignedProfile(Sid profileSid, MockingService mocks ) throws SQLException { |
| 243 | + Sid organizationSid = Sid.generate(Sid.Type.ORGANIZATION); |
| 244 | + Sid acctSidGrandParent = Sid.generate(Sid.Type.ACCOUNT); |
| 245 | + Account.Builder builderGParent = Account.builder(); |
| 246 | + builderGParent.setSid(acctSidGrandParent); |
| 247 | + builderGParent.setOrganizationSid(organizationSid); |
| 248 | + Account accountGrandParent = builderGParent.build(); |
| 249 | + |
| 250 | + Sid acctSidParent = Sid.generate(Sid.Type.ACCOUNT); |
| 251 | + Account.Builder builderParent = Account.builder(); |
| 252 | + builderParent.setSid(acctSidParent); |
| 253 | + builderParent.setOrganizationSid(organizationSid); |
| 254 | + builderParent.setParentSid(acctSidGrandParent); |
| 255 | + Account accountParent = builderParent.build(); |
| 256 | + |
| 257 | + Sid acctSid = Sid.generate(Sid.Type.ACCOUNT); |
| 258 | + Account.Builder builder = Account.builder(); |
| 259 | + builder.setSid(acctSid); |
| 260 | + builder.setOrganizationSid(organizationSid); |
| 261 | + builder.setParentSid(acctSidParent); |
| 262 | + Account account = builder.build(); |
| 263 | + |
| 264 | + when (mocks.mockedAccountsDao.getAccount(acctSidParent)).thenReturn(accountParent); |
| 265 | + when (mocks.mockedAccountsDao.getAccount(acctSidGrandParent)).thenReturn(accountGrandParent); |
| 266 | + when (mocks.mockedAccountsDao.getAccount(acctSid)).thenReturn(account); |
| 267 | + returnProfileAssociation(profileSid, acctSidGrandParent, mocks); |
| 268 | + |
| 269 | + return account; |
| 270 | + } |
| 271 | + |
| 272 | + private Account returnValidAccountWithOnlyParentAssignedProfile(Sid profileSid, MockingService mocks ) throws SQLException { |
| 273 | + Sid organizationSid = Sid.generate(Sid.Type.ORGANIZATION); |
| 274 | + Sid acctSidParent = Sid.generate(Sid.Type.ACCOUNT); |
| 275 | + Account.Builder builderParent = Account.builder(); |
| 276 | + builderParent.setSid(acctSidParent); |
| 277 | + builderParent.setOrganizationSid(organizationSid); |
| 278 | + Account accountParent = builderParent.build(); |
| 279 | + |
| 280 | + Sid acctSid = Sid.generate(Sid.Type.ACCOUNT); |
| 281 | + Account.Builder builder = Account.builder(); |
| 282 | + builder.setSid(acctSid); |
| 283 | + builder.setOrganizationSid(organizationSid); |
| 284 | + builder.setParentSid(acctSidParent); |
| 285 | + Account account = builder.build(); |
| 286 | + |
| 287 | + when (mocks.mockedAccountsDao.getAccount(acctSidParent)).thenReturn(accountParent); |
| 288 | + when (mocks.mockedAccountsDao.getAccount(acctSid)).thenReturn(account); |
| 289 | + returnProfileAssociation(profileSid, acctSidParent, mocks); |
| 290 | + |
| 291 | + return account; |
| 292 | + } |
| 293 | + |
| 294 | + private Organization returnValidOrganization(MockingService mocks ) { |
| 295 | + Sid organizationSid = Sid.generate(Sid.Type.ORGANIZATION); |
| 296 | + Organization.Builder builder = Organization.builder(); |
| 297 | + builder.setSid(organizationSid); |
| 298 | + builder.setDomainName("test"); |
| 299 | + Organization organization = builder.build(); |
| 300 | + return organization; |
| 301 | + } |
| 302 | + |
| 303 | + private ProfileAssociation returnProfileAssociation(Sid profileSid, Sid targetSid, MockingService mocks ) throws SQLException { |
| 304 | + ProfileAssociation assoc = new ProfileAssociation(profileSid, targetSid, null, null); |
| 305 | + when (mocks.mockedProfileAssociationsDao.getProfileAssociationByTargetSid(targetSid.toString())). |
| 306 | + thenReturn(assoc); |
| 307 | + return assoc; |
| 308 | + } |
| 309 | + |
| 310 | + private Profile returnDefaultProfile(MockingService mocks ) throws SQLException { |
| 311 | + Profile profile = new Profile(Profile.DEFAULT_PROFILE_SID, "{}", null, null); |
| 312 | + when (mocks.mockedProfilesDao.getProfile(profile.getSid())). |
| 313 | + thenReturn(profile); |
| 314 | + return profile; |
| 315 | + } |
| 316 | + |
| 317 | + private Profile returnProfile(MockingService mocks ) throws SQLException { |
| 318 | + Profile profile = new Profile(Sid.generate(Sid.Type.PROFILE).toString(), "{}", null, null); |
| 319 | + when (mocks.mockedProfilesDao.getProfile(profile.getSid())).thenReturn(profile); |
| 320 | + return profile; |
| 321 | + |
| 322 | + } |
| 323 | + |
| 324 | + class MockingService { |
| 325 | + |
| 326 | + ServletContext mockedServletContext = Mockito.mock(ServletContext.class); |
| 327 | + DaoManager mockedDaoManager = Mockito.mock(DaoManager.class); |
| 328 | + OrganizationsDao mockedOrganizationsDao = Mockito.mock(OrganizationsDao.class); |
| 329 | + AccountsDao mockedAccountsDao = Mockito.mock(AccountsDao.class); |
| 330 | + ProfilesDao mockedProfilesDao = Mockito.mock(ProfilesDao.class); |
| 331 | + ProfileAssociationsDao mockedProfileAssociationsDao = Mockito.mock(ProfileAssociationsDao.class); |
| 332 | + ProfileService profileService; |
| 333 | + |
| 334 | + public MockingService() { |
| 335 | + when(mockedServletContext.getAttribute(DaoManager.class.getName())). |
| 336 | + thenReturn(mockedDaoManager); |
| 337 | + when(mockedDaoManager.getProfileAssociationsDao()).thenReturn(mockedProfileAssociationsDao); |
| 338 | + when(mockedDaoManager.getProfilesDao()).thenReturn(mockedProfilesDao); |
| 339 | + when(mockedDaoManager.getAccountsDao()).thenReturn(mockedAccountsDao); |
| 340 | + when(mockedDaoManager.getAccountsDao()).thenReturn(mockedAccountsDao); |
| 341 | + profileService = new ProfileServiceImpl(mockedDaoManager); |
| 342 | + } |
| 343 | + } |
| 344 | +} |
0 commit comments