Skip to content

Commit

Permalink
Avoiding NPE after upgrade
Browse files Browse the repository at this point in the history
 * As a result of headers and request parameters not being read from existing config.
  • Loading branch information
tomasbjerre committed Jul 17, 2017
1 parent 7cb847e commit ad23172
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 18 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
# Generic Webhook Plugin Changelog
Changelog of Generic Webhook Plugin.
## Unreleased
### GitHub [#10](https://github.com/jenkinsci/generic-webhook-trigger-plugin/issues/10) replace "-" characters on header variable names

**#10: replace "-" characters on header variable names**


[13802b842bc50d2](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/13802b842bc50d2) Juan Pablo Santos Rodríguez *2017-07-17 11:42:55*


### GitHub [#11](https://github.com/jenkinsci/generic-webhook-trigger-plugin/pull/11) fix #10: replace "-" characters on header variable names

**follow-up on #11, suggested description no longer makes sense here, as it applies to all kind of variables**


[c6ffa25abb7236a](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/c6ffa25abb7236a) Juan Pablo Santos Rodríguez *2017-07-17 14:32:34*

**follow -up on #11, transform noWhitespace(String) from FlattenerUtils into toVariableName, and use it also on headers and request params**


[895979680dd8c03](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/895979680dd8c03) Juan Pablo Santos Rodríguez *2017-07-17 14:31:39*


### No issue

**small typo: flatternJson -> flattenJson**


[72169e5af5d0451](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/72169e5af5d0451) Juan Pablo Santos Rodríguez *2017-07-17 14:33:19*

**Cleaning**


[bedc35d535ae00f](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/bedc35d535ae00f) Tomas Bjerre *2017-07-16 05:09:54*

**Refactoring**


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jenkinsci.plugins.gwt.resolvers;

import static com.google.common.base.Preconditions.checkNotNull;

public class FlattenerUtils {

public static String filter(String string, String regexpFilter) {
Expand All @@ -9,10 +11,9 @@ public static String filter(String string, String regexpFilter) {
return string.replaceAll(regexpFilter, "");
}

public static String toVariableName(String mixedString) {
if (mixedString == null) {
return null;
}
return mixedString.replaceAll("\\s", "_").replaceAll("-", "_");
public static String toVariableName(String variableName) {
return checkNotNull(variableName, "variable name must be set") //
.replaceAll("\\s", "_") //
.replaceAll("-", "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public Map<String, String> flattenJson(String key, String regexFilter, Object re
flattenJson(key + "_" + entry.getKey(), regexFilter, entry.getValue()));
}
} else if (resolved != null) {
String noWhitespaces = toVariableName(key);
resolvedVariables.put(noWhitespaces, filter(resolved.toString(), regexFilter));
String variableName = toVariableName(key);
resolvedVariables.put(variableName, filter(resolved.toString(), regexFilter));
}
return resolvedVariables;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import static com.google.common.collect.Maps.newHashMap;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jenkinsci.plugins.gwt.GenericHeaderVariable;
import org.jenkinsci.plugins.gwt.GenericRequestVariable;
import org.jenkinsci.plugins.gwt.GenericVariable;

import com.google.common.collect.Lists;

public class VariablesResolver {
private List<GenericVariable> configuredGenericVariables = Lists.newArrayList();
private List<GenericVariable> configuredGenericVariables = new ArrayList<>();
private final List<GenericRequestVariable> configuredGenericRequestVariables;
private final String incomingPostContent;
private final Map<String, String[]> incomingParameterMap;
Expand All @@ -31,14 +31,24 @@ public VariablesResolver(
List<GenericVariable> configuredGenericVariables,
List<GenericRequestVariable> configuredGenericRequestVariables,
List<GenericHeaderVariable> configuredGenericHeaderVariables) {
this.incomingPostContent = incomingPostContent;
this.configuredGenericVariables = configuredGenericVariables;
this.incomingParameterMap = incomingParameterMap;
this.configuredGenericRequestVariables = configuredGenericRequestVariables;
this.configuredGenericHeaderVariables = configuredGenericHeaderVariables;
this.incomingPostContent = firstNotNull(incomingPostContent, "");
this.configuredGenericVariables =
firstNotNull(configuredGenericVariables, new ArrayList<GenericVariable>());
this.incomingParameterMap = firstNotNull(incomingParameterMap, new HashMap<String, String[]>());
this.configuredGenericRequestVariables =
firstNotNull(configuredGenericRequestVariables, new ArrayList<GenericRequestVariable>());
this.configuredGenericHeaderVariables =
firstNotNull(configuredGenericHeaderVariables, new ArrayList<GenericHeaderVariable>());
this.incomingHeaders = incomingHeaders;
}

private <T> T firstNotNull(T o1, T o2) {
if (o1 != null) {
return o1;
}
return o2;
}

public Map<String, String> getVariables() {
Map<String, String> resolvedVariables = newHashMap();
resolvedVariables.putAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ private Map<String, String> flattenXmlNode(
String childKey =
expandKey(parentKey, level, fromRootLevel) + "_" + childNode.getNodeName();
if (isXmlLeafNode(childNode)) {
String noWhitespace = toVariableName(childKey);
resolvedVariables.put(noWhitespace, filter(childNode.getTextContent(), regexFilter));
String variableName = toVariableName(childKey);
resolvedVariables.put(variableName, filter(childNode.getTextContent(), regexFilter));
} else {
//leafnode and text inside leafnode are 2 nodes, so /2 to keep counter in line
int leafNodeLevel = i / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class VariablesResolverHeaderTest {

@Test
public void testGenericRequestParameters() throws Exception {
public void testHeadersAndRequestParameters() throws Exception {
String postContent = null;

List<GenericVariable> genericVariables = newArrayList();
Expand Down Expand Up @@ -54,6 +54,103 @@ public void testGenericRequestParameters() throws Exception {
.hasSize(6);
}

@Test
public void testHeaders() throws Exception {
String postContent = null;

List<GenericVariable> genericVariables = newArrayList();

Map<String, String[]> parameterMap = new HashMap<>();
List<GenericRequestVariable> genericRequestVariables = new ArrayList<>();

Map<String, Enumeration<String>> headers = new HashMap<>();
headers.put("someparam", enumeration("some value"));
headers.put("anotherparam", enumeration("another value", "even more"));
headers.put("content-type", enumeration("application/json"));
headers.put("param_not_mapped", enumeration("do not include"));

List<GenericHeaderVariable> genericHeaderVariables = new ArrayList<>();
genericHeaderVariables.add(new GenericHeaderVariable("someparam", ""));
genericHeaderVariables.add(new GenericHeaderVariable("anotherparam", "[^e]"));
genericHeaderVariables.add(new GenericHeaderVariable("content-type", ""));
Map<String, String> variables =
new VariablesResolver(
headers,
parameterMap,
postContent,
genericVariables,
genericRequestVariables,
genericHeaderVariables)
.getVariables();

assertThat(variables) //
.containsEntry("someparam", "some value") //
.containsEntry("someparam_0", "some value") //
.containsEntry("anotherparam_1", "eee") //
.containsEntry("anotherparam_0", "ee") //
.containsEntry("content_type", "application/json") //
.hasSize(6);
}

@Test
public void testHeaderResolvesToNull() throws Exception {
String postContent = null;

List<GenericVariable> genericVariables = newArrayList();

Map<String, String[]> parameterMap = new HashMap<>();
List<GenericRequestVariable> genericRequestVariables = new ArrayList<>();

Map<String, Enumeration<String>> headers = new HashMap<>();
headers.put("someparam", enumeration("some value"));

List<GenericHeaderVariable> genericHeaderVariables = new ArrayList<>();
genericHeaderVariables.add(new GenericHeaderVariable("someparam", null));
Map<String, String> variables =
new VariablesResolver(
headers,
parameterMap,
postContent,
genericVariables,
genericRequestVariables,
genericHeaderVariables)
.getVariables();

assertThat(variables) //
.containsEntry("someparam", "some value") //
.hasSize(2);
}

@Test
public void testHeadersButNoneConfigured() throws Exception {
String postContent = null;

List<GenericVariable> genericVariables = newArrayList();

Map<String, String[]> parameterMap = new HashMap<>();
List<GenericRequestVariable> genericRequestVariables = new ArrayList<>();

Map<String, Enumeration<String>> headers = new HashMap<>();
headers.put("someparam", enumeration("some value"));
headers.put("anotherparam", enumeration("another value", "even more"));
headers.put("content-type", enumeration("application/json"));
headers.put("param_not_mapped", enumeration("do not include"));

List<GenericHeaderVariable> genericHeaderVariables = new ArrayList<>();
Map<String, String> variables =
new VariablesResolver(
headers,
parameterMap,
postContent,
genericVariables,
genericRequestVariables,
genericHeaderVariables)
.getVariables();

assertThat(variables) //
.hasSize(0);
}

private Enumeration<String> enumeration(final String... string) {
return new Enumeration<String>() {
private int i = 0;
Expand Down

0 comments on commit ad23172

Please sign in to comment.