Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tmf: Add support for json-schema file to TmfConfigurationSourceType #141

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<ITmfConfigParamDescriptor> getConfigParamDescriptors();

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing blank line before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* 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;
}
}
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.5
*/
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
Loading