diff --git a/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java b/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java index 91c29df2..8957d00f 100644 --- a/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java +++ b/src/main/java/jenkins/plugins/logstash/persistence/ElasticSearchDao.java @@ -26,22 +26,26 @@ import static com.google.common.collect.Ranges.closedOpen; +import net.sf.json.JSONObject; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.HttpClientBuilder; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.net.URI; import java.net.URISyntaxException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import com.google.common.collect.Range; @@ -155,4 +159,32 @@ private String getErrorMessage(CloseableHttpResponse response) { @Override public IndexerType getIndexerType() { return IndexerType.ELASTICSEARCH; } + + @Override + public JSONObject buildPayload(BuildData buildData, String jenkinsUrl, List logLines) { + JSONObject payload = super.buildPayload(buildData, jenkinsUrl, logLines); + JSONObject buildVariables = payload.getJSONObject("data").getJSONObject("buildVariables"); + escapeKeysIllegalCharacter(buildVariables); + return payload; + } + + + /** + * Escapes any ElasticSearch illegal character to '_' character in all of the keys in the JSON object specified. + * + * @param jsonObject the json object which keys to be escapated + */ + private void escapeKeysIllegalCharacter(JSONObject jsonObject) { + Set illegalKeys = new HashSet(); + for (Object keyObject : jsonObject.keySet()) { + String key = (String) keyObject; + if(key.contains(".")) { + illegalKeys.add(key); + } + } + for (String illegalKey : illegalKeys) { + Object value = jsonObject.remove(illegalKey); + jsonObject.put(illegalKey.replace('.', '_'), value); + } + } } diff --git a/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java b/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java index 17051a92..a1f41455 100644 --- a/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java +++ b/src/test/java/jenkins/plugins/logstash/persistence/ElasticSearchDaoTest.java @@ -1,5 +1,6 @@ package jenkins.plugins.logstash.persistence; +import net.sf.json.JSONObject; import org.apache.commons.lang.CharEncoding; import org.apache.http.HttpEntity; import org.apache.http.StatusLine; @@ -7,21 +8,24 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.*; +import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URI; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; +import static org.junit.Assert.*; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) @@ -230,4 +234,22 @@ public void pushFailStatusCode() throws Exception { } } + + + @Test + public void buildPayloadSuccessTransformPeriod() throws Exception { + BuildData mockBuildData = mock(BuildData.class); + String buildDataString = "{\"buildVariables\":{\"buildInfoConfig.propertiesFile\":\"any.value\", \"foo.bar\":\"boo.boo\"}}"; + when(mockBuildData.toJson()).thenReturn(JSONObject.fromObject(buildDataString)); + + // Unit under test + ElasticSearchDao dao = createDao("http://localhost", 8200, "/jenkins/logstash", "", ""); + JSONObject result = dao.buildPayload(mockBuildData, "http://localhost:8080/jenkins", new ArrayList()); + + // Verify results + JSONObject buildVariables = result.getJSONObject("data").getJSONObject("buildVariables"); + assertEquals(2, buildVariables.size()); + assertThat((Set) buildVariables.keySet(), containsInAnyOrder("buildInfoConfig_propertiesFile", "foo_bar")); + assertThat((Collection) buildVariables.values(), containsInAnyOrder("any.value", "boo.boo")); + } }