Skip to content

Commit

Permalink
tmf: add jsonParameters to ITmfConfiguration and ITmfConfigurationSource
Browse files Browse the repository at this point in the history
This is as alternative to Map<String, Object>.

[Added] jsonParameters to ITmfConfiguration and ITmfConfigurationSource

Signed-off-by: Bernd Hufmann <[email protected]>
  • Loading branch information
bhufmann committed Sep 23, 2024
1 parent 381d0e4 commit 4da7c27
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
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 @@ -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

0 comments on commit 4da7c27

Please sign in to comment.