|
| 1 | +package com.skyflow; |
| 2 | + |
| 3 | +import com.skyflow.config.VaultConfig; |
| 4 | +import com.skyflow.enums.Env; |
| 5 | +import com.skyflow.errors.SkyflowException; |
| 6 | +import com.skyflow.vault.controller.VaultController; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * The four client lifecycle scenarios, mirrored by the ClientOperationsExample sample. |
| 12 | + * |
| 13 | + * <p>Each scenario runs twice: once through {@code SkyflowClientBuilder} and once through the built |
| 14 | + * {@code Skyflow} client. The two go down different code paths — BaseSkyflow.updateVaultConfig calls |
| 15 | + * the template directly and skips the builder's own override — and a bug that dropped the |
| 16 | + * flowvault-specific fields on the client path only was found exactly this way. |
| 17 | + * |
| 18 | + * <p>Unlike the sample, these run inside the com.skyflow package, so they can assert against the |
| 19 | + * controller's own config rather than only the stored copy. |
| 20 | + */ |
| 21 | +public class ClientLifecycleScenarioTests { |
| 22 | + |
| 23 | + private static final String VAULT_ID = "vault1"; |
| 24 | + private static final String NOT_IN_CONFIG_LIST = "VaultId is missing from the config"; |
| 25 | + |
| 26 | + private static VaultConfig config(String clusterId) { |
| 27 | + VaultConfig config = new VaultConfig(); |
| 28 | + config.setVaultId(VAULT_ID); |
| 29 | + config.setClusterId(clusterId); |
| 30 | + config.setEnv(Env.DEV); |
| 31 | + return config; |
| 32 | + } |
| 33 | + |
| 34 | + /** An update carrying a new cluster, env and timeout. clusterId is resent because the incoming |
| 35 | + * config is validated on its own before being merged. */ |
| 36 | + private static VaultConfig update(String clusterId, Env env, Integer timeout) { |
| 37 | + VaultConfig update = config(clusterId); |
| 38 | + update.setEnv(env); |
| 39 | + update.setTimeout(timeout); |
| 40 | + return update; |
| 41 | + } |
| 42 | + |
| 43 | + // ── A: add -> update -> delete -> vault() must fail ─────────────────────── |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testScenarioA_viaBuilder_addUpdateDeleteThenVaultFails() throws SkyflowException { |
| 47 | + Skyflow.SkyflowClientBuilder builder = Skyflow.builder().addVaultConfig(config("cluster1")); |
| 48 | + |
| 49 | + // add |
| 50 | + Assert.assertEquals("cluster1", builder.build().getVaultConfig(VAULT_ID).getClusterId()); |
| 51 | + // update - no error |
| 52 | + builder.updateVaultConfig(update("cluster2", Env.PROD, 30)); |
| 53 | + Assert.assertEquals("cluster2", builder.build().getVaultConfig(VAULT_ID).getClusterId()); |
| 54 | + Assert.assertEquals(Env.PROD, builder.build().getVaultConfig(VAULT_ID).getEnv()); |
| 55 | + Assert.assertEquals(Integer.valueOf(30), builder.build().getVaultConfig(VAULT_ID).getTimeout()); |
| 56 | + // delete |
| 57 | + builder.removeVaultConfig(VAULT_ID); |
| 58 | + Assert.assertNull(builder.build().getVaultConfig(VAULT_ID)); |
| 59 | + |
| 60 | + // vault() -> vault id not found |
| 61 | + Skyflow client = builder.build(); |
| 62 | + try { |
| 63 | + client.vault(); |
| 64 | + Assert.fail("vault() must fail once the vault is removed"); |
| 65 | + } catch (SkyflowException e) { |
| 66 | + Assert.assertTrue(e.getMessage().contains(NOT_IN_CONFIG_LIST)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testScenarioA_viaClient_addUpdateDeleteThenVaultFails() throws SkyflowException { |
| 72 | + Skyflow client = Skyflow.builder().addVaultConfig(config("cluster1")).build(); |
| 73 | + |
| 74 | + client.updateVaultConfig(update("cluster2", Env.PROD, 30)); |
| 75 | + Assert.assertEquals("cluster2", client.getVaultConfig(VAULT_ID).getClusterId()); |
| 76 | + Assert.assertEquals(Integer.valueOf(30), client.getVaultConfig(VAULT_ID).getTimeout()); |
| 77 | + |
| 78 | + client.removeVaultConfig(VAULT_ID); |
| 79 | + Assert.assertNull(client.getVaultConfig(VAULT_ID)); |
| 80 | + |
| 81 | + try { |
| 82 | + client.vault(); |
| 83 | + Assert.fail("vault() must fail once the vault is removed"); |
| 84 | + } catch (SkyflowException e) { |
| 85 | + Assert.assertTrue(e.getMessage().contains(NOT_IN_CONFIG_LIST)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // ── B: add -> delete -> update must throw ───────────────────────────────── |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testScenarioB_viaBuilder_addDeleteThenUpdateThrows() throws SkyflowException { |
| 93 | + Skyflow.SkyflowClientBuilder builder = Skyflow.builder() |
| 94 | + .addVaultConfig(config("cluster1")) |
| 95 | + .removeVaultConfig(VAULT_ID); |
| 96 | + |
| 97 | + try { |
| 98 | + builder.updateVaultConfig(update("cluster2", Env.PROD, 30)); |
| 99 | + Assert.fail("updating a removed vault must throw, not silently re-create it"); |
| 100 | + } catch (SkyflowException e) { |
| 101 | + Assert.assertTrue(e.getMessage().contains(NOT_IN_CONFIG_LIST)); |
| 102 | + } |
| 103 | + |
| 104 | + // the failed update must not have resurrected the vault |
| 105 | + Assert.assertNull(builder.build().getVaultConfig(VAULT_ID)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testScenarioB_viaClient_addDeleteThenUpdateThrows() throws SkyflowException { |
| 110 | + Skyflow client = Skyflow.builder().addVaultConfig(config("cluster1")).build(); |
| 111 | + client.removeVaultConfig(VAULT_ID); |
| 112 | + |
| 113 | + try { |
| 114 | + client.updateVaultConfig(update("cluster2", Env.PROD, 30)); |
| 115 | + Assert.fail("updating a removed vault must throw, not silently re-create it"); |
| 116 | + } catch (SkyflowException e) { |
| 117 | + Assert.assertTrue(e.getMessage().contains(NOT_IN_CONFIG_LIST)); |
| 118 | + } |
| 119 | + |
| 120 | + Assert.assertNull(client.getVaultConfig(VAULT_ID)); |
| 121 | + try { |
| 122 | + client.vault(); |
| 123 | + Assert.fail("vault() must still fail after the rejected update"); |
| 124 | + } catch (SkyflowException e) { |
| 125 | + Assert.assertTrue(e.getMessage().contains(NOT_IN_CONFIG_LIST)); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // ── C: add -> update -> vault() carries the latest config ───────────────── |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testScenarioC_viaBuilder_vaultAfterUpdateHasLatest() throws SkyflowException { |
| 133 | + Skyflow.SkyflowClientBuilder builder = Skyflow.builder().addVaultConfig(config("cluster1")); |
| 134 | + |
| 135 | + builder.updateVaultConfig(update("cluster2", Env.PROD, 15)); |
| 136 | + VaultController vault = builder.build().vault(); |
| 137 | + |
| 138 | + Assert.assertEquals("cluster2", vault.getVaultConfig().getClusterId()); |
| 139 | + Assert.assertEquals(Env.PROD, vault.getVaultConfig().getEnv()); |
| 140 | + Assert.assertEquals(Integer.valueOf(15), vault.getVaultConfig().getTimeout()); |
| 141 | + Assert.assertEquals("https://cluster2.skyvault.skyflowapis.com", vault.currentVaultURL); |
| 142 | + vault.updateExecutorInHTTP(); |
| 143 | + Assert.assertEquals(15_000, vault.sharedHttpClient.callTimeoutMillis()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testScenarioC_viaClient_vaultAfterUpdateHasLatest() throws SkyflowException { |
| 148 | + Skyflow client = Skyflow.builder().addVaultConfig(config("cluster1")).build(); |
| 149 | + |
| 150 | + client.updateVaultConfig(update("cluster2", Env.PROD, 15)); |
| 151 | + VaultController vault = client.vault(); |
| 152 | + |
| 153 | + Assert.assertEquals("cluster2", vault.getVaultConfig().getClusterId()); |
| 154 | + Assert.assertEquals(Env.PROD, vault.getVaultConfig().getEnv()); |
| 155 | + Assert.assertEquals(Integer.valueOf(15), vault.getVaultConfig().getTimeout()); |
| 156 | + Assert.assertEquals("https://cluster2.skyvault.skyflowapis.com", vault.currentVaultURL); |
| 157 | + vault.updateExecutorInHTTP(); |
| 158 | + Assert.assertEquals(15_000, vault.sharedHttpClient.callTimeoutMillis()); |
| 159 | + } |
| 160 | + |
| 161 | + // ── D: add -> vault() -> update -> vault() carries the latest config ────── |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testScenarioD_viaBuilder_heldControllerSeesTheUpdate() throws SkyflowException { |
| 165 | + Skyflow.SkyflowClientBuilder builder = Skyflow.builder().addVaultConfig(config("cluster1")); |
| 166 | + VaultController held = builder.build().vault(); |
| 167 | + Assert.assertEquals("cluster1", held.getVaultConfig().getClusterId()); |
| 168 | + Assert.assertEquals("https://cluster1.skyvault.skyflowapis.dev", held.currentVaultURL); |
| 169 | + |
| 170 | + builder.updateVaultConfig(update("cluster2", Env.PROD, 45)); |
| 171 | + VaultController after = builder.build().vault(); |
| 172 | + |
| 173 | + Assert.assertSame("the reference taken before the update must still be current", held, after); |
| 174 | + Assert.assertEquals("cluster2", held.getVaultConfig().getClusterId()); |
| 175 | + Assert.assertEquals(Integer.valueOf(45), held.getVaultConfig().getTimeout()); |
| 176 | + Assert.assertEquals("https://cluster2.skyvault.skyflowapis.com", held.currentVaultURL); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testScenarioD_viaClient_heldControllerSeesTheUpdate() throws SkyflowException { |
| 181 | + Skyflow client = Skyflow.builder().addVaultConfig(config("cluster1")).build(); |
| 182 | + VaultController held = client.vault(); |
| 183 | + Assert.assertEquals("cluster1", held.getVaultConfig().getClusterId()); |
| 184 | + |
| 185 | + client.updateVaultConfig(update("cluster2", Env.PROD, 45)); |
| 186 | + |
| 187 | + Assert.assertSame("the reference taken before the update must still be current", |
| 188 | + held, client.vault()); |
| 189 | + Assert.assertEquals("cluster2", held.getVaultConfig().getClusterId()); |
| 190 | + Assert.assertEquals(Integer.valueOf(45), held.getVaultConfig().getTimeout()); |
| 191 | + Assert.assertEquals("https://cluster2.skyvault.skyflowapis.com", held.currentVaultURL); |
| 192 | + held.updateExecutorInHTTP(); |
| 193 | + Assert.assertEquals(45_000, held.sharedHttpClient.callTimeoutMillis()); |
| 194 | + } |
| 195 | + |
| 196 | + // ── the vaultUrl variant of C/D, since it resolves differently to clusterId ── |
| 197 | + |
| 198 | + @Test |
| 199 | + public void testScenarioD_viaClient_heldControllerSeesANewVaultUrl() throws SkyflowException { |
| 200 | + VaultConfig initial = config("cluster1"); |
| 201 | + initial.setVaultUrl("https://first.example.com"); |
| 202 | + Skyflow client = Skyflow.builder().addVaultConfig(initial).build(); |
| 203 | + VaultController held = client.vault(); |
| 204 | + Assert.assertEquals("https://first.example.com", held.currentVaultURL); |
| 205 | + |
| 206 | + VaultConfig update = config("cluster1"); |
| 207 | + update.setVaultUrl("https://second.example.com"); |
| 208 | + client.updateVaultConfig(update); |
| 209 | + |
| 210 | + Assert.assertEquals("https://second.example.com", held.currentVaultURL); |
| 211 | + } |
| 212 | +} |
0 commit comments