Skip to content

Commit ccbc629

Browse files
committed
Add an expected version to the configuration map entry update
PUT /configurationMap/{key} takes an optional expectedVersion query parameter. If it is set and doesn't match the current entry's version from ConfigurationProperty.versionOf, nothing is changed and the endpoint returns false. An empty string requires that the key doesn't exist yet. Without the parameter the entry is updated unconditionally, as before, and the endpoint returns true. This lets two clients updating the same key detect a lost update without storing a revision. Reporting it as a false result follows the revision checks on channels, channel groups and code templates, which return false instead of an error status. The check runs inside the controller's synchronized update. The smoke test runs the update against a live server, including calculating the version from the entry the server returned. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Tony Germano <tony@germano.name>
1 parent a3f09e4 commit ccbc629

7 files changed

Lines changed: 168 additions & 12 deletions

File tree

‎server/src/main/java/com/mirth/connect/client/core/Client.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,13 +810,16 @@ public void setConfigurationMap(Map<String, ConfigurationProperty> map) throws C
810810
}
811811

812812
/**
813-
* Updates a single entry in the configuration map, leaving the rest alone.
813+
* Updates a single entry in the configuration map, leaving the rest alone. If expectedVersion is
814+
* not null and doesn't match the entry's current version, nothing is changed and false is
815+
* returned.
814816
*
815817
* @see ConfigurationServletInterface#setConfigurationProperty
818+
* @see ConfigurationProperty#versionOf
816819
*/
817820
@Override
818-
public void setConfigurationProperty(String key, ConfigurationProperty property) throws ClientException {
819-
getServlet(ConfigurationServletInterface.class).setConfigurationProperty(key, property);
821+
public boolean setConfigurationProperty(String key, ConfigurationProperty property, String expectedVersion) throws ClientException {
822+
return getServlet(ConfigurationServletInterface.class).setConfigurationProperty(key, property, expectedVersion);
820823
}
821824

822825
/**

‎server/src/main/java/com/mirth/connect/client/core/api/servlets/ConfigurationServletInterface.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,17 @@ public void setConfigurationMap(@Param("map") @RequestBody(description = "The ne
310310
@Path("/configurationMap/{key}")
311311
@Operation(summary = "Updates a single entry in the configuration map, leaving the rest alone.")
312312
@MirthOperation(name = "setConfigurationProperty", display = "Set configuration map entry", permission = Permissions.CONFIGURATION_MAP_EDIT)
313-
public void setConfigurationProperty(
313+
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
314+
public boolean setConfigurationProperty(
314315
@Param("key") @Parameter(description = "The key of the entry to set. It can't be blank, and a / in the key has to be encoded as %2F.", required = true) @PathParam("key") String key,
315316
@Param("property") @RequestBody(description = "The value and comment to store under the key.", required = true, content = {
316317
@Content(mediaType = MediaType.APPLICATION_XML, examples = {
317318
@ExampleObject(name = "configurationProperty", ref = "../apiexamples/configuration_property_xml") }),
318319
@Content(mediaType = MediaType.APPLICATION_JSON, examples = {
319-
@ExampleObject(name = "configurationProperty", ref = "../apiexamples/configuration_property_json") }) }) ConfigurationProperty property) throws ClientException;
320+
@ExampleObject(name = "configurationProperty", ref = "../apiexamples/configuration_property_json") }) }) ConfigurationProperty property,
321+
@Param("expectedVersion") @Parameter(description = "If set, the entry is only updated if its current version matches. Otherwise nothing is changed and false is returned. "
322+
+ "An entry's version is the hex SHA-256 of the UTF-8 bytes of its value, a unit separator (U+001F), and its comment, with a null value and a null or blank comment treated as empty strings. The comparison ignores case. "
323+
+ "Use an empty string to require that the key doesn't exist yet. If not set, the entry is updated unconditionally.") @QueryParam("expectedVersion") String expectedVersion) throws ClientException;
320324

321325
@GET
322326
@Path("/databaseDrivers")

‎server/src/main/java/com/mirth/connect/server/api/servlets/ConfigurationServlet.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ public void setConfigurationMap(Map<String, ConfigurationProperty> map) {
309309
}
310310

311311
@Override
312-
public void setConfigurationProperty(String key, ConfigurationProperty property) {
312+
public boolean setConfigurationProperty(String key, ConfigurationProperty property, String expectedVersion) {
313313
if (StringUtils.isBlank(key)) {
314314
throw new MirthApiException(Response.status(Response.Status.BAD_REQUEST).entity("The configuration map key cannot be blank.").build());
315315
}
316316

317317
try {
318-
configurationController.setConfigurationProperty(key, property);
318+
return configurationController.setConfigurationProperty(key, property, expectedVersion);
319319
} catch (ControllerException e) {
320320
throw new MirthApiException(e);
321321
}

‎server/src/main/java/com/mirth/connect/server/controllers/ConfigurationController.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,13 @@ public static ConfigurationController getInstance() {
338338
* The key of the entry to set.
339339
* @param property
340340
* The value and comment to store under the key.
341+
* @param expectedVersion
342+
* If not null, the entry is only set if this matches the current entry's version,
343+
* as returned by {@link ConfigurationProperty#versionOf}, ignoring case. An empty
344+
* string requires that the key doesn't exist yet.
345+
* @return false if expectedVersion didn't match and nothing was changed, otherwise true.
341346
*/
342-
public abstract void setConfigurationProperty(String key, ConfigurationProperty property) throws ControllerException;
347+
public abstract boolean setConfigurationProperty(String key, ConfigurationProperty property, String expectedVersion) throws ControllerException;
343348

344349
// properties
345350
public Properties getPropertiesForGroup(String group) {

‎server/src/main/java/com/mirth/connect/server/controllers/DefaultConfigurationController.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,10 +939,15 @@ public synchronized void setConfigurationProperties(Map<String, ConfigurationPro
939939
}
940940

941941
@Override
942-
public synchronized void setConfigurationProperty(String key, ConfigurationProperty property) throws ControllerException {
942+
public synchronized boolean setConfigurationProperty(String key, ConfigurationProperty property, String expectedVersion) throws ControllerException {
943943
Map<String, ConfigurationProperty> properties = getConfigurationProperties();
944+
if (expectedVersion != null && !expectedVersion.equalsIgnoreCase(ConfigurationProperty.versionOf(properties.get(key)))) {
945+
return false;
946+
}
947+
944948
properties.put(key, property);
945949
setConfigurationProperties(properties, true);
950+
return true;
946951
}
947952

948953
@Override

‎server/src/test/java/com/mirth/connect/server/controllers/DefaultConfigurationControllerTest.java‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package com.mirth.connect.server.controllers;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertFalse;
1314
import static org.junit.Assert.assertTrue;
1415
import static org.junit.Assert.fail;
1516
import static org.mockito.ArgumentMatchers.any;
@@ -293,7 +294,7 @@ public void setConfigurationProperty_KeepsOtherEntries() throws Exception {
293294
AtomicReference<String> database = new AtomicReference<String>(INITIAL_CONFIGURATION_MAP_XML);
294295
DefaultConfigurationController configurationController = databaseBackedConfigurationController(database);
295296

296-
configurationController.setConfigurationProperty("B", new ConfigurationProperty("2", "second"));
297+
assertTrue(configurationController.setConfigurationProperty("B", new ConfigurationProperty("2", "second"), null));
297298

298299
Map<String, String> expected = new HashMap<String, String>();
299300
expected.put("A", "1");
@@ -308,6 +309,66 @@ public void setConfigurationProperty_KeepsOtherEntries() throws Exception {
308309
assertEquals("second", saved.get("B").getComment());
309310
}
310311

312+
@Test
313+
public void setConfigurationProperty_ExpectedVersionMatches() throws Exception {
314+
AtomicReference<String> database = new AtomicReference<String>(INITIAL_CONFIGURATION_MAP_XML);
315+
DefaultConfigurationController configurationController = databaseBackedConfigurationController(database);
316+
String version = ConfigurationProperty.versionOf(new ConfigurationProperty("1", "comment"));
317+
318+
assertTrue(configurationController.setConfigurationProperty("A", new ConfigurationProperty("2", "comment"), version));
319+
assertEquals("2", configurationController.getConfigurationMap().get("A"));
320+
}
321+
322+
@Test
323+
public void setConfigurationProperty_ExpectedVersionMatchesIgnoringCase() throws Exception {
324+
AtomicReference<String> database = new AtomicReference<String>(INITIAL_CONFIGURATION_MAP_XML);
325+
DefaultConfigurationController configurationController = databaseBackedConfigurationController(database);
326+
String version = ConfigurationProperty.versionOf(new ConfigurationProperty("1", "comment")).toUpperCase();
327+
328+
assertTrue(configurationController.setConfigurationProperty("A", new ConfigurationProperty("2", "comment"), version));
329+
assertEquals("2", configurationController.getConfigurationMap().get("A"));
330+
}
331+
332+
@Test
333+
public void setConfigurationProperty_ExpectedVersionMismatch() throws Exception {
334+
assertConflict("A", ConfigurationProperty.versionOf(new ConfigurationProperty("0", "comment")));
335+
}
336+
337+
@Test
338+
public void setConfigurationProperty_ExpectedMissing_KeyExists() throws Exception {
339+
assertConflict("A", "");
340+
}
341+
342+
@Test
343+
public void setConfigurationProperty_ExpectedVersion_KeyMissing() throws Exception {
344+
assertConflict("B", ConfigurationProperty.versionOf(new ConfigurationProperty("1", "comment")));
345+
}
346+
347+
@Test
348+
public void setConfigurationProperty_ExpectedMissing_KeyMissing() throws Exception {
349+
AtomicReference<String> database = new AtomicReference<String>(INITIAL_CONFIGURATION_MAP_XML);
350+
DefaultConfigurationController configurationController = databaseBackedConfigurationController(database);
351+
352+
assertTrue(configurationController.setConfigurationProperty("B", new ConfigurationProperty("2", null), ""));
353+
354+
Map<String, String> expected = new HashMap<String, String>();
355+
expected.put("A", "1");
356+
expected.put("B", "2");
357+
assertEquals(expected, configurationController.getConfigurationMap());
358+
}
359+
360+
private void assertConflict(String key, String expectedVersion) throws Exception {
361+
AtomicReference<String> database = new AtomicReference<String>(INITIAL_CONFIGURATION_MAP_XML);
362+
DefaultConfigurationController configurationController = databaseBackedConfigurationController(database);
363+
Map<String, String> before = configurationController.getConfigurationMap();
364+
String savedBefore = database.get();
365+
366+
assertFalse(configurationController.setConfigurationProperty(key, new ConfigurationProperty("2", "second"), expectedVersion));
367+
368+
assertEquals(savedBefore, database.get());
369+
assertEquals(before, configurationController.getConfigurationMap());
370+
}
371+
311372
private DefaultConfigurationController databaseBackedConfigurationController(AtomicReference<String> database) {
312373
DefaultConfigurationController.mirthConfig.setProperty(DefaultConfigurationController.CONFIGURATION_MAP_LOCATION, "database");
313374

‎smoketest/src/test/java/org/openintegrationengine/smoketest/ConfigurationMapEntryTest.java‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,108 @@
33

44
package org.openintegrationengine.smoketest;
55

6+
import static org.junit.jupiter.api.Assertions.assertEquals;
67
import static org.junit.jupiter.api.Assertions.assertFalse;
78
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
810

11+
import java.util.Map;
12+
import java.util.UUID;
13+
14+
import org.junit.jupiter.api.AfterEach;
915
import org.junit.jupiter.api.DisplayName;
1016
import org.junit.jupiter.api.Test;
1117

1218
import com.mirth.connect.client.core.ClientException;
1319
import com.mirth.connect.client.core.api.servlets.ConfigurationServletInterface;
1420
import com.mirth.connect.util.ConfigurationProperty;
1521

16-
/** Exercises the single-entry configuration map update through the Java client and a live server. */
22+
/**
23+
* Exercises the single-entry configuration map update and its expected-version check through the
24+
* Java client and a live server. The matching rules themselves are covered by
25+
* DefaultConfigurationControllerTest; these tests cover what only a live server can show.
26+
*/
1727
@DisplayName("Configuration map entry update")
1828
class ConfigurationMapEntryTest {
1929

30+
/** Unique per run, so the test never touches entries it didn't create. */
31+
private static final String PREFIX = "smoketest." + UUID.randomUUID() + ".";
32+
33+
private static final String KEY = PREFIX + "key";
34+
35+
@AfterEach
36+
void removeTestEntries() throws Exception {
37+
Map<String, ConfigurationProperty> map = configuration().getConfigurationMap();
38+
if (map.keySet().removeIf(key -> key.startsWith(PREFIX))) {
39+
configuration().setConfigurationMap(map);
40+
}
41+
}
42+
2043
/** The key check runs in the servlet, so only a request to a live server reaches it. */
2144
@Test
2245
@DisplayName("rejects a blank key")
2346
void rejectsBlankKey() throws Exception {
24-
assertThrows(ClientException.class, () -> configuration().setConfigurationProperty(" ", new ConfigurationProperty("1", null)));
47+
assertThrows(ClientException.class, () -> configuration().setConfigurationProperty(" ", new ConfigurationProperty("1", null), null));
2548

2649
assertFalse(configuration().getConfigurationMap().containsKey(" "));
2750
}
2851

52+
/**
53+
* A null expected version has to be left out of the request. Sent as an empty string, it would
54+
* require that the key doesn't exist.
55+
*/
56+
@Test
57+
@DisplayName("updates unconditionally without an expected version")
58+
void updatesWithoutExpectedVersion() throws Exception {
59+
assertTrue(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("1", null), null));
60+
61+
assertTrue(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("2", null), null));
62+
63+
assertEquals("2", value(KEY));
64+
}
65+
66+
/** An empty expected version has to reach the server as an empty string, not as null. */
67+
@Test
68+
@DisplayName("creates an entry with an empty expected version only if the key is missing")
69+
void createsOnlyIfMissing() throws Exception {
70+
assertTrue(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("1", null), ""));
71+
72+
assertFalse(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("2", null), ""));
73+
assertEquals("1", value(KEY));
74+
}
75+
76+
/**
77+
* The version a client calculates from what the server returned has to match the one the server
78+
* calculates, including for multi-line and non-ASCII content.
79+
*/
80+
@Test
81+
@DisplayName("updates when the expected version matches the entry read from the server")
82+
void updatesWhenVersionMatches() throws Exception {
83+
configuration().setConfigurationProperty(KEY, new ConfigurationProperty("line 1\nline 2 é", "comment ✓"), null);
84+
ConfigurationProperty current = configuration().getConfigurationMap().get(KEY);
85+
86+
assertTrue(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("2", null), ConfigurationProperty.versionOf(current)));
87+
88+
assertEquals("2", value(KEY));
89+
}
90+
91+
/** A rejected update has to come back as false and leave the stored entry unchanged. */
92+
@Test
93+
@DisplayName("rejects an update based on a stale version")
94+
void rejectsStaleVersion() throws Exception {
95+
configuration().setConfigurationProperty(KEY, new ConfigurationProperty("1", null), null);
96+
String staleVersion = ConfigurationProperty.versionOf(configuration().getConfigurationMap().get(KEY));
97+
assertTrue(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("2", null), staleVersion));
98+
99+
assertFalse(configuration().setConfigurationProperty(KEY, new ConfigurationProperty("3", null), staleVersion));
100+
assertEquals("2", value(KEY));
101+
}
102+
29103
private static ConfigurationServletInterface configuration() {
30104
return SharedServer.get().servlet(ConfigurationServletInterface.class);
31105
}
106+
107+
private static String value(String key) throws Exception {
108+
return configuration().getConfigurationMap().get(key).getValue();
109+
}
32110
}

0 commit comments

Comments
 (0)