Search in sources :

Example 11 with TestSuiteExampleBuilder

use of org.sakuli.builder.TestSuiteExampleBuilder in project sakuli by ConSol.

the class GearmanResultServiceImplTest method testSaveAllResults.

@Test
public void testSaveAllResults() throws Exception {
    when(properties.getServiceType()).thenReturn("passive");
    final String queueName = "check_results";
    when(properties.getServerQueue()).thenReturn(queueName);
    final String host = "99.99.99.20";
    when(properties.getServerHost()).thenReturn(host);
    final int port = 4730;
    when(properties.getServerPort()).thenReturn(port);
    when(properties.getNagiosHost()).thenReturn("win7sakuli");
    when(checkResultBuilder.build()).thenReturn(new NagiosCachedCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult));
    GearmanClient gearmanClient = mock(GearmanClientImpl.class);
    doReturn(gearmanClient).when(testling).getGearmanClient();
    GearmanJobServerConnection connection = mock(GearmanJobServerConnection.class);
    doReturn(connection).when(testling).getGearmanConnection(host, port);
    GearmanJob job = mock(GearmanJob.class);
    doReturn(job).when(testling).creatJob(any(NagiosCheckResult.class));
    Future future = mock(Future.class);
    when(gearmanClient.addJobServer(connection)).thenReturn(true);
    when(gearmanClient.submit(job)).thenReturn(future);
    GearmanJobResult jobResult = mock(GearmanJobResult.class);
    when(future.get()).thenReturn(jobResult);
    when(jobResult.jobSucceeded()).thenReturn(true);
    GearmanJobStatus jobStatus = mock(GearmanJobStatus.class);
    when(jobStatus.isRunning()).thenReturn(false);
    when(gearmanClient.getJobStatus(job)).thenReturn(jobStatus);
    Date stopDate = new GregorianCalendar(2014, 14, 7, 13, 0).getTime();
    TestSuite testSuite = new TestSuiteExampleBuilder().withHost("win7sakuli").withId("sakuli_demo22").withStopDate(stopDate).withStartDate(DateUtils.addSeconds(stopDate, -60)).withTestCases(Collections.singletonList(new TestCaseExampleBuilder().withId("ok_case").withStartDate(DateUtils.addSeconds(stopDate, -12)).withStopDate(stopDate).withWarningTime(13).withCriticalTime(20).buildExample())).buildExample();
    stopDate = DateUtils.addSeconds(stopDate, 300);
    testSuite.addTestCase(new TestCaseExampleBuilder().withId("warn_case").withStartDate(DateUtils.addSeconds(stopDate, -30)).withStopDate(stopDate).withWarningTime(20).withCriticalTime(40).buildExample());
    stopDate = DateUtils.addSeconds(stopDate, 300);
    testSuite.addTestCase(new TestCaseExampleBuilder().withId("warn_in_step").withStartDate(DateUtils.addSeconds(stopDate, -30)).withStopDate(stopDate).withWarningTime(40).withCriticalTime(50).withTestCaseSteps(Arrays.asList(new TestCaseStepExampleBuilder().withName("warn_step_1").withStartDate(DateUtils.addSeconds(stopDate, -29)).withStopDate(DateUtils.addSeconds(stopDate, -20)).withWarningTime(5).buildExample(), new TestCaseStepExampleBuilder().withName("warn_step_2").withStartDate(DateUtils.addSeconds(stopDate, -19)).withStopDate(DateUtils.addSeconds(stopDate, -10)).withWarningTime(5).buildExample())).buildExample());
    stopDate = DateUtils.addSeconds(stopDate, 300);
    testSuite.addTestCase(new TestCaseExampleBuilder().withId("crit_case").withStartDate(DateUtils.addSeconds(stopDate, -14)).withStopDate(stopDate).withWarningTime(10).withCriticalTime(13).buildExample());
    stopDate = DateUtils.addSeconds(stopDate, 300);
    testSuite.addTestCase(new TestCaseExampleBuilder().withId("error_case").withStartDate(DateUtils.addSeconds(stopDate, -14)).withException(new SakuliActionException("exception test message")).buildExample());
    ReflectionTestUtils.setField(testling, "testSuite", testSuite);
    testling.saveAllResults();
    //checks
    verify(gearmanCacheService, never()).cacheResults(anyList());
    verify(gearmanCacheService, never()).getCachedResults();
    verify(exceptionHandler, never()).handleException(any(Throwable.class));
    verify(exceptionHandler, never()).handleException(any(Throwable.class), anyBoolean());
    verify(testling).getGearmanClient();
    verify(testling).getGearmanConnection(host, port);
    verify(gearmanClient).addJobServer(connection);
    verify(gearmanClient).submit(job);
    verify(future).get();
    verify(gearmanClient).shutdown();
    ArgumentCaptor<NagiosCheckResult> checkresult = ArgumentCaptor.forClass(NagiosCheckResult.class);
    verify(testling).creatJob(checkresult.capture());
    assertEquals(checkresult.getValue().getQueueName(), queueName);
    assertEquals(checkresult.getValue().getUuid(), testSuite.getGuid());
    assertEquals(checkresult.getValue().getPayloadString(), testResult);
}
Also used : TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) TestCaseStepExampleBuilder(org.sakuli.builder.TestCaseStepExampleBuilder) GearmanJobServerConnection(org.gearman.common.GearmanJobServerConnection) TestSuite(org.sakuli.datamodel.TestSuite) NagiosCachedCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) Future(java.util.concurrent.Future) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) SakuliActionException(org.sakuli.exceptions.SakuliActionException) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Example 12 with TestSuiteExampleBuilder

