Search in sources :

Example 6 with TestCaseStepExampleBuilder

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

the class CheckMKTemplateOutputBuilderTest method testOK.

@Test
public void testOK() throws Exception {
    doReturn("example_xfce").when(testSuite).getId();
    doReturn(TestSuiteState.OK).when(testSuite).getState();
    doReturn(300).when(testSuite).getWarningTime();
    doReturn(400).when(testSuite).getCriticalTime();
    doReturn(44.99f).when(testSuite).getDuration();
    doReturn(new DateTime(1970, 1, 1, 10, 30, 0).toDate()).when(testSuite).getStartDate();
    doReturn(new DateTime(1970, 1, 1, 10, 30, 44, 99).toDate()).when(testSuite).getStopDate();
    SortedSet<TestCase> testCaseAsSortedSet = new TreeSet<>(Arrays.asList(new TestCaseExampleBuilder().withState(TestCaseState.OK).withWarningTime(20).withCriticalTime(30).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 14, 20).toDate()).withId("case1").withTestCaseSteps(Arrays.asList(new TestCaseStepExampleBuilder().withState(TestCaseStepState.OK).withName("Test_Sahi_landing_page").withWarningTime(5).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 1, 160).toDate()).buildExample(), new TestCaseStepExampleBuilder().withState(TestCaseStepState.OK).withName("Calculation").withWarningTime(10).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 10).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 7, 290).toDate()).buildExample(), new TestCaseStepExampleBuilder().withState(TestCaseStepState.OK).withName("Editor").withWarningTime(10).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 20).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 1, 500).toDate()).buildExample())).buildExample(), new TestCaseExampleBuilder().withState(TestCaseState.OK).withWarningTime(20).withCriticalTime(30).withStartDate(new DateTime(1970, 1, 1, 10, 30, 10).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 23, 580).toDate()).withId("case2").withTestCaseSteps(Arrays.asList(new TestCaseStepExampleBuilder().withState(TestCaseStepState.OK).withName("Test_Sahi_landing_page_(case2)").withWarningTime(5).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 1, 30).toDate()).buildExample(), new TestCaseStepExampleBuilder().withState(TestCaseStepState.OK).withName("Calculation_(case2)").withWarningTime(10).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 10).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 7, 80).toDate()).buildExample(), new TestCaseStepExampleBuilder().withName("Editor_(case2)").withWarningTime(10).withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 20).toDate()).withStopDate(new DateTime(1970, 1, 1, 10, 30, 1, 390).toDate()).buildExample())).buildExample()));
    doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet();
    String output = testling.createOutput();
    Assert.assertEquals(output, loadExpectedOutput(TestCaseState.OK.name()));
}
Also used : TestCase(org.sakuli.datamodel.TestCase) TreeSet(java.util.TreeSet) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) DateTime(org.joda.time.DateTime) TestCaseStepExampleBuilder(org.sakuli.builder.TestCaseStepExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Example 7 with TestCaseStepExampleBuilder

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

the class CommonResultServiceImplTest method testSaveAllResults.

@Test(dataProvider = "states")
public void testSaveAllResults(TestSuiteState testSuiteState, TestCaseState testCaseState, String stateOutputRegex) throws Exception {
    TestCaseStepState stepState = TestCaseStepState.WARNING;
    TestSuite testSuite = new TestSuiteExampleBuilder().withId("LOG_TEST_SUITE").withState(testSuiteState).withException(testSuiteState.isError() ? new SakuliException("TEST") : null).withTestCases(Collections.singletonList(new TestCaseExampleBuilder().withTestCaseSteps(Collections.singletonList(new TestCaseStepExampleBuilder().withState(stepState).buildExample())).withState(testCaseState).buildExample())).buildExample();
    ReflectionTestUtils.setField(testling, "testSuite", testSuite);
    Path logfile = Paths.get(properties.getLogFile());
    testling.saveAllResults();
    String lastLineOfLogFile = getLastLineOfLogFile(logfile, testSuiteState.isError() ? 42 : 39);
    List<String> regExes = getValidationExpressions(testSuiteState, testCaseState, stepState, stateOutputRegex, "TEST");
    List<String> strings = Arrays.asList(lastLineOfLogFile.split("\n"));
    Iterator<String> regExIterator = regExes.iterator();
    verifyOutputLines(strings, regExIterator);
}
Also used : Path(java.nio.file.Path) TestSuite(org.sakuli.datamodel.TestSuite) SakuliException(org.sakuli.exceptions.SakuliException) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) TestCaseStepState(org.sakuli.datamodel.state.TestCaseStepState) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) TestCaseStepExampleBuilder(org.sakuli.builder.TestCaseStepExampleBuilder) Test(org.testng.annotations.Test) LoggerTest(org.sakuli.LoggerTest)

Example 8 with TestCaseStepExampleBuilder

use of org.sakuli.builder.TestCaseStepExampleBuilder 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 9 with TestCaseStepExampleBuilder

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

the class NagiosOutputBuilderTest method testFormatTestCaseTableStateMessageWithScreenshotTestCase.

