use of org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult in project sakuli by ConSol.
the class GearmanResultServiceImplTest method testSaveAllResultsConnectionFailedAddCacheResults.
@Test
public void testSaveAllResultsConnectionFailedAddCacheResults() 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(properties.isCacheEnabled()).thenReturn(true);
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));
when(gearmanClient.addJobServer(connection)).thenReturn(false);
NagiosCheckResult mockedResult1 = Mockito.mock(NagiosCheckResult.class);
when(gearmanCacheService.getCachedResults()).thenReturn(Collections.singletonList(mockedResult1));
NagiosCheckResult newResult = new NagiosCachedCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult);
when(checkResultBuilder.build()).thenReturn(newResult);
doAnswer(invocationOnMock -> {
List<NagiosCheckResult> results = ((List) invocationOnMock.getArguments()[0]);
assertEquals(results.size(), 2L);
assertEquals(results.get(0), newResult);
assertEquals(results.get(1), mockedResult1);
return null;
}).when(gearmanCacheService).cacheResults(anyList());
testling.saveAllResults();
//checks
verify(gearmanCacheService).cacheResults(anyList());
verify(gearmanCacheService).getCachedResults();
verify(exceptionHandler).handleException(any(Throwable.class), eq(true));
verify(testling).getGearmanClient();
verify(testling).getGearmanConnection(host, port);
verify(gearmanClient).addJobServer(connection);
verify(gearmanClient).shutdown();
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult 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);
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult in project sakuli by ConSol.
the class GearmanResultServiceImplTest method testCreateJobEncryptionDisabled.
@Test
public void testCreateJobEncryptionDisabled() 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(properties.isEncryption()).thenReturn(false);
GearmanJob gearmanJob = testling.creatJob(new NagiosCachedCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult));
assertEquals(new String(Base64.decodeBase64(gearmanJob.getData())), testResult);
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult in project sakuli by ConSol.
the class GearmanResultServiceImplTest method testSaveAllResultsJobSubmitFailedAddCacheResults.
@Test
public void testSaveAllResultsJobSubmitFailedAddCacheResults() 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(properties.isCacheEnabled()).thenReturn(true);
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));
when(gearmanClient.addJobServer(connection)).thenReturn(true);
when(gearmanClient.submit(job)).thenThrow(new SakuliRuntimeException("Something went wrong!"));
NagiosCheckResult mockedResult1 = Mockito.mock(NagiosCheckResult.class);
NagiosCheckResult mockedResult2 = Mockito.mock(NagiosCheckResult.class);
when(gearmanCacheService.getCachedResults()).thenReturn(Arrays.asList(mockedResult1, mockedResult2));
NagiosCheckResult newResult = new NagiosCachedCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult);
when(checkResultBuilder.build()).thenReturn(newResult);
doAnswer(invocationOnMock -> {
List<NagiosCheckResult> results = ((List) invocationOnMock.getArguments()[0]);
assertEquals(results.size(), 3L);
assertEquals(results.get(0), newResult);
assertEquals(results.get(1), mockedResult1);
assertEquals(results.get(2), mockedResult2);
return null;
}).when(gearmanCacheService).cacheResults(anyList());
StringBuilder sendOrder = new StringBuilder();
doAnswer(invocationOnMock -> {
Object result = invocationOnMock.getArguments()[1];
Assert.assertTrue(result instanceof NagiosCheckResult);
sendOrder.append(result.hashCode());
return invocationOnMock.callRealMethod();
}).when(testling).sendResult(any(), any());
testling.saveAllResults();
//checks
assertEquals(sendOrder.toString(), "" + mockedResult2.hashCode() + mockedResult1.hashCode() + newResult.hashCode());
verify(gearmanCacheService).cacheResults(anyList());
verify(gearmanCacheService).getCachedResults();
verify(exceptionHandler, times(3)).handleException(any(Throwable.class), eq(true));
verify(testling).getGearmanClient();
verify(testling).getGearmanConnection(host, port);
verify(gearmanClient).addJobServer(connection);
verify(gearmanClient).shutdown();
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCachedCheckResult in project sakuli by ConSol.
the class GearmanResultServiceImplTest method testCreateJobEncryptionEnabled.
@Test
public void testCreateJobEncryptionEnabled() 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(properties.isEncryption()).thenReturn(true);
final String secretKey = "abcdefghijklmnopqrstuvwxyz";
when(properties.getSecretKey()).thenReturn(secretKey);
GearmanJob gearmanJob = testling.creatJob(new NagiosCachedCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult));
assertEquals(Aes.decrypt(gearmanJob.getData(), secretKey), testResult);
}
Aggregations