20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
22
import java .util .Map ;
23
+ import java .util .stream .Collectors ;
23
24
24
25
import static com .ionos .go .plugin .notifier .util .JsonUtil .fromJsonString ;
25
26
import static com .ionos .go .plugin .notifier .util .JsonUtil .toJsonString ;
@@ -42,7 +43,6 @@ class ValidateConfigurationHandler implements GoPluginApiRequestHandler {
42
43
private List <StageStatusRequest > newSampleStageStatusRequests () throws IOException {
43
44
List <StageStatusRequest > response = new ArrayList <>();
44
45
45
- Gson gson = new Gson ();
46
46
StageStatusRequest success = JsonUtil .fromJsonString (
47
47
Helper .readResource ("/sampleSuccess.json" ),
48
48
StageStatusRequest .class );
@@ -56,45 +56,49 @@ private List<StageStatusRequest> newSampleStageStatusRequests() throws IOExcepti
56
56
return response ;
57
57
}
58
58
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
+
59
70
@ Override
60
71
public DefaultGoPluginApiResponse handle (@ NonNull GoPluginApiRequest request ) {
61
72
LOGGER .info ("Request: " + request .requestBody ());
62
73
ValidateConfigurationRequest validateRequest = fromJsonString (request .requestBody (), ValidateConfigurationRequest .class );
74
+
75
+ Map <String , String > flatSettings = toFlatSettings (validateRequest .getPluginSettings ());
63
76
List <ValidateConfigurationResponse > response = new ArrayList <>();
64
77
65
78
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 );
68
81
}
69
- if (validateNonNull (validateRequest , response , Constants .PARAM_CONDITION )) {
70
- validateCondition (validateRequest , response );
82
+ if (validateNonNull (flatSettings , response , Constants .PARAM_CONDITION )) {
83
+ validateCondition (flatSettings , response );
71
84
}
72
- if (validateNonNull (validateRequest , response , Constants .PARAM_TEMPLATE )) {
73
- validateTemplate (validateRequest , response );
85
+ if (validateNonNull (flatSettings , response , Constants .PARAM_TEMPLATE )) {
86
+ validateTemplate (flatSettings , response );
74
87
}
75
- validateProxyUrl (validateRequest , response );
88
+ validateProxyUrl (flatSettings , response );
76
89
} else {
77
90
return DefaultGoPluginApiResponse .error ("Illegal request" );
78
91
}
79
92
80
93
return success (toJsonString (response ));
81
94
}
82
95
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 ) {
84
97
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 ;
96
100
} else {
97
- response .add (new ValidateConfigurationResponse (parameterName , "Request pluginSettings is null " ));
101
+ response .add (new ValidateConfigurationResponse (parameterName , "Request pluginSettings parameter '" + parameterName + "' value is missing " ));
98
102
}
99
103
} else {
100
104
response .add (new ValidateConfigurationResponse (parameterName , "Request is null" ));
@@ -103,14 +107,12 @@ private static boolean validateNonNull(ValidateConfigurationRequest validateRequ
103
107
}
104
108
105
109
106
- private static void validateProxyUrl (ValidateConfigurationRequest validateRequest , List <ValidateConfigurationResponse > response ) {
110
+ private static void validateProxyUrl (Map < String , String > validateRequest , List <ValidateConfigurationResponse > response ) {
107
111
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 )) {
111
113
return ;
112
114
}
113
- String proxyUrl = validateRequest .getPluginSettings (). get (Constants .PARAM_PROXY_URL ). get ( Constants . FIELD_VALUE );
115
+ String proxyUrl = validateRequest .get (Constants .PARAM_PROXY_URL );
114
116
if (!proxyUrl .isEmpty ()) {
115
117
try {
116
118
new URL (proxyUrl );
@@ -120,8 +122,8 @@ private static void validateProxyUrl(ValidateConfigurationRequest validateReques
120
122
}
121
123
}
122
124
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 );
125
127
if (template .isEmpty ()) {
126
128
response .add (new ValidateConfigurationResponse (Constants .PARAM_TEMPLATE , Constants .PARAM_TEMPLATE + " is empty" ));
127
129
} else {
@@ -138,8 +140,8 @@ private void validateTemplate(ValidateConfigurationRequest validateRequest, List
138
140
}
139
141
}
140
142
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 );
143
145
if (condition .isEmpty ()) {
144
146
response .add (new ValidateConfigurationResponse (Constants .PARAM_CONDITION , Constants .PARAM_CONDITION + " is empty" ));
145
147
} else {
@@ -163,9 +165,9 @@ private void validateCondition(ValidateConfigurationRequest validateRequest, Lis
163
165
}
164
166
}
165
167
166
- private static void validateWebhookUrl (ValidateConfigurationRequest validateRequest , List <ValidateConfigurationResponse > response ) {
168
+ private static void validateWebhookUrl (Map < String , String > validateRequest , List <ValidateConfigurationResponse > response ) {
167
169
try {
168
- String webhookUrl = validateRequest .getPluginSettings (). get (Constants .PARAM_WEBHOOK_URL ). get ( Constants . FIELD_VALUE );
170
+ String webhookUrl = validateRequest .get (Constants .PARAM_WEBHOOK_URL );
169
171
new URL (webhookUrl );
170
172
} catch (NullPointerException | MalformedURLException e ) {
171
173
response .add (new ValidateConfigurationResponse (Constants .PARAM_WEBHOOK_URL , "Malformed url: " + e .getMessage ()));
0 commit comments