@Test
public void testFormatTestCaseTableStateMessageWithScreenshotTestCase() throws Exception {
    Path screenshotPath = Paths.get(NagiosOutputBuilder.class.getResource("computer.png").toURI());
    String htmlTemplate = "<tr valign=\"top\"><td class=\"%s\">%s<\\/td><\\/tr>";
    GearmanProperties properties = MonitoringPropertiesTestHelper.initMock(mock(GearmanProperties.class));
    TestCase testCase = new TestCaseExampleBuilder().withState(TestCaseState.ERRORS).withId("case-error").withException(new SakuliExceptionWithScreenshot("EXCEPTION-MESSAGE", screenshotPath)).withTestCaseSteps(Collections.singletonList(new TestCaseStepExampleBuilder().withState(TestCaseStepState.ERRORS).withException(new SakuliExceptionWithScreenshot("STEP-EXCEPTION-MESSAGE", screenshotPath)).buildExample())).buildExample();
    String regex = String.format(htmlTemplate, "serviceCRITICAL", "\\[CRIT\\] case \"case-error\" EXCEPTION: EXCEPTION-MESSAGE" + " - STEP \"step_for_unit_test\": STEP-EXCEPTION-MESSAGE" + "<div.* src=\"data:image\\/png;base64,.*><\\/div>" + "<div.* src=\"data:image\\/png;base64,.*><\\/div>");
    BaseTest.assertRegExMatch(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), regex);
}
Also used : Path(java.nio.file.Path) GearmanProperties(org.sakuli.services.forwarder.gearman.GearmanProperties) TestCase(org.sakuli.datamodel.TestCase) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) TestCaseStepExampleBuilder(org.sakuli.builder.TestCaseStepExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Example 10 with TestCaseStepExampleBuilder

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

the class NagiosOutputBuilderTest method testFormatTestCaseTableStateMessage.

@Test
public void testFormatTestCaseTableStateMessage() throws Exception {
    String htmlTemplate = "<tr valign=\"top\"><td class=\"%s\">%s</td></tr>";
    GearmanProperties properties = MonitoringPropertiesTestHelper.initMock(mock(GearmanProperties.class));
    TestCase testCase = new TestCaseExampleBuilder().withState(TestCaseState.OK).withId("case-ok").buildExample();
    assertEquals(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), String.format(htmlTemplate, "serviceOK", "[OK] case \"case-ok\" ran in 3.00s - ok"));
    Date startDate = new Date();
    testCase = new TestCaseExampleBuilder().withId("case-warning").withState(TestCaseState.WARNING).withStartDate(startDate).withStopDate(DateUtils.addMilliseconds(startDate, 5500)).withWarningTime(5).buildExample();
    assertEquals(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), String.format(htmlTemplate, "serviceWARNING", "[WARN] case \"case-warning\" over runtime (5.50s/warn at 5s)"));
    testCase = new TestCaseExampleBuilder().withState(TestCaseState.WARNING_IN_STEP).withId("case-warning").withTestCaseSteps(Arrays.asList(new TestCaseStepExampleBuilder().withName("step-name").withState(TestCaseStepState.WARNING).withStartDate(startDate).withStopDate(DateUtils.addMilliseconds(startDate, 3154)).withWarningTime(3).buildExample(), new TestCaseStepExampleBuilder().withName("step-name2").withState(TestCaseStepState.WARNING).withStartDate(DateUtils.addMilliseconds(startDate, 4000)).withStopDate(DateUtils.addMilliseconds(startDate, 4154)).withWarningTime(1).buildExample())).buildExample();
    assertEquals(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), String.format(htmlTemplate, "serviceWARNING", "[WARN] case \"case-warning\" (3.00s) ok, step \"step-name\" over runtime (3.15s/warn at 3s), " + "step \"step-name2\" over runtime (0.15s/warn at 1s)"));
    testCase = new TestCaseExampleBuilder().withState(TestCaseState.CRITICAL).withId("case-critical").withStartDate(startDate).withStopDate(DateUtils.addMilliseconds(startDate, 8888)).withCriticalTime(7).buildExample();
    assertEquals(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), String.format(htmlTemplate, "serviceCRITICAL", "[CRIT] case \"case-critical\" over runtime (8.89s/crit at 7s)"));
    testCase = new TestCaseExampleBuilder().withState(TestCaseState.ERRORS).withId("case-error").withException(new SakuliException("EXCEPTION-MESSAGE")).buildExample();
    assertEquals(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), String.format(htmlTemplate, "serviceCRITICAL", "[CRIT] case \"case-error\" EXCEPTION: EXCEPTION-MESSAGE"));
}
Also used : GearmanProperties(org.sakuli.services.forwarder.gearman.GearmanProperties) TestCase(org.sakuli.datamodel.TestCase) SakuliException(org.sakuli.exceptions.SakuliException) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) Date(java.util.Date) TestCaseStepExampleBuilder(org.sakuli.builder.TestCaseStepExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Aggregations

TestCaseStepExampleBuilder (org.sakuli.builder.TestCaseStepExampleBuilder)19 Test (org.testng.annotations.Test)19 TestCaseExampleBuilder (org.sakuli.builder.TestCaseExampleBuilder)17 BaseTest (org.sakuli.BaseTest)15 TestCase (org.sakuli.datamodel.TestCase)11 DateTime (org.joda.time.DateTime)9 TreeSet (java.util.TreeSet)7 TestSuiteExampleBuilder (org.sakuli.builder.TestSuiteExampleBuilder)6 TestSuite (org.sakuli.datamodel.TestSuite)6 Path (java.nio.file.Path)4 SakuliException (org.sakuli.exceptions.SakuliException)4 TestCaseStepBuilder (org.sakuli.datamodel.builder.TestCaseStepBuilder)3 SakuliExceptionWithScreenshot (org.sakuli.exceptions.SakuliExceptionWithScreenshot)3 GearmanProperties (org.sakuli.services.forwarder.gearman.GearmanProperties)3 Date (java.util.Date)2 Future (java.util.concurrent.Future)1 GearmanJobServerConnection (org.gearman.common.GearmanJobServerConnection)1 LoggerTest (org.sakuli.LoggerTest)1 TestCaseStepState (org.sakuli.datamodel.state.TestCaseStepState)1 SakuliActionException (org.sakuli.exceptions.SakuliActionException)1