Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit 3036081

Browse files
committedJul 27, 2023
Simplify validation by reducing map layer
1 parent 70c1c22 commit 3036081

File tree

2 files changed

+78
-32
lines changed

2 files changed

+78
-32
lines changed
 

‎src/main/java/com/ionos/go/plugin/notifier/ValidateConfigurationHandler.java

+34-32
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.stream.Collectors;
2324

2425
import static com.ionos.go.plugin.notifier.util.JsonUtil.fromJsonString;
2526
import static com.ionos.go.plugin.notifier.util.JsonUtil.toJsonString;
@@ -42,7 +43,6 @@ class ValidateConfigurationHandler implements GoPluginApiRequestHandler {
4243
private List<StageStatusRequest> newSampleStageStatusRequests() throws IOException {
4344
List<StageStatusRequest> response = new ArrayList<>();
4445

45-
Gson gson = new Gson();
4646
StageStatusRequest success = JsonUtil.fromJsonString(
4747
Helper.readResource("/sampleSuccess.json"),
4848
StageStatusRequest.class);
@@ -56,45 +56,49 @@ private List<StageStatusRequest> newSampleStageStatusRequests() throws IOExcepti
5656
return response;
5757
}
5858

59+
/** Convert the settings to a flat map.
60+
* @param settingsWithValue settings in GoCD format.
61+
* @return a flat map of settings-key to settings-value.
62+
* */
63+
static Map<String, String> toFlatSettings(Map<String, Map<String, String>> settingsWithValue) {
64+
return settingsWithValue.entrySet()
65+
.stream()
66+
.filter(e -> e.getValue().containsKey(Constants.FIELD_VALUE))
67+
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().get(Constants.FIELD_VALUE)));
68+
}
69+
5970
@Override
6071
public DefaultGoPluginApiResponse handle(@NonNull GoPluginApiRequest request) {
6172
LOGGER.info("Request: " + request.requestBody());
6273
ValidateConfigurationRequest validateRequest = fromJsonString(request.requestBody(), ValidateConfigurationRequest.class);
74+
75+
Map<String, String> flatSettings = toFlatSettings(validateRequest.getPluginSettings());
6376
List<ValidateConfigurationResponse> response = new ArrayList<>();
6477

6578
if (validateRequest.getPluginSettings() != null) {
66-
if (validateNonNull(validateRequest, response, Constants.PARAM_WEBHOOK_URL)) {
67-
validateWebhookUrl(validateRequest, response);
79+
if (validateNonNull(flatSettings, response, Constants.PARAM_WEBHOOK_URL)) {
80+
validateWebhookUrl(flatSettings, response);
6881
}
69-
if (validateNonNull(validateRequest, response, Constants.PARAM_CONDITION)) {
70-
validateCondition(validateRequest, response);
82+
if (validateNonNull(flatSettings, response, Constants.PARAM_CONDITION)) {
83+
validateCondition(flatSettings, response);
7184
}
72-
if (validateNonNull(validateRequest, response, Constants.PARAM_TEMPLATE)) {
73-
validateTemplate(validateRequest, response);
85+
if (validateNonNull(flatSettings, response, Constants.PARAM_TEMPLATE)) {
86+
validateTemplate(flatSettings, response);
7487
}
75-
validateProxyUrl(validateRequest, response);
88+
validateProxyUrl(flatSettings, response);
7689
} else {
7790
return DefaultGoPluginApiResponse.error("Illegal request");
7891
}
7992

8093
return success(toJsonString(response));
8194
}
8295

83-
private static boolean validateNonNull(ValidateConfigurationRequest validateRequest, List<ValidateConfigurationResponse> response, String parameterName) {
96+
private static boolean validateNonNull(Map<String, String> validateRequest, List<ValidateConfigurationResponse> response, String parameterName) {
8497
if (validateRequest != null) {
85-
if (validateRequest.getPluginSettings() != null) {
86-
Map<String, String> valueMap = validateRequest.getPluginSettings().get(parameterName);
87-
if (valueMap != null) {
88-
if (valueMap.get(Constants.FIELD_VALUE) != null) {
89-
return true;
90-
} else {
91-
response.add(new ValidateConfigurationResponse(parameterName, "Request pluginSettings parameter '"+parameterName+"' value is missing"));
92-
}
93-
} else {
94-
response.add(new ValidateConfigurationResponse(parameterName, "Request pluginSettings parameter '"+parameterName+"' is null"));
95-
}
98+
if (validateRequest.containsKey(parameterName)) {
99+
return true;
96100
} else {
97-
response.add(new ValidateConfigurationResponse(parameterName, "Request pluginSettings is null"));
101+
response.add(new ValidateConfigurationResponse(parameterName, "Request pluginSettings parameter '"+parameterName+"' value is missing"));
98102
}
99103
} else {
100104
response.add(new ValidateConfigurationResponse(parameterName, "Request is null"));
@@ -103,14 +107,12 @@ private static boolean validateNonNull(ValidateConfigurationRequest validateRequ
103107
}
104108

105109

106-
private static void validateProxyUrl(ValidateConfigurationRequest validateRequest, List<ValidateConfigurationResponse> response) {
110+
private static void validateProxyUrl(Map<String, String> validateRequest, List<ValidateConfigurationResponse> response) {
107111
if (validateRequest == null
108-
|| validateRequest.getPluginSettings() == null
109-
|| validateRequest.getPluginSettings().get(Constants.PARAM_PROXY_URL) == null
110-
|| validateRequest.getPluginSettings().get(Constants.PARAM_PROXY_URL).get(Constants.FIELD_VALUE) == null) {
112+
|| !validateRequest.containsKey(Constants.PARAM_PROXY_URL)) {
111113
return;
112114
}
113-
String proxyUrl = validateRequest.getPluginSettings().get(Constants.PARAM_PROXY_URL).get(Constants.FIELD_VALUE);
115+
String proxyUrl = validateRequest.get(Constants.PARAM_PROXY_URL);
114116
if (!proxyUrl.isEmpty()) {
115117
try {
116118
new URL(proxyUrl);
@@ -120,8 +122,8 @@ private static void validateProxyUrl(ValidateConfigurationRequest validateReques
120122
}
121123
}
122124

123-
private void validateTemplate(ValidateConfigurationRequest validateRequest, List<ValidateConfigurationResponse> response) {
124-
String template = validateRequest.getPluginSettings().get(Constants.PARAM_TEMPLATE).get(Constants.FIELD_VALUE);
125+
private void validateTemplate(Map<String, String> validateRequest, List<ValidateConfigurationResponse> response) {
126+
String template = validateRequest.get(Constants.PARAM_TEMPLATE);
125127
if (template.isEmpty()) {
126128
response.add(new ValidateConfigurationResponse(Constants.PARAM_TEMPLATE, Constants.PARAM_TEMPLATE + " is empty"));
127129
} else {
@@ -138,8 +140,8 @@ private void validateTemplate(ValidateConfigurationRequest validateRequest, List
138140
}
139141
}
140142

141-
private void validateCondition(ValidateConfigurationRequest validateRequest, List<ValidateConfigurationResponse> response) {
142-
String condition = validateRequest.getPluginSettings().get(Constants.PARAM_CONDITION).get(Constants.FIELD_VALUE);
143+
private void validateCondition(Map<String, String> validateRequest, List<ValidateConfigurationResponse> response) {
144+
String condition = validateRequest.get(Constants.PARAM_CONDITION);
143145
if (condition.isEmpty()) {
144146
response.add(new ValidateConfigurationResponse(Constants.PARAM_CONDITION, Constants.PARAM_CONDITION + " is empty"));
145147
} else {
@@ -163,9 +165,9 @@ private void validateCondition(ValidateConfigurationRequest validateRequest, Lis
163165
}
164166
}
165167

166-
private static void validateWebhookUrl(ValidateConfigurationRequest validateRequest, List<ValidateConfigurationResponse> response) {
168+
private static void validateWebhookUrl(Map<String, String> validateRequest, List<ValidateConfigurationResponse> response) {
167169
try {
168-
String webhookUrl = validateRequest.getPluginSettings().get(Constants.PARAM_WEBHOOK_URL).get(Constants.FIELD_VALUE);
170+
String webhookUrl = validateRequest.get(Constants.PARAM_WEBHOOK_URL);
169171
new URL(webhookUrl);
170172
} catch (NullPointerException | MalformedURLException e) {
171173
response.add(new ValidateConfigurationResponse(Constants.PARAM_WEBHOOK_URL, "Malformed url: " + e.getMessage()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.ionos.go.plugin.notifier;
2+
3+
import com.ionos.go.plugin.notifier.util.Helper;
4+
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
5+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
6+
import org.apache.hc.core5.http.HttpStatus;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.util.Collections;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
17+
18+
public class ValidateConfigurationHandlerTest extends CommonTestBase {
19+
20+
@Test
21+
public void toFlatSettingsWithTwoEntries() {
22+
Map<String, Map<String, String>> input = new HashMap<>();
23+
input.put("foo", Collections.singletonMap(Constants.FIELD_VALUE, "bar"));
24+
input.put("my", Collections.singletonMap(Constants.FIELD_VALUE, "value"));
25+
26+
Map<String, String> actual = ValidateConfigurationHandler.toFlatSettings(input);
27+
Map<String, String> expected = new HashMap<>();
28+
expected.put("foo", "bar");
29+
expected.put("my", "value");
30+
assertEquals(expected, actual);
31+
}
32+
33+
@Test
34+
public void toFlatSettingsWithOneNonValue() {
35+
Map<String, Map<String, String>> input = new HashMap<>();
36+
input.put("foo", Collections.singletonMap(Constants.FIELD_VALUE, "bar"));
37+
input.put("my", Collections.singletonMap("baz", "value"));
38+
39+
Map<String, String> actual = ValidateConfigurationHandler.toFlatSettings(input);
40+
Map<String, String> expected = new HashMap<>();
41+
expected.put("foo", "bar");
42+
assertEquals(expected, actual);
43+
}
44+
}

0 commit comments

Comments
 (0)