|
3 | 3 |
|
4 | 4 | package org.openintegrationengine.smoketest; |
5 | 5 |
|
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
6 | 7 | import static org.junit.jupiter.api.Assertions.assertFalse; |
7 | 8 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 10 |
|
| 11 | +import java.util.Map; |
| 12 | +import java.util.UUID; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.AfterEach; |
9 | 15 | import org.junit.jupiter.api.DisplayName; |
10 | 16 | import org.junit.jupiter.api.Test; |
11 | 17 |
|
12 | 18 | import com.mirth.connect.client.core.ClientException; |
13 | 19 | import com.mirth.connect.client.core.api.servlets.ConfigurationServletInterface; |
14 | 20 | import com.mirth.connect.util.ConfigurationProperty; |
15 | 21 |
|
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 | + */ |
17 | 27 | @DisplayName("Configuration map entry update") |
18 | 28 | class ConfigurationMapEntryTest { |
19 | 29 |
|
| 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 | + |
20 | 43 | /** The key check runs in the servlet, so only a request to a live server reaches it. */ |
21 | 44 | @Test |
22 | 45 | @DisplayName("rejects a blank key") |
23 | 46 | 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)); |
25 | 48 |
|
26 | 49 | assertFalse(configuration().getConfigurationMap().containsKey(" ")); |
27 | 50 | } |
28 | 51 |
|
| 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 | + |
29 | 103 | private static ConfigurationServletInterface configuration() { |
30 | 104 | return SharedServer.get().servlet(ConfigurationServletInterface.class); |
31 | 105 | } |
| 106 | + |
| 107 | + private static String value(String key) throws Exception { |
| 108 | + return configuration().getConfigurationMap().get(key).getValue(); |
| 109 | + } |
32 | 110 | } |
0 commit comments