use of org.sakuli.builder.TestSuiteExampleBuilder in project sakuli by ConSol.

the class NagiosOutputBuilderTest method testGetStatusSummary.

@Test
public void testGetStatusSummary() throws Exception {
    TestSuite testSuite = new TestSuiteExampleBuilder().withState(TestSuiteState.OK).withId("TEST-SUITE-ID").withTestCases(Collections.singletonList(new TestCaseExampleBuilder().withId("TEST-CASE-ID").buildExample())).buildExample();
    String lastRun = AbstractOutputBuilder.dateFormat.format(testSuite.getStopDate());
    String expectedHTML = "[OK] Sakuli suite \"TEST-SUITE-ID\" ok (120.00s). (Last suite run: " + lastRun + ")\\\\n" + String.format(ScreenshotDiv.STYLE_TEMPLATE, testling.getOutputScreenshotDivWidth()) + "<table style=\"border-collapse: collapse;\">" + "<tr valign=\"top\">" + "<td class=\"serviceOK\">[OK] Sakuli suite \"TEST-SUITE-ID\" ok (120.00s). (Last suite run: " + lastRun + ")" + "</td>" + "</tr>" + "<tr valign=\"top\">" + "<td class=\"serviceOK\">[OK] case \"TEST-CASE-ID\" ran in 3.00s - ok</td>" + "</tr>" + "</table>";
    String statusSummary = testling.getStatusSummary(testSuite, gearmanProperties);
    assertEquals(statusSummary, expectedHTML);
    ReflectionTestUtils.setField(testling, "testSuite", testSuite);
    ReflectionTestUtils.setField(testling, "gearmanProperties", gearmanProperties);
    NagiosOutput output = testling.build();
    String substringStatusSummary = output.getOutputString().substring(0, output.getOutputString().indexOf("|"));
    assertEquals(substringStatusSummary, expectedHTML);
}
Also used : NagiosOutput(org.sakuli.services.forwarder.gearman.model.NagiosOutput) TestSuite(org.sakuli.datamodel.TestSuite) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Example 13 with TestSuiteExampleBuilder

use of org.sakuli.builder.TestSuiteExampleBuilder in project sakuli by ConSol.

the class Icinga2OutputBuilderTest method testBuildErrorInCase.

@Test
public void testBuildErrorInCase() throws Exception {
    TestSuite testSuite = new TestSuiteExampleBuilder().withState(TestSuiteState.ERRORS).withId("TEST-SUITE-ID").withTestCases(Collections.singletonList(new TestCaseExampleBuilder().withId("TEST-CASE-ID").withState(TestCaseState.ERRORS).withException(new SakuliException("MY-TEST-ERROR-CASE")).buildExample())).buildExample();
    ReflectionTestUtils.setField(testling, "testSuite", testSuite);
    Assert.assertEquals(testling.build(), "[CRIT] Sakuli suite \"TEST-SUITE-ID\" (120.00s) EXCEPTION: 'CASE \"TEST-CASE-ID\": MY-TEST-ERROR-CASE'. (Last suite run: 17.08.14 14:02:00)\n" + "[CRIT] case \"TEST-CASE-ID\" EXCEPTION: MY-TEST-ERROR-CASE");
}
Also used : TestSuite(org.sakuli.datamodel.TestSuite) SakuliException(org.sakuli.exceptions.SakuliException) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) Test(org.testng.annotations.Test)

Example 14 with TestSuiteExampleBuilder

use of org.sakuli.builder.TestSuiteExampleBuilder in project sakuli by ConSol.

