@Test public void shouldTriggerJobAndReturnItsURL() throws Exception { when(jobService.startAsyncJob("someJobType")).thenReturn(Optional.of("theJobId")); mockMvc .perform( MockMvcRequestBuilders.post("/some-microservice/internal/jobs/someJobType") .servletPath("/internal/jobs/someJobType")) .andExpect(MockMvcResultMatchers.status().is(204)) .andExpect( MockMvcResultMatchers.header() .string("Location", "http://localhost/some-microservice/internal/jobs/theJobId")); verify(jobService).startAsyncJob("someJobType"); }
@Test public void shouldReturn404IfJobIsUnknown() throws Exception { // given when(jobService.findJob(any(String.class))).thenReturn(Optional.<JobInfo>empty()); // when mockMvc .perform(MockMvcRequestBuilders.get("/some-microservice/internal/jobs/42")) .andExpect(MockMvcResultMatchers.status().is(404)); }
@Test @SuppressWarnings("unchecked") public void shouldReturnAllJobsOfTypeAsHtml() { JobInfo firstJob = newJobInfo("42", "SOME_TYPE", systemDefaultZone(), "localhost"); when(jobService.findJobs(Optional.of("SOME_TYPE"), 100)).thenReturn(asList(firstJob)); ModelAndView modelAndView = jobsController.getJobsAsHtml("SOME_TYPE", 100, mock(HttpServletRequest.class)); List<JobRepresentation> jobs = (List<JobRepresentation>) modelAndView.getModel().get("jobs"); assertThat(jobs, is(asList(representationOf(firstJob, false, "")))); }
@Test public void shouldReturnAllJobs() throws IOException { // given JobInfo firstJob = newJobInfo("42", "TEST", fixed(ofEpochMilli(0), systemDefault()), "localhost"); JobInfo secondJob = newJobInfo("42", "TEST", fixed(ofEpochMilli(1), systemDefault()), "localhost"); when(jobService.findJobs(Optional.<String>empty(), 100)) .thenReturn(asList(firstJob, secondJob)); // when Object job = jobsController.getJobsAsJson(null, 100, mock(HttpServletRequest.class)); // then assertThat( job, is(asList(representationOf(firstJob, false, ""), representationOf(secondJob, false, "")))); }
@Test public void shouldReturnJobIfJobExists() throws Exception { // given ZoneId cet = ZoneId.of("CET"); OffsetDateTime now = OffsetDateTime.now(cet); JobInfo expectedJob = newJobInfo("42", "TEST", fixed(now.toInstant(), cet), "localhost"); when(jobService.findJob("42")).thenReturn(Optional.of(expectedJob)); String nowAsString = ISO_OFFSET_DATE_TIME.format(now); mockMvc .perform( MockMvcRequestBuilders.get("/some-microservice/internal/jobs/42") .servletPath("/internal/jobs/42")) .andExpect(MockMvcResultMatchers.status().is(200)) .andExpect(MockMvcResultMatchers.jsonPath("$.status").value("OK")) .andExpect(MockMvcResultMatchers.jsonPath("$.messages").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$.jobType").value("TEST")) .andExpect(MockMvcResultMatchers.jsonPath("$.hostname").value("localhost")) .andExpect(MockMvcResultMatchers.jsonPath("$.started").value(nowAsString)) .andExpect(MockMvcResultMatchers.jsonPath("$.stopped").value("")) .andExpect(MockMvcResultMatchers.jsonPath("$.lastUpdated").value(nowAsString)) .andExpect( MockMvcResultMatchers.jsonPath("$.jobUri") .value("http://localhost/some-microservice/internal/jobs/42")) .andExpect(MockMvcResultMatchers.jsonPath("$.links").isArray()) .andExpect( MockMvcResultMatchers.jsonPath("$.links[0].href") .value("http://localhost/some-microservice/internal/jobs/42")) .andExpect( MockMvcResultMatchers.jsonPath("$.links[1].href") .value("http://localhost/some-microservice/internal/jobdefinitions/TEST")) .andExpect( MockMvcResultMatchers.jsonPath("$.links[2].href") .value("http://localhost/some-microservice/internal/jobs")) .andExpect( MockMvcResultMatchers.jsonPath("$.links[3].href") .value("http://localhost/some-microservice/internal/jobs?type=TEST")) .andExpect(MockMvcResultMatchers.jsonPath("$.runtime").value("")) .andExpect(MockMvcResultMatchers.jsonPath("$.state").value("Running")); verify(jobService).findJob("42"); }