Deserialize JSON data in Java

In my previous post, I introduced a way to generate Java classes out of actual JSON data. This time, I will talk about how you can actually call Jenkins REST API and deserialize the data to usable objects.

Jenkins can give you the list of slave nodes in JSON format with the URL like the following.

http://your-jenkins-server/computer/api/json

When you hit the URL with your browser, it gives you the JSON data like the following.

{
  "_class" : "hudson.model.ComputerSet",
  "busyExecutors" : 0,
  "computer" : [
    {
      "_class" : "hudson.model.Hudson$MasterComputer",
      "actions" : [
        
      ],
      "assignedLabels" : [
        {
          "name" : "master"
        }
      ],
      "description" : "the master Jenkins node",
      "displayName" : "master",
      "executors" : [
        {
          
        }
      ],
      "icon" : "computer.png",
      "iconClassName" : "icon-computer",
      "idle" : true,
      "jnlpAgent" : false,
      "launchSupported" : true,
      "loadStatistics" : {
        "_class" : "hudson.model.Label$1"
      },
      "manualLaunchAllowed" : true,
      "monitorData" : {
        "hudson.node_monitors.SwapSpaceMonitor" : {
          "_class" : "hudson.node_monitors.SwapSpaceMonitor$MemoryUsage2",
          "availablePhysicalMemory" : 270237696,
          "availableSwapSpace" : 4160737280,
          "totalPhysicalMemory" : 3954294784,
          "totalSwapSpace" : 4160745472
        },
        "hudson.node_monitors.TemporarySpaceMonitor" : {
          "_class" : "hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace",
          "timestamp" : 1548569608435,
          "path" : "/tmp",
          "size" : 38455533568
        },
        "hudson.node_monitors.DiskSpaceMonitor" : {
          "_class" : "hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace",
          "timestamp" : 1548569607737,
          "path" : "/var/jenkins_home",
          "size" : 20490350592
        },
        "hudson.node_monitors.ArchitectureMonitor" : "Linux (amd64)",
        "hudson.node_monitors.ResponseTimeMonitor" : {
          "_class" : "hudson.node_monitors.ResponseTimeMonitor$Data",
          "timestamp" : 1548569607764,
          "average" : 0
        },
        "hudson.node_monitors.ClockMonitor" : {
          "_class" : "hudson.util.ClockDifference",
          "diff" : 0
        }
      },
      "numExecutors" : 1,
      "offline" : false,
      "offlineCause" : null,
      "offlineCauseReason" : "",
      "oneOffExecutors" : [
        
      ],
      "temporarilyOffline" : false
    },
    {
      "_class" : "hudson.slaves.SlaveComputer",
      "actions" : [
        
      ],
      "assignedLabels" : [
        {
          "name" : "Linux"
        },
        {
          "name" : "build-lnx-01"
        }
      ],
      "description" : "",
      "displayName" : "build-lnx-01",
      "executors" : [
        {
          
        }
      ],
      "icon" : "computer.png",
      "iconClassName" : "icon-computer",
      "idle" : true,
      "jnlpAgent" : true,
      "launchSupported" : false,
      "loadStatistics" : {
        "_class" : "hudson.model.Label$1"
      },
      "manualLaunchAllowed" : true,
      "monitorData" : {
        "hudson.node_monitors.SwapSpaceMonitor" : {
          "_class" : "hudson.node_monitors.SwapSpaceMonitor$MemoryUsage2",
          "availablePhysicalMemory" : 270155776,
          "availableSwapSpace" : 4160737280,
          "totalPhysicalMemory" : 3954294784,
          "totalSwapSpace" : 4160745472
        },
        "hudson.node_monitors.TemporarySpaceMonitor" : {
          "_class" : "hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace",
          "timestamp" : 1548569608449,
          "path" : "/tmp",
          "size" : 38455533568
        },
        "hudson.node_monitors.DiskSpaceMonitor" : {
          "_class" : "hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace",
          "timestamp" : 1548569607743,
          "path" : "/home/jenkins",
          "size" : 20490350592
        },
        "hudson.node_monitors.ArchitectureMonitor" : "Linux (amd64)",
        "hudson.node_monitors.ResponseTimeMonitor" : {
          "_class" : "hudson.node_monitors.ResponseTimeMonitor$Data",
          "timestamp" : 1548569607767,
          "average" : 10
        },
        "hudson.node_monitors.ClockMonitor" : {
          "_class" : "hudson.util.ClockDifference",
          "diff" : -2
        }
      },
      "numExecutors" : 1,
      "offline" : false,
      "offlineCause" : null,
      "offlineCauseReason" : "",
      "oneOffExecutors" : [
        
      ],
      "temporarilyOffline" : false,
      "absoluteRemotePath" : "/home/jenkins"
    },
    {
      "_class" : "hudson.slaves.SlaveComputer",
      "actions" : [
        
      ],
      "assignedLabels" : [
        {
          "name" : "Windows"
        },
        {
          "name" : "build-win-01"
        }
      ],
      "description" : "",
      "displayName" : "build-win-01",
      "executors" : [
        {
          
        }
      ],
      "icon" : "computer-x.png",
      "iconClassName" : "icon-computer-x",
      "idle" : true,
      "jnlpAgent" : true,
      "launchSupported" : false,
      "loadStatistics" : {
        "_class" : "hudson.model.Label$1"
      },
      "manualLaunchAllowed" : true,
      "monitorData" : {
        "hudson.node_monitors.SwapSpaceMonitor" : null,
        "hudson.node_monitors.TemporarySpaceMonitor" : null,
        "hudson.node_monitors.DiskSpaceMonitor" : null,
        "hudson.node_monitors.ArchitectureMonitor" : null,
        "hudson.node_monitors.ResponseTimeMonitor" : null,
        "hudson.node_monitors.ClockMonitor" : null
      },
      "numExecutors" : 1,
      "offline" : true,
      "offlineCause" : null,
      "offlineCauseReason" : "",
      "oneOffExecutors" : [
        
      ],
      "temporarilyOffline" : false,
      "absoluteRemotePath" : null
    }
  ],
  "displayName" : "Nodes",
  "totalExecutors" : 2
}

