Skip to content

Commit 0fdfb36

Browse files
unit test stay in junit4
1 parent c21bae8 commit 0fdfb36

File tree

2 files changed

+121
-104
lines changed

2 files changed

+121
-104
lines changed

pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<groupId>fr.ias</groupId>
66
<artifactId>uws4j</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
8-
<packaging>pom</packaging>
98

109

1110
<properties>
@@ -112,10 +111,10 @@
112111
<plugin>
113112
<groupId>org.springframework.boot</groupId>
114113
<artifactId>spring-boot-maven-plugin</artifactId>
115-
<configuration>
114+
<!-- <configuration>
116115
<classifier>exec</classifier>
117116
</configuration>
118-
</plugin>
117+
--> </plugin>
119118
<plugin>
120119
<groupId>org.apache.maven.plugins</groupId>
121120
<artifactId>maven-surefire-plugin</artifactId>

src/test/java/fr/ias/ivoa/uws4j/controllers/JobControllerTest.java

+119-101
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ public class JobControllerTest {
5050
@MockBean JobService jobService;
5151
@MockBean ConverterService converterService;
5252

53-
54-
5553
@Test
5654
public void getAttributes() throws Exception {
5755

58-
//__GIVEN
56+
// __GIVEN
5957
Job job = new Job();
6058
job.setJobId("123456");
6159
job.setRunId("run_id_999");
@@ -66,21 +64,23 @@ public void getAttributes() throws Exception {
6664
job.setPhase(ExecutionPhase.COMPLETED.toString());
6765
LocalDateTime quote = LocalDateTime.now().plusHours(1);
6866
job.setQuote(quote);
69-
67+
7068
JobList jobList = new JobList("xmatch");
71-
69+
7270
when(jobService.getJobList("xmatch")).thenReturn(jobList);
7371
when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);
7472

7573
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.RUNID))).thenReturn(Optional.of("run_id_999"));
7674
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));
7877
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));
8080
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+
8484
Map<ScalarJobAttribute, Object> expectations = new EnumMap<>(ScalarJobAttribute.class);
8585
expectations.put(ScalarJobAttribute.RUNID, "run_id_999");
8686
expectations.put(ScalarJobAttribute.JOBID, "123456");
@@ -89,32 +89,32 @@ public void getAttributes() throws Exception {
8989
expectations.put(ScalarJobAttribute.EXECUTIONDURATION, 200);
9090
expectations.put(ScalarJobAttribute.OWNER, "owner");
9191
expectations.put(ScalarJobAttribute.PHASE, ExecutionPhase.COMPLETED.toString());
92-
92+
9393
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+
}
102105
}
103-
104106
}
105-
107+
106108
@Test
107109
public void getUnexpectedAttr() throws Exception {
108110
//__GIEVN__
109-
when(jobService.findJobAttribute(any(), any())).thenThrow(IllegalArgumentException.class);
111+
when(jobService.findJobAttribute(any(), any())).thenThrow(new IllegalArgumentException("getUnexpectedAttr"));
110112

111113
// WHEN_THEN
112114
mvc.perform(get("/uws/xmatch/123456/foo"))
113115
.andExpect(status().isNotFound());
114-
115116
}
116-
117-
117+
118118
@Test
119119
public void getParameters() throws Exception {
120120
// GIVEN
@@ -125,40 +125,54 @@ public void getParameters() throws Exception {
125125
job.setParameters(new LinkedHashMap<>());
126126
job.getParameters().put("foo", "bar");
127127
job.getParameters().put("nb", "1");
128+
128129
when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);
129130

130131
// 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+
139159
// 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>");
159171
}
172+
160173

161-
174+
175+
162176
@Test
163177
public void getResult() throws Exception {
164178
// GIVEN
@@ -170,54 +184,58 @@ public void getResult() throws Exception {
170184

171185

172186
// 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}]");
221209
}
222210

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+
223241
}

0 commit comments

Comments
 (0)