|
| 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.user.dns; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | + |
| 29 | +import org.apache.cloudstack.api.ServerApiException; |
| 30 | +import org.apache.cloudstack.api.response.DnsServerResponse; |
| 31 | +import org.apache.cloudstack.dns.DnsProviderType; |
| 32 | +import org.apache.cloudstack.dns.DnsServer; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +public class AddDnsServerCmdTest extends BaseDnsCmdTest { |
| 36 | + |
| 37 | + private AddDnsServerCmd createCmd() throws Exception { |
| 38 | + AddDnsServerCmd cmd = new AddDnsServerCmd(); |
| 39 | + setField(cmd, "dnsProviderManager", dnsProviderManager); |
| 40 | + setField(cmd, "name", "test-dns"); |
| 41 | + setField(cmd, "url", "http://dns.example.com"); |
| 42 | + setField(cmd, "provider", "PowerDNS"); |
| 43 | + setField(cmd, "credentials", "api-key-123"); |
| 44 | + setField(cmd, "port", 8081); |
| 45 | + setField(cmd, "isPublic", true); |
| 46 | + setField(cmd, "publicDomainSuffix", "public.example.com"); |
| 47 | + setField(cmd, "nameServers", Arrays.asList("ns1.example.com", "ns2.example.com")); |
| 48 | + setField(cmd, "externalServerId", "localhost"); |
| 49 | + setField(cmd, "dnsUserName", "admin@example.com"); |
| 50 | + return cmd; |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testAccessors() throws Exception { |
| 55 | + AddDnsServerCmd cmd = createCmd(); |
| 56 | + |
| 57 | + assertEquals("test-dns", cmd.getName()); |
| 58 | + assertEquals("http://dns.example.com", cmd.getUrl()); |
| 59 | + assertEquals("api-key-123", cmd.getCredentials()); |
| 60 | + assertEquals(Integer.valueOf(8081), cmd.getPort()); |
| 61 | + assertTrue(cmd.isPublic()); |
| 62 | + assertEquals("public.example.com", cmd.getPublicDomainSuffix()); |
| 63 | + assertEquals(Arrays.asList("ns1.example.com", "ns2.example.com"), cmd.getNameServers()); |
| 64 | + assertEquals(DnsProviderType.PowerDNS, cmd.getProvider()); |
| 65 | + assertEquals("localhost", cmd.getExternalServerId()); |
| 66 | + assertEquals("admin@example.com", cmd.getDnsUserName()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testIsPublicFalse() throws Exception { |
| 71 | + AddDnsServerCmd cmd = createCmd(); |
| 72 | + setField(cmd, "isPublic", false); |
| 73 | + assertFalse(cmd.isPublic()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testIsPublicNull() throws Exception { |
| 78 | + AddDnsServerCmd cmd = createCmd(); |
| 79 | + setField(cmd, "isPublic", null); |
| 80 | + assertFalse(cmd.isPublic()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetEntityOwnerId() throws Exception { |
| 85 | + AddDnsServerCmd cmd = createCmd(); |
| 86 | + assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testGetProviderDefault() throws Exception { |
| 91 | + AddDnsServerCmd cmd = createCmd(); |
| 92 | + setField(cmd, "provider", null); |
| 93 | + assertEquals(DnsProviderType.PowerDNS, cmd.getProvider()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testGetProviderCaseInsensitive() throws Exception { |
| 98 | + AddDnsServerCmd cmd = createCmd(); |
| 99 | + setField(cmd, "provider", "powerdns"); |
| 100 | + assertEquals(DnsProviderType.PowerDNS, cmd.getProvider()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testExecuteSuccess() throws Exception { |
| 105 | + AddDnsServerCmd cmd = createCmd(); |
| 106 | + |
| 107 | + DnsServer mockServer = mock(DnsServer.class); |
| 108 | + DnsServerResponse mockResponse = new DnsServerResponse(); |
| 109 | + mockResponse.setName("test-dns"); |
| 110 | + |
| 111 | + when(dnsProviderManager.addDnsServer(cmd)).thenReturn(mockServer); |
| 112 | + when(dnsProviderManager.createDnsServerResponse(mockServer)).thenReturn(mockResponse); |
| 113 | + |
| 114 | + cmd.execute(); |
| 115 | + |
| 116 | + DnsServerResponse response = (DnsServerResponse) cmd.getResponseObject(); |
| 117 | + assertNotNull(response); |
| 118 | + assertEquals("adddnsserverresponse", response.getResponseName()); |
| 119 | + verify(dnsProviderManager).addDnsServer(cmd); |
| 120 | + verify(dnsProviderManager).createDnsServerResponse(mockServer); |
| 121 | + } |
| 122 | + |
| 123 | + @Test(expected = ServerApiException.class) |
| 124 | + public void testExecuteReturnsNull() throws Exception { |
| 125 | + AddDnsServerCmd cmd = createCmd(); |
| 126 | + when(dnsProviderManager.addDnsServer(cmd)).thenReturn(null); |
| 127 | + cmd.execute(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test(expected = ServerApiException.class) |
| 131 | + public void testExecuteThrowsException() throws Exception { |
| 132 | + AddDnsServerCmd cmd = createCmd(); |
| 133 | + when(dnsProviderManager.addDnsServer(cmd)).thenThrow(new RuntimeException("Connection refused")); |
| 134 | + cmd.execute(); |
| 135 | + } |
| 136 | +} |
0 commit comments