|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 | import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
5 | 6 |
|
6 | 7 | import org.junit.Before; |
7 | 8 | import org.junit.Test; |
@@ -59,4 +60,63 @@ public void dropsInvalidGlobalTreatment() { |
59 | 60 | assertNull(sanitized.getGlobal()); |
60 | 61 | assertEquals(0, sanitized.getByFlag().size()); |
61 | 62 | } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void byFlagTreatmentIsDroppedWhenInvalidFormat() { |
| 66 | + Map<String, FallbackTreatment> byFlag = new HashMap<>(); |
| 67 | + byFlag.put(VALID_FLAG, new FallbackTreatment("on.off")); |
| 68 | + byFlag.put("valid_num_dot", new FallbackTreatment("123.on")); |
| 69 | + byFlag.put("null_treatment", new FallbackTreatment(null)); |
| 70 | + |
| 71 | + FallbackConfiguration config = FallbackConfiguration.builder() |
| 72 | + .global(null) |
| 73 | + .byFlag(byFlag) |
| 74 | + .build(); |
| 75 | + |
| 76 | + FallbackConfiguration sanitized = mSanitizer.sanitize(config); |
| 77 | + |
| 78 | + // Only the valid one should remain |
| 79 | + assertEquals(1, sanitized.getByFlag().size()); |
| 80 | + assertEquals("123.on", sanitized.getByFlag().get("valid_num_dot").getTreatment()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void globalTreatmentIsDroppedWhenInvalidFormat() { |
| 85 | + Map<String, FallbackTreatment> byFlag = new HashMap<>(); |
| 86 | + byFlag.put(VALID_FLAG, new FallbackTreatment("on_1-2")); |
| 87 | + byFlag.put("null_treatment", new FallbackTreatment(null)); |
| 88 | + |
| 89 | + FallbackConfiguration config = FallbackConfiguration.builder() |
| 90 | + // Global invalid due to regex (letters cannot be followed by '.') |
| 91 | + .global(new FallbackTreatment("on.off")) |
| 92 | + .byFlag(byFlag) |
| 93 | + .build(); |
| 94 | + |
| 95 | + FallbackConfiguration sanitized = mSanitizer.sanitize(config); |
| 96 | + |
| 97 | + assertNull(sanitized.getGlobal()); |
| 98 | + // Ensure only the valid by-flag entry is preserved |
| 99 | + assertEquals(1, sanitized.getByFlag().size()); |
| 100 | + assertEquals("on_1-2", sanitized.getByFlag().get(VALID_FLAG).getTreatment()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void validFormatTreatmentIsNotDropped() { |
| 105 | + Map<String, FallbackTreatment> byFlag = new HashMap<>(); |
| 106 | + byFlag.put("numWithDot", new FallbackTreatment("123.on")); |
| 107 | + byFlag.put(VALID_FLAG, new FallbackTreatment("on_1-2")); |
| 108 | + |
| 109 | + FallbackConfiguration config = FallbackConfiguration.builder() |
| 110 | + .global(new FallbackTreatment("on")) |
| 111 | + .byFlag(byFlag) |
| 112 | + .build(); |
| 113 | + |
| 114 | + FallbackConfiguration sanitized = mSanitizer.sanitize(config); |
| 115 | + |
| 116 | + assertEquals(2, sanitized.getByFlag().size()); |
| 117 | + assertTrue(sanitized.getByFlag().containsKey("numWithDot")); |
| 118 | + assertEquals("123.on", sanitized.getByFlag().get("numWithDot").getTreatment()); |
| 119 | + assertEquals("on_1-2", sanitized.getByFlag().get(VALID_FLAG).getTreatment()); |
| 120 | + assertEquals("on", sanitized.getGlobal().getTreatment()); |
| 121 | + } |
62 | 122 | } |
0 commit comments