aboutsummaryrefslogtreecommitdiffstats
path: root/hosted-api
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2020-06-22 17:52:49 +0200
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2020-06-22 17:52:49 +0200
commitdb44411be03f7770456240ff11ed33a4cfe53725 (patch)
treea9e6890c521402034584db47694288dfaf511fa7 /hosted-api
parent1007b2fc5878f3abd3c49bc4cace2cfb673f050e (diff)
TestDescriptor: Add JSON serialization and construction from non-JSON
Diffstat (limited to 'hosted-api')
-rw-r--r--hosted-api/pom.xml12
-rw-r--r--hosted-api/src/main/java/ai/vespa/hosted/api/TestDescriptor.java56
-rw-r--r--hosted-api/src/test/java/ai/vespa/hosted/api/TestDescriptorTest.java17
3 files changed, 78 insertions, 7 deletions
diff --git a/hosted-api/pom.xml b/hosted-api/pom.xml
index 2a42f890ba4..b066cb158e0 100644
--- a/hosted-api/pom.xml
+++ b/hosted-api/pom.xml
@@ -33,6 +33,12 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>yolean</artifactId>
+ <version>${project.version}</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
@@ -44,6 +50,12 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>testutil</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/hosted-api/src/main/java/ai/vespa/hosted/api/TestDescriptor.java b/hosted-api/src/main/java/ai/vespa/hosted/api/TestDescriptor.java
index 37858148ef0..08cd3932ae7 100644
--- a/hosted-api/src/main/java/ai/vespa/hosted/api/TestDescriptor.java
+++ b/hosted-api/src/main/java/ai/vespa/hosted/api/TestDescriptor.java
@@ -3,18 +3,30 @@ package ai.vespa.hosted.api;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Inspector;
+import com.yahoo.slime.JsonFormat;
+import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeStream;
import com.yahoo.slime.SlimeUtils;
+import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import static com.yahoo.yolean.Exceptions.uncheck;
+
/**
* @author mortent
*/
public class TestDescriptor {
public static final String DEFAULT_FILENAME = "META-INF/ai.vespa/testDescriptor.json";
+ public static final String CURRENT_VERSION = "1.0";
+
+ private static final String JSON_FIELD_VERSION = "version";
+ private static final String JSON_FIELD_CONFIGURED_TESTS = "configuredTests";
+ private static final String JSON_FIELD_SYSTEM_TESTS = "systemTests";
+ private static final String JSON_FIELD_STAGING_TESTS = "stagingTests";
+ private static final String JSON_FIELD_PRODUCTION_TESTS = "productionTests";
private final Map<TestCategory, List<String>> configuredTestClasses;
private final String version;
@@ -27,16 +39,26 @@ public class TestDescriptor {
public static TestDescriptor fromJsonString(String testDescriptor) {
var slime = SlimeUtils.jsonToSlime(testDescriptor);
var root = slime.get();
- var version = root.field("version").asString();
- var testRoot = root.field("configuredTests");
- var systemTests = getJsonArray(testRoot, "systemTests");
- var stagingTests = getJsonArray(testRoot, "stagingTests");
- var productionTests = getJsonArray(testRoot, "productionTests");
- return new TestDescriptor(version, Map.of(
+ var version = root.field(JSON_FIELD_VERSION).asString();
+ var testRoot = root.field(JSON_FIELD_CONFIGURED_TESTS);
+ var systemTests = getJsonArray(testRoot, JSON_FIELD_SYSTEM_TESTS);
+ var stagingTests = getJsonArray(testRoot, JSON_FIELD_STAGING_TESTS);
+ var productionTests = getJsonArray(testRoot, JSON_FIELD_PRODUCTION_TESTS);
+ return new TestDescriptor(version, toMap(systemTests, stagingTests, productionTests));
+ }
+
+ public static TestDescriptor from(
+ String version, List<String> systemTests, List<String> stagingTests, List<String> productionTests) {
+ return new TestDescriptor(version, toMap(systemTests, stagingTests, productionTests));
+ }
+
+ private static Map<TestCategory, List<String>> toMap(
+ List<String> systemTests, List<String> stagingTests, List<String> productionTests) {
+ return Map.of(
TestCategory.systemtest, systemTests,
TestCategory.stagingtest, stagingTests,
TestCategory.productiontest, productionTests
- ));
+ );
}
private static List<String> getJsonArray(Cursor cursor, String field) {
@@ -51,6 +73,26 @@ public class TestDescriptor {
return List.copyOf(configuredTestClasses.get(category));
}
+ public String toJson() {
+ Slime slime = new Slime();
+ Cursor root = slime.setObject();
+ root.setString(JSON_FIELD_VERSION, this.version);
+ Cursor tests = root.setObject(JSON_FIELD_CONFIGURED_TESTS);
+ addJsonArrayForTests(tests, JSON_FIELD_SYSTEM_TESTS, TestCategory.systemtest);
+ addJsonArrayForTests(tests, JSON_FIELD_STAGING_TESTS, TestCategory.stagingtest);
+ addJsonArrayForTests(tests, JSON_FIELD_PRODUCTION_TESTS, TestCategory.productiontest);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ uncheck(() -> new JsonFormat(/*compact*/false).encode(out, slime));
+ return out.toString();
+ }
+
+ private void addJsonArrayForTests(Cursor testsRoot, String fieldName, TestCategory category) {
+ List<String> tests = configuredTestClasses.get(category);
+ if (tests.isEmpty()) return;
+ Cursor cursor = testsRoot.setArray(fieldName);
+ tests.forEach(cursor::addString);
+ }
+
@Override
public String toString() {
return "TestClassDescriptor{" +
diff --git a/hosted-api/src/test/java/ai/vespa/hosted/api/TestDescriptorTest.java b/hosted-api/src/test/java/ai/vespa/hosted/api/TestDescriptorTest.java
index 2676d9d79da..7e59af9ced8 100644
--- a/hosted-api/src/test/java/ai/vespa/hosted/api/TestDescriptorTest.java
+++ b/hosted-api/src/test/java/ai/vespa/hosted/api/TestDescriptorTest.java
@@ -1,6 +1,7 @@
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.api;
+import com.yahoo.test.json.JsonTestHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -67,4 +68,20 @@ public class TestDescriptorTest {
var productionTests = testClassDescriptor.getConfiguredTests(TestDescriptor.TestCategory.productiontest);
Assertions.assertIterableEquals(List.of("ai.vespa.test.ProductionTest1", "ai.vespa.test.ProductionTest2"), productionTests);
}
+
+ @Test
+ public void generatesCorrectJson() {
+ String json = "{\n" +
+ " \"version\": \"1.0\",\n" +
+ " \"configuredTests\": {\n" +
+ " \"systemTests\": [\n" +
+ " \"ai.vespa.test.SystemTest1\",\n" +
+ " \"ai.vespa.test.SystemTest2\"\n" +
+ " ]\n" +
+ " " +
+ " }\n" +
+ "}\n";
+ var descriptor = TestDescriptor.fromJsonString(json);
+ JsonTestHelper.assertJsonEquals(json, descriptor.toJson());
+ }
}