From 0fdfb360985fd4c7b9dd0f1ec619d9eca069ed9c Mon Sep 17 00:00:00 2001 From: Marc DeXeT Date: Tue, 18 Dec 2018 00:10:04 +0100 Subject: [PATCH] Because of https://github.com/spring-projects/spring-boot/issues/12470, unit test stay in junit4 --- pom.xml | 5 +- .../uws4j/controllers/JobControllerTest.java | 220 ++++++++++-------- 2 files changed, 121 insertions(+), 104 deletions(-) diff --git a/pom.xml b/pom.xml index 4010701..9a77dee 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,6 @@ fr.ias uws4j 0.0.1-SNAPSHOT - pom @@ -112,10 +111,10 @@ org.springframework.boot spring-boot-maven-plugin - + org.apache.maven.plugins maven-surefire-plugin diff --git a/src/test/java/fr/ias/ivoa/uws4j/controllers/JobControllerTest.java b/src/test/java/fr/ias/ivoa/uws4j/controllers/JobControllerTest.java index 0e4031d..093a8d2 100644 --- a/src/test/java/fr/ias/ivoa/uws4j/controllers/JobControllerTest.java +++ b/src/test/java/fr/ias/ivoa/uws4j/controllers/JobControllerTest.java @@ -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"); @@ -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 expectations = new EnumMap<>(ScalarJobAttribute.class); expectations.put(ScalarJobAttribute.RUNID, "run_id_999"); expectations.put(ScalarJobAttribute.JOBID, "123456"); @@ -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 @@ -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("" - + "" - + "bar" - + ""); - } + MvcResult result = mvc.perform(get("/uws/xmatch/123456/parameters").accept(APPLICATION_XML_VALUE)) + .andDo(print()) + .andExpect(status().isOk()) + .andReturn(); + + assertThat(result.getResponse().getContentAsString()) + .isEqualTo("" + + "" + + "bar" + + ""); } + - + + @Test public void getResult() throws Exception { // GIVEN @@ -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("" - + "" - + "" - + ""); - } + 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("" + + "" + + "" + + ""); + } + }