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 jsonParameters to ITmfConfiguration and ITmfConfigurationSource #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -14,6 +14,7 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

import java.util.Collections;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
Expand All @@ -36,7 +37,8 @@ public class TmfConfigurationTest {
private static final String DESC = "descriptor";
private static final String SOURCE_ID = "my-source-id";
private static final @NonNull Map<@NonNull String, @NonNull Object> PARAM = ImmutableMap.of("path", "/tmp/home/my-test.xml");
private static final String EXPECTED_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={path=/tmp/home/my-test.xml}]";
private static final String EXPECTED_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={path=/tmp/home/my-test.xml}, fJsonParameters=]";
private static final String EXPECTED2_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={}, fJsonParameters={\"path\":\"/tmp/my-test.xml\"}]";

// ------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -137,7 +139,8 @@ public void testEquality() {
.setId(ID)
.setDescription(DESC)
.setSourceTypeId(SOURCE_ID)
.setParameters(PARAM);
.setParameters(PARAM)
.setJsonParameters("{\"path\":\"" + PATH +"\"}");
ITmfConfiguration baseConfiguration = builder.build();

// Make sure it is equal to itself
Expand Down Expand Up @@ -188,6 +191,15 @@ public void testToString() {
.setSourceTypeId(SOURCE_ID)
.setParameters(PARAM);
assertEquals(EXPECTED_TO_STRING, builder.build().toString());

builder = new TmfConfiguration.Builder()
.setName(PATH)
.setId(ID)
.setDescription(DESC)
.setSourceTypeId(SOURCE_ID)
.setParameters(Collections.emptyMap())
.setJsonParameters("{\"path\":\"" + PATH +"\"}");
assertEquals(EXPECTED2_TO_STRING, builder.build().toString());
}

/**
Expand All @@ -212,8 +224,19 @@ public void testHashCode() {

ITmfConfiguration config2 = builder.build();

builder = new TmfConfiguration.Builder()
.setName(PATH + "1")
.setId(ID + "1")
.setDescription(DESC + "1")
.setSourceTypeId(SOURCE_ID + "1")
.setJsonParameters("{\"path\":\"" + PATH +"\"}");

ITmfConfiguration config3 = builder.build();

assertEquals(config1.hashCode(), config1.hashCode());
assertEquals(config2.hashCode(), config2.hashCode());
assertEquals(config3.hashCode(), config3.hashCode());
assertNotEquals(config1.hashCode(), config2.hashCode());
assertNotEquals(config2.hashCode(), config3.hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ public interface ITmfConfiguration {
String getSourceTypeId();

/**
* @return optional informational parameters to return. Can be used to show
* more details to users of the configuration instance.
* @return optional parameters representing the configuration parameters used to create
* this configuration. This is intended to be used instead of {@link #getJsonParameters()}.
*/
Map<String, Object> getParameters();

/**
* @return optional JSON string representing the configuration parameters used to create
* this configuration. This is intended to be used instead of {@link #getParameters()}.
*
* @since 9.5
*/
default String getJsonParameters() {
return ""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException;

import com.google.gson.Gson;

/**
* Interface to implement for providing a configuration source.
*
Expand All @@ -43,6 +45,29 @@ public interface ITmfConfigurationSource {
*/
ITmfConfiguration create(Map<String, Object> parameters) throws TmfConfigurationException;

/**
* Creates a new configuration instance.
* <p>
* The parameters to be provided are described by
* {@link ITmfConfigurationSourceType#getConfigParamDescriptors()}.
*
* @param parameters
* The query parameters as JSON string used to create a configuration instance.
* @return a new {@link ITmfConfiguration} if successful
* @throws TmfConfigurationException
* If the creation of the configuration fails
* @since 9.5
*/
default ITmfConfiguration create(String parameters) throws TmfConfigurationException {
try {
@SuppressWarnings("null")
Map<String, Object> map = new Gson().fromJson(parameters, Map.class);
return create(map);
} catch (Exception e) {
throw new TmfConfigurationException("Can't convert json string to Map to update configuration", e); //$NON-NLS-1$
}
}

/**
* Updates a configuration instance.
* <p>
Expand All @@ -59,6 +84,31 @@ public interface ITmfConfigurationSource {
*/
ITmfConfiguration update(String id, Map<String, Object> parameters) throws TmfConfigurationException;

/**
* Updates a configuration instance.
* <p>
* The parameters to be provided are described by
* {@link ITmfConfigurationSourceType#getConfigParamDescriptors()}.
*
* @param id
* The configuration ID of the configuration to update
* @param parameters
* The query parameters as JSON string used to update a configuration instance
* @return a new {@link ITmfConfiguration} if successful
* @throws TmfConfigurationException
* If the update of the configuration fails
* @since 9.5
*/
default ITmfConfiguration update(String id, String parameters) throws TmfConfigurationException {
try {
@SuppressWarnings("null")
Map<String, Object> map = new Gson().fromJson(parameters, Map.class);
return update(id, map);
} catch (Exception e) {
throw new TmfConfigurationException("Can't convert json string to Map to update configuration", e); //$NON-NLS-1$
}
}

/**
* Gets a configuration instance.
*
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.5
*/
default @Nullable File getSchemaFile() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public class TmfConfiguration implements ITmfConfiguration {
private final String fDescription;
private final String fSourceTypeId;
private final Map<String, Object> fParameters;
private final String fJsonParameters;

/**
* Constructor
*
* @param bulider
* @param builder
* the builder object to create the descriptor
*/
private TmfConfiguration(Builder builder) {
Expand All @@ -43,6 +44,7 @@ private TmfConfiguration(Builder builder) {
fDescription = builder.fDescription;
fSourceTypeId = Objects.requireNonNull(builder.fSourceTypeId);
fParameters = builder.fParameters;
fJsonParameters = builder.fJsonParameters;
}

@Override
Expand Down Expand Up @@ -70,6 +72,11 @@ public Map<String, Object> getParameters() {
return fParameters;
}

@Override
public String getJsonParameters() {
return fJsonParameters;
}

@Override
@SuppressWarnings("nls")
public String toString() {
Expand All @@ -80,6 +87,7 @@ public String toString() {
.append(", fType=").append(getSourceTypeId())
.append(", fId=").append(getId())
.append(", fParameters=").append(getParameters())
.append(", fJsonParameters=").append(getJsonParameters())
.append("]").toString();
}

Expand All @@ -90,12 +98,13 @@ public boolean equals(@Nullable Object arg0) {
}
TmfConfiguration other = (TmfConfiguration) arg0;
return Objects.equals(fName, other.fName) && Objects.equals(fId, other.fId)
&& Objects.equals(fSourceTypeId, other.fSourceTypeId) && Objects.equals(fDescription, other.fDescription) && Objects.equals(fParameters, other.fParameters);
&& Objects.equals(fSourceTypeId, other.fSourceTypeId) && Objects.equals(fDescription, other.fDescription)
&& Objects.equals(fParameters, other.fParameters) && Objects.equals(fJsonParameters, other.fJsonParameters);
}

@Override
public int hashCode() {
return Objects.hash(fName, fId, fSourceTypeId, fDescription, fParameters);
return Objects.hash(fName, fId, fSourceTypeId, fDescription, fParameters, fJsonParameters);
}

/**
Expand All @@ -108,6 +117,7 @@ public static class Builder {
private String fDescription = ""; //$NON-NLS-1$
private String fSourceTypeId = ""; //$NON-NLS-1$
private Map<String, Object> fParameters = new HashMap<>();
private String fJsonParameters = ""; //$NON-NLS-1$

/**
* Constructor
Expand Down Expand Up @@ -178,6 +188,21 @@ public Builder setParameters(Map<String, Object> parameters) {
return this;
}

/**
* Sets the optional JSON parameters of the {@link ITmfConfiguration}
* instance
*
* @param jsonParameters
* the optional JSON parameters of the {@link ITmfConfiguration}
* instance
* @return the builder instance
* @since 9.5
*/
public Builder setJsonParameters(String jsonParameters) {
fJsonParameters = jsonParameters;
return this;
}

/**
* The method to construct an instance of {@link ITmfConfiguration}
*
Expand Down
Loading
Loading