2023-01-09 14:56:53 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-01-31 18:20:27 +01:00
|
|
|
package sop.testsuite.external;
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
import com.google.gson.Gson;
|
2023-01-09 14:56:53 +01:00
|
|
|
import sop.SOP;
|
2023-01-31 18:20:27 +01:00
|
|
|
import sop.external.ExternalSOP;
|
|
|
|
import sop.testsuite.SOPInstanceFactory;
|
2023-01-09 14:56:53 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
2023-01-22 16:47:44 +01:00
|
|
|
import java.io.InputStreamReader;
|
2023-01-31 18:20:27 +01:00
|
|
|
import java.util.HashMap;
|
2023-01-22 16:47:44 +01:00
|
|
|
import java.util.List;
|
2023-01-31 18:20:27 +01:00
|
|
|
import java.util.Map;
|
2023-01-09 14:56:53 +01:00
|
|
|
import java.util.Properties;
|
|
|
|
|
2023-01-31 18:35:48 +01:00
|
|
|
/**
|
|
|
|
* This implementation of {@link SOPInstanceFactory} reads the JSON file at
|
|
|
|
* <pre>external-sop/src/main/resources/sop/testsuite/external/config.json</pre>
|
2023-01-31 18:40:14 +01:00
|
|
|
* to determine configured external test backends.
|
2023-01-31 18:35:48 +01:00
|
|
|
*/
|
2023-01-31 18:20:27 +01:00
|
|
|
public class ExternalSOPInstanceFactory extends SOPInstanceFactory {
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-31 18:20:27 +01:00
|
|
|
@Override
|
|
|
|
public Map<String, SOP> provideSOPInstances() {
|
|
|
|
Map<String, SOP> backends = new HashMap<>();
|
2023-01-22 16:47:44 +01:00
|
|
|
TestSuite suite = readConfiguration();
|
2023-01-22 16:53:50 +01:00
|
|
|
if (suite != null && !suite.backends.isEmpty()) {
|
|
|
|
for (TestSubject subject : suite.backends) {
|
|
|
|
if (!new File(subject.sop).exists()) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
Properties env = new Properties();
|
|
|
|
if (subject.env != null) {
|
|
|
|
for (Var var : subject.env) {
|
|
|
|
env.put(var.key, var.value);
|
|
|
|
}
|
2023-01-22 16:47:44 +01:00
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
SOP sop = new ExternalSOP(subject.sop, env);
|
2023-01-31 18:20:27 +01:00
|
|
|
backends.put(subject.name, sop);
|
2023-01-22 16:53:50 +01:00
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|
2023-01-31 18:20:27 +01:00
|
|
|
return backends;
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
public static TestSuite readConfiguration() {
|
|
|
|
Gson gson = new Gson();
|
2023-01-31 18:20:27 +01:00
|
|
|
InputStream inputStream = ExternalSOPInstanceFactory.class.getResourceAsStream("config.json");
|
2023-01-22 16:47:44 +01:00
|
|
|
if (inputStream == null) {
|
|
|
|
return null;
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
InputStreamReader reader = new InputStreamReader(inputStream);
|
2023-01-31 18:20:27 +01:00
|
|
|
return gson.fromJson(reader, TestSuite.class);
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
// JSON DTOs
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
public static class TestSuite {
|
|
|
|
List<TestSubject> backends;
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
public static class TestSubject {
|
|
|
|
String name;
|
|
|
|
String sop;
|
|
|
|
List<Var> env;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Var {
|
|
|
|
String key;
|
|
|
|
String value;
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|
|
|
|
}
|