|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.cloud.agent.properties; |
| 21 | + |
| 22 | +import com.cloud.utils.PropertiesUtil; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Properties; |
| 26 | +import junit.framework.TestCase; |
| 27 | +import org.apache.commons.beanutils.ConvertUtils; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.Mockito; |
| 33 | +import org.powermock.api.mockito.PowerMockito; |
| 34 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 35 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 36 | + |
| 37 | +@RunWith(PowerMockRunner.class) |
| 38 | +@PrepareForTest({PropertiesUtil.class, ConvertUtils.class}) |
| 39 | +public class AgentPropertiesFileHandlerTest extends TestCase { |
| 40 | + |
| 41 | + @Mock |
| 42 | + AgentProperties.Property<String> agentPropertiesStringMock; |
| 43 | + |
| 44 | + @Mock |
| 45 | + AgentProperties.Property<Integer> agentPropertiesIntegerMock; |
| 46 | + |
| 47 | + @Mock |
| 48 | + File fileMock; |
| 49 | + |
| 50 | + @Mock |
| 51 | + Properties propertiesMock; |
| 52 | + |
| 53 | + @Test |
| 54 | + public void validateGetPropertyValueFileNotFoundReturnDefaultValue() throws Exception{ |
| 55 | + String expectedResult = "default value"; |
| 56 | + Mockito.doReturn(expectedResult).when(agentPropertiesStringMock).getDefaultValue(); |
| 57 | + |
| 58 | + PowerMockito.mockStatic(PropertiesUtil.class); |
| 59 | + PowerMockito.doReturn(null).when(PropertiesUtil.class, "findConfigFile", Mockito.anyString()); |
| 60 | + |
| 61 | + String result = AgentPropertiesFileHandler.getPropertyValue(agentPropertiesStringMock); |
| 62 | + |
| 63 | + Assert.assertEquals(expectedResult, result); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void validateGetPropertyValueLoadFromFileThrowsIOExceptionReturnDefaultValue() throws Exception{ |
| 68 | + String expectedResult = "default value"; |
| 69 | + Mockito.doReturn(expectedResult).when(agentPropertiesStringMock).getDefaultValue(); |
| 70 | + |
| 71 | + PowerMockito.mockStatic(PropertiesUtil.class); |
| 72 | + PowerMockito.doReturn(fileMock).when(PropertiesUtil.class, "findConfigFile", Mockito.anyString()); |
| 73 | + PowerMockito.doThrow(new IOException()).when(PropertiesUtil.class, "loadFromFile", Mockito.any()); |
| 74 | + |
| 75 | + String result = AgentPropertiesFileHandler.getPropertyValue(agentPropertiesStringMock); |
| 76 | + |
| 77 | + Assert.assertEquals(expectedResult, result); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void validateGetPropertyValuePropertyIsEmptyReturnDefaultValue() throws Exception{ |
| 82 | + String expectedResult = "default value"; |
| 83 | + Mockito.doReturn(expectedResult).when(agentPropertiesStringMock).getDefaultValue(); |
| 84 | + Mockito.doReturn("name").when(agentPropertiesStringMock).getName(); |
| 85 | + |
| 86 | + PowerMockito.mockStatic(PropertiesUtil.class); |
| 87 | + PowerMockito.doReturn(fileMock).when(PropertiesUtil.class, "findConfigFile", Mockito.anyString()); |
| 88 | + PowerMockito.doReturn(propertiesMock).when(PropertiesUtil.class, "loadFromFile", Mockito.any()); |
| 89 | + PowerMockito.doReturn("").when(propertiesMock).getProperty(Mockito.anyString()); |
| 90 | + |
| 91 | + String result = AgentPropertiesFileHandler.getPropertyValue(agentPropertiesStringMock); |
| 92 | + |
| 93 | + Assert.assertEquals(expectedResult, result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void validateGetPropertyValuePropertyIsNullReturnDefaultValue() throws Exception{ |
| 98 | + String expectedResult = "default value"; |
| 99 | + Mockito.doReturn(expectedResult).when(agentPropertiesStringMock).getDefaultValue(); |
| 100 | + Mockito.doReturn("name").when(agentPropertiesStringMock).getName(); |
| 101 | + |
| 102 | + PowerMockito.mockStatic(PropertiesUtil.class); |
| 103 | + PowerMockito.doReturn(fileMock).when(PropertiesUtil.class, "findConfigFile", Mockito.anyString()); |
| 104 | + PowerMockito.doReturn(propertiesMock).when(PropertiesUtil.class, "loadFromFile", Mockito.any()); |
| 105 | + PowerMockito.doReturn(null).when(propertiesMock).getProperty(Mockito.anyString()); |
| 106 | + |
| 107 | + String result = AgentPropertiesFileHandler.getPropertyValue(agentPropertiesStringMock); |
| 108 | + |
| 109 | + Assert.assertEquals(expectedResult, result); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void validateGetPropertyValueValidPropertyReturnPropertyValue() throws Exception{ |
| 114 | + String expectedResult = "test"; |
| 115 | + Mockito.doReturn("default value").when(agentPropertiesStringMock).getDefaultValue(); |
| 116 | + Mockito.doReturn("name").when(agentPropertiesStringMock).getName(); |
| 117 | + |
| 118 | + PowerMockito.mockStatic(PropertiesUtil.class); |
| 119 | + PowerMockito.doReturn(fileMock).when(PropertiesUtil.class, "findConfigFile", Mockito.anyString()); |
| 120 | + PowerMockito.doReturn(propertiesMock).when(PropertiesUtil.class, "loadFromFile", Mockito.any()); |
| 121 | + Mockito.doReturn(expectedResult).when(propertiesMock).getProperty(Mockito.anyString()); |
| 122 | + |
| 123 | + String result = AgentPropertiesFileHandler.getPropertyValue(agentPropertiesStringMock); |
| 124 | + |
| 125 | + Assert.assertEquals(expectedResult, result); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void validateGetPropertyValueValidIntegerPropertyReturnPropertyValue() throws Exception{ |
| 130 | + Integer expectedResult = 2; |
| 131 | + Mockito.doReturn(1).when(agentPropertiesIntegerMock).getDefaultValue(); |
| 132 | + Mockito.doReturn("name").when(agentPropertiesIntegerMock).getName(); |
| 133 | + |
| 134 | + PowerMockito.mockStatic(PropertiesUtil.class); |
| 135 | + PowerMockito.doReturn(fileMock).when(PropertiesUtil.class, "findConfigFile", Mockito.anyString()); |
| 136 | + PowerMockito.doReturn(propertiesMock).when(PropertiesUtil.class, "loadFromFile", Mockito.any()); |
| 137 | + Mockito.doReturn(String.valueOf(expectedResult)).when(propertiesMock).getProperty(Mockito.anyString()); |
| 138 | + |
| 139 | + Integer result = AgentPropertiesFileHandler.getPropertyValue(agentPropertiesIntegerMock); |
| 140 | + |
| 141 | + Assert.assertEquals(expectedResult, result); |
| 142 | + } |
| 143 | +} |
0 commit comments