Skip to content

Commit

Permalink
fix dep and FindBug issues
Browse files Browse the repository at this point in the history
  • Loading branch information
akostadinov committed Dec 21, 2017
1 parent 9c57498 commit f69b2de
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.1.0</version>
<version>2.13.0</version>
<type>jar</type>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

@CheckForNull
public SecureGroovyScript getSecureGroovyScript() {
return secureGroovyScript;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package jenkins.plugins.logstash.persistence;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -59,17 +59,14 @@ abstract class AbstractLogstashIndexerDao implements LogstashIndexerDao {
}

@Override
@SuppressFBWarnings(
value="STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE",
justification="TODO: not sure how to fix this")
public JSONObject buildPayload(BuildData buildData, String jenkinsUrl, List<String> logLines) {
JSONObject payload = new JSONObject();
payload.put("data", buildData.toJson());
payload.put("message", logLines);
payload.put("source", "jenkins");
payload.put("source_host", jenkinsUrl);
payload.put("@buildTimestamp", buildData.getTimestamp());
payload.put("@timestamp", BuildData.DATE_FORMATTER.format(Calendar.getInstance().getTime()));
payload.put("@timestamp", BuildData.formatDateIso(new Date()));
payload.put("@version", 1);

return payload;
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/jenkins/plugins/logstash/persistence/BuildData.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import net.sf.json.JSONObject;
import javax.annotation.CheckForNull;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -66,11 +68,7 @@
* @since 1.0.0
*/
public class BuildData {
// ISO 8601 date format
@SuppressFBWarnings(
value="STCAL_STATIC_SIMPLE_DATE_FORMAT_INSTANCE",
justification="TODO: not sure how to fix this")
public transient static final DateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
private transient static final FastDateFormat DATE_FORMATTER = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZ");
private final static Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());
public static class TestData {
int totalCount, skipCount, failCount, passCount;
Expand Down Expand Up @@ -121,8 +119,8 @@ public TestData(Action action) {
}
}

@CheckForNull protected String result;
protected String id;
protected String result;
protected String projectName;
protected String fullProjectName;
protected String displayName;
Expand Down Expand Up @@ -215,11 +213,14 @@ public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener) {
}
}

@SuppressFBWarnings(
value={"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE","STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE"},
justification="TODO: not sure how to fix this")
// ISO 8601 date format
public static String formatDateIso(Date date) {
return DATE_FORMATTER.format(date);
}

private void initData(Run<?, ?> build, Date currentTime) {
result = build.getResult() == null ? null : build.getResult().toString();
Result buildResult = build.getResult();
result = buildResult == null ? null : buildResult.toString();
id = build.getId();
projectName = build.getParent().getName();
fullProjectName = build.getParent().getFullName();
Expand All @@ -235,7 +236,7 @@ private void initData(Run<?, ?> build, Date currentTime) {
}

buildDuration = currentTime.getTime() - build.getStartTimeInMillis();
timestamp = DATE_FORMATTER.format(build.getTimestamp().getTime());
timestamp = formatDateIso(build.getTime());
}

@Override
Expand Down Expand Up @@ -349,11 +350,8 @@ public String getTimestamp() {
return timestamp;
}

@SuppressFBWarnings(
value="STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE",
justification="TODO: not sure how to fix this")
public void setTimestamp(Calendar timestamp) {
this.timestamp = DATE_FORMATTER.format(timestamp.getTime());
this.timestamp = formatDateIso(timestamp.getTime());
}

