-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Aliaksandr Stsiapanay <[email protected]>
- Loading branch information
1 parent
873f0a7
commit 4145de2
Showing
5 changed files
with
245 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/test/java/com/epam/aidial/core/config/FileConfigStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.epam.aidial.core.config; | ||
|
||
import com.epam.aidial.core.AiDial; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.json.JsonObject; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class FileConfigStoreTest { | ||
|
||
private FileConfigStore store; | ||
|
||
private static JsonObject SETTINGS; | ||
|
||
@BeforeAll | ||
public static void beforeAll() throws IOException { | ||
String file = "aidial.settings.json"; | ||
try (InputStream stream = AiDial.class.getClassLoader().getResourceAsStream(file)) { | ||
Objects.requireNonNull(stream, "Default resource file with settings is not found"); | ||
String json = new String(stream.readAllBytes(), StandardCharsets.UTF_8); | ||
SETTINGS = new JsonObject(json); | ||
} | ||
} | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
store = new FileConfigStore(mock(Vertx.class), SETTINGS.getJsonObject("config")); | ||
} | ||
|
||
@Test | ||
public void testAssociateDeploymentWithApiKey_DuplicateKey() { | ||
Config config = new Config(); | ||
Key k1 = new Key(); | ||
k1.setProject("some"); | ||
config.getKeys().put("k1", k1); | ||
Application app = new Application(); | ||
app.setName("app"); | ||
app.setApiKey("k1"); | ||
store.associateDeploymentWithApiKey(config, app); | ||
assertNotNull(app.getApiKey()); | ||
assertNotEquals("k1", app.getApiKey()); | ||
assertEquals(2, config.getKeys().size()); | ||
assertEquals(app.getName(), config.getKeys().get(app.getApiKey()).getProject()); | ||
assertNotNull(config.getKeys().get(app.getApiKey()).getKey()); | ||
assertEquals(k1.getProject(), config.getKeys().get("k1").getProject()); | ||
} | ||
|
||
@Test | ||
public void testAssociateDeploymentWithApiKey_MissedKey() { | ||
Config config = new Config(); | ||
Key k1 = new Key(); | ||
k1.setProject("some"); | ||
config.getKeys().put("k1", k1); | ||
Application app = new Application(); | ||
app.setName("app"); | ||
store.associateDeploymentWithApiKey(config, app); | ||
assertNotNull(app.getApiKey()); | ||
assertNotEquals("k1", app.getApiKey()); | ||
assertEquals(2, config.getKeys().size()); | ||
assertEquals(app.getName(), config.getKeys().get(app.getApiKey()).getProject()); | ||
assertNotNull(config.getKeys().get(app.getApiKey()).getKey()); | ||
assertEquals(k1.getProject(), config.getKeys().get("k1").getProject()); | ||
} | ||
|
||
@Test | ||
public void testAssociateDeploymentWithApiKey_DifferentKey() { | ||
Config config = new Config(); | ||
Key k1 = new Key(); | ||
k1.setProject("some"); | ||
config.getKeys().put("k1", k1); | ||
Application app = new Application(); | ||
app.setName("app"); | ||
app.setApiKey("k2"); | ||
store.associateDeploymentWithApiKey(config, app); | ||
assertNotNull(app.getApiKey()); | ||
assertNotEquals("k1", app.getApiKey()); | ||
assertEquals(2, config.getKeys().size()); | ||
assertEquals(app.getName(), config.getKeys().get(app.getApiKey()).getProject()); | ||
assertNotNull(config.getKeys().get(app.getApiKey()).getKey()); | ||
assertEquals(k1.getProject(), config.getKeys().get("k1").getProject()); | ||
} | ||
|
||
@Test | ||
public void testAssociateDeploymentWithApiKey_Reload() { | ||
String apiKey = null; | ||
for (int i = 0; i < 3; i++) { | ||
Config config = new Config(); | ||
Key k1 = new Key(); | ||
k1.setProject("some"); | ||
config.getKeys().put("k1", k1); | ||
Application app = new Application(); | ||
app.setName("app"); | ||
|
||
store.associateDeploymentWithApiKey(config, app); | ||
|
||
if (i == 0) { | ||
apiKey = app.getApiKey(); | ||
} else { | ||
assertEquals(apiKey, app.getApiKey()); | ||
} | ||
assertNotNull(app.getApiKey()); | ||
assertNotEquals("k1", app.getApiKey()); | ||
assertEquals(2, config.getKeys().size()); | ||
assertEquals(app.getName(), config.getKeys().get(app.getApiKey()).getProject()); | ||
assertNotNull(config.getKeys().get(app.getApiKey()).getKey()); | ||
assertEquals(k1.getProject(), config.getKeys().get("k1").getProject()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAssociateDeploymentWithApiKey_ReloadKeyChanged() { | ||
String apiKey = null; | ||
for (int i = 0; i < 5; i++) { | ||
Config config = new Config(); | ||
Key k1 = new Key(); | ||
k1.setProject("some"); | ||
config.getKeys().put("k1", k1); | ||
Application app = new Application(); | ||
app.setName("app"); | ||
if (i == 2) { | ||
app.setApiKey("k2"); | ||
} | ||
if (i == 4) { | ||
app.setApiKey(null); | ||
} | ||
store.associateDeploymentWithApiKey(config, app); | ||
assertNotNull(app.getApiKey()); | ||
assertNotEquals("k1", app.getApiKey()); | ||
assertEquals(2, config.getKeys().size()); | ||
assertEquals(app.getName(), config.getKeys().get(app.getApiKey()).getProject()); | ||
assertNotNull(config.getKeys().get(app.getApiKey()).getKey()); | ||
assertEquals(k1.getProject(), config.getKeys().get("k1").getProject()); | ||
switch (i) { | ||
case 1 -> assertEquals(apiKey, app.getApiKey()); | ||
case 2, 3 -> assertEquals("k2", app.getApiKey()); | ||
case 4 -> assertNotEquals(apiKey, app.getApiKey()); | ||
default -> apiKey = app.getApiKey(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.