Skip to content

Commit 0afc7b2

Browse files
committed
Fix warnings from use of older code style
1 parent 105d782 commit 0afc7b2

File tree

20 files changed

+127
-113
lines changed

20 files changed

+127
-113
lines changed

build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plugins {
2020

2121
apply from: 'plugin-helpers.gradle'
2222

23-
project.ext.pluginVersion = '1.7.2'
23+
project.ext.pluginVersion = '1.8.0'
2424
project.ext.fullVersion = project.git.distVersion() ? "${project.pluginVersion}-${project.git.distVersion()}" : project.pluginVersion
2525

2626
project.ext.pluginDesc = [
@@ -64,14 +64,13 @@ subprojects { Project sub ->
6464
dependencies {
6565
compileOnly project.deps.gocdPluginApi
6666
implementation 'com.google.code.gson:gson:2.10.1'
67-
implementation 'commons-io:commons-io:2.16.1'
6867
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
68+
6969
constraints {
7070
implementation('commons-codec:commons-codec:1.16.1') {
7171
because 'Transitive dependency of apache httpclient has reported vulnerabilities'
7272
}
7373
}
74-
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0'
7574

7675
testImplementation project.deps.gocdPluginApi
7776
testImplementation 'org.mockito:mockito-core:5.11.0'

common/src/main/java/com/tw/go/plugin/BuildStatusNotifierPlugin.java

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@
2828
import com.tw.go.plugin.provider.Provider;
2929
import com.tw.go.plugin.setting.PluginSettings;
3030
import com.tw.go.plugin.util.JSONUtils;
31-
import org.apache.commons.io.IOUtils;
32-
import org.apache.commons.lang3.StringUtils;
31+
import com.tw.go.plugin.util.ValidationUtils;
3332

3433
import java.io.IOException;
34+
import java.io.InputStream;
35+
import java.nio.charset.StandardCharsets;
3536
import java.util.*;
3637

37-
import static java.util.Arrays.asList;
38-
3938
public abstract class BuildStatusNotifierPlugin implements GoPlugin {
40-
private static Logger LOGGER = Logger.getLoggerFor(BuildStatusNotifierPlugin.class);
39+
private static final Logger LOGGER = Logger.getLoggerFor(BuildStatusNotifierPlugin.class);
4140

4241
public static final String EXTENSION_NAME = "notification";
43-
public static final List<String> goSupportedVersions = asList("1.0");
42+
public static final List<String> goSupportedVersions = List.of("1.0");
4443

4544
public static final String PLUGIN_SETTINGS_GET_CONFIGURATION = "go.plugin-settings.get-configuration";
4645
public static final String PLUGIN_SETTINGS_GET_VIEW = "go.plugin-settings.get-view";
@@ -75,20 +74,21 @@ public void initializeGoApplicationAccessor(GoApplicationAccessor goApplicationA
7574
@Override
7675
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) {
7776
String requestName = goPluginApiRequest.requestName();
78-
if (requestName.equals(PLUGIN_SETTINGS_GET_CONFIGURATION)) {
79-
return handleGetPluginSettingsConfiguration();
80-
} else if (requestName.equals(PLUGIN_SETTINGS_GET_VIEW)) {
81-
try {
82-
return handleGetPluginSettingsView();
83-
} catch (IOException e) {
84-
return renderJSON(500, String.format("Failed to find template: %s", e.getMessage()));
85-
}
86-
} else if (requestName.equals(PLUGIN_SETTINGS_VALIDATE_CONFIGURATION)) {
87-
return handleValidatePluginSettingsConfiguration(goPluginApiRequest);
88-
} else if (requestName.equals(REQUEST_NOTIFICATIONS_INTERESTED_IN)) {
89-
return handleNotificationsInterestedIn();
90-
} else if (requestName.equals(REQUEST_STAGE_STATUS)) {
91-
return handleStageNotification(goPluginApiRequest);
77+
switch (requestName) {
78+
case PLUGIN_SETTINGS_GET_CONFIGURATION:
79+
return handleGetPluginSettingsConfiguration();
80+
case PLUGIN_SETTINGS_GET_VIEW:
81+
try {
82+
return handleGetPluginSettingsView();
83+
} catch (IOException e) {
84+
return renderJSON(500, String.format("Failed to find template: %s", e.getMessage()));
85+
}
86+
case PLUGIN_SETTINGS_VALIDATE_CONFIGURATION:
87+
return handleValidatePluginSettingsConfiguration(goPluginApiRequest);
88+
case REQUEST_NOTIFICATIONS_INTERESTED_IN:
89+
return handleNotificationsInterestedIn();
90+
case REQUEST_STAGE_STATUS:
91+
return handleStageNotification(goPluginApiRequest);
9292
}
9393
return renderJSON(NOT_FOUND_RESPONSE_CODE, null);
9494
}
@@ -103,67 +103,72 @@ private GoPluginApiResponse handleGetPluginSettingsConfiguration() {
103103
}
104104

105105
private GoPluginApiResponse handleGetPluginSettingsView() throws IOException {
106-
Map<String, Object> response = new HashMap<String, Object>();
106+
Map<String, Object> response = new HashMap<>();
107107

108-
response.put("template", IOUtils.toString(getClass().getResourceAsStream("/" + provider.configurationView().templateName()), "UTF-8"));
109-
return renderJSON(SUCCESS_RESPONSE_CODE, response);
108+
try (InputStream is = Objects.requireNonNull(getClass().getResourceAsStream("/" + provider.configurationView().templateName()))) {
109+
response.put("template", new String(is.readAllBytes(), StandardCharsets.UTF_8));
110+
return renderJSON(SUCCESS_RESPONSE_CODE, response);
111+
}
110112
}
111113

114+
@SuppressWarnings("unchecked")
112115
private GoPluginApiResponse handleValidatePluginSettingsConfiguration(GoPluginApiRequest goPluginApiRequest) {
113116
Map<String, Object> fields = (Map<String, Object>) JSONUtils.fromJSON(goPluginApiRequest.requestBody());
114117
List<Map<String, Object>> response = provider.validateConfig((Map<String, Object>) fields.get("plugin-settings"));
115118
return renderJSON(SUCCESS_RESPONSE_CODE, response);
116119
}
117120

118121

122+
@SuppressWarnings("unchecked")
119123
public PluginSettings getPluginSettings() {
120-
Map<String, Object> requestMap = new HashMap<String, Object>();
124+
Map<String, Object> requestMap = new HashMap<>();
121125
requestMap.put("plugin-id", provider.pluginId());
122126
GoApiResponse response = goApplicationAccessor.submit(createGoApiRequest(GET_PLUGIN_SETTINGS, JSONUtils.toJSON(requestMap)));
123-
Map<String, String> responseBodyMap = response.responseBody() == null ? new HashMap<String, String>() : (Map<String, String>) JSONUtils.fromJSON(response.responseBody());
127+
Map<String, String> responseBodyMap = response.responseBody() == null ? new HashMap<>() : (Map<String, String>) JSONUtils.fromJSON(response.responseBody());
124128
return provider.pluginSettings(responseBodyMap);
125129
}
126130

127131
GoPluginApiResponse handleNotificationsInterestedIn() {
128-
Map<String, Object> response = new HashMap<String, Object>();
129-
response.put("notifications", Arrays.asList(REQUEST_STAGE_STATUS));
132+
Map<String, Object> response = new HashMap<>();
133+
response.put("notifications", List.of(REQUEST_STAGE_STATUS));
130134
return renderJSON(SUCCESS_RESPONSE_CODE, response);
131135
}
132136

137+
@SuppressWarnings("unchecked")
133138
GoPluginApiResponse handleStageNotification(GoPluginApiRequest goPluginApiRequest) {
134139
Map<String, Object> dataMap = (Map<String, Object>) JSONUtils.fromJSON(goPluginApiRequest.requestBody());
135140

136141
int responseCode = SUCCESS_RESPONSE_CODE;
137-
Map<String, Object> response = new HashMap<String, Object>();
138-
List<String> messages = new ArrayList<String>();
142+
Map<String, Object> response = new HashMap<>();
143+
List<String> messages = new ArrayList<>();
139144
try {
140145
PluginSettings pluginSettings = getPluginSettings();
141146
String serverBaseURLToUse = pluginSettings.getServerBaseURL();
142-
if (StringUtils.isEmpty(serverBaseURLToUse)) {
147+
if (ValidationUtils.isEmpty(serverBaseURLToUse)) {
143148
serverBaseURLToUse = System.getProperty("go.plugin.build.status.go-server", "http://localhost:8153");
144149
}
145150

146-
Map pipeline = (Map) dataMap.get("pipeline");
147-
Map stage = (Map) pipeline.get("stage");
151+
Map<String, Object> pipeline = (Map<String, Object>) dataMap.get("pipeline");
152+
Map<String, Object> stage = (Map<String, Object>) pipeline.get("stage");
148153

149154
String pipelineStage = String.format("%s/%s", pipeline.get("name"), stage.get("name"));
150155
String pipelineInstance = String.format("%s/%s/%s/%s", pipeline.get("name"), pipeline.get("counter"), stage.get("name"), stage.get("counter"));
151156
String trackbackURL = String.format("%s/go/pipelines/%s", serverBaseURLToUse, pipelineInstance);
152157
String result = (String) stage.get("result");
153158

154-
List<Map> materialRevisions = (List<Map>) pipeline.get("build-cause");
155-
for (Map materialRevision : materialRevisions) {
156-
Map material = (Map) materialRevision.get("material");
159+
List<Map<String, Object>> materialRevisions = (List<Map<String, Object>>) pipeline.get("build-cause");
160+
for (Map<String, Object> materialRevision : materialRevisions) {
161+
Map<String, Object> material = (Map<String, Object>) materialRevision.get("material");
157162
if (isMaterialFromTypes(material, provider.pollerPluginIds())) {
158-
Map materialConfiguration = (Map) material.get("scm-configuration");
163+
Map<String, Object> materialConfiguration = (Map<String, Object>) material.get("scm-configuration");
159164
String url = (String) materialConfiguration.get("url");
160165

161-
List<Map> modifications = (List<Map>) materialRevision.get("modifications");
166+
List<Map<String, Object>> modifications = (List<Map<String, Object>>) materialRevision.get("modifications");
162167
String revision = (String) modifications.get(0).get("revision");
163-
Map modificationData = (Map) modifications.get(0).get("data");
168+
Map<String, Object> modificationData = (Map<String, Object>) modifications.get(0).get("data");
164169
String prBranch = (String) modificationData.getOrDefault("PR_BRANCH", modificationData.get("PR_ID"));
165170

166-
if (StringUtils.isEmpty(prBranch)) {
171+
if (ValidationUtils.isEmpty(prBranch)) {
167172
prBranch = (String) modificationData.get("CURRENT_BRANCH");
168173
}
169174

@@ -188,8 +193,8 @@ GoPluginApiResponse handleStageNotification(GoPluginApiRequest goPluginApiReques
188193
return renderJSON(responseCode, response);
189194
}
190195

191-
private boolean isMaterialFromTypes(Map material, List<String> pollerPluginIds) {
192-
if (((String) material.get("type")).equalsIgnoreCase("scm")) {
196+
private boolean isMaterialFromTypes(Map<String, Object> material, List<String> pollerPluginIds) {
197+
if ("scm".equals(material.get("type"))) {
193198
return pollerPluginIds.contains((String) material.get("plugin-id"));
194199
}
195200

common/src/main/java/com/tw/go/plugin/provider/DefaultProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public abstract class DefaultProvider implements Provider {
2828

29-
private PluginConfigurationView pluginConfigurationView;
29+
private final PluginConfigurationView pluginConfigurationView;
3030

3131
public DefaultProvider(PluginConfigurationView pluginConfigurationView) {
3232
this.pluginConfigurationView = pluginConfigurationView;

common/src/main/java/com/tw/go/plugin/setting/DefaultPluginConfigurationView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String templateName() {
3636

3737
@Override
3838
public Map<String, Object> fields() {
39-
Map<String, Object> response = new HashMap<String, Object>();
39+
Map<String, Object> response = new HashMap<>();
4040
response.put(PLUGIN_SETTINGS_SERVER_BASE_URL, createField("Server Base URL", null, true, false, "0"));
4141
response.put(PLUGIN_SETTINGS_END_POINT, createField("End Point", null, true, false, "1"));
4242
response.put(PLUGIN_SETTINGS_USERNAME, createField("Username", null, true, false, "2"));

common/src/main/java/com/tw/go/plugin/util/ConfigurationUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public class ConfigurationUtils {
2323

2424
public static Map<String, Object> createField(String displayName, String defaultValue, boolean isRequired, boolean isSecure, String displayOrder) {
25-
Map<String, Object> fieldProperties = new HashMap<String, Object>();
25+
Map<String, Object> fieldProperties = new HashMap<>();
2626
fieldProperties.put("display-name", displayName);
2727
fieldProperties.put("default-value", defaultValue);
2828
fieldProperties.put("required", isRequired);

common/src/main/java/com/tw/go/plugin/util/ValidationUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121

2222
public class ValidationUtils {
2323

24+
public static boolean isEmpty(String value) {
25+
return value == null || value.isEmpty();
26+
}
2427
public static Map<String, Object> getValidationError(String fieldName, String message) {
25-
Map<String, Object> validationError = new HashMap<String, Object>();
28+
Map<String, Object> validationError = new HashMap<>();
2629
validationError.put("key", fieldName);
2730
validationError.put("message", message);
2831
return validationError;

common/src/test/java/com/tw/go/plugin/provider/DefaultProviderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
2323

24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -45,7 +46,7 @@ public String pluginId() {
4546

4647
@Override
4748
public List<String> pollerPluginIds() {
48-
return null;
49+
return Collections.emptyList();
4950
}
5051

5152
@Override
@@ -67,7 +68,7 @@ public void setUp() {
6768

6869
@Test
6970
public void shouldReturnSettingsObject() {
70-
Map<String, String> responseBodyMap = new HashMap<String, String>();
71+
Map<String, String> responseBodyMap = new HashMap<>();
7172

7273
responseBodyMap.put(PLUGIN_SETTINGS_SERVER_BASE_URL, "url");
7374
responseBodyMap.put(PLUGIN_SETTINGS_END_POINT, "endpoint");

gerrit-cs-status/src/main/java/com/tw/go/plugin/provider/GerritConfigurationView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public String templateName() {
3535

3636
@Override
3737
public Map<String, Object> fields() {
38-
Map<String, Object> response = new HashMap<String, Object>();
38+
Map<String, Object> response = new HashMap<>();
3939
response.put(PLUGIN_SETTINGS_SERVER_BASE_URL, createField("Server Base URL", null, true, false, "0"));
4040
response.put(PLUGIN_SETTINGS_END_POINT, createField("End Point", null, true, false, "1"));
4141
response.put(PLUGIN_SETTINGS_USERNAME, createField("Username", null, true, false, "2"));

gerrit-cs-status/src/main/java/com/tw/go/plugin/provider/GerritProvider.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.tw.go.plugin.util.AuthenticationType;
2323
import com.tw.go.plugin.util.HTTPClient;
2424
import com.tw.go.plugin.util.JSONUtils;
25-
import org.apache.commons.lang3.StringUtils;
25+
import com.tw.go.plugin.util.ValidationUtils;
2626

2727
import java.util.ArrayList;
2828
import java.util.HashMap;
@@ -31,7 +31,6 @@
3131

3232
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.*;
3333
import static com.tw.go.plugin.util.ValidationUtils.getValidationError;
34-
import static java.util.Collections.singletonList;
3534

3635
public class GerritProvider extends DefaultProvider {
3736

@@ -61,7 +60,7 @@ public String pluginId() {
6160

6261
@Override
6362
public List<String> pollerPluginIds() {
64-
return singletonList(GERRIT_CS_POLLER_PLUGIN_ID);
63+
return List.of(GERRIT_CS_POLLER_PLUGIN_ID);
6564
}
6665

6766
@Override
@@ -74,26 +73,26 @@ public void updateStatus(String url, PluginSettings pluginSettings, String branc
7473
String passwordToUse = settings.getPassword();
7574
String codeReviewLabel = settings.getReviewLabel();
7675

77-
if (StringUtils.isEmpty(endPointToUse)) {
76+
if (ValidationUtils.isEmpty(endPointToUse)) {
7877
endPointToUse = System.getProperty("go.plugin.build.status.gerrit.endpoint");
7978
}
80-
if (StringUtils.isEmpty(usernameToUse)) {
79+
if (ValidationUtils.isEmpty(usernameToUse)) {
8180
usernameToUse = System.getProperty("go.plugin.build.status.gerrit.username");
8281
}
83-
if (StringUtils.isEmpty(passwordToUse)) {
82+
if (ValidationUtils.isEmpty(passwordToUse)) {
8483
passwordToUse = System.getProperty("go.plugin.build.status.gerrit.password");
8584
}
86-
if (StringUtils.isEmpty(codeReviewLabel)) {
85+
if (ValidationUtils.isEmpty(codeReviewLabel)) {
8786
codeReviewLabel = System.getProperty("go.plugin.build.status.gerrit.codeReviewLabel");
8887
}
8988

9089
String commitDetailsURL = String.format("%s/a/changes/?q=commit:%s", endPointToUse, revision);
9190
String commitDetailsResponse = httpClient.getRequest(commitDetailsURL, AuthenticationType.DIGEST, usernameToUse, passwordToUse);
9291
CommitDetails commitDetails = new ResponseParser().parseCommitDetails(commitDetailsResponse);
9392

94-
Map<String, Object> request = new HashMap<String, Object>();
93+
Map<String, Object> request = new HashMap<>();
9594
request.put("message", String.format("%s: %s", pipelineInstance, trackbackURL));
96-
Map<String, Object> labels = new HashMap<String, Object>();
95+
Map<String, Object> labels = new HashMap<>();
9796
request.put("labels", labels);
9897
labels.put(codeReviewLabel, getCodeReviewValue(result));
9998
String updateStatusURL = String.format("%s/a/changes/%s/revisions/%s/review", endPointToUse, commitDetails.getId(), revision);
@@ -102,7 +101,7 @@ public void updateStatus(String url, PluginSettings pluginSettings, String branc
102101

103102
@Override
104103
public List<Map<String, Object>> validateConfig(Map<String, Object> fields) {
105-
List<Map<String, Object>> response = new ArrayList<Map<String, Object>>();
104+
List<Map<String, Object>> response = new ArrayList<>();
106105
if (!fields.containsKey(GerritConfigurationView.PLUGIN_SETTINGS_REVIEW_LABEL)) {
107106
response.add(getValidationError(
108107
GerritConfigurationView.PLUGIN_SETTINGS_REVIEW_LABEL,

gerrit-cs-status/src/test/java/com/tw/go/plugin/GerritBuildStatusNotifierPluginTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Map;
3737

3838
import static com.tw.go.plugin.BuildStatusNotifierPlugin.PLUGIN_SETTINGS_GET_CONFIGURATION;
39-
import static java.util.Collections.singletonList;
4039
import static org.assertj.core.api.Assertions.assertThat;
4140
import static org.mockito.Mockito.*;
4241
import static org.mockito.MockitoAnnotations.openMocks;
@@ -61,7 +60,7 @@ public void setUp() {
6160
pluginSettingsResponse.setResponseBody(JSONUtils.toJSON(new HashMap<String, String>()));
6261
when(goApplicationAccessor.submit(any(GoApiRequest.class))).thenReturn(pluginSettingsResponse);
6362
when(provider.pluginId()).thenReturn(PLUGIN_ID);
64-
when(provider.pollerPluginIds()).thenReturn(singletonList(POLLER_PLUGIN_ID));
63+
when(provider.pollerPluginIds()).thenReturn(List.of(POLLER_PLUGIN_ID));
6564

6665
plugin.initializeGoApplicationAccessor(goApplicationAccessor);
6766
plugin.setProvider(provider);
@@ -100,7 +99,7 @@ public void shouldReturnPluginSettings() throws Exception {
10099
Provider mockProvider = mock(Provider.class);
101100
PluginConfigurationView mockConfigView = mock(PluginConfigurationView.class);
102101
when(mockProvider.configurationView()).thenReturn(mockConfigView);
103-
Map<String, Object> fields = new HashMap<String, Object>();
102+
Map<String, Object> fields = new HashMap<>();
104103
when(mockConfigView.fields()).thenReturn(fields);
105104

106105
plugin.setProvider(mockProvider);

0 commit comments

Comments
 (0)