1
+ package org .commonwl .view .workflow ;
2
+
3
+ import org .commonwl .view .github .GitHubService ;
4
+ import org .commonwl .view .github .GithubDetails ;
5
+ import org .junit .Before ;
6
+ import org .junit .Test ;
7
+ import org .mockito .Mockito ;
8
+ import org .springframework .validation .BeanPropertyBindingResult ;
9
+ import org .springframework .validation .Errors ;
10
+
11
+ import java .io .IOException ;
12
+
13
+ import static org .junit .Assert .*;
14
+ import static org .mockito .Matchers .anyObject ;
15
+ import static org .mockito .Matchers .anyString ;
16
+ import static org .mockito .Mockito .when ;
17
+
18
+ /**
19
+ * Tests the validator. Parsing is already checked in GithubServiceTest
20
+ */
21
+ public class WorkflowFormValidatorTest {
22
+
23
+ /**
24
+ * Workflow form validator to test
25
+ */
26
+ private WorkflowFormValidator workflowFormValidator ;
27
+
28
+ /**
29
+ * Set up new validator with mock Github service
30
+ */
31
+ @ Before
32
+ public void setUp () throws Exception {
33
+ // Mock Github service which always returns non-null for downloads
34
+ GitHubService mockGithubService = Mockito .mock (GitHubService .class );
35
+ when (mockGithubService .downloadFile (anyObject ())).thenReturn ("" );
36
+ when (mockGithubService .detailsFromDirURL (anyString ())).thenCallRealMethod ();
37
+ when (mockGithubService .detailsFromFileURL (anyString ())).thenCallRealMethod ();
38
+
39
+ workflowFormValidator = new WorkflowFormValidator (mockGithubService );
40
+ }
41
+
42
+ /**
43
+ * Github Directory URL
44
+ */
45
+ @ Test
46
+ public void directoryURL () throws Exception {
47
+
48
+ WorkflowForm dirURL = new WorkflowForm ("https://github.com/common-workflow-language/cwltool/tree/master/cwltool/schemas" );
49
+
50
+ Errors errors = new BeanPropertyBindingResult (dirURL , "workflowForm" );
51
+ GithubDetails details = workflowFormValidator .validateAndParse (dirURL , errors );
52
+
53
+ assertNotNull (details );
54
+ assertFalse (errors .hasErrors ());
55
+
56
+ }
57
+
58
+ /**
59
+ * Github File URL
60
+ */
61
+ @ Test
62
+ public void fileURL () throws Exception {
63
+
64
+ WorkflowForm fileURL = new WorkflowForm ("https://github.com/nlesc-sherlock/deeplearning/blob/master/CWLworkflow/pipeline.cwl" );
65
+
66
+ Errors errors = new BeanPropertyBindingResult (fileURL , "workflowForm" );
67
+ GithubDetails details = workflowFormValidator .validateAndParse (fileURL , errors );
68
+
69
+ assertNotNull (details );
70
+ assertFalse (errors .hasErrors ());
71
+
72
+ }
73
+
74
+ /**
75
+ * Empty URL
76
+ */
77
+ @ Test
78
+ public void emptyURL () throws Exception {
79
+
80
+ WorkflowForm emptyURL = new WorkflowForm ("" );
81
+
82
+ Errors errors = new BeanPropertyBindingResult (emptyURL , "workflowForm" );
83
+ GithubDetails willBeNull = workflowFormValidator .validateAndParse (emptyURL , errors );
84
+
85
+ assertNull (willBeNull );
86
+ assertTrue (errors .hasErrors ());
87
+
88
+ }
89
+
90
+ /**
91
+ * Invalid URL
92
+ */
93
+ @ Test
94
+ public void invalidURL () throws Exception {
95
+
96
+ WorkflowForm invalidURL = new WorkflowForm ("https://google.com/clearly/not/github/url" );
97
+
98
+ Errors errors = new BeanPropertyBindingResult (invalidURL , "workflowForm" );
99
+ GithubDetails details = workflowFormValidator .validateAndParse (invalidURL , errors );
100
+
101
+ assertNull (details );
102
+ assertTrue (errors .hasErrors ());
103
+
104
+ }
105
+
106
+ /**
107
+ * Invalid URL
108
+ */
109
+ @ Test
110
+ public void cannotDownloadFile () throws Exception {
111
+
112
+ // Mock Github service which always throws an exception for downloads
113
+ GitHubService mockGithubService = Mockito .mock (GitHubService .class );
114
+ when (mockGithubService .downloadFile (anyObject ())).thenThrow (new IOException ("404 Error" ));
115
+ when (mockGithubService .detailsFromDirURL (anyString ())).thenCallRealMethod ();
116
+ when (mockGithubService .detailsFromFileURL (anyString ())).thenCallRealMethod ();
117
+
118
+ WorkflowFormValidator validator = new WorkflowFormValidator (mockGithubService );
119
+
120
+ WorkflowForm validFileURL = new WorkflowForm ("https://github.com/nlesc-sherlock/deeplearning/blob/master/CWLworkflow/pipeline.cwl" );
121
+
122
+ Errors errors = new BeanPropertyBindingResult (validFileURL , "workflowForm" );
123
+ GithubDetails details = validator .validateAndParse (validFileURL , errors );
124
+
125
+ assertNull (details );
126
+ assertTrue (errors .hasErrors ());
127
+
128
+ }
129
+
130
+
131
+ }
0 commit comments