From: Stephane Gamard Date: Thu, 14 Aug 2014 14:39:12 +0000 (+0200) Subject: Increased coverage for ListUpdateScripts X-Git-Tag: 4.5-RC1~147 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=35a5827103aa7cbd35d8553f19f40924cb9ca55f;p=sonarqube.git Increased coverage for ListUpdateScripts --- diff --git a/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java b/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java index c17c56b97c9..46c4ceed487 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java +++ b/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java @@ -25,7 +25,9 @@ import org.elasticsearch.script.AbstractExecutableScript; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.NativeScriptFactory; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; public class ListUpdate extends AbstractExecutableScript { @@ -104,14 +106,19 @@ public class ListUpdate extends AbstractExecutableScript { if (fieldValue == null && value != null) { // 0. The field does not exist (this is a upsert then) - source.put(field, value); + List values = new ArrayList(1); + values.add(value); + source.put(field, values); } else if (!XContentMapValues.isArray(fieldValue) && value != null) { // 1. The field is not yet a list Map currentFieldValue = XContentMapValues.nodeMapValue(fieldValue, "current FieldValue"); if (XContentMapValues.nodeStringValue(currentFieldValue.get(idField), null).equals(idValue)) { source.put(field, value); } else { - source.put(field, org.elasticsearch.common.collect.ImmutableSet.of(fieldValue, value)); + List values = new ArrayList(2); + values.add(fieldValue); + values.add(value); + source.put(field, values); } } else { // 3. field is a list diff --git a/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java b/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java index 93cf4216439..5b60a9d5eb7 100644 --- a/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java +++ b/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java @@ -24,6 +24,7 @@ import org.elasticsearch.script.ExecutableScript; import org.junit.Before; import org.junit.Test; +import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -98,4 +99,84 @@ public class UpdateListScriptTest { assertThat(e.getMessage()).isEqualTo("Missing 'idField' parameter"); } } + + + @Test + public void update_list() throws Exception { + + String listField = "listField"; + Collection> mapFields; + Map source = new HashMap(); + source.put("field1", "value1"); + + // 0 Create list when field does not exists + Map params = new HashMap(); + params.put(ListUpdate.FIELD, listField); + params.put(ListUpdate.ID_FIELD, "key"); + params.put(ListUpdate.ID_VALUE, "1"); + params.put(ListUpdate.VALUE, mapOf("key", "1", "value", "A")); + + ExecutableScript script = factory.newScript(params); + script.setNextVar("ctx", ImmutableMap.of("_source",source)); + script.run(); + + mapFields = (Collection)source.get(listField); + System.out.println("source = " + source); + assertThat(mapFields).hasSize(1); + + // Add item to existing list + params = new HashMap(); + params.put(ListUpdate.FIELD, listField); + params.put(ListUpdate.ID_FIELD, "key"); + params.put(ListUpdate.ID_VALUE, "2"); + params.put(ListUpdate.VALUE, mapOf("key", "2", "value", "B")); + script = factory.newScript(params); + script.setNextVar("ctx", ImmutableMap.of("_source",source)); + script.run(); + mapFields = (Collection)source.get(listField); + assertThat(mapFields).hasSize(2); + + // updated first item in list + params = new HashMap(); + params.put(ListUpdate.FIELD, listField); + params.put(ListUpdate.ID_FIELD, "key"); + params.put(ListUpdate.ID_VALUE, "1"); + params.put(ListUpdate.VALUE, mapOf("key", "1", "value", "a")); + script = factory.newScript(params); + script.setNextVar("ctx", ImmutableMap.of("_source",source)); + script.run(); + mapFields = (Collection)source.get(listField); + assertThat(mapFields).hasSize(2); + + // updated second item in list + params = new HashMap(); + params.put(ListUpdate.FIELD, listField); + params.put(ListUpdate.ID_FIELD, "key"); + params.put(ListUpdate.ID_VALUE, "2"); + params.put(ListUpdate.VALUE, mapOf("key", "2", "value","b")); + script = factory.newScript(params); + script.setNextVar("ctx", ImmutableMap.of("_source",source)); + script.run(); + mapFields = (Collection)source.get(listField); + assertThat(mapFields).hasSize(2); + + // delete first item + params = new HashMap(); + params.put(ListUpdate.FIELD, listField); + params.put(ListUpdate.ID_FIELD, "key"); + params.put(ListUpdate.ID_VALUE, "1"); + params.put(ListUpdate.VALUE, null); + script = factory.newScript(params); + script.setNextVar("ctx", ImmutableMap.of("_source",source)); + script.run(); + mapFields = (Collection)source.get(listField); + assertThat(mapFields).hasSize(1); + } + + private Map mapOf(String k, String v, String k1, String v1) { + Map map = new HashMap(); + map.put(k, v); + map.put(k1, v1); + return map; + } } \ No newline at end of file