Let’s deserialize the data!

package com.hoge.jenkins.lib;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JenkinsServer extends Base implements IJenkinsServer
{
    public JenkinsServer()
    {

    }

    public ComputerCollection getComputers(String Url) throws IOException
    {
        var client = new OkHttpClient();
        var request = new Request.Builder()
                .url(Url)
                .get()
                .build();

        var call = client.newCall(request);
        var response = call.execute();

        var mapper = new ObjectMapper();
        return mapper.readValue(response.body().string(), ComputerCollection.class);
    }
}

The function getComputers uses OkHttp. I looked around for HTTP client component and I think OkHttp feels more modern than other components. It basically gets the JSON data via HTTP GET call and the last line of the function deserializes the data. Imagine having to parse the JSON data and write the code to convert the data to Java objects… This is a very clean way of consuming REST API data.

If you take a look at the deserialized objects in IntelliJ, it looks like the following image. The data is cleanly mapped to the usable objects in memory.

Evaluating the returned object

If you just try this yourself, you might get authentication error. I haven’t gone through how to include authentication header in the code. I will post another article how to add authentication code. I will be basically modifying the getComputers function in this post.

JSON to Java Class

There are cases where you have JSON data and you want to deal with it in Java. Having to hand code all the classes and their members that represent the data can be very time consuming and error-prone.

I found a very useful website where you can dump the data and gives you the classes. It’s called jsonschema2pojo. The author publishes its source code on GitHub.

For example, I have JSON data that comes back from Jenkins JSON API which lists all the slave nodes. You can get the data from a URL like http://jenkins.hoge.com/computer/api/json If you just copy the data and paste it into jsonschema2pojo, it generates Java classes. For my example, I changed the following settings.

If you click Preview button at the bottom of the screen, you will get Java classes that represent the JSON data. Can you imagine having hand code the following code… And you still have a lot to work on!

