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

Global Properties Idempotency #24

Merged
merged 8 commits into from
Feb 5, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@
@Immutable
public final class GlobalPropertiesPluginData implements PluginData {

private static void validateGlobalPropertyIsNotDefined(final Data data, final GlobalPropertyId globalPropertyId) {
final PropertyDefinition propertyDefinition = data.globalPropertyDefinitions.get(globalPropertyId);
if (propertyDefinition != null) {
throw new ContractException(PropertyError.DUPLICATE_PROPERTY_DEFINITION, globalPropertyId);
}
}



/**
* Builder class for GloblaInitialData
*
Expand All @@ -49,11 +40,11 @@ private Builder(Data data) {
}

/**
* Returns the {@link GlobalInitialData} from the collected information
* Returns the {@link GlobalPropertiesPluginData} from the collected information
* supplied to this builder. Clears the builder's state.
*
* @throws ContractException
* <li>{@linkplain PropertyError.UNKNOWN_PROPERTY_ID}</li>
* <li>{@linkplain PropertyError#UNKNOWN_PROPERTY_ID}</li>
* if a global property value was associated with a global
* property id that was not defined
*
Expand All @@ -78,6 +69,7 @@ public GlobalPropertiesPluginData build() {

/**
* Defines a global property
* Duplicate inputs override previous inputs.
*
* @throws ContractException
*
Expand All @@ -88,16 +80,12 @@ public GlobalPropertiesPluginData build() {
* <li>{@linkplain PropertyError#NULL_PROPERTY_DEFINITION}
* </li> if the property definition is null
*
* <li>{@linkplain PropertyError#DUPLICATE_PROPERTY_DEFINITION}
* </li> if a property definition for the given global
* property id was previously defined.
*
*/
public Builder defineGlobalProperty(final GlobalPropertyId globalPropertyId, final PropertyDefinition propertyDefinition) {
ensureDataMutability();
validateGlobalPropertyIdNotNull(globalPropertyId);
validateGlobalPropertyDefinitionNotNull(propertyDefinition);
validateGlobalPropertyIsNotDefined(data, globalPropertyId);
data.globalPropertyDefinitions.put(globalPropertyId, propertyDefinition);
return this;
}
Expand All @@ -111,7 +99,8 @@ private void ensureDataMutability() {

/**
* Sets the global property value that overrides the default value of
* the corresponding property definition
* the corresponding property definition.
* Duplicate inputs override previous inputs.
*
* @throws ContractException
*
Expand All @@ -120,25 +109,16 @@ private void ensureDataMutability() {
*
* <li>{@linkplain PropertyError#NULL_PROPERTY_VALUE}</li>if
* the global property value is null
*
// * <li>{@linkplain PropertyError#DUPLICATE_PROPERTY_VALUE_ASSIGNMENT}
// * </li>if the global property value was previously defined
// * for the given global property id
*
*
*/
public Builder setGlobalPropertyValue(final GlobalPropertyId globalPropertyId, final Object propertyValue) {
ensureDataMutability();
validateGlobalPropertyIdNotNull(globalPropertyId);
validateGlobalPropertyValueNotNull(propertyValue);
//validateGlobalPropertyValueNotAssigned(data, globalPropertyId);
data.globalPropertyValues.put(globalPropertyId, propertyValue);
return this;
}
// private static void validateGlobalPropertyValueNotAssigned(final Data data, final GlobalPropertyId globalPropertyId) {
// if (data.globalPropertyValues.containsKey(globalPropertyId)) {
// throw new ContractException(PropertyError.DUPLICATE_PROPERTY_VALUE_ASSIGNMENT, globalPropertyId);
// }
// }

private void validateData() {
if (!dataIsMutable) {
Expand Down Expand Up @@ -234,7 +214,7 @@ private GlobalPropertiesPluginData(final Data data) {
* the global property id is known
*/
public PropertyDefinition getGlobalPropertyDefinition(final GlobalPropertyId globalPropertyId) {
validategGlobalPropertyIdExists(data, globalPropertyId);
validateGlobalPropertyIdExists(data, globalPropertyId);
return data.globalPropertyDefinitions.get(globalPropertyId);
}

Expand Down Expand Up @@ -264,15 +244,15 @@ public <T extends GlobalPropertyId> Set<T> getGlobalPropertyIds() {
*/
@SuppressWarnings("unchecked")
public <T> T getGlobalPropertyValue(final GlobalPropertyId globalPropertyId) {
validategGlobalPropertyIdExists(data, globalPropertyId);
validateGlobalPropertyIdExists(data, globalPropertyId);
T result = (T) data.globalPropertyValues.get(globalPropertyId);
if (result == null) {
result = (T) data.globalPropertyDefinitions.get(globalPropertyId).getDefaultValue().get();
}
return result;
}

private static void validategGlobalPropertyIdExists(final Data data, final GlobalPropertyId globalPropertyId) {
private static void validateGlobalPropertyIdExists(final Data data, final GlobalPropertyId globalPropertyId) {
if (globalPropertyId == null) {
throw new ContractException(PropertyError.NULL_PROPERTY_ID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,43 @@ public void testDefineGlobalProperty() {

// define a few global properties
PropertyDefinition propertyDefinition = PropertyDefinition.builder().setType(Integer.class).setDefaultValue(34).build();
PropertyDefinition propertyDefinition2 = PropertyDefinition.builder().setType(Integer.class).setDefaultValue(57).build();
GlobalPropertyId globalPropertyId = new SimpleGlobalPropertyId("id 1");
expectedPropertyDefinitions.put(globalPropertyId, propertyDefinition);
builder.defineGlobalProperty(globalPropertyId, propertyDefinition2);
// replacing data to show that the value persists
builder.defineGlobalProperty(globalPropertyId, propertyDefinition);
// adding duplicate data to show that the value persists
builder.defineGlobalProperty(globalPropertyId, propertyDefinition);
expectedPropertyDefinitions.put(globalPropertyId, propertyDefinition);

propertyDefinition = PropertyDefinition.builder().setType(Double.class).setDefaultValue(234.34).build();
propertyDefinition2 = PropertyDefinition.builder().setType(Double.class).setDefaultValue(795.88).build();
globalPropertyId = new SimpleGlobalPropertyId("id 2");
expectedPropertyDefinitions.put(globalPropertyId, propertyDefinition);
builder.defineGlobalProperty(globalPropertyId, propertyDefinition2);
// replacing data to show that the value persists
builder.defineGlobalProperty(globalPropertyId, propertyDefinition);
// adding duplicate data to show that the value persists
builder.defineGlobalProperty(globalPropertyId, propertyDefinition);
expectedPropertyDefinitions.put(globalPropertyId, propertyDefinition);

propertyDefinition = PropertyDefinition.builder().setType(String.class).setDefaultValue("default value").build();
propertyDefinition2 = PropertyDefinition.builder().setType(String.class).setDefaultValue("second default").build();
globalPropertyId = new SimpleGlobalPropertyId("id 3");
expectedPropertyDefinitions.put(globalPropertyId, propertyDefinition);
builder.defineGlobalProperty(globalPropertyId, propertyDefinition2);
// replacing data to show that the value persists
builder.defineGlobalProperty(globalPropertyId, propertyDefinition);
// adding duplicate data to show that the value persists
builder.defineGlobalProperty(globalPropertyId, propertyDefinition);
expectedPropertyDefinitions.put(globalPropertyId, propertyDefinition);

// build the initial data
GlobalPropertiesPluginData globalInitialData = builder.build();

// show that the expected property ids are there
Set<GlobalPropertyId> actualGlobalPropertyIds = globalInitialData.getGlobalPropertyIds();
Set<GlobalPropertyId> expectedGlobalPropertyIds = expectedPropertyDefinitions.keySet();
assertEquals(expectedGlobalPropertyIds, actualGlobalPropertyIds);

// show that the property definitions are retrieved by their ids
for (GlobalPropertyId gpid : expectedPropertyDefinitions.keySet()) {
PropertyDefinition expectedPropertyDefinition = expectedPropertyDefinitions.get(gpid);
Expand All @@ -112,13 +132,6 @@ public void testDefineGlobalProperty() {
// if the property definition is null
contractException = assertThrows(ContractException.class, () -> builder.defineGlobalProperty(new SimpleGlobalPropertyId("id"), null));
assertEquals(PropertyError.NULL_PROPERTY_DEFINITION, contractException.getErrorType());

// if a property definition for the given global property id was
// previously defined.
builder.defineGlobalProperty(new SimpleGlobalPropertyId("id"), propDef);
contractException = assertThrows(ContractException.class, () -> builder.defineGlobalProperty(new SimpleGlobalPropertyId("id"), propDef));
assertEquals(PropertyError.DUPLICATE_PROPERTY_DEFINITION, contractException.getErrorType());

}

@Test
Expand All @@ -142,12 +155,24 @@ public void testSetGlobalPropertyValue() {
// set the values
for (TestGlobalPropertyId testGlobalPropertyId : TestGlobalPropertyId.values()) {
int value = randomGenerator.nextInt();
int value2 = randomGenerator.nextInt();
builder.setGlobalPropertyValue(testGlobalPropertyId, value2);
// replacing data to show that the value persists
builder.setGlobalPropertyValue(testGlobalPropertyId, value);
// duplicating data to show that the value persists
builder.setGlobalPropertyValue(testGlobalPropertyId, value);
expectedValues.put(testGlobalPropertyId, value);
}

// show that the expected values are present
// build the initial data
GlobalPropertiesPluginData globalInitialData = builder.build();

// show that the expected property ids are there
Set<GlobalPropertyId> actualGlobalPropertyIds = globalInitialData.getGlobalPropertyIds();
Set<GlobalPropertyId> expectedGlobalPropertyIds = expectedValues.keySet();
assertEquals(expectedGlobalPropertyIds, actualGlobalPropertyIds);

// show that the expected values are present
for (TestGlobalPropertyId testGlobalPropertyId : TestGlobalPropertyId.values()) {
Integer expectedGlobalPropertyValue = expectedValues.get(testGlobalPropertyId);
Integer actualGlobalPropertyValue = globalInitialData.getGlobalPropertyValue(testGlobalPropertyId);
Expand All @@ -167,17 +192,6 @@ public void testSetGlobalPropertyValue() {
// if the global property value is null
contractException = assertThrows(ContractException.class, () -> builder.setGlobalPropertyValue(TestGlobalPropertyId.GLOBAL_PROPERTY_1_BOOLEAN_MUTABLE, null));
assertEquals(PropertyError.NULL_PROPERTY_VALUE, contractException.getErrorType());

// // if the global property value was previously defined for the given
// // global property id
// builder.setGlobalPropertyValue(TestGlobalPropertyId.GLOBAL_PROPERTY_1_BOOLEAN_MUTABLE,
// 4);
// contractException = assertThrows(ContractException.class, () ->
// builder.setGlobalPropertyValue(TestGlobalPropertyId.GLOBAL_PROPERTY_1_BOOLEAN_MUTABLE,
// 5));
// assertEquals(PropertyError.DUPLICATE_PROPERTY_VALUE_ASSIGNMENT,
// contractException.getErrorType());

}

@Test
Expand Down