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

Commit 1ef3c14

Browse files
committed
Add real-world JSONs to make the validation better
1 parent fdc41dd commit 1ef3c14

File tree

3 files changed

+79
-37
lines changed

3 files changed

+79
-37
lines changed

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

+26-37
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.ionos.go.plugin.notifier;
22

3+
import com.google.gson.Gson;
34
import com.ionos.go.plugin.notifier.message.GoPluginApiRequestHandler;
45
import com.ionos.go.plugin.notifier.message.incoming.StageStatusRequest;
56
import com.ionos.go.plugin.notifier.message.incoming.ValidateConfigurationRequest;
67
import com.ionos.go.plugin.notifier.message.outgoing.ValidateConfigurationResponse;
78
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;
811
import com.thoughtworks.go.plugin.api.logging.Logger;
912
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
1013
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
1114
import lombok.NonNull;
1215

16+
import java.io.IOException;
1317
import java.net.MalformedURLException;
1418
import java.net.URL;
15-
import java.time.ZonedDateTime;
1619
import java.util.ArrayList;
17-
import java.util.Collections;
1820
import java.util.List;
1921
import java.util.Map;
2022

@@ -31,38 +33,21 @@ class ValidateConfigurationHandler implements GoPluginApiRequestHandler {
3133
this.serverInfo = serverInfo;
3234
}
3335

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.
3537
* */
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);
6651

6752
return response;
6853
}
@@ -138,7 +123,9 @@ private void validateTemplate(ValidateConfigurationRequest validateRequest, List
138123
} else {
139124
try {
140125
TemplateHandler handler = new TemplateHandler("template", template);
141-
handler.eval(newSampleStageStatusRequest(), serverInfo);
126+
for (StageStatusRequest sample : newSampleStageStatusRequests()) {
127+
handler.eval(sample, serverInfo);
128+
}
142129
}
143130
catch (Exception e) {
144131
LOGGER.warn("Exception in " + Constants.PARAM_TEMPLATE, e);
@@ -154,9 +141,11 @@ private void validateCondition(ValidateConfigurationRequest validateRequest, Lis
154141
} else {
155142
try {
156143
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+
}
160149
}
161150
}
162151
catch (Exception e) {

src/main/resources/sampleFailed.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"pipeline":{"name":"go-gchat-notifier-plugin","counter":"5","label":"dfbcc4c8f5892cbd099f6c5e7528b105e137b8f9","group":"Github","build-cause":[{"material":{"git-configuration":{"shallow-clone":false,"branch":"master","url":"https://github.com/1and1/go-gchat-notifier-plugin.git"},"fingerprint":"8f4ca9be1cc118c32c73783b83285b840e0c2feff12fb7abdb62e23199ea73a2","type":"git"},"changed":true,"modifications":[{"revision":"dfbcc4c8f5892cbd099f6c5e7528b105e137b8f9","modified-time":"2023-07-19T12:46:19.000+0000","data":{}}]}],"stage":{"name":"Snapshot","counter":"1","approval-type":"success","approved-by":"changes","previous-stage-name":"Test","previous-stage-counter":1,"state":"Failed","result":"Failed","create-time":"2023-07-19T12:48:29.828+0000","last-transition-time":"2023-07-19T12:48:51.212+0000","jobs":[{"name":"Deploy","schedule-time":"2023-07-19T12:48:29.828+0000","assign-time":"2023-07-19T12:48:33.731+0000","complete-time":"2023-07-19T12:48:51.212+0000","state":"Completed","result":"Failed","agent-uuid":"9ea12491-3730-4032-91a4-aec8d24af553"}]}}}

src/main/resources/sampleSuccess.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"pipeline" : {
3+
"build-cause" : [
4+
{
5+
"changed" : true,
6+
"material" : {
7+
"fingerprint" : "8f4ca9be1cc118c32c73783b83285b840e0c2feff12fb7abdb62e23199ea73a2",
8+
"git-configuration" : {
9+
"branch" : "master",
10+
"shallow-clone" : false,
11+
"url" : "https://github.com/1and1/go-gchat-notifier-plugin.git"
12+
},
13+
"type" : "git"
14+
},
15+
"modifications" : [
16+
{
17+
"data" : {},
18+
"modified-time" : "2023-07-19T13:15:34.000+0000",
19+
"revision" : "d7b2629fda5048b8eea42b1bee968a8fd949eadd"
20+
}
21+
]
22+
}
23+
],
24+
"counter" : "8",
25+
"group" : "Github",
26+
"label" : "d7b2629fda5048b8eea42b1bee968a8fd949eadd",
27+
"name" : "go-gchat-notifier-plugin",
28+
"stage" : {
29+
"approval-type" : "success",
30+
"approved-by" : "changes",
31+
"counter" : "1",
32+
"create-time" : "2023-07-19T13:16:56.571+0000",
33+
"jobs" : [
34+
{
35+
"agent-uuid" : "51549723-2fc3-4965-8657-4d5785d86611",
36+
"assign-time" : "2023-07-19T13:16:59.097+0000",
37+
"complete-time" : "2023-07-19T13:17:09.671+0000",
38+
"name" : "Deploy",
39+
"result" : "Passed",
40+
"schedule-time" : "2023-07-19T13:16:56.570+0000",
41+
"state" : "Completed"
42+
}
43+
],
44+
"last-transition-time" : "2023-07-19T13:17:09.671+0000",
45+
"name" : "Snapshot",
46+
"previous-stage-counter" : 1,
47+
"previous-stage-name" : "Test",
48+
"result" : "Passed",
49+
"state" : "Passed"
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)