public String getRootProjectName() {
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.List;

import static org.hamcrest.core.StringContains.containsString;
Expand Down Expand Up @@ -104,15 +104,13 @@ public void before() throws Exception {
when(mockBuild.getParent()).thenReturn(mockProject);
when(mockBuild.getBuiltOn()).thenReturn(null);
when(mockBuild.getNumber()).thenReturn(123456);
when(mockBuild.getTimestamp()).thenReturn(new GregorianCalendar());
when(mockBuild.getTime()).thenReturn(new Date());
when(mockBuild.getRootBuild()).thenReturn(mockBuild);
when(mockBuild.getBuildVariables()).thenReturn(Collections.emptyMap());
when(mockBuild.getSensitiveBuildVariables()).thenReturn(Collections.emptySet());
when(mockBuild.getEnvironments()).thenReturn(null);
when(mockBuild.getAction(AbstractTestResultAction.class)).thenReturn(mockTestResultAction);
when(mockBuild.getLog(0)).thenReturn(Arrays.asList());
when(mockBuild.getLog(3)).thenReturn(Arrays.asList("line 1", "line 2", "line 3", "Log truncated..."));
when(mockBuild.getLog(Integer.MAX_VALUE)).thenReturn(Arrays.asList("line 1", "line 2", "line 3", "line 4"));
when(mockBuild.getEnvironment(null)).thenReturn(new EnvVars());

when(mockTestResultAction.getTotalCount()).thenReturn(0);
Expand Down Expand Up @@ -158,7 +156,7 @@ public void constructorSuccess() throws Exception {
// Verify that the BuildData constructor is what is being called here.
// This also lets us verify that in the instantiation failure cases we do not construct BuildData.
verify(mockBuild).getId();
verify(mockBuild, times(2)).getResult();
verify(mockBuild, times(1)).getResult();
verify(mockBuild, times(2)).getParent();
verify(mockBuild, times(2)).getProject();
verify(mockBuild, times(1)).getStartTimeInMillis();
Expand All @@ -169,7 +167,7 @@ public void constructorSuccess() throws Exception {
verify(mockBuild).getAction(AbstractTestResultAction.class);
verify(mockBuild).getBuiltOn();
verify(mockBuild, times(2)).getNumber();
verify(mockBuild).getTimestamp();
verify(mockBuild).getTime();
verify(mockBuild, times(4)).getRootBuild();
verify(mockBuild).getBuildVariables();
verify(mockBuild).getSensitiveBuildVariables();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -69,10 +68,9 @@ public void before() throws Exception {
when(mockBuild.getDisplayName()).thenReturn("BuildData Test");
when(mockBuild.getFullDisplayName()).thenReturn("BuildData Test #123456");
when(mockBuild.getDescription()).thenReturn("Mock project for testing BuildData");
when(mockBuild.getProject()).thenReturn(mockProject);
when(mockBuild.getParent()).thenReturn(mockProject);
when(mockBuild.getNumber()).thenReturn(123456);
when(mockBuild.getTimestamp()).thenReturn(new GregorianCalendar());
when(mockBuild.getTime()).thenReturn(new Date());
when(mockBuild.getRootBuild()).thenReturn(mockBuild);
when(mockBuild.getBuildVariables()).thenReturn(Collections.emptyMap());
when(mockBuild.getSensitiveBuildVariables()).thenReturn(Collections.emptySet());
Expand Down Expand Up @@ -116,7 +114,7 @@ private void verifyMocks() throws Exception
verify(mockProject).getFullName();

verify(mockBuild).getId();
verify(mockBuild, times(2)).getResult();
verify(mockBuild, times(1)).getResult();
verify(mockBuild, times(2)).getParent();
verify(mockBuild).getDisplayName();
verify(mockBuild).getFullDisplayName();
Expand All @@ -126,7 +124,7 @@ private void verifyMocks() throws Exception
verify(mockBuild).getAction(AbstractTestResultAction.class);
verify(mockBuild).getBuiltOn();
verify(mockBuild).getNumber();
verify(mockBuild).getTimestamp();
verify(mockBuild).getTime();
verify(mockBuild, times(4)).getRootBuild();
verify(mockBuild).getBuildVariables();
verify(mockBuild).getSensitiveBuildVariables();
Expand Down

0 comments on commit f69b2de

Please sign in to comment.