|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This code is licensed under the MIT License. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files(the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions : |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +package com.microsoft.identity.common.internal.cache |
| 24 | + |
| 25 | +import com.microsoft.identity.common.components.InMemoryStorageSupplier |
| 26 | +import org.junit.Assert |
| 27 | +import org.junit.Test |
| 28 | + |
| 29 | +class WebAppsAccountIdRegistryTest { |
| 30 | + private val accountId1 = "11111111-1111-1111-1111-111111111111.aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" |
| 31 | + private val accountId2 = "22222222-2222-2222-2222-222222222222.bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb" |
| 32 | + private val accountId3 = "33333333-3333-3333-3333-333333333333.cccccccc-cccc-cccc-cccc-cccccccccccc" |
| 33 | + |
| 34 | + private val clientId1 = "99999999-1111-4444-8888-121212121212" |
| 35 | + private val clientId2 = "aaaaaaaa-2222-5555-9999-131313131313" |
| 36 | + private val clientId3 = "bbbbbbbb-3333-6666-aaaa-141414141414" |
| 37 | + |
| 38 | + @Test |
| 39 | + fun testAddClient_veryBasicTest() { |
| 40 | + val registry = createRegistry() |
| 41 | + registry.addClient(accountId1, clientId1) |
| 42 | + Assert.assertEquals(1, registry.getClients(accountId1).size) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun testRemoveClient_veryBasicTest() { |
| 47 | + val registry = createRegistry() |
| 48 | + registry.addClient(accountId1, clientId1) |
| 49 | + registry.addClient(accountId1, clientId2) |
| 50 | + Assert.assertEquals(2, registry.getClients(accountId1).size) |
| 51 | + registry.removeClient(accountId1, clientId1) |
| 52 | + Assert.assertEquals(1, registry.getClients(accountId1).size) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun testRemoveClient_accountEntryCleanedUp() { |
| 57 | + val registry = createRegistry() |
| 58 | + registry.addClient(accountId1, clientId1) |
| 59 | + registry.removeClient(accountId1, clientId1) |
| 60 | + Assert.assertEquals(0, registry.getClients(accountId1).size) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun testManyCombinedAddAndRemove() { |
| 65 | + val registry = createRegistry() |
| 66 | + registry.addClient(accountId1, clientId1) |
| 67 | + registry.addClient(accountId1, clientId2) |
| 68 | + registry.addClient(accountId2, clientId1) |
| 69 | + registry.addClient(accountId2, clientId3) |
| 70 | + registry.addClient(accountId3, clientId1) |
| 71 | + Assert.assertEquals(2, registry.getClients(accountId1).size) |
| 72 | + Assert.assertEquals(2, registry.getClients(accountId2).size) |
| 73 | + Assert.assertEquals(1, registry.getClients(accountId3).size) |
| 74 | + |
| 75 | + registry.removeClient(accountId1, clientId1) |
| 76 | + Assert.assertEquals(1, registry.getClients(accountId1).size) |
| 77 | + |
| 78 | + registry.removeClient(accountId1, clientId2) |
| 79 | + Assert.assertEquals(0, registry.getClients(accountId1).size) |
| 80 | + |
| 81 | + registry.removeClient(accountId2, clientId3) |
| 82 | + Assert.assertEquals(1, registry.getClients(accountId2).size) |
| 83 | + |
| 84 | + registry.removeClient(accountId2, clientId1) |
| 85 | + Assert.assertEquals(0, registry.getClients(accountId2).size) |
| 86 | + |
| 87 | + registry.removeClient(accountId3, clientId1) |
| 88 | + Assert.assertEquals(0, registry.getClients(accountId3).size) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun testAddClient_addSameClient() { |
| 93 | + val registry = createRegistry() |
| 94 | + registry.addClient(accountId1, clientId1) |
| 95 | + registry.addClient(accountId1, clientId1) |
| 96 | + Assert.assertEquals(1, registry.getClients(accountId1).size) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun testRemoveAccount_removeAccount() { |
| 101 | + val registry = createRegistry() |
| 102 | + registry.addClient(accountId1, clientId1) |
| 103 | + registry.addClient(accountId1, clientId2) |
| 104 | + registry.addClient(accountId2, clientId1) |
| 105 | + registry.removeAccount(accountId1) |
| 106 | + Assert.assertEquals(0, registry.getClients(accountId1).size) |
| 107 | + Assert.assertEquals(1, registry.getClients(accountId2).size) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun testContains_containsClientId() { |
| 112 | + val registry = createRegistry() |
| 113 | + registry.addClient(accountId1, clientId1) |
| 114 | + Assert.assertTrue(registry.contains(accountId1, clientId1)) |
| 115 | + Assert.assertFalse(registry.contains(accountId1, clientId2)) |
| 116 | + Assert.assertFalse(registry.contains(accountId2, clientId1)) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun testPersistenceAcrossInstances() { |
| 121 | + val storageSupplier = InMemoryStorageSupplier() |
| 122 | + val registry1 = WebAppsAccountIdRegistry.create(storageSupplier) |
| 123 | + registry1.addClient(accountId1, clientId1) |
| 124 | + registry1.addClient(accountId1, clientId2) |
| 125 | + registry1.addClient(accountId2, clientId1) |
| 126 | + |
| 127 | + val registry2 = WebAppsAccountIdRegistry.create(storageSupplier) |
| 128 | + Assert.assertEquals(2, registry2.getClients(accountId1).size) |
| 129 | + Assert.assertEquals(1, registry2.getClients(accountId2).size) |
| 130 | + |
| 131 | + registry2.removeClient(accountId1, clientId1) |
| 132 | + Assert.assertEquals(1, registry2.getClients(accountId1).size) |
| 133 | + |
| 134 | + val registry3 = WebAppsAccountIdRegistry.create(storageSupplier) |
| 135 | + Assert.assertEquals(1, registry3.getClients(accountId1).size) |
| 136 | + Assert.assertEquals(1, registry3.getClients(accountId2).size) |
| 137 | + |
| 138 | + registry3.removeAccount(accountId2) |
| 139 | + Assert.assertEquals(0, registry3.getClients(accountId2).size) |
| 140 | + } |
| 141 | + |
| 142 | + private fun createRegistry(): WebAppsAccountIdRegistry { |
| 143 | + return WebAppsAccountIdRegistry.create(InMemoryStorageSupplier()) |
| 144 | + } |
| 145 | +} |
0 commit comments