the class GearmanCacheServiceTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    ReflectionTestUtils.setField(checkResultBuilder, "testSuite", new TestSuiteExampleBuilder().buildExample());
    when(nagiosOutputBuilder.build()).thenReturn(new NagiosOutput());
    MonitoringPropertiesTestHelper.initMock(gearmanProperties);
}
Also used : NagiosOutput(org.sakuli.services.forwarder.gearman.model.NagiosOutput) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 15 with TestSuiteExampleBuilder

use of org.sakuli.builder.TestSuiteExampleBuilder in project sakuli by ConSol.

the class NagiosOutputBuilderTest method testFormatTestSuiteTableExceptionWithScreenshot.

@Test
public void testFormatTestSuiteTableExceptionWithScreenshot() throws Exception {
    Path screenshotPath = Paths.get(NagiosOutputBuilder.class.getResource("computer.png").toURI());
    Date startDate = new Date();
    TestSuite testSuiteExample = new TestSuiteExampleBuilder().withId("sakuli-123").withState(TestSuiteState.ERRORS).withStartDate(startDate).withException(new SakuliExceptionWithScreenshot("TEST-ERROR", screenshotPath)).withStopDate(DateUtils.addSeconds(startDate, 120)).buildExample();
    String result = testling.formatTestSuiteTableStateMessage(testSuiteExample, gearmanProperties.getTemplateSuiteTable());
    String lastRun = AbstractOutputBuilder.dateFormat.format(testSuiteExample.getStopDate());
    final String separator = "<div";
    assertEquals(result.substring(0, result.indexOf(separator)), "<tr valign=\"top\"><td class=\"serviceCRITICAL\">[CRIT] Sakuli suite \"sakuli-123\"" + " (120.00s) EXCEPTION: 'TEST-ERROR'. (Last suite run: " + lastRun + ")");
    String screenshotHash = result.substring(result.indexOf(ScreenshotDiv.DEFAULT_SAKULI_SCREENSHOT_DIV_ID) + ScreenshotDiv.DEFAULT_SAKULI_SCREENSHOT_DIV_ID.length());
    screenshotHash = screenshotHash.substring(0, screenshotHash.indexOf("\">"));
    String start_1 = "<div id=\"sakuli_screenshot" + screenshotHash + "\">" + "<div id=\"openModal_sakuli_screenshot" + screenshotHash + "\" class=\"modalDialog\">" + "<a href=\"#close\" title=\"Close\" class=\"close\">Close X</a>" + "<a href=\"#openModal_sakuli_screenshot" + screenshotHash + "\"><img class=\"screenshot\" src=\"";
    String start = start_1 + "data:image/png;base64,";
    String end = "</a></div></div></td></tr>";
    String substring = result.substring(result.indexOf(separator));
    assertEquals(substring.substring(0, start.length()), start);
    assertEquals(substring.substring(substring.length() - end.length()), end);
    //now check the remove function
    String resultWithOutBase64Data = ScreenshotDivConverter.removeBase64ImageDataString(substring);
    assertEquals(resultWithOutBase64Data, start_1 + "\" >" + end);
}
Also used : Path(java.nio.file.Path) TestSuite(org.sakuli.datamodel.TestSuite) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) Date(java.util.Date) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Aggregations

TestSuiteExampleBuilder (org.sakuli.builder.TestSuiteExampleBuilder)23 TestSuite (org.sakuli.datamodel.TestSuite)21 Test (org.testng.annotations.Test)21 TestCaseExampleBuilder (org.sakuli.builder.TestCaseExampleBuilder)15 SakuliException (org.sakuli.exceptions.SakuliException)8 BaseTest (org.sakuli.BaseTest)6 TestCaseStepExampleBuilder (org.sakuli.builder.TestCaseStepExampleBuilder)6 Date (java.util.Date)3 Path (java.nio.file.Path)2 NagiosOutput (org.sakuli.services.forwarder.gearman.model.NagiosOutput)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 Future (java.util.concurrent.Future)1 GearmanJobServerConnection (org.gearman.common.GearmanJobServerConnection)1 LoggerTest (org.sakuli.LoggerTest)1 TestCaseStepBuilder (org.sakuli.datamodel.builder.TestCaseStepBuilder)1 TestCaseStepState (org.sakuli.datamodel.state.TestCaseStepState)1 SakuliActionException (org.sakuli.exceptions.SakuliActionException)1 SakuliExceptionHandler (org.sakuli.exceptions.SakuliExceptionHandler)1 SakuliExceptionWithScreenshot (org.sakuli.exceptions.SakuliExceptionWithScreenshot)1 NagiosCachedCheckResult (org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult)1