NiciraCheckHealthCommandWrapper extends CommandWrapper<CheckHealthCommand, Answer, NiciraNvpResource> { @Override public Answer execute(final CheckHealthCommand command, final NiciraNvpResource serverResource) { final NiciraNvpApi niciraNvpApi = serverResource.getNiciraNvpApi(); boolean healthy = true; try { final ControlClusterStatus clusterStatus = niciraNvpApi.getControlClusterStatus(); final String status = clusterStatus.getClusterStatus(); if (clusterIsUnstable(status)) { s_logger.warn("Control cluster is not stable. Current status is " + status); healthy = false; } } catch (final NiciraNvpApiException e) { s_logger.error("Exception caught while checking control cluster status during health check", e); healthy = false; } return new CheckHealthAnswer(command, healthy); }  @Override Answer execute(final CheckHealthCommand command, final NiciraNvpResource serverResource);  }
@Test public void tetsExecuteWhenClusterIsNotStable() throws Exception { when(niciraApi.getControlClusterStatus()).thenReturn(new ControlClusterStatus()); final NiciraCheckHealthCommandWrapper commandWrapper = new NiciraCheckHealthCommandWrapper(); final Answer answer = commandWrapper.execute(new CheckHealthCommand(), niciraResource); assertThat(answer.getResult(), equalTo(false)); }
@SuppressWarnings("unchecked") @Test public void tetsExecuteWhenApiThrowsException() throws Exception { when(niciraApi.getControlClusterStatus()).thenThrow(NiciraNvpApiException.class); final NiciraCheckHealthCommandWrapper commandWrapper = new NiciraCheckHealthCommandWrapper(); final Answer answer = commandWrapper.execute(new CheckHealthCommand(), niciraResource); assertThat(answer.getResult(), equalTo(false)); }
@Test public void tetsExecuteWhenClusterIsStable() throws Exception { final ControlClusterStatus statusValue = mock(ControlClusterStatus.class); when(statusValue.getClusterStatus()).thenReturn("stable"); when(niciraApi.getControlClusterStatus()).thenReturn(statusValue); final NiciraCheckHealthCommandWrapper commandWrapper = new NiciraCheckHealthCommandWrapper(); final Answer answer = commandWrapper.execute(new CheckHealthCommand(), niciraResource); assertThat(answer.getResult(), equalTo(true)); }