1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ package org .commonwl .view .workflow ;
21
+
22
+ import org .apache .commons .io .IOUtils ;
23
+ import org .commonwl .view .cwl .RDFService ;
24
+ import org .commonwl .view .git .GitDetails ;
25
+ import org .junit .Before ;
26
+ import org .junit .Test ;
27
+ import org .junit .runner .RunWith ;
28
+ import org .mockito .Mockito ;
29
+ import org .springframework .boot .test .context .SpringBootTest ;
30
+ import org .springframework .core .io .FileSystemResource ;
31
+ import org .springframework .test .context .junit4 .SpringRunner ;
32
+ import org .springframework .test .web .servlet .MockMvc ;
33
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
34
+
35
+ import java .io .File ;
36
+ import java .io .FileInputStream ;
37
+
38
+ import static org .mockito .Matchers .*;
39
+ import static org .mockito .Mockito .when ;
40
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
41
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
42
+
43
+ /**
44
+ * Tests the controller for workflow related functionality
45
+ */
46
+ @ RunWith (SpringRunner .class )
47
+ @ SpringBootTest
48
+ public class WorkflowPermalinkControllerTest {
49
+
50
+ private MockMvc mockMvc ;
51
+ private byte [] rdfResponse ;
52
+ private FileSystemResource png = new FileSystemResource ("src/test/resources/graphviz/testVis.png" );
53
+ private FileSystemResource svg = new FileSystemResource ("src/test/resources/graphviz/testVis.svg" );
54
+ private FileSystemResource dot = new FileSystemResource ("src/test/resources/graphviz/testWorkflow.dot" );
55
+
56
+ @ Before
57
+ public void setUp () throws Exception {
58
+
59
+ Workflow mockWorkflow = Mockito .mock (Workflow .class );
60
+ when (mockWorkflow .getRetrievedFrom ())
61
+ .thenReturn (new GitDetails ("https://github.com/MarkRobbo/workflows.git" ,
62
+ "master" , "path/to/workflow.cwl" ));
63
+
64
+ WorkflowService mockWorkflowService = Mockito .mock (WorkflowService .class );
65
+ when (mockWorkflowService .findByCommitAndPath (anyString (), anyString ()))
66
+ .thenReturn (mockWorkflow );
67
+ when (mockWorkflowService .getWorkflowGraph (eq ("svg" ), anyObject ())).thenReturn (svg );
68
+ when (mockWorkflowService .getWorkflowGraph (eq ("png" ), anyObject ())).thenReturn (png );
69
+ when (mockWorkflowService .getWorkflowGraph (eq ("xdot" ), anyObject ())).thenReturn (dot );
70
+ when (mockWorkflowService .getROBundle (anyObject ())).thenReturn (new File ("src/test/resources/nonsense.zip" ));
71
+
72
+ RDFService mockRdfService = Mockito .mock (RDFService .class );
73
+ when (mockRdfService .graphExists (anyString ())).thenReturn (true );
74
+
75
+ File turtleFile = new File ("src/test/resources/cwl/make_to_cwl/dna.ttl" );
76
+ FileInputStream fileInputStream = new FileInputStream (turtleFile );
77
+ rdfResponse = IOUtils .toByteArray (fileInputStream );
78
+ when (mockRdfService .getModel (anyString (), anyString ()))
79
+ .thenReturn (rdfResponse );
80
+
81
+ // Mock controller/MVC
82
+ WorkflowPermalinkController underTest = new WorkflowPermalinkController (
83
+ mockWorkflowService ,
84
+ mockRdfService );
85
+
86
+ mockMvc = MockMvcBuilders
87
+ .standaloneSetup (underTest )
88
+ .build ();
89
+ }
90
+
91
+ @ Test
92
+ public void goToViewer () throws Exception {
93
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
94
+ .header ("accept" , "text/html" ))
95
+ .andExpect (status ().isFound ())
96
+ .andExpect (redirectedUrl ("/workflows/github.com/MarkRobbo/workflows/blob/commitidhere/path/to/workflow.cwl" ));
97
+ }
98
+
99
+ @ Test
100
+ public void goToRawFile () throws Exception {
101
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
102
+ .header ("accept" , "application/x-yaml" ))
103
+ .andExpect (status ().isFound ())
104
+ .andExpect (redirectedUrl ("https://raw.githubusercontent.com/MarkRobbo/workflows/commitidhere/path/to/workflow.cwl" ));
105
+ }
106
+
107
+ @ Test
108
+ public void getRdfTurtle () throws Exception {
109
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
110
+ .header ("accept" , "text/turtle" ))
111
+ .andExpect (status ().isOk ())
112
+ .andExpect (content ().contentType ("text/turtle" ))
113
+ .andExpect (content ().bytes (rdfResponse ));
114
+ }
115
+
116
+ @ Test
117
+ public void getRdfJsonld () throws Exception {
118
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
119
+ .header ("accept" , "application/ld+json" ))
120
+ .andExpect (status ().isOk ())
121
+ .andExpect (content ().contentType ("application/ld+json" ))
122
+ .andExpect (content ().bytes (rdfResponse ));
123
+ }
124
+
125
+ @ Test
126
+ public void getRdfXml () throws Exception {
127
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
128
+ .header ("accept" , "application/rdf+xml" ))
129
+ .andExpect (status ().isOk ())
130
+ .andExpect (content ().contentType ("application/rdf+xml" ))
131
+ .andExpect (content ().bytes (rdfResponse ));
132
+ }
133
+
134
+ @ Test
135
+ public void getPng () throws Exception {
136
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
137
+ .header ("accept" , "image/png" ))
138
+ .andExpect (status ().isOk ())
139
+ .andExpect (content ().contentType ("image/png" ))
140
+ .andExpect (content ().bytes (IOUtils .toByteArray (png .getInputStream ())));
141
+ }
142
+
143
+ @ Test
144
+ public void getSvg () throws Exception {
145
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
146
+ .header ("accept" , "image/svg+xml" ))
147
+ .andExpect (status ().isOk ())
148
+ .andExpect (content ().contentType ("image/svg+xml" ))
149
+ .andExpect (content ().bytes (IOUtils .toByteArray (svg .getInputStream ())));
150
+ }
151
+
152
+ @ Test
153
+ public void getDot () throws Exception {
154
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
155
+ .header ("accept" , "text/vnd+graphviz" ))
156
+ .andExpect (status ().isOk ())
157
+ .andExpect (content ().contentType ("text/vnd+graphviz" ))
158
+ .andExpect (content ().bytes (IOUtils .toByteArray (dot .getInputStream ())));
159
+ }
160
+
161
+ @ Test
162
+ public void getRoBundle () throws Exception {
163
+ mockMvc .perform (get ("/git/commitidhere/path/to/workflow.cwl" )
164
+ .header ("accept" , "application/vnd.wf4ever.robundle+zip" ))
165
+ .andExpect (status ().isOk ())
166
+ .andExpect (content ().contentType ("application/vnd.wf4ever.robundle+zip" ));
167
+ }
168
+ }
0 commit comments