1
1
package com .ionos .go .plugin .notifier ;
2
2
3
+ import com .google .gson .Gson ;
3
4
import com .ionos .go .plugin .notifier .message .GoPluginApiRequestHandler ;
4
5
import com .ionos .go .plugin .notifier .message .incoming .StageStatusRequest ;
5
6
import com .ionos .go .plugin .notifier .message .incoming .ValidateConfigurationRequest ;
6
7
import com .ionos .go .plugin .notifier .message .outgoing .ValidateConfigurationResponse ;
7
8
import com .ionos .go .plugin .notifier .template .TemplateHandler ;
9
+ import com .ionos .go .plugin .notifier .util .Helper ;
10
+ import com .ionos .go .plugin .notifier .util .JsonUtil ;
8
11
import com .thoughtworks .go .plugin .api .logging .Logger ;
9
12
import com .thoughtworks .go .plugin .api .request .GoPluginApiRequest ;
10
13
import com .thoughtworks .go .plugin .api .response .DefaultGoPluginApiResponse ;
11
14
import lombok .NonNull ;
12
15
16
+ import java .io .IOException ;
13
17
import java .net .MalformedURLException ;
14
18
import java .net .URL ;
15
- import java .time .ZonedDateTime ;
16
19
import java .util .ArrayList ;
17
- import java .util .Collections ;
18
20
import java .util .List ;
19
21
import java .util .Map ;
20
22
@@ -31,38 +33,21 @@ class ValidateConfigurationHandler implements GoPluginApiRequestHandler {
31
33
this .serverInfo = serverInfo ;
32
34
}
33
35
34
- /** Creates a sample object for testing the condition and the template with.
36
+ /** Creates sample objects for testing the condition and the template with.
35
37
* */
36
- private StageStatusRequest newSampleStageStatusRequest () {
37
- StageStatusRequest response = new StageStatusRequest ();
38
- StageStatusRequest .Pipeline pipeline = new StageStatusRequest .Pipeline ();
39
- StageStatusRequest .Stage stage = new StageStatusRequest .Stage ();
40
- StageStatusRequest .Job job = new StageStatusRequest .Job ();
41
-
42
- job .setName ("jobname" );
43
- job .setAssignTime (ZonedDateTime .now ());
44
- job .setCompleteTime (ZonedDateTime .now ());
45
- job .setScheduleTime (ZonedDateTime .now ());
46
- job .setState ("failed" );
47
- job .setResult ("cancelled" );
48
-
49
- stage .setName ("stagename" );
50
- stage .setApprovedBy ("John Doe" );
51
- stage .setCounter ("1" );
52
- stage .setPreviousStageCounter (0 );
53
- stage .setApprovalType ("foo" );
54
- stage .setState ("failed" );
55
- stage .setResult ("cancelled" );
56
- stage .setCreateTime (ZonedDateTime .now ());
57
- stage .setJobs (new StageStatusRequest .Job [] {job });
58
-
59
- pipeline .setStage (stage );
60
- pipeline .setName ("pipelinename" );
61
- pipeline .setCounter ("0" );
62
- pipeline .setGroup ("pipelinegroup" );
63
- pipeline .setBuildCause (new ArrayList <>());
64
-
65
- response .setPipeline (pipeline );
38
+ private List <StageStatusRequest > newSampleStageStatusRequests () throws IOException {
39
+ List <StageStatusRequest > response = new ArrayList <>();
40
+
41
+ Gson gson = new Gson ();
42
+ StageStatusRequest success = JsonUtil .fromJsonString (
43
+ Helper .readResource ("/sampleSuccess.json" ),
44
+ StageStatusRequest .class );
45
+ response .add (success );
46
+
47
+ StageStatusRequest failed = JsonUtil .fromJsonString (
48
+ Helper .readResource ("/sampleFailed.json" ),
49
+ StageStatusRequest .class );
50
+ response .add (failed );
66
51
67
52
return response ;
68
53
}
@@ -138,7 +123,9 @@ private void validateTemplate(ValidateConfigurationRequest validateRequest, List
138
123
} else {
139
124
try {
140
125
TemplateHandler handler = new TemplateHandler ("template" , template );
141
- handler .eval (newSampleStageStatusRequest (), serverInfo );
126
+ for (StageStatusRequest sample : newSampleStageStatusRequests ()) {
127
+ handler .eval (sample , serverInfo );
128
+ }
142
129
}
143
130
catch (Exception e ) {
144
131
LOGGER .warn ("Exception in " + Constants .PARAM_TEMPLATE , e );
@@ -154,9 +141,11 @@ private void validateCondition(ValidateConfigurationRequest validateRequest, Lis
154
141
} else {
155
142
try {
156
143
TemplateHandler handler = new TemplateHandler (Constants .PARAM_CONDITION , condition );
157
- String shouldBeBool = handler .eval (newSampleStageStatusRequest (), serverInfo );
158
- if (!(shouldBeBool .equals ("true" ) || shouldBeBool .equals ("false" ))) {
159
- response .add (new ValidateConfigurationResponse (Constants .PARAM_CONDITION , "Condition should eval to true or false, but evals to: " + shouldBeBool ));
144
+ for (StageStatusRequest sample : newSampleStageStatusRequests ()) {
145
+ String shouldBeBool = handler .eval (sample , serverInfo );
146
+ if (!(shouldBeBool .equals ("true" ) || shouldBeBool .equals ("false" ))) {
147
+ response .add (new ValidateConfigurationResponse (Constants .PARAM_CONDITION , "Condition should eval to true or false, but evals to: " + shouldBeBool ));
148
+ }
160
149
}
161
150
}
162
151
catch (Exception e ) {
0 commit comments