|
| 1 | +package com.cloud.usage; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.charset.Charset; |
| 6 | + |
| 7 | +import org.apache.commons.io.FileUtils; |
| 8 | +import org.apache.commons.lang3.math.NumberUtils; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.mockito.InOrder; |
| 13 | +import org.mockito.Mockito; |
| 14 | +import org.powermock.api.mockito.PowerMockito; |
| 15 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 16 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 17 | + |
| 18 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 19 | + |
| 20 | +@RunWith(PowerMockRunner.class) |
| 21 | +@PrepareForTest({FileUtils.class, UsageManagerImpl.class, NumberUtils.class}) |
| 22 | +public class UsageManagerImplTest { |
| 23 | + |
| 24 | + private static final String CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE = "/var/run/cloudstack-usage.service.pid"; |
| 25 | + private UsageManagerImpl usageManagerImpl = new UsageManagerImpl(); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void configureUsageManagerServicePidTestCompleteExecutionFlow() throws Exception { |
| 29 | + prepareAndVerifyTestConfigureUsageManagerServicePidTest(true, true, 1, 1234, "1234" + System.getProperty("line.separator")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test(expected=CloudRuntimeException.class) |
| 33 | + public void configureUsageManagerServicePidTestFileDoesNotExist() throws Exception { |
| 34 | + prepareAndVerifyTestConfigureUsageManagerServicePidTest(false, true, 0, 1234, "1234" + System.getProperty("line.separator")); |
| 35 | + } |
| 36 | + |
| 37 | + @Test(expected = CloudRuntimeException.class) |
| 38 | + public void configureUsageManagerServicePidTestCannotReadFile() throws Exception { |
| 39 | + prepareAndVerifyTestConfigureUsageManagerServicePidTest(true, false, 1, 1234, "1234" + System.getProperty("line.separator")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test(expected = CloudRuntimeException.class) |
| 43 | + public void configureUsageManagerServicePidTestPidEqualsToZero() throws Exception { |
| 44 | + prepareAndVerifyTestConfigureUsageManagerServicePidTest(true, true, 1, 0, "abc"); |
| 45 | + } |
| 46 | + |
| 47 | + private void prepareAndVerifyTestConfigureUsageManagerServicePidTest(boolean fileExists, boolean canReadFile, int timesCanReadIsExecuted, int expectedPid, |
| 48 | + String readedStringFromFile) |
| 49 | + throws Exception, IOException { |
| 50 | + File usageServicePid = Mockito.mock(File.class); |
| 51 | + PowerMockito.whenNew(File.class).withArguments(CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE).thenReturn(usageServicePid); |
| 52 | + |
| 53 | + Mockito.when(usageServicePid.exists()).thenReturn(fileExists); |
| 54 | + Mockito.when(usageServicePid.canRead()).thenReturn(canReadFile); |
| 55 | + |
| 56 | + PowerMockito.mockStatic(FileUtils.class); |
| 57 | + Mockito.when(FileUtils.readFileToString(usageServicePid, Charset.defaultCharset())).thenReturn(readedStringFromFile); |
| 58 | + |
| 59 | + int result = usageManagerImpl.configureUsageManagerServicePid(); |
| 60 | + |
| 61 | + InOrder inOrder = Mockito.inOrder(usageServicePid); |
| 62 | + inOrder.verify(usageServicePid).exists(); |
| 63 | + inOrder.verify(usageServicePid, Mockito.times(timesCanReadIsExecuted)).canRead(); |
| 64 | + |
| 65 | + Assert.assertEquals(expectedPid, result); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments