Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
unit test stay in junit4
  • Loading branch information
marcdexet-cnrs committed Dec 17, 2018
1 parent c21bae8 commit 0fdfb36
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 104 deletions.
5 changes: 2 additions & 3 deletions pom.xml
Expand Up @@ -5,7 +5,6 @@
<groupId>fr.ias</groupId>
<artifactId>uws4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>


<properties>
Expand Down Expand Up @@ -112,10 +111,10 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- <configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
--> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
220 changes: 119 additions & 101 deletions src/test/java/fr/ias/ivoa/uws4j/controllers/JobControllerTest.java
Expand Up @@ -50,12 +50,10 @@ public class JobControllerTest {
@MockBean JobService jobService;
@MockBean ConverterService converterService;



@Test
public void getAttributes() throws Exception {

//__GIVEN
// __GIVEN
Job job = new Job();
job.setJobId("123456");
job.setRunId("run_id_999");
Expand All @@ -66,21 +64,23 @@ public void getAttributes() throws Exception {
job.setPhase(ExecutionPhase.COMPLETED.toString());
LocalDateTime quote = LocalDateTime.now().plusHours(1);
job.setQuote(quote);

JobList jobList = new JobList("xmatch");

when(jobService.getJobList("xmatch")).thenReturn(jobList);
when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);

when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.RUNID))).thenReturn(Optional.of("run_id_999"));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.JOBID))).thenReturn(Optional.of("123456"));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.DESTRUCTION))).thenReturn(Optional.of(destruction));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.DESTRUCTION)))
.thenReturn(Optional.of(destruction));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.QUOTE))).thenReturn(Optional.of(quote));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.EXECUTIONDURATION))).thenReturn(Optional.of(200));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.EXECUTIONDURATION)))
.thenReturn(Optional.of(200));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.OWNER))).thenReturn(Optional.of("owner"));
when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.PHASE))).thenReturn(Optional.of(ExecutionPhase.COMPLETED.toString()));

when(jobService.findJobAttribute(eq(job), eq(ScalarJobAttribute.PHASE)))
.thenReturn(Optional.of(ExecutionPhase.COMPLETED.toString()));

Map<ScalarJobAttribute, Object> expectations = new EnumMap<>(ScalarJobAttribute.class);
expectations.put(ScalarJobAttribute.RUNID, "run_id_999");
expectations.put(ScalarJobAttribute.JOBID, "123456");
Expand All @@ -89,32 +89,32 @@ public void getAttributes() throws Exception {
expectations.put(ScalarJobAttribute.EXECUTIONDURATION, 200);
expectations.put(ScalarJobAttribute.OWNER, "owner");
expectations.put(ScalarJobAttribute.PHASE, ExecutionPhase.COMPLETED.toString());

for (ScalarJobAttribute attr : ScalarJobAttribute.values()) {
MvcResult result = mvc.perform(get("/uws/xmatch/123456/{attr}", attr.toString().toLowerCase()))
.andExpect(status().isOk())
.andReturn();

Object expected = expectations.get(attr);
assertThat(result.getResponse().getContentAsString())
.as("Expectation for %s}", attr)
.isEqualTo(String.valueOf(expected));
try {
MvcResult result = mvc.perform(get("/uws/xmatch/123456/{attr}", attr.toString().toLowerCase()))
.andExpect(status().isOk()).andReturn();

Object expected = expectations.get(attr);
assertThat(result.getResponse().getContentAsString()).as("Expectation for %s}", attr)
.isEqualTo(String.valueOf(expected));
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw e;
}
}

}

@Test
public void getUnexpectedAttr() throws Exception {
//__GIEVN__
when(jobService.findJobAttribute(any(), any())).thenThrow(IllegalArgumentException.class);
when(jobService.findJobAttribute(any(), any())).thenThrow(new IllegalArgumentException("getUnexpectedAttr"));

// WHEN_THEN
mvc.perform(get("/uws/xmatch/123456/foo"))
.andExpect(status().isNotFound());

}



@Test
public void getParameters() throws Exception {
// GIVEN
Expand All @@ -125,40 +125,54 @@ public void getParameters() throws Exception {
job.setParameters(new LinkedHashMap<>());
job.getParameters().put("foo", "bar");
job.getParameters().put("nb", "1");

when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);

// WHEN_THEN
{
MvcResult result = mvc.perform(get("/uws/xmatch/123456/parameters").accept(APPLICATION_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString()).isEqualTo("{\"foo\":\"bar\",\"nb\":\"1\"}");
}
MvcResult result = mvc.perform(get("/uws/xmatch/123456/parameters").accept(APPLICATION_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString()).isEqualTo("{\"foo\":\"bar\",\"nb\":\"1\"}");
}

@Test
public void getParametersAsXML() throws Exception {
// GIVEN
Job job = new Job();
JobList jobList = new JobList("xmatch");
when(jobService.getJobList("xmatch")).thenReturn(jobList);
when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);


Parameters xmlParameters = new Parameters();
Parameter parm = new Parameter();
parm.setId("foo");
parm.setContent("bar");
xmlParameters.getParameter().add(parm);


when(converterService.translateToXML(eq(Parameters.class), any())).thenReturn(xmlParameters);


// WHEN_THEN
{
Parameters xmlParameters = new Parameters();
Parameter parm = new Parameter();
parm.setId("foo");
parm.setContent("bar");
xmlParameters.getParameter().add(parm);
when(converterService.translateToXML(eq(Parameters.class), any())).thenReturn(xmlParameters);
MvcResult result = mvc.perform(get("/uws/xmatch/123456/parameters").accept(APPLICATION_XML_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<parameters xmlns=\"http://www.ivoa.net/xml/UWS/v1.0\" "
+ "xmlns:ns2=\"http://www.w3.org/1999/xlink\">"
+ "<parameter id=\"foo\">bar</parameter>"
+ "</parameters>");
}
MvcResult result = mvc.perform(get("/uws/xmatch/123456/parameters").accept(APPLICATION_XML_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<parameters xmlns=\"http://www.ivoa.net/xml/UWS/v1.0\" "
+ "xmlns:ns2=\"http://www.w3.org/1999/xlink\">"
+ "<parameter id=\"foo\">bar</parameter>"
+ "</parameters>");
}





@Test
public void getResult() throws Exception {
// GIVEN
Expand All @@ -170,54 +184,58 @@ public void getResult() throws Exception {


// WHEN_THEN
{
Result r = new Result();
r.setHref("http://from/nowhere");
r.setId("foo");
r.setMimeType("mimetype");
r.setType("png");
r.setSize(10L);
job.setResults(new LinkedList<>());
job.getResults().add(r);

when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);
MvcResult result = mvc.perform(get("/uws/xmatch/123456/results").accept(APPLICATION_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.isEqualTo("[{\"id\":\"foo\","
+ "\"href\":\"http://from/nowhere\","
+ "\"type\":\"png\","
+ "\"mimeType\":\"mimetype\","
+ "\"redirection\":false,"
+ "\"size\":10}]");
}
// WHEN_THEN
{
Results results = new Results();

ResultReference r = new ResultReference();
r.setHref("http://from/nowhere");
r.setId("foo");
r.setMimeType("mimetype");
r.setType("png");
r.setSize(10L);
results.getResult().add(r);
when(converterService.translateToXML(eq(Results.class), any())).thenReturn(results);
MvcResult result = mvc.perform(get("/uws/xmatch/123456/results").accept(APPLICATION_XML_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<results xmlns=\"http://www.ivoa.net/xml/UWS/v1.0\" "
+ "xmlns:ns2=\"http://www.w3.org/1999/xlink\">"
+ "<result id=\"foo\" size=\"10\" mime-type=\"mimetype\" ns2:type=\"png\" ns2:href=\"http://from/nowhere\"/>"
+ "</results>");
}
Result r = new Result();
r.setHref("http://from/nowhere");
r.setId("foo");
r.setMimeType("mimetype");
r.setType("png");
r.setSize(10L);
job.setResults(new LinkedList<>());
job.getResults().add(r);

when(jobService.getJob(eq("123456"), eq(jobList))).thenReturn(job);
MvcResult result = mvc.perform(get("/uws/xmatch/123456/results").accept(APPLICATION_JSON_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.isEqualTo("[{\"id\":\"foo\","
+ "\"href\":\"http://from/nowhere\","
+ "\"type\":\"png\","
+ "\"mimeType\":\"mimetype\","
+ "\"redirection\":false,"
+ "\"size\":10}]");
}


public void getResultAsXml() throws Exception {

// GIVEN
Results results = new Results();

ResultReference r = new ResultReference();
r.setHref("http://from/nowhere");
r.setId("foo");
r.setMimeType("mimetype");
r.setType("png");
r.setSize(10L);
results.getResult().add(r);
when(converterService.translateToXML(eq(Results.class), any())).thenReturn(results);

// WHEN_THEN

MvcResult result = mvc.perform(get("/uws/xmatch/123456/results").accept(APPLICATION_XML_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<results xmlns=\"http://www.ivoa.net/xml/UWS/v1.0\" "
+ "xmlns:ns2=\"http://www.w3.org/1999/xlink\">"
+ "<result id=\"foo\" size=\"10\" mime-type=\"mimetype\" ns2:type=\"png\" ns2:href=\"http://from/nowhere\"/>"
+ "</results>");
}

}

0 comments on commit 0fdfb36

Please sign in to comment.