@@ -50,12 +50,10 @@ public class JobControllerTest {
50
50
@ MockBean JobService jobService ;
51
51
@ MockBean ConverterService converterService ;
52
52
53
-
54
-
55
53
@ Test
56
54
public void getAttributes () throws Exception {
57
55
58
- //__GIVEN
56
+ // __GIVEN
59
57
Job job = new Job ();
60
58
job .setJobId ("123456" );
61
59
job .setRunId ("run_id_999" );
@@ -66,21 +64,23 @@ public void getAttributes() throws Exception {
66
64
job .setPhase (ExecutionPhase .COMPLETED .toString ());
67
65
LocalDateTime quote = LocalDateTime .now ().plusHours (1 );
68
66
job .setQuote (quote );
69
-
67
+
70
68
JobList jobList = new JobList ("xmatch" );
71
-
69
+
72
70
when (jobService .getJobList ("xmatch" )).thenReturn (jobList );
73
71
when (jobService .getJob (eq ("123456" ), eq (jobList ))).thenReturn (job );
74
72
75
73
when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .RUNID ))).thenReturn (Optional .of ("run_id_999" ));
76
74
when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .JOBID ))).thenReturn (Optional .of ("123456" ));
77
- when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .DESTRUCTION ))).thenReturn (Optional .of (destruction ));
75
+ when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .DESTRUCTION )))
76
+ .thenReturn (Optional .of (destruction ));
78
77
when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .QUOTE ))).thenReturn (Optional .of (quote ));
79
- when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .EXECUTIONDURATION ))).thenReturn (Optional .of (200 ));
78
+ when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .EXECUTIONDURATION )))
79
+ .thenReturn (Optional .of (200 ));
80
80
when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .OWNER ))).thenReturn (Optional .of ("owner" ));
81
- when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .PHASE ))). thenReturn ( Optional . of ( ExecutionPhase . COMPLETED . toString ()));
82
-
83
-
81
+ when (jobService .findJobAttribute (eq (job ), eq (ScalarJobAttribute .PHASE )))
82
+ . thenReturn ( Optional . of ( ExecutionPhase . COMPLETED . toString ()));
83
+
84
84
Map <ScalarJobAttribute , Object > expectations = new EnumMap <>(ScalarJobAttribute .class );
85
85
expectations .put (ScalarJobAttribute .RUNID , "run_id_999" );
86
86
expectations .put (ScalarJobAttribute .JOBID , "123456" );
@@ -89,32 +89,32 @@ public void getAttributes() throws Exception {
89
89
expectations .put (ScalarJobAttribute .EXECUTIONDURATION , 200 );
90
90
expectations .put (ScalarJobAttribute .OWNER , "owner" );
91
91
expectations .put (ScalarJobAttribute .PHASE , ExecutionPhase .COMPLETED .toString ());
92
-
92
+
93
93
for (ScalarJobAttribute attr : ScalarJobAttribute .values ()) {
94
- MvcResult result = mvc .perform (get ("/uws/xmatch/123456/{attr}" , attr .toString ().toLowerCase ()))
95
- .andExpect (status ().isOk ())
96
- .andReturn ();
97
-
98
- Object expected = expectations .get (attr );
99
- assertThat (result .getResponse ().getContentAsString ())
100
- .as ("Expectation for %s}" , attr )
101
- .isEqualTo (String .valueOf (expected ));
94
+ try {
95
+ MvcResult result = mvc .perform (get ("/uws/xmatch/123456/{attr}" , attr .toString ().toLowerCase ()))
96
+ .andExpect (status ().isOk ()).andReturn ();
97
+
98
+ Object expected = expectations .get (attr );
99
+ assertThat (result .getResponse ().getContentAsString ()).as ("Expectation for %s}" , attr )
100
+ .isEqualTo (String .valueOf (expected ));
101
+ } catch (IllegalArgumentException e ) {
102
+ e .printStackTrace ();
103
+ throw e ;
104
+ }
102
105
}
103
-
104
106
}
105
-
107
+
106
108
@ Test
107
109
public void getUnexpectedAttr () throws Exception {
108
110
//__GIEVN__
109
- when (jobService .findJobAttribute (any (), any ())).thenThrow (IllegalArgumentException . class );
111
+ when (jobService .findJobAttribute (any (), any ())).thenThrow (new IllegalArgumentException ( "getUnexpectedAttr" ) );
110
112
111
113
// WHEN_THEN
112
114
mvc .perform (get ("/uws/xmatch/123456/foo" ))
113
115
.andExpect (status ().isNotFound ());
114
-
115
116
}
116
-
117
-
117
+
118
118
@ Test
119
119
public void getParameters () throws Exception {
120
120
// GIVEN
@@ -125,40 +125,54 @@ public void getParameters() throws Exception {
125
125
job .setParameters (new LinkedHashMap <>());
126
126
job .getParameters ().put ("foo" , "bar" );
127
127
job .getParameters ().put ("nb" , "1" );
128
+
128
129
when (jobService .getJob (eq ("123456" ), eq (jobList ))).thenReturn (job );
129
130
130
131
// WHEN_THEN
131
- {
132
- MvcResult result = mvc .perform (get ("/uws/xmatch/123456/parameters" ).accept (APPLICATION_JSON_VALUE ))
133
- .andDo (print ())
134
- .andExpect (status ().isOk ())
135
- .andReturn ();
136
-
137
- assertThat (result .getResponse ().getContentAsString ()).isEqualTo ("{\" foo\" :\" bar\" ,\" nb\" :\" 1\" }" );
138
- }
132
+ MvcResult result = mvc .perform (get ("/uws/xmatch/123456/parameters" ).accept (APPLICATION_JSON_VALUE ))
133
+ .andDo (print ())
134
+ .andExpect (status ().isOk ())
135
+ .andReturn ();
136
+
137
+ assertThat (result .getResponse ().getContentAsString ()).isEqualTo ("{\" foo\" :\" bar\" ,\" nb\" :\" 1\" }" );
138
+ }
139
+
140
+ @ Test
141
+ public void getParametersAsXML () throws Exception {
142
+ // GIVEN
143
+ Job job = new Job ();
144
+ JobList jobList = new JobList ("xmatch" );
145
+ when (jobService .getJobList ("xmatch" )).thenReturn (jobList );
146
+ when (jobService .getJob (eq ("123456" ), eq (jobList ))).thenReturn (job );
147
+
148
+
149
+ Parameters xmlParameters = new Parameters ();
150
+ Parameter parm = new Parameter ();
151
+ parm .setId ("foo" );
152
+ parm .setContent ("bar" );
153
+ xmlParameters .getParameter ().add (parm );
154
+
155
+
156
+ when (converterService .translateToXML (eq (Parameters .class ), any ())).thenReturn (xmlParameters );
157
+
158
+
139
159
// WHEN_THEN
140
- {
141
- Parameters xmlParameters = new Parameters ();
142
- Parameter parm = new Parameter ();
143
- parm .setId ("foo" );
144
- parm .setContent ("bar" );
145
- xmlParameters .getParameter ().add (parm );
146
- when (converterService .translateToXML (eq (Parameters .class ), any ())).thenReturn (xmlParameters );
147
- MvcResult result = mvc .perform (get ("/uws/xmatch/123456/parameters" ).accept (APPLICATION_XML_VALUE ))
148
- .andDo (print ())
149
- .andExpect (status ().isOk ())
150
- .andReturn ();
151
-
152
- assertThat (result .getResponse ().getContentAsString ())
153
- .isEqualTo ("<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" yes\" ?>"
154
- + "<parameters xmlns=\" http://www.ivoa.net/xml/UWS/v1.0\" "
155
- + "xmlns:ns2=\" http://www.w3.org/1999/xlink\" >"
156
- + "<parameter id=\" foo\" >bar</parameter>"
157
- + "</parameters>" );
158
- }
160
+ MvcResult result = mvc .perform (get ("/uws/xmatch/123456/parameters" ).accept (APPLICATION_XML_VALUE ))
161
+ .andDo (print ())
162
+ .andExpect (status ().isOk ())
163
+ .andReturn ();
164
+
165
+ assertThat (result .getResponse ().getContentAsString ())
166
+ .isEqualTo ("<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" yes\" ?>"
167
+ + "<parameters xmlns=\" http://www.ivoa.net/xml/UWS/v1.0\" "
168
+ + "xmlns:ns2=\" http://www.w3.org/1999/xlink\" >"
169
+ + "<parameter id=\" foo\" >bar</parameter>"
170
+ + "</parameters>" );
159
171
}
172
+
160
173
161
-
174
+
175
+
162
176
@ Test
163
177
public void getResult () throws Exception {
164
178
// GIVEN
@@ -170,54 +184,58 @@ public void getResult() throws Exception {
170
184
171
185
172
186
// WHEN_THEN
173
- {
174
- Result r = new Result ();
175
- r .setHref ("http://from/nowhere" );
176
- r .setId ("foo" );
177
- r .setMimeType ("mimetype" );
178
- r .setType ("png" );
179
- r .setSize (10L );
180
- job .setResults (new LinkedList <>());
181
- job .getResults ().add (r );
182
-
183
- when (jobService .getJob (eq ("123456" ), eq (jobList ))).thenReturn (job );
184
- MvcResult result = mvc .perform (get ("/uws/xmatch/123456/results" ).accept (APPLICATION_JSON_VALUE ))
185
- .andDo (print ())
186
- .andExpect (status ().isOk ())
187
- .andReturn ();
188
-
189
- assertThat (result .getResponse ().getContentAsString ())
190
- .isEqualTo ("[{\" id\" :\" foo\" ,"
191
- + "\" href\" :\" http://from/nowhere\" ,"
192
- + "\" type\" :\" png\" ,"
193
- + "\" mimeType\" :\" mimetype\" ,"
194
- + "\" redirection\" :false,"
195
- + "\" size\" :10}]" );
196
- }
197
- // WHEN_THEN
198
- {
199
- Results results = new Results ();
200
-
201
- ResultReference r = new ResultReference ();
202
- r .setHref ("http://from/nowhere" );
203
- r .setId ("foo" );
204
- r .setMimeType ("mimetype" );
205
- r .setType ("png" );
206
- r .setSize (10L );
207
- results .getResult ().add (r );
208
- when (converterService .translateToXML (eq (Results .class ), any ())).thenReturn (results );
209
- MvcResult result = mvc .perform (get ("/uws/xmatch/123456/results" ).accept (APPLICATION_XML_VALUE ))
210
- .andDo (print ())
211
- .andExpect (status ().isOk ())
212
- .andReturn ();
213
-
214
- assertThat (result .getResponse ().getContentAsString ())
215
- .isEqualTo ("<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" yes\" ?>"
216
- + "<results xmlns=\" http://www.ivoa.net/xml/UWS/v1.0\" "
217
- + "xmlns:ns2=\" http://www.w3.org/1999/xlink\" >"
218
- + "<result id=\" foo\" size=\" 10\" mime-type=\" mimetype\" ns2:type=\" png\" ns2:href=\" http://from/nowhere\" />"
219
- + "</results>" );
220
- }
187
+ Result r = new Result ();
188
+ r .setHref ("http://from/nowhere" );
189
+ r .setId ("foo" );
190
+ r .setMimeType ("mimetype" );
191
+ r .setType ("png" );
192
+ r .setSize (10L );
193
+ job .setResults (new LinkedList <>());
194
+ job .getResults ().add (r );
195
+
196
+ when (jobService .getJob (eq ("123456" ), eq (jobList ))).thenReturn (job );
197
+ MvcResult result = mvc .perform (get ("/uws/xmatch/123456/results" ).accept (APPLICATION_JSON_VALUE ))
198
+ .andDo (print ())
199
+ .andExpect (status ().isOk ())
200
+ .andReturn ();
201
+
202
+ assertThat (result .getResponse ().getContentAsString ())
203
+ .isEqualTo ("[{\" id\" :\" foo\" ,"
204
+ + "\" href\" :\" http://from/nowhere\" ,"
205
+ + "\" type\" :\" png\" ,"
206
+ + "\" mimeType\" :\" mimetype\" ,"
207
+ + "\" redirection\" :false,"
208
+ + "\" size\" :10}]" );
221
209
}
222
210
211
+
212
+ public void getResultAsXml () throws Exception {
213
+
214
+ // GIVEN
215
+ Results results = new Results ();
216
+
217
+ ResultReference r = new ResultReference ();
218
+ r .setHref ("http://from/nowhere" );
219
+ r .setId ("foo" );
220
+ r .setMimeType ("mimetype" );
221
+ r .setType ("png" );
222
+ r .setSize (10L );
223
+ results .getResult ().add (r );
224
+ when (converterService .translateToXML (eq (Results .class ), any ())).thenReturn (results );
225
+
226
+ // WHEN_THEN
227
+
228
+ MvcResult result = mvc .perform (get ("/uws/xmatch/123456/results" ).accept (APPLICATION_XML_VALUE ))
229
+ .andDo (print ())
230
+ .andExpect (status ().isOk ())
231
+ .andReturn ();
232
+
233
+ assertThat (result .getResponse ().getContentAsString ())
234
+ .isEqualTo ("<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" yes\" ?>"
235
+ + "<results xmlns=\" http://www.ivoa.net/xml/UWS/v1.0\" "
236
+ + "xmlns:ns2=\" http://www.w3.org/1999/xlink\" >"
237
+ + "<result id=\" foo\" size=\" 10\" mime-type=\" mimetype\" ns2:type=\" png\" ns2:href=\" http://from/nowhere\" />"
238
+ + "</results>" );
239
+ }
240
+
223
241
}
0 commit comments