|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.usage; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.Charset; |
| 22 | + |
| 23 | +import org.apache.commons.io.FileUtils; |
| 24 | +import org.apache.commons.lang3.math.NumberUtils; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.mockito.InOrder; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.powermock.api.mockito.PowerMockito; |
| 31 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 32 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 33 | + |
| 34 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 35 | + |
| 36 | +@RunWith(PowerMockRunner.class) |
| 37 | +@PrepareForTest({FileUtils.class, UsageManagerImpl.class, NumberUtils.class}) |
| 38 | +public class UsageManagerImplTest { |
| 39 | + |
| 40 | + private static final String CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE = "/var/run/cloudstack-usage.service.pid"; |
| 41 | + private UsageManagerImpl usageManagerImpl = new UsageManagerImpl(); |
| 42 | + |
| 43 | + @Test |
| 44 | + public void retrieveUsageManagerServicePidTestCompleteExecutionFlow() throws Exception { |
| 45 | + prepareAndVerifyTestRetrieveUsageManagerServicePidTest(true, true, 1, 1234, "1234" + System.getProperty("line.separator")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test(expected=CloudRuntimeException.class) |
| 49 | + public void retrieveUsageManagerServicePidTestFileDoesNotExist() throws Exception { |
| 50 | + prepareAndVerifyTestRetrieveUsageManagerServicePidTest(false, true, 0, 1234, "1234" + System.getProperty("line.separator")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test(expected = CloudRuntimeException.class) |
| 54 | + public void retrieveUsageManagerServicePidTestCannotReadFile() throws Exception { |
| 55 | + prepareAndVerifyTestRetrieveUsageManagerServicePidTest(true, false, 1, 1234, "1234" + System.getProperty("line.separator")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test(expected = CloudRuntimeException.class) |
| 59 | + public void retrieveUsageManagerServicePidTestPidEqualsToZero() throws Exception { |
| 60 | + prepareAndVerifyTestRetrieveUsageManagerServicePidTest(true, true, 1, 0, "abc"); |
| 61 | + } |
| 62 | + |
| 63 | + private void prepareAndVerifyTestRetrieveUsageManagerServicePidTest(boolean fileExists, boolean canReadFile, int timesCanReadIsExecuted, int expectedPid, |
| 64 | + String readedStringFromFile) |
| 65 | + throws Exception, IOException { |
| 66 | + File usageServicePid = Mockito.mock(File.class); |
| 67 | + PowerMockito.whenNew(File.class).withArguments(CLOUDSTACK_USAGE_SERVER_SERVICE_PID_FILE).thenReturn(usageServicePid); |
| 68 | + |
| 69 | + Mockito.when(usageServicePid.exists()).thenReturn(fileExists); |
| 70 | + Mockito.when(usageServicePid.canRead()).thenReturn(canReadFile); |
| 71 | + |
| 72 | + PowerMockito.mockStatic(FileUtils.class); |
| 73 | + Mockito.when(FileUtils.readFileToString(usageServicePid, Charset.defaultCharset())).thenReturn(readedStringFromFile); |
| 74 | + |
| 75 | + int result = usageManagerImpl.retrieveUsageManagerServicePid(); |
| 76 | + |
| 77 | + InOrder inOrder = Mockito.inOrder(usageServicePid); |
| 78 | + inOrder.verify(usageServicePid).exists(); |
| 79 | + inOrder.verify(usageServicePid, Mockito.times(timesCanReadIsExecuted)).canRead(); |
| 80 | + |
| 81 | + Assert.assertEquals(expectedPid, result); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments