Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-40155: Fix event's scope deserialization #143

Merged
merged 2 commits into from
Dec 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package hudson.plugins.tfs.model.servicehooks;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public enum EventScope {
/**
* No input scope specified.
Expand Down Expand Up @@ -30,4 +36,28 @@ public enum EventScope {
* Deployment scope.
*/
Deployment,

;

private static final Map<String, EventScope> CASE_INSENSITIVE_LOOKUP;

static {
final Map<String, EventScope> map = new TreeMap<String, EventScope>(String.CASE_INSENSITIVE_ORDER);
for (final EventScope value : EventScope.values()) {
map.put(value.name(), value);
}
CASE_INSENSITIVE_LOOKUP = Collections.unmodifiableMap(map);
}

@SuppressWarnings("unused" /* Invoked by Jackson via @JsonCreator */)
@JsonCreator
public static EventScope caseInsensitiveValueOf(final String name) {
if (name == null) {
throw new NullPointerException("Name is null");
}
if (!CASE_INSENSITIVE_LOOKUP.containsKey(name)) {
throw new IllegalArgumentException("No enum constant " + name);
}
return CASE_INSENSITIVE_LOOKUP.get(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"id": "6872ee8c-b333-4eff-bfb9-0d5274943566",
"eventType": "git.pullrequest.merged",
"publisherId": "tfs",
"scope": "all",
"message": {
"text": "Jamal Hartnett has created a pull request merge commit",
"html": "Jamal Hartnett has created a pull request merge commit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"id": "03c164c2-8912-4d5e-8009-3707d5f83734",
"eventType": "git.push",
"publisherId": "tfs",
"scope": "all",
"message": {
"text": "Jamal Hartnett pushed updates to branch master of repository Fabrikam-Fiber-Git.",
"html": "Jamal Hartnett pushed updates to branch master of repository Fabrikam-Fiber-Git.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hudson.plugins.tfs.model.servicehooks;

import hudson.plugins.tfs.util.EndpointHelper;
import org.junit.Assert;
import org.junit.Test;

/**
* A class to test {@link EventScope}.
*/
public class EventScopeTest {

@Test
public void deserialize_enumCasing() throws Exception {
final String input = "{\"scope\": \"all\"}";

final Event actual = EndpointHelper.MAPPER.readValue(input, Event.class);

Assert.assertEquals(EventScope.All, actual.getScope());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"id": "03c164c2-8912-4d5e-8009-3707d5f83734",
"eventType": "git.push",
"publisherId": "tfs",
"scope": "all",
"message": {
"text": "Jamal Hartnett pushed updates to branch master of repository Fabrikam-Fiber-Git.",
"html": "Jamal Hartnett pushed updates to branch master of repository Fabrikam-Fiber-Git.",
Expand Down