Ticket 748: Handling of incomplete /what/source key-value pairs jenkins-baltrad-db-421
authorMats Vernersson <mats.vernersson@smhi.se>
Tue, 26 Jun 2018 11:46:55 +0000 (13:46 +0200)
committerMats Vernersson <mats.vernersson@smhi.se>
Tue, 26 Jun 2018 11:46:55 +0000 (13:46 +0200)
fromString-function in Source.java has been updated so that source-strings containing keys without values are not triggering InvalidSource-exceptions. Instead, keys without values are silently ignored.

client/java/src/eu/baltrad/bdb/oh5/Source.java
client/java/test/eu/baltrad/bdb/oh5/SourceTest.java

index 1187320..488522e 100644 (file)
@@ -90,11 +90,11 @@ public class Source {
     String[] elements = StringUtils.split(source, ",");
     for (int i=0; i < elements.length; i++) {
       String[] keyValuePair = StringUtils.split(elements[i], ":");
-      if (keyValuePair.length != 2) {
-        throw new InvalidSource(source);
-      } else {
+      if (keyValuePair.length == 2) {
         result.put(keyValuePair[0], keyValuePair[1]);
-      }
+      } else if (keyValuePair.length != 1 || !elements[i].endsWith(":")) {
+        throw new InvalidSource(source);
+      } // else: we have a key without value - ignore without throwing exception
     }
     if (source.isEmpty()) {
       throw new InvalidSource();
index dc0f48a..4d03e8a 100644 (file)
@@ -62,6 +62,15 @@ public class SourceTest {
     assertEquals("12345", classUnderTest.get("WMO"));
     assertEquals("eefoo", classUnderTest.get("NOD"));
   }
+  
+  @Test
+  public void fromString_incompleteKeyValuePair() {
+    classUnderTest = Source.fromString("WMO:12345,NOD:eefoo,CMT:");
+    assertEquals(2, classUnderTest.getKeys().size());
+    assertEquals("12345", classUnderTest.get("WMO"));
+    assertEquals("eefoo", classUnderTest.get("NOD"));
+    assertEquals(null, classUnderTest.get("CMT"));
+  }
 
   @Test
   public void fromString_empty() {