|
| 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 | +// with 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 org.apache.cloudstack.api.command.admin.config; |
| 18 | + |
| 19 | +import org.apache.cloudstack.api.response.ConfigurationResponse; |
| 20 | +import org.apache.cloudstack.config.Configuration; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.MockedStatic; |
| 27 | +import org.mockito.Mockito; |
| 28 | +import org.mockito.junit.MockitoJUnitRunner; |
| 29 | + |
| 30 | +import com.cloud.utils.crypt.DBEncryptionUtil; |
| 31 | + |
| 32 | +@RunWith(MockitoJUnitRunner.class) |
| 33 | +public class UpdateCfgCmdTest { |
| 34 | + |
| 35 | + private UpdateCfgCmd updateCfgCmd; |
| 36 | + |
| 37 | + private MockedStatic<DBEncryptionUtil> mockedStatic; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() { |
| 41 | + updateCfgCmd = new UpdateCfgCmd(); |
| 42 | + mockedStatic = Mockito.mockStatic(DBEncryptionUtil.class); |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + public void tearDown() { |
| 47 | + mockedStatic.close(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void setResponseValueSetsEncryptedValueWhenConfigurationIsEncrypted() { |
| 52 | + ConfigurationResponse response = new ConfigurationResponse(); |
| 53 | + Configuration cfg = Mockito.mock(Configuration.class); |
| 54 | + Mockito.when(cfg.isEncrypted()).thenReturn(true); |
| 55 | + Mockito.when(cfg.getValue()).thenReturn("testValue"); |
| 56 | + Mockito.when(DBEncryptionUtil.encrypt("testValue")).thenReturn("encryptedValue"); |
| 57 | + updateCfgCmd.setResponseValue(response, cfg); |
| 58 | + Assert.assertEquals("encryptedValue", response.getValue()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void setResponseValueSetsPlainValueWhenConfigurationIsNotEncrypted() { |
| 63 | + ConfigurationResponse response = new ConfigurationResponse(); |
| 64 | + Configuration cfg = Mockito.mock(Configuration.class); |
| 65 | + Mockito.when(cfg.isEncrypted()).thenReturn(false); |
| 66 | + Mockito.when(cfg.getValue()).thenReturn("testValue"); |
| 67 | + updateCfgCmd.setResponseValue(response, cfg); |
| 68 | + Assert.assertEquals("testValue", response.getValue()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void setResponseValueHandlesNullConfigurationValueGracefully() { |
| 73 | + ConfigurationResponse response = new ConfigurationResponse(); |
| 74 | + Configuration cfg = Mockito.mock(Configuration.class); |
| 75 | + Mockito.when(cfg.isEncrypted()).thenReturn(false); |
| 76 | + Mockito.when(cfg.getValue()).thenReturn(null); |
| 77 | + updateCfgCmd.setResponseValue(response, cfg); |
| 78 | + Assert.assertNull(response.getValue()); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments