1
1
package jenkinsci .plugins .influxdb .generators ;
2
2
3
+ import com .influxdb .client .write .Point ;
3
4
import hudson .EnvVars ;
4
5
import hudson .model .Job ;
5
6
import hudson .model .Run ;
6
7
import hudson .model .TaskListener ;
8
+ import hudson .tasks .junit .CaseResult ;
9
+ import hudson .tasks .junit .SuiteResult ;
7
10
import hudson .tasks .test .AbstractTestResultAction ;
11
+ import jenkins .model .Jenkins ;
8
12
import jenkinsci .plugins .influxdb .renderer .ProjectNameRenderer ;
9
13
import org .apache .commons .lang .StringUtils ;
10
14
import org .junit .Assert ;
11
15
import org .junit .Before ;
12
16
import org .junit .Test ;
13
17
import org .mockito .Mockito ;
14
18
19
+ import java .util .ArrayList ;
20
+ import java .util .Collections ;
21
+ import java .util .List ;
22
+
23
+ import static org .junit .Assert .assertTrue ;
24
+
15
25
public class JUnitPointGeneratorTest {
16
26
17
27
private static final String JOB_NAME = "master" ;
@@ -22,6 +32,7 @@ public class JUnitPointGeneratorTest {
22
32
private TaskListener listener ;
23
33
private ProjectNameRenderer measurementRenderer ;
24
34
35
+ private CaseResult caseResult ;
25
36
private long currTime ;
26
37
27
38
@ Before
@@ -30,10 +41,12 @@ public void before() {
30
41
Job job = Mockito .mock (Job .class );
31
42
listener = Mockito .mock (TaskListener .class );
32
43
measurementRenderer = new ProjectNameRenderer (CUSTOM_PREFIX , null );
44
+ caseResult = Mockito .mock (CaseResult .class );
33
45
34
46
Mockito .when (build .getNumber ()).thenReturn (BUILD_NUMBER );
35
47
Mockito .when (build .getParent ()).thenReturn (job );
36
48
Mockito .when (job .getName ()).thenReturn (JOB_NAME );
49
+ Mockito .when (job .getRelativeNameFrom (Mockito .nullable (Jenkins .class ))).thenReturn ("" );
37
50
38
51
currTime = System .currentTimeMillis ();
39
52
}
@@ -46,7 +59,7 @@ public void hasReport_tests_exist_and_flag_is_true() {
46
59
Mockito .when (build .getAction (AbstractTestResultAction .class )).thenReturn (Mockito .mock (AbstractTestResultAction .class ));
47
60
48
61
JUnitPointGenerator junitGen = new JUnitPointGenerator (build , listener , measurementRenderer , currTime , StringUtils .EMPTY , CUSTOM_PREFIX , envVars );
49
- Assert . assertTrue (junitGen .hasReport ());
62
+ assertTrue (junitGen .hasReport ());
50
63
}
51
64
52
65
@ Test
@@ -95,4 +108,37 @@ public void hasReport_no_tests_and_flag_is_missing() {
95
108
JUnitPointGenerator junitGen = new JUnitPointGenerator (build , listener , measurementRenderer , currTime , StringUtils .EMPTY , CUSTOM_PREFIX , envVars );
96
109
Assert .assertFalse (junitGen .hasReport ());
97
110
}
111
+
112
+ @ Test
113
+ public void measurement_successfully_generated () {
114
+ CaseResult .Status status = Mockito .mock (CaseResult .Status .class );
115
+ SuiteResult suiteResult = Mockito .mock (SuiteResult .class );
116
+ Mockito .when (caseResult .getStatus ()).thenReturn (status );
117
+ List <CaseResult > passedTests = new ArrayList <>();
118
+ passedTests .add (caseResult );
119
+ AbstractTestResultAction testResultAction = Mockito .mock (AbstractTestResultAction .class );
120
+ Mockito .when (testResultAction .getFailedTests ()).thenReturn (Collections .emptyList ());
121
+ Mockito .when (testResultAction .getSkippedTests ()).thenReturn (Collections .emptyList ());
122
+ Mockito .when (testResultAction .getPassedTests ()).thenReturn (passedTests );
123
+ Mockito .when (build .getAction (AbstractTestResultAction .class )).thenReturn (testResultAction );
124
+ Mockito .when (caseResult .getSuiteResult ()).thenReturn (suiteResult );
125
+
126
+ Mockito .when (caseResult .getSuiteResult ().getName ()).thenReturn ("my_suite" );
127
+ Mockito .when (caseResult .getName ()).thenReturn ("my_test" );
128
+ Mockito .when (caseResult .getClassName ()).thenReturn ("my_class_name" );
129
+ Mockito .when (caseResult .getStatus ().toString ()).thenReturn ("PASSED" );
130
+ Mockito .when (caseResult .getStatus ().ordinal ()).thenReturn (0 );
131
+ Mockito .when (caseResult .getDuration ()).thenReturn (10.0f );
132
+
133
+ JUnitPointGenerator generator = new JUnitPointGenerator (build , listener , measurementRenderer , currTime , StringUtils .EMPTY , StringUtils .EMPTY , new EnvVars ());
134
+ Point [] points = generator .generate ();
135
+ String lineProtocol = points [0 ].toLineProtocol ();
136
+
137
+ assertTrue (lineProtocol .contains ("suite_name=\" my_suite\" " ));
138
+ assertTrue (lineProtocol .contains ("test_name=\" my_test\" " ));
139
+ assertTrue (lineProtocol .contains ("test_class_full_name=\" my_class_name\" " ));
140
+ assertTrue (lineProtocol .contains ("test_status=\" PASSED\" " ));
141
+ assertTrue (lineProtocol .contains ("test_status_ordinal=0" ));
142
+ assertTrue (lineProtocol .contains ("test_duration=10.0" ));
143
+ }
98
144
}
0 commit comments