MachineStatsChecker { public MachineStats getStats(Sigar sigar) { CpuStats cpuStats = CpuStats.builder() .build(); SwapStats swapStats = SwapStats.builder() .build(); MemoryStats memoryStats = MemoryStats.builder() .build(); try { swapStats = getSwapStats(sigar); cpuStats = getCpuStats(sigar); getMemoryStats(sigar); } catch (SigarException | UnsatisfiedLinkError e) { logger.error("Error during sigar stats operation", e); } finally { sigar.close(); } return getMachineStats(swapStats, cpuStats, memoryStats); }  MachineStats getStats(Sigar sigar);  }
@Test public void testGetStats() throws SigarException { when(sigar.getSwap()).thenReturn(swap) .thenReturn(swap) .thenReturn(swap) .thenReturn(swap) .thenReturn(swap); when(sigar.getCpuPerc()).thenReturn(cpuPerc) .thenReturn(cpuPerc) .thenReturn(cpuPerc); CpuInfo[] cpuInfos = new CpuInfo[] { cpuInfo }; when(sigar.getCpuInfoList()).thenReturn(cpuInfos) .thenReturn(cpuInfos) .thenReturn(cpuInfos); double[] doubleValue = new double[] { 0.0 }; when(sigar.getLoadAverage()).thenReturn(doubleValue); when(sigar.getMem()).thenReturn(mem) .thenReturn(mem) .thenReturn(mem) .thenReturn(mem) .thenReturn(mem); machineStatsChecker.getStats(sigar); verify(sigar, times(5)).getSwap(); verify(swap, times(1)).getPageIn(); verify(swap, times(1)).getPageOut(); verify(swap, times(1)).getFree(); verify(swap, times(1)).getUsed(); verify(swap, times(1)).getTotal(); verify(sigar, times(1)).getCpuPerc(); verify(cpuPerc, times(1)).getIdle(); verify(sigar, times(3)).getCpuInfoList(); verify(cpuInfo, times(1)).getTotalCores(); verify(cpuInfo, times(1)).getMhz(); verify(cpuInfo, times(1)).getModel(); verify(sigar, times(1)).getLoadAverage(); verify(sigar, times(5)).getMem(); verify(mem, times(1)).getActualFree(); verify(mem, times(1)).getActualUsed(); verify(mem, times(1)).getTotal(); verify(mem, times(1)).getUsedPercent(); verify(mem, times(1)).getFreePercent(); verify(sigar, times(1)).close(); }
@Test public void testGetStatsSigarExceptio() throws SigarException { when(sigar.getSwap()).thenThrow(new SigarException()); machineStatsChecker.getStats(sigar); verify(sigar, times(1)).getSwap(); verify(sigar, times(1)).close(); }