Skip to content

Commit 5c5ed67

Browse files
authored
Simplify graph API tests (#71)
1 parent ee0188b commit 5c5ed67

File tree

5 files changed

+94
-156
lines changed

5 files changed

+94
-156
lines changed

src/main/frontend/multi-pipeline-graph-view/app.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import * as React from "react";
2-
import {
3-
FunctionComponent,
4-
} from "react";
2+
import { FunctionComponent } from "react";
53

64
import { MultiPipelineGraph } from "./multi-pipeline-graph/main";
75

86
import "./app.scss";
97
import "./multi-pipeline-graph/styles/main.scss";
108

11-
129
const App: FunctionComponent = () => {
1310
return (
1411
<div>

src/main/frontend/multi-pipeline-graph-view/multi-pipeline-graph/main/SingleRun.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export const SingleRun: (data: Props) => JSX.Element = ({ run }) => {
1616

1717
const handleNodeClick = (nodeName: string, id: number) => {
1818
console.log(nodeName, id);
19-
window.location.href = `../${
20-
run.id
21-
}/pipeline-console?selected-node=${id}`;
19+
window.location.href = `../${run.id}/pipeline-console?selected-node=${id}`;
2220
};
2321
return (
2422
<tr>

src/main/frontend/multi-pipeline-graph-view/multi-pipeline-graph/styles/main.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
.PWGx-PipelineGraph {
1717
margin-right: auto;
1818
}
19-
}
19+
}
Lines changed: 75 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.jenkins.plugins.pipelinegraphview.utils;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.hasSize;
54
import static org.hamcrest.Matchers.is;
65

76
import hudson.model.Result;
87
import java.util.List;
8+
import java.util.Optional;
99
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
1010
import org.junit.Rule;
1111
import org.junit.Test;
@@ -24,38 +24,27 @@ public void createGraph_unstableSmokes() throws Exception {
2424
PipelineGraph graph = api.createGraph();
2525

2626
List<PipelineStage> stages = graph.getStages();
27-
assertThat(stages, hasSize(4));
28-
PipelineStage pipelineStage = stages.get(0);
29-
30-
assertThat(pipelineStage.getCompletePercent(), is(50));
31-
assertThat(pipelineStage.getName(), is("unstable-one"));
32-
assertThat(pipelineStage.getTitle(), is("unstable-one"));
33-
assertThat(pipelineStage.getType(), is("STAGE"));
34-
assertThat(pipelineStage.getState(), is("UNSTABLE"));
35-
36-
pipelineStage = stages.get(1);
37-
38-
assertThat(pipelineStage.getCompletePercent(), is(50));
39-
assertThat(pipelineStage.getName(), is("success"));
40-
assertThat(pipelineStage.getTitle(), is("success"));
41-
assertThat(pipelineStage.getType(), is("STAGE"));
42-
assertThat(pipelineStage.getState(), is("SUCCESS"));
43-
44-
pipelineStage = stages.get(2);
45-
46-
assertThat(pipelineStage.getCompletePercent(), is(50));
47-
assertThat(pipelineStage.getName(), is("unstable-two"));
48-
assertThat(pipelineStage.getTitle(), is("unstable-two"));
49-
assertThat(pipelineStage.getType(), is("STAGE"));
50-
assertThat(pipelineStage.getState(), is("UNSTABLE"));
51-
52-
pipelineStage = stages.get(3);
53-
54-
assertThat(pipelineStage.getCompletePercent(), is(50));
55-
assertThat(pipelineStage.getName(), is("failure"));
56-
assertThat(pipelineStage.getTitle(), is("failure"));
57-
assertThat(pipelineStage.getType(), is("STAGE"));
58-
assertThat(pipelineStage.getState(), is("FAILURE"));
27+
28+
String stagesString =
29+
TestUtils.collectStagesAsString(
30+
stages,
31+
(PipelineStage stage) ->
32+
String.format(
33+
"{%d,%s,%s,%s,%s}",
34+
stage.getCompletePercent(),
35+
stage.getName(),
36+
stage.getTitle(),
37+
stage.getType(),
38+
stage.getState()));
39+
assertThat(
40+
stagesString,
41+
is(
42+
String.join(
43+
"",
44+
"{50,unstable-one,unstable-one,STAGE,UNSTABLE},",
45+
"{50,success,success,STAGE,SUCCESS},",
46+
"{50,unstable-two,unstable-two,STAGE,UNSTABLE},",
47+
"{50,failure,failure,STAGE,FAILURE}")));
5948
}
6049

6150
@Test
@@ -66,48 +55,29 @@ public void createGraph_complexSmokes() throws Exception {
6655
PipelineGraph graph = api.createGraph();
6756

6857
List<PipelineStage> stages = graph.getStages();
69-
assertThat(stages, hasSize(4));
70-
71-
// Top level stages
72-
// Non-Parallel Stage
73-
PipelineStage pipelineStage = stages.get(0);
74-
assertThat(pipelineStage.getName(), is("Non-Parallel Stage"));
75-
76-
List<PipelineStage> children = pipelineStage.getChildren();
77-
assertThat(children, hasSize(0));
78-
79-
// Parallel Stage
80-
pipelineStage = stages.get(1);
81-
assertThat(pipelineStage.getName(), is("Parallel Stage"));
82-
83-
children = pipelineStage.getChildren();
84-
assertThat(children, hasSize(3));
85-
86-
// Parallel Stage - children
87-
PipelineStage child = children.get(0);
88-
assertThat(child.getName(), is("Branch A"));
8958

90-
child = children.get(1);
91-
assertThat(child.getName(), is("Branch B"));
92-
93-
// As this is a graph view, and Branch C doesn't have and steps, it doesn't get added as a node.
94-
// Instead it's first child with steps 'Nested 1' gets added as a node, and all other children
95-
// get added add siblings. The 'getSeqContainerName' property of the Nested 1 noe gets set to
96-
// it's
97-
// parent's display name ('Branch C') so the frontend can add a lable.
98-
child = children.get(2);
99-
assertThat(child.getName(), is("Nested 1"));
100-
assertThat(child.getSeqContainerName(), is("Branch C"));
101-
102-
PipelineStage sibling = child.getNextSibling();
103-
assertThat(sibling.getName(), is("Nested 2"));
104-
105-
// Skipped stage
106-
pipelineStage = stages.get(2);
107-
assertThat(pipelineStage.getName(), is("Skipped stage"));
108-
109-
children = pipelineStage.getChildren();
110-
assertThat(children, hasSize(0));
59+
String stagesString =
60+
TestUtils.collectStagesAsString(
61+
stages,
62+
(PipelineStage stage) ->
63+
String.format(
64+
"{%s,%s}",
65+
stage.getName(), Optional.ofNullable(stage.getSeqContainerName()).orElse("-")));
66+
67+
// As this is a graph view, and 'Branch C' doesn't have and steps, it doesn't get added as a
68+
// node.
69+
// Instead, its first child with steps 'Nested 1' gets added as a node, and all other children
70+
// get added as siblings. The 'getSeqContainerName' property of the 'Nested 1' now gets set to
71+
// its parent's display name ('Branch C') so the frontend can add a label.
72+
assertThat(
73+
stagesString,
74+
is(
75+
String.join(
76+
"",
77+
"{Non-Parallel Stage,-},",
78+
"{Parallel Stage,-}[{Branch A,-},{Branch B,-},{Nested 1,Branch C}],",
79+
"{Skipped stage,-},",
80+
"{Parallel Stage 2,-}[{Branch A,-},{Branch B,-},{Nested 1,Branch C}]")));
11181
}
11282

11383
@Test
@@ -119,38 +89,27 @@ public void createTree_unstableSmokes() throws Exception {
11989
PipelineGraph graph = api.createTree();
12090

12191
List<PipelineStage> stages = graph.getStages();
122-
assertThat(stages, hasSize(4));
123-
PipelineStage pipelineStage = stages.get(0);
124-
125-
assertThat(pipelineStage.getCompletePercent(), is(50));
126-
assertThat(pipelineStage.getName(), is("unstable-one"));
127-
assertThat(pipelineStage.getTitle(), is("unstable-one"));
128-
assertThat(pipelineStage.getType(), is("STAGE"));
129-
assertThat(pipelineStage.getState(), is("UNSTABLE"));
130-
131-
pipelineStage = stages.get(1);
132-
133-
assertThat(pipelineStage.getCompletePercent(), is(50));
134-
assertThat(pipelineStage.getName(), is("success"));
135-
assertThat(pipelineStage.getTitle(), is("success"));
136-
assertThat(pipelineStage.getType(), is("STAGE"));
137-
assertThat(pipelineStage.getState(), is("SUCCESS"));
138-
139-
pipelineStage = stages.get(2);
140-
141-
assertThat(pipelineStage.getCompletePercent(), is(50));
142-
assertThat(pipelineStage.getName(), is("unstable-two"));
143-
assertThat(pipelineStage.getTitle(), is("unstable-two"));
144-
assertThat(pipelineStage.getType(), is("STAGE"));
145-
assertThat(pipelineStage.getState(), is("UNSTABLE"));
146-
147-
pipelineStage = stages.get(3);
148-
149-
assertThat(pipelineStage.getCompletePercent(), is(50));
150-
assertThat(pipelineStage.getName(), is("failure"));
151-
assertThat(pipelineStage.getTitle(), is("failure"));
152-
assertThat(pipelineStage.getType(), is("STAGE"));
153-
assertThat(pipelineStage.getState(), is("FAILURE"));
92+
93+
String stagesString =
94+
TestUtils.collectStagesAsString(
95+
stages,
96+
(PipelineStage stage) ->
97+
String.format(
98+
"{%d,%s,%s,%s,%s}",
99+
stage.getCompletePercent(),
100+
stage.getName(),
101+
stage.getTitle(),
102+
stage.getType(),
103+
stage.getState()));
104+
assertThat(
105+
stagesString,
106+
is(
107+
String.join(
108+
"",
109+
"{50,unstable-one,unstable-one,STAGE,UNSTABLE},",
110+
"{50,success,success,STAGE,SUCCESS},",
111+
"{50,unstable-two,unstable-two,STAGE,UNSTABLE},",
112+
"{50,failure,failure,STAGE,FAILURE}")));
154113
}
155114

156115
@Test
@@ -161,48 +120,16 @@ public void createTree_complexSmokes() throws Exception {
161120
PipelineGraph graph = api.createTree();
162121

163122
List<PipelineStage> stages = graph.getStages();
164-
assertThat(stages, hasSize(4));
165-
166-
// Top level stages
167-
// Non-Parallel Stage
168-
PipelineStage pipelineStage = stages.get(0);
169-
assertThat(pipelineStage.getName(), is("Non-Parallel Stage"));
170-
171-
List<PipelineStage> children = pipelineStage.getChildren();
172-
assertThat(children, hasSize(0));
173-
174-
// Parallel Stage
175-
pipelineStage = stages.get(1);
176-
assertThat(pipelineStage.getName(), is("Parallel Stage"));
177-
178-
children = pipelineStage.getChildren();
179-
assertThat(children, hasSize(3));
180-
181-
// Parallel Stage - children
182-
PipelineStage child = children.get(0);
183-
assertThat(child.getName(), is("Branch A"));
184-
185-
child = children.get(1);
186-
assertThat(child.getName(), is("Branch B"));
187-
188-
child = children.get(2);
189-
assertThat(child.getName(), is("Branch C"));
190-
191-
children = child.getChildren();
192-
assertThat(children, hasSize(2));
193-
194-
// Branch C - children
195-
child = children.get(0);
196-
assertThat(child.getName(), is("Nested 1"));
197-
198-
child = children.get(1);
199-
assertThat(child.getName(), is("Nested 2"));
200-
201-
// Skipped stage
202-
pipelineStage = stages.get(2);
203-
assertThat(pipelineStage.getName(), is("Skipped stage"));
204123

205-
children = pipelineStage.getChildren();
206-
assertThat(children, hasSize(0));
124+
String stagesString = TestUtils.collectStagesAsString(stages, PipelineStage::getName);
125+
assertThat(
126+
stagesString,
127+
is(
128+
String.join(
129+
"",
130+
"Non-Parallel Stage,",
131+
"Parallel Stage[Branch A,Branch B,Branch C[Nested 1,Nested 2]],",
132+
"Skipped stage,",
133+
"Parallel Stage 2[Branch A,Branch B,Branch C[Nested 1,Nested 2]]")));
207134
}
208135
}

src/test/java/io/jenkins/plugins/pipelinegraphview/utils/TestUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.net.URL;
77
import java.util.ArrayList;
88
import java.util.List;
9+
import java.util.function.Function;
910
import java.util.logging.Level;
1011
import java.util.logging.Logger;
12+
import java.util.stream.Collectors;
1113
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
1214
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
1315
import org.jenkinsci.plugins.workflow.graph.FlowNode;
@@ -54,4 +56,18 @@ public static List<FlowNode> getNodesByDisplayName(WorkflowRun run, String displ
5456
}
5557
return matchingNodes;
5658
}
59+
60+
public static String collectStagesAsString(
61+
List<PipelineStage> stages, Function<PipelineStage, String> converter) {
62+
return stages.stream()
63+
.map(
64+
(PipelineStage stage) ->
65+
stage.getChildren().isEmpty()
66+
? converter.apply(stage)
67+
: String.format(
68+
"%s[%s]",
69+
converter.apply(stage),
70+
collectStagesAsString(stage.getChildren(), converter)))
71+
.collect(Collectors.joining(","));
72+
}
5773
}

0 commit comments

Comments
 (0)