@Test public void shouldStartBeforeRun() throws Exception { //given JobConfig jobConfig = JobConfig.exampleConfig(); //when JLineupRunStatus beforeStatus = testee.startBeforeRun(jobConfig); beforeStatus.getCurrentJobStepFuture().get().get(); //then verify(jLineupRunnerFactory).createBeforeRun(beforeStatus.getId(), jobConfig); verify(jLineupRunnerBefore).run(); }
@Test public void shouldStartAfterRun() throws Exception { //given JobConfig jobConfig = JobConfig.exampleConfig(); when(jLineupRunnerBefore.run()).thenReturn(true); JLineupRunStatus beforeStatus = testee.startBeforeRun(jobConfig); //when beforeStatus.getCurrentJobStepFuture().get().get(); JLineupRunStatus afterStatus = testee.startAfterRun(beforeStatus.getId()); afterStatus.getCurrentJobStepFuture().get().get(); //then verify(jLineupRunnerFactory).createAfterRun(beforeStatus.getId(), jobConfig); verify(jLineupRunnerAfter).run(); }
@Test public void shouldContainReportPathInResult() throws Exception { //given JobConfig jobConfig = JobConfig.exampleConfig(); when(jLineupRunnerBefore.run()).thenReturn(true); JLineupRunStatus beforeStatus = testee.startBeforeRun(jobConfig); when(jLineupRunnerAfter.run()).thenReturn(true); beforeStatus.getCurrentJobStepFuture().get().get(); JLineupRunStatus afterStatus = testee.startAfterRun(beforeStatus.getId()); afterStatus.getCurrentJobStepFuture().get().get(); //when Optional<JLineupRunStatus> status = testee.getRun(beforeStatus.getId()); //then assertTrue(status.isPresent()); assertThat(status.get().getState(), is(State.FINISHED_WITHOUT_DIFFERENCES)); assertThat(status.get().getReports().getHtmlUrl(), is("/reports/report-" + beforeStatus.getId() + "/report.html")); assertThat(status.get().getReports().getJsonUrl(), is("/reports/report-" + beforeStatus.getId() + "/report.json")); }
@Test(expected = WebDriverException.class) public void shouldCheckForErrors() { //given when(webDriver.manage().logs().get(LogType.BROWSER)).thenReturn(new LogEntries(of(new LogEntry(Level.SEVERE, Instant.now().toEpochMilli(), "Fehler!")))); //when new LogErrorChecker().checkForErrors(webDriver, JobConfig.exampleConfig()); //then expect exception fail(); }
@Test public void shouldUseDefinedNumberOfThreadsIfConfiguredAndBelowMax() throws BrowserNotInstalledException { //Given JobConfig jobConfig = copyOfBuilder(exampleConfig()) .withThreads(2) .build(); //When JobConfig sanitizedConfig = jLineupRunnerFactory.sanitizeJobConfig(jobConfig); //Then assertThat(sanitizedConfig.threads, is(2)); }
@Test public void shouldUseMaxThreadsPerJobIfNoThreadsAreConfigured() throws BrowserNotInstalledException { //Given JobConfig jobConfig = copyOfBuilder(exampleConfig()) .withThreads(0) .build(); //When JobConfig sanitizedConfig = jLineupRunnerFactory.sanitizeJobConfig(jobConfig); //Then assertThat(sanitizedConfig.threads, is(jLineupWebProperties.getMaxThreadsPerJob())); }
@Test(expected = BrowserNotInstalledException.class) public void shouldThrowBrowserNotInstalledException() throws BrowserNotInstalledException { // Given JobConfig jobConfig = copyOfBuilder(exampleConfig()) .withBrowser(FIREFOX) .build(); //When jLineupRunnerFactory.sanitizeJobConfig(jobConfig); }
@Test public void shouldReduceNumberOfThreadsToMax() throws BrowserNotInstalledException { //Given JobConfig jobConfig = copyOfBuilder(exampleConfig()) .withThreads(200) .build(); //When JobConfig sanitizedConfig = jLineupRunnerFactory.sanitizeJobConfig(jobConfig); //Then assertThat(sanitizedConfig.threads, is(jLineupWebProperties.getMaxThreadsPerJob())); } }
@Test public void shouldWriteComparisonReportAsJson() throws Exception { ScreenshotComparisonResult screenshotComparisonResult = new ScreenshotComparisonResult("url", 1337, 1338, 0d, "before", "after", "differenceImageFileName", 0); final Summary localSummary = new Summary(false, 0d, 0d); final Summary globalSummary = new Summary(false, 0d, 0d); Report report = new Report(globalSummary, singletonMap("test", new UrlReport(singletonList(screenshotComparisonResult), localSummary)), JobConfig.exampleConfig()); String expectedString = "[\n" + " {\n" + " \"url\": \"url\",\n" + " \"width\": 1337,\n" + " \"verticalScrollPosition\": 1338,\n" + " \"difference\": 0.0,\n" + " \"screenshotBeforeFileName\": \"before\",\n" + " \"screenshotAfterFileName\": \"after\",\n" + " \"differenceImageFileName\": \"differenceImageFileName\",\n" + " \"maxSingleColorDifference\": 0\n" + " }\n" + "]"; testee.writeComparisonReportAsJson(report); Mockito.verify(fileServiceMock).writeJsonReport(expectedString); } }
@Test public void shouldSanitizeJobConfig() throws BrowserNotInstalledException { //Given JobConfig evilJobConfig = copyOfBuilder(exampleConfig()) .withBrowser(CHROME_HEADLESS) .withThreads(Integer.MAX_VALUE) .withDebug(true) .withLogToFile(true) .withReportFormat(1) .build(); //When JobConfig sanitizedJobConfig = jLineupRunnerFactory.sanitizeJobConfig(evilJobConfig); //Then assertThat(sanitizedJobConfig.threads, is(jLineupWebProperties.getMaxThreadsPerJob())); assertThat(sanitizedJobConfig.debug, is(false)); assertThat(sanitizedJobConfig.logToFile, is(false)); assertThat(sanitizedJobConfig.reportFormat, is(DEFAULT_REPORT_FORMAT)); }
@Test public void shouldReturnRun() throws Exception { // given when(jLineupService.getRun("someId")).thenReturn(Optional.of(runStatusBuilder() .withId("someId") .withState(State.BEFORE_RUNNING) .withJobConfig(exampleConfig()) .build())); // when ResultActions result = mvc .perform(get("/testContextPath/runs/someId") .contextPath("/testContextPath") .accept(MediaType.APPLICATION_JSON)); // then result.andExpect(status().isOk()); }
final Summary globalSummary = new Summary(false, 0d, 0d); final Summary localSummary = new Summary(false, 0d, 0d); Report report = new Report(globalSummary, Collections.singletonMap("test", new UrlReport(singletonList(screenshotComparisonResult), localSummary)), exampleConfig());
@Test public void shouldReturn422ForUnsupportedBrowser() throws Exception { // given JobConfig jobConfig = exampleConfig(); when(jLineupService.startBeforeRun(jobConfig)).thenThrow(new BrowserNotInstalledException(jobConfig.browser)); // when ResultActions result = mvc .perform(post("/runs") .content(JobConfig.prettyPrint(jobConfig)) .contentType(MediaType.APPLICATION_JSON)); // then result .andExpect(status().isUnprocessableEntity()) .andExpect(content().string(containsString(jobConfig.browser.name()))); }
@Test public void shouldStartAfterRun() throws Exception { // given JLineupRunStatus run = runStatusBuilder().withId("someRunId").withJobConfig(exampleConfig()).withState(State.AFTER_RUNNING).build(); when(jLineupService.startAfterRun("someRunId")).thenReturn(run); // when ResultActions result = mvc .perform(post("/testContextPath/runs/someRunId") .contextPath("/testContextPath") .contentType(MediaType.APPLICATION_JSON)); // then result .andExpect(status().isAccepted()) .andExpect(header().string("Location", "/testContextPath/runs/someRunId")); }
@Test public void shouldReturn422IfConfigValidationFails() throws Exception { // given JobConfig jobConfig = JobConfig.copyOfBuilder(exampleConfig()).withUrls(null).build(); when(jLineupService.startBeforeRun(jobConfig)).thenThrow(new ConfigValidationException("Validation message")); // when ResultActions result = mvc .perform(post("/runs") .content(JobConfig.prettyPrint(jobConfig)) .contentType(MediaType.APPLICATION_JSON)); // then result .andExpect(status().isUnprocessableEntity()) .andExpect(content().string("\"Validation message\"")); }
@Test public void shouldStartNewRun() throws Exception { // given JobConfig jobConfig = exampleConfig(); JLineupRunStatus run = runStatusBuilder().withId("someNewId").withJobConfig(jobConfig).withState(State.BEFORE_RUNNING).build(); when(jLineupService.startBeforeRun(any())).thenReturn(run); // when ResultActions result = mvc .perform(post("/testContextPath/runs") .contextPath("/testContextPath") .content(JobConfig.prettyPrint(jobConfig)) .contentType(MediaType.APPLICATION_JSON)); // then result .andExpect(status().isAccepted()) .andExpect(content().json("{\"id\":\"someNewId\"}")) .andExpect(header().string("Location", "/testContextPath/runs/someNewId")); verify(jLineupService).startBeforeRun(jobConfig); }
@Test public void shouldIncludeLinksWithContextPath() throws Exception { // given Instant startTime = Instant.ofEpochMilli(1000); when(jLineupService.getRun("someId")).thenReturn(Optional.of(runStatusBuilder() .withId("someId") .withState(State.FINISHED_WITHOUT_DIFFERENCES) .withJobConfig(exampleConfig()) .withReports(JLineupRunStatus.Reports.reportsBuilder() .withHtmlUrl("/htmlReport/report.html") .withJsonUrl("/jsonReport/report.json") .withLogUrl("/log/log.log") .build()) .withStartTime(startTime) .build())); // when ResultActions result = mvc .perform(get("/testContextPath/runs/someId") .contextPath("/testContextPath") .accept(MediaType.APPLICATION_JSON)); // then result.andExpect(status().isOk()); result.andExpect(content().json( "{\"id\":\"someId\",\"state\":\"FINISHED_WITHOUT_DIFFERENCES\",\"startTime\":\"1970-01-01T00:00:01Z\",\"endTime\":null,\"reports\":{\"htmlUrl\":\"http://localhost/testContextPath/htmlReport/report.html\",\"jsonUrl\":\"http://localhost/testContextPath/jsonReport/report.json\",\"logUrl\":\"http://localhost/testContextPath/log/log.log\"}}" )); }