|
| 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.test; |
| 18 | + |
| 19 | +import com.cloud.configuration.Configuration; |
| 20 | +import com.cloud.configuration.ConfigurationService; |
| 21 | +import com.cloud.exception.InvalidParameterValueException; |
| 22 | +import com.cloud.resource.ResourceService; |
| 23 | +import junit.framework.Assert; |
| 24 | +import junit.framework.TestCase; |
| 25 | +import org.apache.cloudstack.api.ResponseGenerator; |
| 26 | +import org.apache.cloudstack.api.ServerApiException; |
| 27 | +import org.apache.cloudstack.api.command.admin.config.UpdateCfgCmd; |
| 28 | +import org.apache.cloudstack.api.response.ConfigurationResponse; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.rules.ExpectedException; |
| 33 | +import org.mockito.Mockito; |
| 34 | + |
| 35 | +public class UpdateCfgCmdTest extends TestCase{ |
| 36 | + |
| 37 | + private UpdateCfgCmd updateCfgCmd; |
| 38 | + private ConfigurationService configService; |
| 39 | + private ResponseGenerator responseGenerator; |
| 40 | + |
| 41 | + @Rule |
| 42 | + public ExpectedException expectedException = ExpectedException.none(); |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setUp() { |
| 46 | + responseGenerator = Mockito.mock(ResponseGenerator.class); |
| 47 | + configService = Mockito.mock(ConfigurationService.class); |
| 48 | + updateCfgCmd = new UpdateCfgCmd(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testExecuteForEmptyResult() { |
| 53 | + updateCfgCmd._configService = configService; |
| 54 | + |
| 55 | + try { |
| 56 | + updateCfgCmd.execute(); |
| 57 | + } catch (ServerApiException exception) { |
| 58 | + Assert.assertEquals("Failed to update config", |
| 59 | + exception.getDescription()); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testExecuteForNullResult() { |
| 66 | + |
| 67 | + updateCfgCmd._configService = configService; |
| 68 | + |
| 69 | + try { |
| 70 | + Mockito.when( |
| 71 | + configService.updateConfiguration(updateCfgCmd)) |
| 72 | + .thenReturn(null); |
| 73 | + } catch (InvalidParameterValueException e) { |
| 74 | + // TODO Auto-generated catch block |
| 75 | + e.printStackTrace(); |
| 76 | + } catch (IllegalArgumentException e) { |
| 77 | + // TODO Auto-generated catch block |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + try { |
| 81 | + updateCfgCmd.execute(); |
| 82 | + } catch (ServerApiException exception) { |
| 83 | + Assert.assertEquals("Failed to update config", |
| 84 | + exception.getDescription()); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testCreateSuccess() { |
| 92 | + |
| 93 | + Configuration cfg = Mockito.mock(Configuration.class); |
| 94 | + updateCfgCmd._configService = configService; |
| 95 | + updateCfgCmd._responseGenerator = responseGenerator; |
| 96 | + |
| 97 | + try { |
| 98 | + Mockito.when( |
| 99 | + configService.updateConfiguration(updateCfgCmd)) |
| 100 | + .thenReturn(cfg); |
| 101 | + }catch (Exception e){ |
| 102 | + Assert.fail("Received exception when success expected " + e.getMessage()); |
| 103 | + } |
| 104 | + |
| 105 | + ConfigurationResponse response = new ConfigurationResponse(); |
| 106 | + response.setName("Test case"); |
| 107 | + Mockito.when(responseGenerator.createConfigurationResponse(cfg)).thenReturn(response); |
| 108 | + |
| 109 | + updateCfgCmd.execute(); |
| 110 | + Mockito.verify(responseGenerator).createConfigurationResponse(cfg); |
| 111 | + ConfigurationResponse actualResponse = (ConfigurationResponse) updateCfgCmd.getResponseObject(); |
| 112 | + Assert.assertEquals(response, actualResponse); |
| 113 | + Assert.assertEquals("updateconfigurationresponse", response.getResponseName()); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments