Skip to content

Commit

Permalink
tmf: Add support for json-schema file to TmfConfigurationSourceType
Browse files Browse the repository at this point in the history
https://json-schema.org/

[Added] support for json-schema file to TmfConfigurationSourceType

Signed-off-by: Bernd Hufmann <[email protected]>
  • Loading branch information
bhufmann committed Aug 30, 2024
1 parent 4e417fb commit 6ba8935
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
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;
import org.eclipse.tracecompass.tmf.core.config.ITmfConfigParamDescriptor;
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;

Expand All @@ -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
Expand All @@ -58,6 +92,8 @@ public void testBuilder() {
assertEquals(ID, config.getId());
assertEquals(DESC, config.getDescription());
assertEquals(PARAM, config.getConfigParamDescriptors());


}

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -49,4 +52,16 @@ public interface ITmfConfigurationSourceType {
* @return A list of query parameter descriptors to be passed
*/
List<ITmfConfigParamDescriptor> getConfigParamDescriptors();
/**
* A string containing a json-schema describing the query parameters
* to be passed when creating a configuration instance of this type.
*
* Note: Use either {@link #getConfigParamDescriptors()} or {@link #getSchemaFile()}
*
* @return A file containing a valid json-schema or null if not used
* @since 9.4
*/
default @Nullable File getSchemaFile() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ITmfConfigParamDescriptor> fParamDescriptors;

/**
Expand All @@ -41,6 +43,7 @@ private TmfConfigurationSourceType(Builder builder) {
fName = builder.fName;
fDescription = builder.fDescription;
fParamDescriptors = builder.fDescriptors;
fSchemaFile = builder.fSchemaFile;
}

@Override
Expand All @@ -63,15 +66,23 @@ public List<ITmfConfigParamDescriptor> 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();
}

Expand All @@ -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
Expand All @@ -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<ITmfConfigParamDescriptor> fDescriptors = new ArrayList<>();

/**
Expand Down Expand Up @@ -156,6 +169,19 @@ public Builder setConfigParamDescriptors(List<ITmfConfigParamDescriptor> descrip
return this;
}

/**
* Sets the json-schema of the configuration source type
*
* @param schema
* the json schema file
* @return the builder instance.
* @since 9.4
*/
public Builder setSchemaFile(@Nullable File schema) {
fSchemaFile = schema;
return this;
}

/**
* The method to construct an instance of
* {@link ITmfConfigurationSourceType}
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 6ba8935

Please sign in to comment.