From 89cda5cbc450e6c6197ec7797cbf2151a86d076a Mon Sep 17 00:00:00 2001 From: Bernd Hufmann Date: Mon, 26 Aug 2024 16:29:04 -0400 Subject: [PATCH] tmf: Add support for json-schema file to TmfConfigurationSourceType For schema specification see https://json-schema.org/ [Added] support for json-schema file to TmfConfigurationSourceType Signed-off-by: Bernd Hufmann --- .../TmfConfigurationSourceTypeTest.java | 58 ++++++++++++++++++- .../config/ITmfConfigurationSourceType.java | 27 +++++++++ .../config/TmfConfigurationSourceType.java | 32 +++++++++- 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationSourceTypeTest.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationSourceTypeTest.java index 75c69d07fc..cdc2f7314f 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationSourceTypeTest.java +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/config/TmfConfigurationSourceTypeTest.java @@ -14,6 +14,8 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; import java.util.List; import org.eclipse.jdt.annotation.NonNull; @@ -21,7 +23,10 @@ import org.eclipse.tracecompass.tmf.core.config.ITmfConfigurationSourceType; import org.eclipse.tracecompass.tmf.core.config.TmfConfigParamDescriptor; import org.eclipse.tracecompass.tmf.core.config.TmfConfigurationSourceType; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import com.google.common.collect.ImmutableList; @@ -33,11 +38,40 @@ public class TmfConfigurationSourceTypeTest { // ------------------------------------------------------------------------ // Test data // ------------------------------------------------------------------------ + private static final TemporaryFolder TEMPORARY_FOLDER = new TemporaryFolder(); private static final String PATH = "/tmp/my-test.xml"; private static final String ID = "my-test.xml"; private static final String DESC = "descriptor"; + private static File fsSchemaFile; private static final @NonNull List<@NonNull ITmfConfigParamDescriptor> PARAM = ImmutableList.of(new TmfConfigParamDescriptor.Builder().setKeyName("path").build()); - private static final String EXPECTED_TO_STRING = "TmfConfigurationSourceType[fName=/tmp/my-test.xml, fDescription=descriptor, fId=my-test.xml, fKeys=[TmfConfigParamDescriptor[fKeyName=path, fDataType=STRING, fIsRequired=true, fDescription=]]]"; + private static final String EXPECTED_TO_STRING = "TmfConfigurationSourceType[fName=/tmp/my-test.xml, fDescription=descriptor, fId=my-test.xml, fKeys=[TmfConfigParamDescriptor[fKeyName=path, fDataType=STRING, fIsRequired=true, fDescription=]], fSchemaFile=null]"; + private static final String EXPECTED_TO_STRING_WITH_SCHEMA = "TmfConfigurationSourceType[fName=/tmp/my-test.xml, fDescription=descriptor, fId=my-test.xml, fKeys=[], fSchemaFile=schema.json]"; + + private static final String SCHEMA_FILE_NAME = "schema.json"; + + // ------------------------------------------------------------------------ + // Class setup and cleanup + // ------------------------------------------------------------------------ + + /** + * Class setup + * + * @throws IOException + * if IO error happens + */ + @BeforeClass + public static void setupClass() throws IOException { + TEMPORARY_FOLDER.create(); + fsSchemaFile = TEMPORARY_FOLDER.newFile(SCHEMA_FILE_NAME); + } + + /** + * Class cleanup + */ + @AfterClass + public static void cleanupClass(){ + TEMPORARY_FOLDER.delete(); + } // ------------------------------------------------------------------------ // Tests @@ -58,6 +92,8 @@ public void testBuilder() { assertEquals(ID, config.getId()); assertEquals(DESC, config.getDescription()); assertEquals(PARAM, config.getConfigParamDescriptors()); + + } /** @@ -116,6 +152,20 @@ public void testBuilderMissingParams() { // success } + // Test non-existing JSON schema file + File schemaFile = new File("schema.json"); + builder = new TmfConfigurationSourceType.Builder() + .setName(PATH) + .setId("\n") // blank + .setDescription(DESC) + .setSchemaFile(schemaFile); + try { + builder.build(); + fail("No exception created"); + } catch (IllegalStateException e) { + // success + } + // Test successful builder builder = new TmfConfigurationSourceType.Builder() .setId(ID) @@ -177,6 +227,12 @@ public void testToString() { .setDescription(DESC) .setConfigParamDescriptors(PARAM); assertEquals(EXPECTED_TO_STRING, builder.build().toString()); + builder = new TmfConfigurationSourceType.Builder() + .setName(PATH) + .setId(ID) + .setDescription(DESC) + .setSchemaFile(fsSchemaFile); + assertEquals(EXPECTED_TO_STRING_WITH_SCHEMA, builder.build().toString()); } /** diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSourceType.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSourceType.java index 4537d3f91e..4bdc9f116d 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSourceType.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/ITmfConfigurationSourceType.java @@ -11,8 +11,11 @@ package org.eclipse.tracecompass.tmf.core.config; +import java.io.File; import java.util.List; +import org.eclipse.jdt.annotation.Nullable; + /** * Interface to implement that describes a configuration source. * @@ -46,7 +49,31 @@ public interface ITmfConfigurationSourceType { * Gets a list of query parameter keys to be passed when creating * configuration instance of this type. * + * Implement either {@link #getConfigParamDescriptors()} or + * {@link #getSchemaFile()}, but not both. + * + * Note: If list is empty use {@link #getSchemaFile()} instead. + * * @return A list of query parameter descriptors to be passed */ List getConfigParamDescriptors(); + + /** + * Gets a file containing a json-schema describing the query parameters to + * be passed when creating a configuration instance of this type. + * + * See https://json-schema.org/ for specification of the schema. + * + * Implement either {@link #getConfigParamDescriptors()} or + * {@link #getSchemaFile()}, but not both. + * + * Note: If the schema file is null, use + * {@link #getConfigParamDescriptors()} instead. + * + * @return A file containing a valid json-schema or null if not used + * @since 9.5 + */ + default @Nullable File getSchemaFile() { + return null; + } } diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfigurationSourceType.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfigurationSourceType.java index 0215a9d754..3623c03968 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfigurationSourceType.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfigurationSourceType.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.tracecompass.tmf.core.config; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -28,6 +29,7 @@ public class TmfConfigurationSourceType implements ITmfConfigurationSourceType { private final String fId; private final String fName; private final String fDescription; + private final @Nullable File fSchemaFile; private final List fParamDescriptors; /** @@ -41,6 +43,7 @@ private TmfConfigurationSourceType(Builder builder) { fName = builder.fName; fDescription = builder.fDescription; fParamDescriptors = builder.fDescriptors; + fSchemaFile = builder.fSchemaFile; } @Override @@ -63,15 +66,23 @@ public List getConfigParamDescriptors() { return fParamDescriptors; } + @Override + public @Nullable File getSchemaFile() { + return fSchemaFile; + } + @Override @SuppressWarnings("nls") public String toString() { + File schemaFile = getSchemaFile(); + String schemaFileName = schemaFile == null ? "null" : schemaFile.getName(); return new StringBuilder(getClass().getSimpleName()) .append("[") .append("fName=").append(getName()) .append(", fDescription=").append(getDescription()) .append(", fId=").append(getId()) .append(", fKeys=").append(getConfigParamDescriptors()) + .append(", fSchemaFile=").append(schemaFileName) .append("]").toString(); } @@ -82,7 +93,8 @@ public boolean equals(@Nullable Object arg0) { } TmfConfigurationSourceType other = (TmfConfigurationSourceType) arg0; return Objects.equals(fName, other.fName) && Objects.equals(fId, other.fId) && Objects.equals(fDescription, other.fDescription) - && Objects.equals(fParamDescriptors, other.fParamDescriptors); + && Objects.equals(fParamDescriptors, other.fParamDescriptors) + && Objects.equals(fSchemaFile, other.fSchemaFile); } @Override @@ -98,6 +110,7 @@ public static class Builder { private String fId = ""; //$NON-NLS-1$ private String fName = ""; //$NON-NLS-1$ private String fDescription = ""; //$NON-NLS-1$ + private @Nullable File fSchemaFile = null; private List fDescriptors = new ArrayList<>(); /** @@ -156,6 +169,19 @@ public Builder setConfigParamDescriptors(List descrip return this; } + /** + * Sets the json-schema of the configuration source type + * + * @param schema + * the json schema file + * @return the builder instance. + * @since 9.5 + */ + public Builder setSchemaFile(@Nullable File schema) { + fSchemaFile = schema; + return this; + } + /** * The method to construct an instance of * {@link ITmfConfigurationSourceType} @@ -170,6 +196,10 @@ public ITmfConfigurationSourceType build() { if (fName.isBlank()) { throw new IllegalStateException("Configuration source type name not set"); //$NON-NLS-1$ } + + if (fSchemaFile != null && !fSchemaFile.exists()) { + throw new IllegalStateException("Configuration source type schema file doesn't exist"); //$NON-NLS-1$ + } return new TmfConfigurationSourceType(this); } }