-----------------------------------com.hoge.jenkins.lib.AssignedLabel.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name"
})
public class AssignedLabel {

@JsonProperty("name")
private String name;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.Computer.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class",
"actions",
"assignedLabels",
"description",
"displayName",
"executors",
"icon",
"iconClassName",
"idle",
"jnlpAgent",
"launchSupported",
"loadStatistics",
"manualLaunchAllowed",
"monitorData",
"numExecutors",
"offline",
"offlineCause",
"offlineCauseReason",
"oneOffExecutors",
"temporarilyOffline",
"absoluteRemotePath"
})
public class Computer {

@JsonProperty("_class")
private String _class;
@JsonProperty("actions")
private List<Object> actions = null;
@JsonProperty("assignedLabels")
private List<AssignedLabel> assignedLabels = null;
@JsonProperty("description")
private String description;
@JsonProperty("displayName")
private String displayName;
@JsonProperty("executors")
private List<Executor> executors = null;
@JsonProperty("icon")
private String icon;
@JsonProperty("iconClassName")
private String iconClassName;
@JsonProperty("idle")
private Boolean idle;
@JsonProperty("jnlpAgent")
private Boolean jnlpAgent;
@JsonProperty("launchSupported")
private Boolean launchSupported;
@JsonProperty("loadStatistics")
private LoadStatistics loadStatistics;
@JsonProperty("manualLaunchAllowed")
private Boolean manualLaunchAllowed;
@JsonProperty("monitorData")
private MonitorData monitorData;
@JsonProperty("numExecutors")
private Integer numExecutors;
@JsonProperty("offline")
private Boolean offline;
@JsonProperty("offlineCause")
private OfflineCause offlineCause;
@JsonProperty("offlineCauseReason")
private String offlineCauseReason;
@JsonProperty("oneOffExecutors")
private List<Object> oneOffExecutors = null;
@JsonProperty("temporarilyOffline")
private Boolean temporarilyOffline;
@JsonProperty("absoluteRemotePath")
private Object absoluteRemotePath;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonProperty("actions")
public List<Object> getActions() {
return actions;
}

@JsonProperty("actions")
public void setActions(List<Object> actions) {
this.actions = actions;
}

@JsonProperty("assignedLabels")
public List<AssignedLabel> getAssignedLabels() {
return assignedLabels;
}

@JsonProperty("assignedLabels")
public void setAssignedLabels(List<AssignedLabel> assignedLabels) {
this.assignedLabels = assignedLabels;
}

@JsonProperty("description")
public String getDescription() {
return description;
}

@JsonProperty("description")
public void setDescription(String description) {
this.description = description;
}

@JsonProperty("displayName")
public String getDisplayName() {
return displayName;
}

@JsonProperty("displayName")
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

@JsonProperty("executors")
public List<Executor> getExecutors() {
return executors;
}

@JsonProperty("executors")
public void setExecutors(List<Executor> executors) {
this.executors = executors;
}

@JsonProperty("icon")
public String getIcon() {
return icon;
}

@JsonProperty("icon")
public void setIcon(String icon) {
this.icon = icon;
}

@JsonProperty("iconClassName")
public String getIconClassName() {
return iconClassName;
}

@JsonProperty("iconClassName")
public void setIconClassName(String iconClassName) {
this.iconClassName = iconClassName;
}

@JsonProperty("idle")
public Boolean getIdle() {
return idle;
}

@JsonProperty("idle")
public void setIdle(Boolean idle) {
this.idle = idle;
}

@JsonProperty("jnlpAgent")
public Boolean getJnlpAgent() {
return jnlpAgent;
}

@JsonProperty("jnlpAgent")
public void setJnlpAgent(Boolean jnlpAgent) {
this.jnlpAgent = jnlpAgent;
}

@JsonProperty("launchSupported")
public Boolean getLaunchSupported() {
return launchSupported;
}

@JsonProperty("launchSupported")
public void setLaunchSupported(Boolean launchSupported) {
this.launchSupported = launchSupported;
}

@JsonProperty("loadStatistics")
public LoadStatistics getLoadStatistics() {
return loadStatistics;
}

@JsonProperty("loadStatistics")
public void setLoadStatistics(LoadStatistics loadStatistics) {
this.loadStatistics = loadStatistics;
}

@JsonProperty("manualLaunchAllowed")
public Boolean getManualLaunchAllowed() {
return manualLaunchAllowed;
}

@JsonProperty("manualLaunchAllowed")
public void setManualLaunchAllowed(Boolean manualLaunchAllowed) {
this.manualLaunchAllowed = manualLaunchAllowed;
}

@JsonProperty("monitorData")
public MonitorData getMonitorData() {
return monitorData;
}

@JsonProperty("monitorData")
public void setMonitorData(MonitorData monitorData) {
this.monitorData = monitorData;
}

@JsonProperty("numExecutors")
public Integer getNumExecutors() {
return numExecutors;
}

@JsonProperty("numExecutors")
public void setNumExecutors(Integer numExecutors) {
this.numExecutors = numExecutors;
}

@JsonProperty("offline")
public Boolean getOffline() {
return offline;
}

@JsonProperty("offline")
public void setOffline(Boolean offline) {
this.offline = offline;
}

@JsonProperty("offlineCause")
public OfflineCause getOfflineCause() {
return offlineCause;
}

@JsonProperty("offlineCause")
public void setOfflineCause(OfflineCause offlineCause) {
this.offlineCause = offlineCause;
}

@JsonProperty("offlineCauseReason")
public String getOfflineCauseReason() {
return offlineCauseReason;
}

@JsonProperty("offlineCauseReason")
public void setOfflineCauseReason(String offlineCauseReason) {
this.offlineCauseReason = offlineCauseReason;
}

@JsonProperty("oneOffExecutors")
public List<Object> getOneOffExecutors() {
return oneOffExecutors;
}

@JsonProperty("oneOffExecutors")
public void setOneOffExecutors(List<Object> oneOffExecutors) {
this.oneOffExecutors = oneOffExecutors;
}

@JsonProperty("temporarilyOffline")
public Boolean getTemporarilyOffline() {
return temporarilyOffline;
}

@JsonProperty("temporarilyOffline")
public void setTemporarilyOffline(Boolean temporarilyOffline) {
this.temporarilyOffline = temporarilyOffline;
}

@JsonProperty("absoluteRemotePath")
public Object getAbsoluteRemotePath() {
return absoluteRemotePath;
}

@JsonProperty("absoluteRemotePath")
public void setAbsoluteRemotePath(Object absoluteRemotePath) {
this.absoluteRemotePath = absoluteRemotePath;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.ComputerCollection.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class",
"busyExecutors",
"computer",
"displayName",
"totalExecutors"
})
public class ComputerCollection {

@JsonProperty("_class")
private String _class;
@JsonProperty("busyExecutors")
private Integer busyExecutors;
@JsonProperty("computer")
private List<Computer> computer = null;
@JsonProperty("displayName")
private String displayName;
@JsonProperty("totalExecutors")
private Integer totalExecutors;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonProperty("busyExecutors")
public Integer getBusyExecutors() {
return busyExecutors;
}

@JsonProperty("busyExecutors")
public void setBusyExecutors(Integer busyExecutors) {
this.busyExecutors = busyExecutors;
}

@JsonProperty("computer")
public List<Computer> getComputer() {
return computer;
}

@JsonProperty("computer")
public void setComputer(List<Computer> computer) {
this.computer = computer;
}

@JsonProperty("displayName")
public String getDisplayName() {
return displayName;
}

@JsonProperty("displayName")
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

@JsonProperty("totalExecutors")
public Integer getTotalExecutors() {
return totalExecutors;
}

@JsonProperty("totalExecutors")
public void setTotalExecutors(Integer totalExecutors) {
this.totalExecutors = totalExecutors;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.Executor.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({

})
public class Executor {

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.LoadStatistics.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class"
})
public class LoadStatistics {

@JsonProperty("_class")
private String _class;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.MonitorData.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"hudson.node_monitors.SwapSpaceMonitor",
"hudson.node_monitors.TemporarySpaceMonitor",
"hudson.node_monitors.DiskSpaceMonitor",
"hudson.node_monitors.ArchitectureMonitor",
"hudson.node_monitors.ResponseTimeMonitor",
"hudson.node_monitors.ClockMonitor"
})
public class MonitorData {

@JsonProperty("hudson.node_monitors.SwapSpaceMonitor")
private Object hudsonNodeMonitorsSwapSpaceMonitor;
@JsonProperty("hudson.node_monitors.TemporarySpaceMonitor")
private Object hudsonNodeMonitorsTemporarySpaceMonitor;
@JsonProperty("hudson.node_monitors.DiskSpaceMonitor")
private Object hudsonNodeMonitorsDiskSpaceMonitor;
@JsonProperty("hudson.node_monitors.ArchitectureMonitor")
private Object hudsonNodeMonitorsArchitectureMonitor;
@JsonProperty("hudson.node_monitors.ResponseTimeMonitor")
private Object hudsonNodeMonitorsResponseTimeMonitor;
@JsonProperty("hudson.node_monitors.ClockMonitor")
private Object hudsonNodeMonitorsClockMonitor;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("hudson.node_monitors.SwapSpaceMonitor")
public Object getHudsonNodeMonitorsSwapSpaceMonitor() {
return hudsonNodeMonitorsSwapSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.SwapSpaceMonitor")
public void setHudsonNodeMonitorsSwapSpaceMonitor(Object hudsonNodeMonitorsSwapSpaceMonitor) {
this.hudsonNodeMonitorsSwapSpaceMonitor = hudsonNodeMonitorsSwapSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.TemporarySpaceMonitor")
public Object getHudsonNodeMonitorsTemporarySpaceMonitor() {
return hudsonNodeMonitorsTemporarySpaceMonitor;
}

@JsonProperty("hudson.node_monitors.TemporarySpaceMonitor")
public void setHudsonNodeMonitorsTemporarySpaceMonitor(Object hudsonNodeMonitorsTemporarySpaceMonitor) {
this.hudsonNodeMonitorsTemporarySpaceMonitor = hudsonNodeMonitorsTemporarySpaceMonitor;
}

@JsonProperty("hudson.node_monitors.DiskSpaceMonitor")
public Object getHudsonNodeMonitorsDiskSpaceMonitor() {
return hudsonNodeMonitorsDiskSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.DiskSpaceMonitor")
public void setHudsonNodeMonitorsDiskSpaceMonitor(Object hudsonNodeMonitorsDiskSpaceMonitor) {
this.hudsonNodeMonitorsDiskSpaceMonitor = hudsonNodeMonitorsDiskSpaceMonitor;
}

@JsonProperty("hudson.node_monitors.ArchitectureMonitor")
public Object getHudsonNodeMonitorsArchitectureMonitor() {
return hudsonNodeMonitorsArchitectureMonitor;
}

@JsonProperty("hudson.node_monitors.ArchitectureMonitor")
public void setHudsonNodeMonitorsArchitectureMonitor(Object hudsonNodeMonitorsArchitectureMonitor) {
this.hudsonNodeMonitorsArchitectureMonitor = hudsonNodeMonitorsArchitectureMonitor;
}

@JsonProperty("hudson.node_monitors.ResponseTimeMonitor")
public Object getHudsonNodeMonitorsResponseTimeMonitor() {
return hudsonNodeMonitorsResponseTimeMonitor;
}

@JsonProperty("hudson.node_monitors.ResponseTimeMonitor")
public void setHudsonNodeMonitorsResponseTimeMonitor(Object hudsonNodeMonitorsResponseTimeMonitor) {
this.hudsonNodeMonitorsResponseTimeMonitor = hudsonNodeMonitorsResponseTimeMonitor;
}

@JsonProperty("hudson.node_monitors.ClockMonitor")
public Object getHudsonNodeMonitorsClockMonitor() {
return hudsonNodeMonitorsClockMonitor;
}

@JsonProperty("hudson.node_monitors.ClockMonitor")
public void setHudsonNodeMonitorsClockMonitor(Object hudsonNodeMonitorsClockMonitor) {
this.hudsonNodeMonitorsClockMonitor = hudsonNodeMonitorsClockMonitor;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.hoge.jenkins.lib.OfflineCause.java-----------------------------------

package com.hoge.jenkins.lib;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_class"
})
public class OfflineCause {

@JsonProperty("_class")
private String _class;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("_class")
public String getClass_() {
return _class;
}

@JsonProperty("_class")
public void setClass_(String _class) {
this._class = _class;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

I will write how you can use the generated classes with the JSON data in my later blog posts.

UnitTestBase

I’m developing an app and I know I will write a good number of test cases in JUnit. I decided to create a base class for all unit test classes. It looks like the following.

import org.junit.Test;
import java.io.IOException;

public class JenkinsServerTest extends UnitTestBase
{
    public JenkinsServerTest() throws IOException {
        super();
    }

    @Test
    public void test_ReadJenkinsBaseUrl()
    {
        assertEquals("http://jenkins.hoge.com", Settings.getProperty("JenkinsBaseUrl"));
    }
}

It basically exposes Properties (Settings) object to the child classes that extends this UnitTestBase. This way I won’t have to write the code to read the settings from the properties file. The code below shows a test class that uses the exposed Settings from the base class.

import org.junit.Test;
import java.io.IOException;

public class JenkinsServerTest extends UnitTestBase
{
    public JenkinsServerTest() throws IOException {
        super();
    }

    @Test
    public void test_ReadJenkinsBaseUrl()
    {
        assertEquals("http://jenkins.hoge.com", Settings.getProperty("JenkinsBaseUrl"));
    }
}

JetBrains

If you are a developer, you have heard of the name. I’m a big fan of JetBrains. I was a C# developer and ReSharper has been a really good investment and it has made me a better C# developer.

Now that I’m with a company that uses Java and I have had opportunities to develop some Java application, I gave IntellJ IDEA a try and it has been a pleasure. I cannot live without the IDE anymore.

One day about a month ago, I got an email saying that I get to buy the license to use all JetBrains’ IDE products for $149 annually so I wasted no time to purchase the license. I just checked their web site and all products pack is at $649. How did $149 happen? I had been a ReSharper customer for a few years so I guess JetBrains gave me a good deal?

If you are serious about software development, JetBrains products are totally worth your money. I enjoy using their products everyday.