2020package io .securecodebox .engine .execution ;
2121
2222import com .fasterxml .jackson .databind .ObjectMapper ;
23+ import com .fasterxml .jackson .datatype .jdk8 .Jdk8Module ;
2324import io .securecodebox .TestHelper ;
2425import io .securecodebox .constants .DefaultFields ;
26+ import io .securecodebox .engine .service .ExecutionTimeService ;
2527import io .securecodebox .model .execution .ScanProcessExecution ;
2628import io .securecodebox .model .execution .ScanProcessExecutionFactory ;
2729import io .securecodebox .model .findings .OsiLayer ;
3638import org .mockito .MockitoAnnotations ;
3739import org .mockito .stubbing .Answer ;
3840
41+ import java .time .LocalDateTime ;
42+ import java .time .ZoneOffset ;
43+ import java .util .Date ;
44+ import java .util .Optional ;
3945import java .util .UUID ;
4046
4147import static org .junit .Assert .assertEquals ;
4248import static org .junit .Assert .assertNotNull ;
4349import static org .mockito .Matchers .any ;
44- import static org .mockito .Matchers .anyString ;
4550import static org .mockito .Matchers .eq ;
4651import static org .mockito .Mockito .atLeastOnce ;
4752import static org .mockito .Mockito .doAnswer ;
5560 */
5661public class DefaultScanProcessExecutionTest {
5762
58- private static final String DEFAULT_EXECUTION = "{\" id\" :\" 5a4e9d37-09b0-4109-badd-d79dfa8fce2a\" ,\" context\" :\" TEST_CONTEXT\" ,\" automated\" :false,\" scanners\" :[{\" id\" :\" 62fa8ffb-e3bc-433e-b322-9c02108c5171\" ,\" type\" :\" Test_SCANNER\" ,\" findings\" :[{\" id\" :\" 49bf7fd3-8512-4d73-a28f-608e493cd726\" ,\" name\" :\" BAD_TEST_FINDIG\" ,\" description\" :\" Some coder has tested this!\" ,\" category\" :\" COOL_TEST_STUFF\" ,\" osi_layer\" :\" NOT_APPLICABLE\" ,\" severity\" :\" HIGH\" ,\" reference\" :{\" id\" :\" UNI_CODE_STUFF\" ,\" source\" :\" RISCOOL\" },\" hint\" :\" You might wan't to blame Rüdiger!\" ,\" attributes\" :{\" TEST\" :\" Kekse\" ,\" HORRIBLE\" :\" Coke\" },\" location\" :\" mett.brot.securecodebox.io\" ,\" false_positive\" :false}],\" rawFindings\" :\" [{\\ \" pudding\\ \" :\\ \" Bier\\ \" }]\" }]}" ;
63+ private static final String DEFAULT_EXECUTION = "{\" id\" :\" 5a4e9d37-09b0-4109-badd-d79dfa8fce2a\" ,\" context\" :\" TEST_CONTEXT\" ,\" automated\" :false,\" scanners\" :[{\" id\" :\" 62fa8ffb-e3bc-433e-b322-9c02108c5171\" ,\" type\" :\" Test_SCANNER\" ,\" findings\" :[{\" id\" :\" 49bf7fd3-8512-4d73-a28f-608e493cd726\" ,\" name\" :\" BAD_TEST_FINDIG\" ,\" description\" :\" Some coder has tested this!\" ,\" category\" :\" COOL_TEST_STUFF\" ,\" osi_layer\" :\" NOT_APPLICABLE\" ,\" severity\" :\" HIGH\" ,\" reference\" :{\" id\" :\" UNI_CODE_STUFF\" ,\" source\" :\" RISCOOL\" },\" hint\" :\" You might wan't to blame Rüdiger!\" ,\" attributes\" :{\" TEST\" :\" Kekse\" ,\" HORRIBLE\" :\" Coke\" },\" location\" :\" mett.brot.securecodebox.io\" ,\" false_positive\" :false}],\" rawFindings\" :\" [{\\ \" pudding\\ \" :\\ \" Bier\\ \" }]\" }], \" startDate \" :504295320000, \" endDate \" :504295620000, \" durationInMilliSeconds \" :300000 }" ;
5964 public static final String SCANNER_SERIALIZE_RESULT = "{\" id\" :\" 62fa8ffb-e3bc-433e-b322-9c02108c5171\" ,\" type\" :\" Test_SCANNER\" ,\" findings\" :[{\" id\" :\" 49bf7fd3-8512-4d73-a28f-608e493cd726\" ,\" name\" :\" BAD_TEST_FINDIG\" ,\" description\" :\" Some coder has tested this!\" ,\" category\" :\" COOL_TEST_STUFF\" ,\" osi_layer\" :\" NOT_APPLICABLE\" ,\" severity\" :\" HIGH\" ,\" reference\" :{\" id\" :\" UNI_CODE_STUFF\" ,\" source\" :\" RISCOOL\" },\" hint\" :\" You might wan't to blame Rüdiger!\" ,\" attributes\" :{\" TEST\" :\" Kekse\" ,\" HORRIBLE\" :\" Coke\" },\" location\" :\" mett.brot.securecodebox.io\" ,\" false_positive\" :false}],\" rawFindings\" :\" [{\\ \" pudding\\ \" :\\ \" Bier\\ \" }]\" }" ;
6065
6166 String findingCache = "" ;
@@ -66,37 +71,51 @@ public class DefaultScanProcessExecutionTest {
6671 @ Mock
6772 ScanProcessExecutionFactory processExecutionFactory ;
6873 @ Mock
69- DelegateExecution executionMock ;
74+ DelegateExecution execution ;
75+ @ Mock
76+ ExecutionTimeService executionTimeService ;
7077
7178 DefaultScanProcessExecution underTest ;
7279
7380 @ Before
7481 public void setUp () {
7582 MockitoAnnotations .initMocks (this );
76- underTest = new DefaultScanProcessExecution (executionMock );
83+ underTest = new DefaultScanProcessExecution (execution );
84+
85+ objectMapper .registerModule (new Jdk8Module ());
7786
78- when (processExecutionFactory .get (executionMock )).thenReturn (underTest );
79- when (executionMock .hasVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()))).thenReturn (true );
80- when (executionMock .getVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()))).thenAnswer ((answer ) -> findingCache );
87+ when (executionTimeService .getStartDate ()).thenReturn (
88+ Date .from (LocalDateTime .of (1985 , 12 , 24 , 18 , 2 ).toInstant (ZoneOffset .UTC ))
89+ );
90+ when (executionTimeService .getEndDate ()).thenReturn (Optional .of (
91+ Date .from (LocalDateTime .of (1985 , 12 , 24 , 18 , 7 ).toInstant (ZoneOffset .UTC ))
92+ ));
93+ underTest .executionTimeService = executionTimeService ;
94+
95+ when (processExecutionFactory .get (execution )).thenReturn (underTest );
96+ when (execution .hasVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()))).thenReturn (true );
97+ when (execution .getVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()))).thenAnswer ((answer ) -> findingCache );
8198 doAnswer ((Answer ) invocation -> {
8299 findingCache = (String ) ((ObjectValueImpl )invocation .getArgument (1 )).getValue ();
83100 return Void .TYPE ;
84- }).when (executionMock ).setVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()), any ());
101+ }).when (execution ).setVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()), any ());
85102
86- when (executionMock .hasVariable (eq (DefaultFields .PROCESS_TARGETS .name ()))).thenReturn (true );
87- when (executionMock .getVariable (eq (DefaultFields .PROCESS_TARGETS .name ()))).thenAnswer ((answer ) -> targetCache );
103+ when (execution .hasVariable (eq (DefaultFields .PROCESS_TARGETS .name ()))).thenReturn (true );
104+ when (execution .getVariable (eq (DefaultFields .PROCESS_TARGETS .name ()))).thenAnswer ((answer ) -> targetCache );
88105 doAnswer ((Answer ) invocation -> {
89106 targetCache = (String ) ((ObjectValueImpl )invocation .getArgument (1 )).getValue ();
90107 return Void .TYPE ;
91- }).when (executionMock ).setVariable (eq (DefaultFields .PROCESS_TARGETS .name ()), any ());
108+ }).when (execution ).setVariable (eq (DefaultFields .PROCESS_TARGETS .name ()), any ());
92109 }
93110
94111 @ Test
95112 public void testSerialize () throws Exception {
96113 DelegateExecution process = mockDelegateExcecution ();
97114
98- ScanProcessExecution execution = new DefaultScanProcessExecution (process );
99- String s = objectMapper .writeValueAsString (execution );
115+ DefaultScanProcessExecution execution = new DefaultScanProcessExecution (process );
116+
117+ execution .executionTimeService = executionTimeService ;
118+ String s = objectMapper .writeValueAsString ((ScanProcessExecution ) execution );
100119
101120 System .out .println (s );
102121 assertEquals (DEFAULT_EXECUTION , s );
@@ -126,9 +145,9 @@ public void testAppendAndClearFindings() throws Exception {
126145 underTest .appendFinding (TestHelper .createBasicFinding (finding1Id ));
127146 underTest .appendFinding (TestHelper .createBasicFindingDifferent (finding2Id ));
128147
129- Mockito .verify (executionMock , times (2 )).setVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()), any ());
148+ Mockito .verify (execution , times (2 )).setVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()), any ());
130149
131- ScanProcessExecution processExecution = processExecutionFactory .get (executionMock );
150+ ScanProcessExecution processExecution = processExecutionFactory .get (execution );
132151
133152 assertEquals (2 , processExecution .getFindings ().size ());
134153
@@ -163,9 +182,9 @@ public void testAppendAndClearFindings() throws Exception {
163182 //
164183 underTest .clearFindings ();
165184
166- Mockito .verify (executionMock , atLeastOnce ()).getVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()));
167- Mockito .verify (executionMock , times (3 )).setVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()), any ());
168- Mockito .verifyNoMoreInteractions (executionMock );
185+ Mockito .verify (execution , atLeastOnce ()).getVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()));
186+ Mockito .verify (execution , times (3 )).setVariable (eq (DefaultFields .PROCESS_FINDINGS .name ()), any ());
187+ Mockito .verifyNoMoreInteractions (execution );
169188 assertEquals (0 , processExecution .getFindings ().size ());
170189 }
171190
@@ -177,9 +196,9 @@ public void testAppendAndClearTargets() throws Exception {
177196 underTest .appendTarget (TestHelper .createBaiscTarget ());
178197 underTest .appendTarget (TestHelper .createTarget ("http://w1.w2.www" , "some wired" ));
179198
180- Mockito .verify (executionMock , times (2 )).setVariable (eq (DefaultFields .PROCESS_TARGETS .name ()), any ());
199+ Mockito .verify (execution , times (2 )).setVariable (eq (DefaultFields .PROCESS_TARGETS .name ()), any ());
181200
182- ScanProcessExecution processExecution = processExecutionFactory .get (executionMock );
201+ ScanProcessExecution processExecution = processExecutionFactory .get (execution );
183202
184203 assertEquals (2 , processExecution .getTargets ().size ());
185204
@@ -201,9 +220,9 @@ public void testAppendAndClearTargets() throws Exception {
201220 // Clear targets
202221 //
203222 underTest .clearTargets ();
204- Mockito .verify (executionMock , atLeastOnce ()).getVariable (eq (DefaultFields .PROCESS_TARGETS .name ()));
205- Mockito .verify (executionMock , times (3 )).setVariable (eq (DefaultFields .PROCESS_TARGETS .name ()), any ());
206- Mockito .verifyNoMoreInteractions (executionMock );
223+ Mockito .verify (execution , atLeastOnce ()).getVariable (eq (DefaultFields .PROCESS_TARGETS .name ()));
224+ Mockito .verify (execution , times (3 )).setVariable (eq (DefaultFields .PROCESS_TARGETS .name ()), any ());
225+ Mockito .verifyNoMoreInteractions (execution );
207226 assertEquals (0 , processExecution .getTargets ().size ());
208227
209228 }
0 commit comments