unit tests

This commit is contained in:
Manoj Kumar 2026-04-01 12:13:54 +05:30
parent f524bbe0e2
commit 9d4e141ef5
No known key found for this signature in database
GPG Key ID: E952B7234D2C6F88
22 changed files with 2274 additions and 16 deletions

View File

@ -0,0 +1,136 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DnsServerResponse;
import org.apache.cloudstack.dns.DnsProviderType;
import org.apache.cloudstack.dns.DnsServer;
import org.junit.Test;
public class AddDnsServerCmdTest extends BaseDnsCmdTest {
private AddDnsServerCmd createCmd() throws Exception {
AddDnsServerCmd cmd = new AddDnsServerCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "name", "test-dns");
setField(cmd, "url", "http://dns.example.com");
setField(cmd, "provider", "PowerDNS");
setField(cmd, "credentials", "api-key-123");
setField(cmd, "port", 8081);
setField(cmd, "isPublic", true);
setField(cmd, "publicDomainSuffix", "public.example.com");
setField(cmd, "nameServers", Arrays.asList("ns1.example.com", "ns2.example.com"));
setField(cmd, "externalServerId", "localhost");
setField(cmd, "dnsUserName", "admin@example.com");
return cmd;
}
@Test
public void testAccessors() throws Exception {
AddDnsServerCmd cmd = createCmd();
assertEquals("test-dns", cmd.getName());
assertEquals("http://dns.example.com", cmd.getUrl());
assertEquals("api-key-123", cmd.getCredentials());
assertEquals(Integer.valueOf(8081), cmd.getPort());
assertTrue(cmd.isPublic());
assertEquals("public.example.com", cmd.getPublicDomainSuffix());
assertEquals(Arrays.asList("ns1.example.com", "ns2.example.com"), cmd.getNameServers());
assertEquals(DnsProviderType.PowerDNS, cmd.getProvider());
assertEquals("localhost", cmd.getExternalServerId());
assertEquals("admin@example.com", cmd.getDnsUserName());
}
@Test
public void testIsPublicFalse() throws Exception {
AddDnsServerCmd cmd = createCmd();
setField(cmd, "isPublic", false);
assertFalse(cmd.isPublic());
}
@Test
public void testIsPublicNull() throws Exception {
AddDnsServerCmd cmd = createCmd();
setField(cmd, "isPublic", null);
assertFalse(cmd.isPublic());
}
@Test
public void testGetEntityOwnerId() throws Exception {
AddDnsServerCmd cmd = createCmd();
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testGetProviderDefault() throws Exception {
AddDnsServerCmd cmd = createCmd();
setField(cmd, "provider", null);
assertEquals(DnsProviderType.PowerDNS, cmd.getProvider());
}
@Test
public void testGetProviderCaseInsensitive() throws Exception {
AddDnsServerCmd cmd = createCmd();
setField(cmd, "provider", "powerdns");
assertEquals(DnsProviderType.PowerDNS, cmd.getProvider());
}
@Test
public void testExecuteSuccess() throws Exception {
AddDnsServerCmd cmd = createCmd();
DnsServer mockServer = mock(DnsServer.class);
DnsServerResponse mockResponse = new DnsServerResponse();
mockResponse.setName("test-dns");
when(dnsProviderManager.addDnsServer(cmd)).thenReturn(mockServer);
when(dnsProviderManager.createDnsServerResponse(mockServer)).thenReturn(mockResponse);
cmd.execute();
DnsServerResponse response = (DnsServerResponse) cmd.getResponseObject();
assertNotNull(response);
assertEquals("adddnsserverresponse", response.getResponseName());
verify(dnsProviderManager).addDnsServer(cmd);
verify(dnsProviderManager).createDnsServerResponse(mockServer);
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsNull() throws Exception {
AddDnsServerCmd cmd = createCmd();
when(dnsProviderManager.addDnsServer(cmd)).thenReturn(null);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
AddDnsServerCmd cmd = createCmd();
when(dnsProviderManager.addDnsServer(cmd)).thenThrow(new RuntimeException("Connection refused"));
cmd.execute();
}
}

View File

@ -0,0 +1,97 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DnsZoneNetworkMapResponse;
import org.junit.Test;
import com.cloud.network.Network;
import com.cloud.user.Account;
public class AssociateDnsZoneToNetworkCmdTest extends BaseDnsCmdTest {
private static final long NETWORK_ID = 200L;
private AssociateDnsZoneToNetworkCmd createCmd() throws Exception {
AssociateDnsZoneToNetworkCmd cmd = new AssociateDnsZoneToNetworkCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "_entityMgr", entityManager);
setField(cmd, "dnsZoneId", ENTITY_ID);
setField(cmd, "networkId", NETWORK_ID);
setField(cmd, "subDomain", "dev");
return cmd;
}
@Test
public void testAccessors() throws Exception {
AssociateDnsZoneToNetworkCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getDnsZoneId());
assertEquals(Long.valueOf(NETWORK_ID), cmd.getNetworkId());
assertEquals("dev", cmd.getSubDomain());
}
@Test
public void testGetEntityOwnerIdWithNetwork() throws Exception {
AssociateDnsZoneToNetworkCmd cmd = createCmd();
Network mockNetwork = mock(Network.class);
when(mockNetwork.getAccountId()).thenReturn(ACCOUNT_ID);
when(entityManager.findById(Network.class, NETWORK_ID))
.thenReturn(mockNetwork);
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testGetEntityOwnerIdNetworkNotFound() throws Exception {
AssociateDnsZoneToNetworkCmd cmd = createCmd();
when(entityManager.findById(Network.class, NETWORK_ID))
.thenReturn(null);
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
}
@Test
public void testExecuteSuccess() throws Exception {
AssociateDnsZoneToNetworkCmd cmd = createCmd();
DnsZoneNetworkMapResponse mockResponse =
new DnsZoneNetworkMapResponse();
when(dnsProviderManager.associateZoneToNetwork(cmd))
.thenReturn(mockResponse);
cmd.execute();
DnsZoneNetworkMapResponse response =
(DnsZoneNetworkMapResponse) cmd.getResponseObject();
assertNotNull(response);
assertEquals("associatednszonetonetworkresponse",
response.getResponseName());
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
AssociateDnsZoneToNetworkCmd cmd = createCmd();
when(dnsProviderManager.associateZoneToNetwork(cmd))
.thenThrow(new RuntimeException("Error"));
cmd.execute();
}
}

View File

@ -0,0 +1,88 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.dns.DnsProviderManager;
import org.junit.After;
import org.junit.Before;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import com.cloud.user.Account;
import com.cloud.utils.db.EntityManager;
/**
* Shared setup for all DNS command unit tests.
*/
public abstract class BaseDnsCmdTest {
protected static final long ACCOUNT_ID = 42L;
protected static final long ENTITY_ID = 100L;
protected DnsProviderManager dnsProviderManager;
protected EntityManager entityManager;
protected Account callingAccount;
private MockedStatic<CallContext> callContextMock;
@Before
public void setUp() {
dnsProviderManager = mock(DnsProviderManager.class);
entityManager = mock(EntityManager.class);
callingAccount = mock(Account.class);
when(callingAccount.getId()).thenReturn(ACCOUNT_ID);
CallContext callContext = mock(CallContext.class);
when(callContext.getCallingAccount()).thenReturn(callingAccount);
callContextMock = Mockito.mockStatic(CallContext.class);
callContextMock.when(CallContext::current).thenReturn(callContext);
}
@After
public void tearDown() {
callContextMock.close();
}
/**
* Sets a private/inherited field value via reflection.
*/
protected void setField(Object target, String fieldName, Object value) throws Exception {
Field field = null;
Class<?> clazz = target.getClass();
while (clazz != null) {
try {
field = clazz.getDeclaredField(fieldName);
break;
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
}
if (field == null) {
throw new NoSuchFieldException(fieldName + " not found in hierarchy of " + target.getClass().getName());
}
field.setAccessible(true);
field.set(target, value);
}
}

View File

@ -0,0 +1,117 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DnsRecordResponse;
import org.apache.cloudstack.dns.DnsRecord;
import org.junit.Test;
import com.cloud.event.EventTypes;
import com.cloud.exception.InvalidParameterValueException;
public class CreateDnsRecordCmdTest extends BaseDnsCmdTest {
private CreateDnsRecordCmd createCmd() throws Exception {
CreateDnsRecordCmd cmd = new CreateDnsRecordCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "dnsZoneId", ENTITY_ID);
setField(cmd, "name", "www");
setField(cmd, "type", "A");
setField(cmd, "contents", Arrays.asList("192.168.1.1"));
setField(cmd, "ttl", 7200);
return cmd;
}
@Test
public void testAccessors() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getDnsZoneId());
assertEquals("www", cmd.getName());
assertEquals(DnsRecord.RecordType.A, cmd.getType());
assertEquals(Arrays.asList("192.168.1.1"), cmd.getContents());
assertEquals(Integer.valueOf(7200), cmd.getTtl());
}
@Test
public void testGetTtlDefault() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
setField(cmd, "ttl", null);
assertEquals(Integer.valueOf(3600), cmd.getTtl());
}
@Test
public void testGetTypeCname() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
setField(cmd, "type", "CNAME");
assertEquals(DnsRecord.RecordType.CNAME, cmd.getType());
}
@Test(expected = InvalidParameterValueException.class)
public void testGetTypeInvalid() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
setField(cmd, "type", "INVALID");
cmd.getType();
}
@Test
public void testGetEntityOwnerId() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testEventType() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
assertEquals(EventTypes.EVENT_DNS_RECORD_CREATE, cmd.getEventType());
}
@Test
public void testEventDescription() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
assertEquals("Creating DNS Record: www", cmd.getEventDescription());
}
@Test
public void testExecuteSuccess() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
DnsRecordResponse mockResponse = new DnsRecordResponse();
mockResponse.setName("www");
when(dnsProviderManager.createDnsRecord(cmd)).thenReturn(mockResponse);
cmd.execute();
DnsRecordResponse response = (DnsRecordResponse) cmd.getResponseObject();
assertNotNull(response);
assertEquals("creatednsrecordresponse", response.getResponseName());
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
CreateDnsRecordCmd cmd = createCmd();
when(dnsProviderManager.createDnsRecord(cmd)).thenThrow(new RuntimeException("Provider error"));
cmd.execute();
}
}

View File

@ -0,0 +1,157 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DnsZoneResponse;
import org.apache.cloudstack.dns.DnsZone;
import org.junit.Test;
import com.cloud.event.EventTypes;
public class CreateDnsZoneCmdTest extends BaseDnsCmdTest {
private CreateDnsZoneCmd createCmd() throws Exception {
CreateDnsZoneCmd cmd = new CreateDnsZoneCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "name", "example.com");
setField(cmd, "dnsServerId", ENTITY_ID);
setField(cmd, "type", "Public");
setField(cmd, "description", "Test zone");
return cmd;
}
@Test
public void testAccessors() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
assertEquals("example.com", cmd.getName());
assertEquals(Long.valueOf(ENTITY_ID), cmd.getDnsServerId());
assertEquals(DnsZone.ZoneType.Public, cmd.getType());
assertEquals("Test zone", cmd.getDescription());
}
@Test
public void testGetTypePrivate() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
setField(cmd, "type", "Private");
assertEquals(DnsZone.ZoneType.Private, cmd.getType());
}
@Test
public void testGetTypeDefaultsToPublicWhenNull() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
setField(cmd, "type", null);
assertEquals(DnsZone.ZoneType.Public, cmd.getType());
}
@Test
public void testGetTypeDefaultsToPublicWhenBlank() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
setField(cmd, "type", "");
assertEquals(DnsZone.ZoneType.Public, cmd.getType());
}
@Test
public void testGetEntityOwnerId() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testEventType() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
assertEquals(EventTypes.EVENT_DNS_ZONE_CREATE, cmd.getEventType());
}
@Test
public void testEventDescription() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
assertEquals("creating DNS zone: example.com", cmd.getEventDescription());
}
@Test
public void testCreateSuccess() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
DnsZone mockZone = mock(DnsZone.class);
when(mockZone.getId()).thenReturn(ENTITY_ID);
when(mockZone.getUuid()).thenReturn("uuid-123");
when(dnsProviderManager.allocateDnsZone(cmd)).thenReturn(mockZone);
cmd.create();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getEntityId());
assertEquals("uuid-123", cmd.getEntityUuid());
}
@Test(expected = ServerApiException.class)
public void testCreateReturnsNull() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.allocateDnsZone(cmd)).thenReturn(null);
cmd.create();
}
@Test(expected = ServerApiException.class)
public void testCreateThrowsException() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.allocateDnsZone(cmd)).thenThrow(new RuntimeException("DB error"));
cmd.create();
}
@Test
public void testExecuteSuccess() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
cmd.setEntityId(ENTITY_ID);
DnsZone mockZone = mock(DnsZone.class);
DnsZoneResponse mockResponse = new DnsZoneResponse();
mockResponse.setName("example.com");
when(dnsProviderManager.provisionDnsZone(ENTITY_ID)).thenReturn(mockZone);
when(dnsProviderManager.createDnsZoneResponse(mockZone)).thenReturn(mockResponse);
cmd.execute();
DnsZoneResponse response = (DnsZoneResponse) cmd.getResponseObject();
assertNotNull(response);
assertEquals("creatednszoneresponse", response.getResponseName());
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsNull() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
cmd.setEntityId(ENTITY_ID);
when(dnsProviderManager.provisionDnsZone(ENTITY_ID)).thenReturn(null);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
CreateDnsZoneCmd cmd = createCmd();
cmd.setEntityId(ENTITY_ID);
when(dnsProviderManager.provisionDnsZone(ENTITY_ID)).thenThrow(new RuntimeException("Provider error"));
cmd.execute();
}
}

View File

@ -0,0 +1,102 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.dns.DnsRecord;
import org.junit.Test;
import com.cloud.event.EventTypes;
import com.cloud.exception.InvalidParameterValueException;
public class DeleteDnsRecordCmdTest extends BaseDnsCmdTest {
private DeleteDnsRecordCmd createCmd() throws Exception {
DeleteDnsRecordCmd cmd = new DeleteDnsRecordCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "dnsZoneId", ENTITY_ID);
setField(cmd, "name", "www");
setField(cmd, "type", "A");
return cmd;
}
@Test
public void testAccessors() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getDnsZoneId());
assertEquals("www", cmd.getName());
assertEquals(DnsRecord.RecordType.A, cmd.getType());
}
@Test(expected = InvalidParameterValueException.class)
public void testGetTypeInvalid() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
setField(cmd, "type", "BOGUS");
cmd.getType();
}
@Test
public void testGetEntityOwnerId() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testEventType() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
assertEquals(EventTypes.EVENT_DNS_RECORD_DELETE, cmd.getEventType());
}
@Test
public void testEventDescription() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
assertEquals("Deleting DNS Record: www", cmd.getEventDescription());
}
@Test
public void testExecuteSuccess() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsRecord(cmd)).thenReturn(true);
cmd.execute();
SuccessResponse response = (SuccessResponse) cmd.getResponseObject();
assertNotNull(response);
verify(dnsProviderManager).deleteDnsRecord(cmd);
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsFalse() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsRecord(cmd)).thenReturn(false);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
DeleteDnsRecordCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsRecord(cmd)).thenThrow(new RuntimeException("Error"));
cmd.execute();
}
}

View File

@ -0,0 +1,119 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.dns.DnsServer;
import org.junit.Test;
import com.cloud.event.EventTypes;
import com.cloud.user.Account;
public class DeleteDnsServerCmdTest extends BaseDnsCmdTest {
private DeleteDnsServerCmd createCmd() throws Exception {
DeleteDnsServerCmd cmd = new DeleteDnsServerCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "_entityMgr", entityManager);
setField(cmd, "id", ENTITY_ID);
return cmd;
}
@Test
public void testGetId() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getId());
}
@Test
public void testGetCleanupDefault() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
assertTrue(cmd.getCleanup());
}
@Test
public void testGetCleanupFalse() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
setField(cmd, "cleanup", false);
assertFalse(cmd.getCleanup());
}
@Test
public void testGetEntityOwnerIdWithServer() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
DnsServer mockServer = mock(DnsServer.class);
when(mockServer.getAccountId()).thenReturn(ACCOUNT_ID);
when(entityManager.findById(DnsServer.class, ENTITY_ID)).thenReturn(mockServer);
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testGetEntityOwnerIdServerNotFound() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
when(entityManager.findById(DnsServer.class, ENTITY_ID)).thenReturn(null);
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
}
@Test
public void testEventType() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
assertEquals(EventTypes.EVENT_DNS_SERVER_DELETE, cmd.getEventType());
}
@Test
public void testEventDescription() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
assertEquals("Deleting DNS server ID: " + ENTITY_ID, cmd.getEventDescription());
}
@Test
public void testExecuteSuccess() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsServer(cmd)).thenReturn(true);
cmd.execute();
SuccessResponse response = (SuccessResponse) cmd.getResponseObject();
assertNotNull(response);
verify(dnsProviderManager).deleteDnsServer(cmd);
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsFalse() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsServer(cmd)).thenReturn(false);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
DeleteDnsServerCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsServer(cmd)).thenThrow(new RuntimeException("Error"));
cmd.execute();
}
}

View File

@ -0,0 +1,104 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.dns.DnsZone;
import org.junit.Test;
import com.cloud.event.EventTypes;
import com.cloud.user.Account;
public class DeleteDnsZoneCmdTest extends BaseDnsCmdTest {
private DeleteDnsZoneCmd createCmd() throws Exception {
DeleteDnsZoneCmd cmd = new DeleteDnsZoneCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "_entityMgr", entityManager);
setField(cmd, "id", ENTITY_ID);
return cmd;
}
@Test
public void testGetId() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getId());
}
@Test
public void testGetEntityOwnerIdWithZone() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
DnsZone mockZone = mock(DnsZone.class);
when(mockZone.getAccountId()).thenReturn(ACCOUNT_ID);
when(entityManager.findById(DnsZone.class, ENTITY_ID)).thenReturn(mockZone);
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testGetEntityOwnerIdZoneNotFound() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
when(entityManager.findById(DnsZone.class, ENTITY_ID)).thenReturn(null);
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
}
@Test
public void testEventType() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
assertEquals(EventTypes.EVENT_DNS_ZONE_DELETE, cmd.getEventType());
}
@Test
public void testEventDescription() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
assertEquals("Deleting DNS Zone ID: " + ENTITY_ID, cmd.getEventDescription());
}
@Test
public void testExecuteSuccess() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsZone(ENTITY_ID)).thenReturn(true);
cmd.execute();
SuccessResponse response = (SuccessResponse) cmd.getResponseObject();
assertNotNull(response);
verify(dnsProviderManager).deleteDnsZone(ENTITY_ID);
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsFalse() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsZone(ENTITY_ID)).thenReturn(false);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
DeleteDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.deleteDnsZone(ENTITY_ID)).thenThrow(new RuntimeException("Error"));
cmd.execute();
}
}

View File

@ -0,0 +1,100 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.junit.Test;
import com.cloud.network.Network;
import com.cloud.user.Account;
public class DisassociateDnsZoneFromNetworkCmdTest extends BaseDnsCmdTest {
private static final long NETWORK_ID = 200L;
private DisassociateDnsZoneFromNetworkCmd createCmd() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd =
new DisassociateDnsZoneFromNetworkCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "_entityMgr", entityManager);
setField(cmd, "networkId", NETWORK_ID);
return cmd;
}
@Test
public void testGetNetworkId() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd = createCmd();
assertEquals(Long.valueOf(NETWORK_ID), cmd.getNetworkId());
}
@Test
public void testGetEntityOwnerIdWithNetwork() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd = createCmd();
Network mockNetwork = mock(Network.class);
when(mockNetwork.getAccountId()).thenReturn(ACCOUNT_ID);
when(entityManager.findById(Network.class, NETWORK_ID))
.thenReturn(mockNetwork);
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testGetEntityOwnerIdNetworkNotFound() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd = createCmd();
when(entityManager.findById(Network.class, NETWORK_ID))
.thenReturn(null);
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
}
@Test
public void testExecuteSuccess() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd = createCmd();
when(dnsProviderManager.disassociateZoneFromNetwork(cmd))
.thenReturn(true);
cmd.execute();
SuccessResponse response =
(SuccessResponse) cmd.getResponseObject();
assertNotNull(response);
verify(dnsProviderManager).disassociateZoneFromNetwork(cmd);
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsFalse() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd = createCmd();
when(dnsProviderManager.disassociateZoneFromNetwork(cmd))
.thenReturn(false);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
DisassociateDnsZoneFromNetworkCmd cmd = createCmd();
when(dnsProviderManager.disassociateZoneFromNetwork(cmd))
.thenThrow(new RuntimeException("Error"));
cmd.execute();
}
}

View File

@ -0,0 +1,83 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
import org.apache.cloudstack.api.response.DnsProviderResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.junit.Test;
public class ListDnsProvidersCmdTest extends BaseDnsCmdTest {
private ListDnsProvidersCmd createCmd() throws Exception {
ListDnsProvidersCmd cmd = new ListDnsProvidersCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
return cmd;
}
@Test
public void testExecute() throws Exception {
ListDnsProvidersCmd cmd = createCmd();
when(dnsProviderManager.listProviderNames()).thenReturn(Arrays.asList("PowerDNS"));
cmd.execute();
@SuppressWarnings("unchecked")
ListResponse<DnsProviderResponse> response =
(ListResponse<DnsProviderResponse>) cmd.getResponseObject();
assertNotNull(response);
assertEquals("listdnsprovidersresponse", response.getResponseName());
assertNotNull(response.getResponses());
assertEquals(1, response.getResponses().size());
}
@Test
public void testExecuteMultipleProviders() throws Exception {
ListDnsProvidersCmd cmd = createCmd();
when(dnsProviderManager.listProviderNames())
.thenReturn(Arrays.asList("PowerDNS", "Cloudflare"));
cmd.execute();
@SuppressWarnings("unchecked")
ListResponse<DnsProviderResponse> response =
(ListResponse<DnsProviderResponse>) cmd.getResponseObject();
assertNotNull(response);
assertEquals(2, response.getResponses().size());
}
@Test
public void testExecuteEmptyList() throws Exception {
ListDnsProvidersCmd cmd = createCmd();
when(dnsProviderManager.listProviderNames())
.thenReturn(Collections.emptyList());
cmd.execute();
@SuppressWarnings("unchecked")
ListResponse<DnsProviderResponse> response =
(ListResponse<DnsProviderResponse>) cmd.getResponseObject();
assertNotNull(response);
assertEquals(0, response.getResponses().size());
}
}

View File

@ -0,0 +1,56 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.response.DnsRecordResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.junit.Test;
public class ListDnsRecordsCmdTest extends BaseDnsCmdTest {
private ListDnsRecordsCmd createCmd() throws Exception {
ListDnsRecordsCmd cmd = new ListDnsRecordsCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "dnsZoneId", ENTITY_ID);
return cmd;
}
@Test
public void testGetDnsZoneId() throws Exception {
ListDnsRecordsCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getDnsZoneId());
}
@Test
public void testExecute() throws Exception {
ListDnsRecordsCmd cmd = createCmd();
ListResponse<DnsRecordResponse> mockListResponse = new ListResponse<>();
when(dnsProviderManager.listDnsRecords(cmd)).thenReturn(mockListResponse);
cmd.execute();
@SuppressWarnings("unchecked")
ListResponse<DnsRecordResponse> response = (ListResponse<DnsRecordResponse>) cmd.getResponseObject();
assertNotNull(response);
assertEquals("listdnsrecordsresponse", response.getResponseName());
}
}

View File

@ -0,0 +1,67 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.response.DnsServerResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.dns.DnsProviderType;
import org.junit.Test;
public class ListDnsServersCmdTest extends BaseDnsCmdTest {
private ListDnsServersCmd createCmd() throws Exception {
ListDnsServersCmd cmd = new ListDnsServersCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "id", ENTITY_ID);
setField(cmd, "providerType", "PowerDNS");
return cmd;
}
@Test
public void testAccessors() throws Exception {
ListDnsServersCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getId());
assertEquals(DnsProviderType.PowerDNS, cmd.getProviderType());
}
@Test
public void testGetProviderTypeNull() throws Exception {
ListDnsServersCmd cmd = createCmd();
setField(cmd, "providerType", null);
assertEquals(DnsProviderType.PowerDNS, cmd.getProviderType());
}
@Test
public void testExecute() throws Exception {
ListDnsServersCmd cmd = createCmd();
ListResponse<DnsServerResponse> mockListResponse = new ListResponse<>();
when(dnsProviderManager.listDnsServers(cmd)).thenReturn(mockListResponse);
cmd.execute();
@SuppressWarnings("unchecked")
ListResponse<DnsServerResponse> response = (ListResponse<DnsServerResponse>) cmd.getResponseObject();
assertNotNull(response);
assertEquals("listdnsserversresponse", response.getResponseName());
assertEquals("dnsserver", response.getObjectName());
}
}

View File

@ -0,0 +1,60 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.response.DnsZoneResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.junit.Test;
public class ListDnsZonesCmdTest extends BaseDnsCmdTest {
private ListDnsZonesCmd createCmd() throws Exception {
ListDnsZonesCmd cmd = new ListDnsZonesCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "id", ENTITY_ID);
setField(cmd, "dnsServerId", 200L);
setField(cmd, "name", "example.com");
return cmd;
}
@Test
public void testAccessors() throws Exception {
ListDnsZonesCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getId());
assertEquals(Long.valueOf(200L), cmd.getDnsServerId());
assertEquals("example.com", cmd.getName());
}
@Test
public void testExecute() throws Exception {
ListDnsZonesCmd cmd = createCmd();
ListResponse<DnsZoneResponse> mockListResponse = new ListResponse<>();
when(dnsProviderManager.listDnsZones(cmd)).thenReturn(mockListResponse);
cmd.execute();
@SuppressWarnings("unchecked")
ListResponse<DnsZoneResponse> response = (ListResponse<DnsZoneResponse>) cmd.getResponseObject();
assertNotNull(response);
assertEquals("listdnszonesresponse", response.getResponseName());
}
}

View File

@ -0,0 +1,143 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DnsServerResponse;
import org.apache.cloudstack.dns.DnsServer;
import org.junit.Test;
import com.cloud.user.Account;
public class UpdateDnsServerCmdTest extends BaseDnsCmdTest {
private UpdateDnsServerCmd createCmd() throws Exception {
UpdateDnsServerCmd cmd = new UpdateDnsServerCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "_entityMgr", entityManager);
setField(cmd, "id", ENTITY_ID);
setField(cmd, "name", "updated-dns");
setField(cmd, "url", "http://updated.dns.com");
setField(cmd, "credentials", "new-api-key");
setField(cmd, "port", 9090);
setField(cmd, "isPublic", true);
setField(cmd, "publicDomainSuffix", "updated.example.com");
setField(cmd, "nameServers", "ns1.updated.com,ns2.updated.com");
setField(cmd, "state", "Enabled");
return cmd;
}
@Test
public void testAccessors() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getId());
assertEquals("updated-dns", cmd.getName());
assertEquals("http://updated.dns.com", cmd.getUrl());
assertEquals("new-api-key", cmd.getCredentials());
assertEquals(Integer.valueOf(9090), cmd.getPort());
assertTrue(cmd.isPublic());
assertEquals("updated.example.com", cmd.getPublicDomainSuffix());
assertEquals("ns1.updated.com,ns2.updated.com", cmd.getNameServers());
assertEquals(DnsServer.State.Enabled, cmd.getState());
}
@Test
public void testGetStateDisabled() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
setField(cmd, "state", "Disabled");
assertEquals(DnsServer.State.Disabled, cmd.getState());
}
@Test
public void testGetStateNull() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
setField(cmd, "state", null);
assertNull(cmd.getState());
}
@Test
public void testGetStateBlank() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
setField(cmd, "state", "");
assertNull(cmd.getState());
}
@Test(expected = IllegalArgumentException.class)
public void testGetStateInvalid() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
setField(cmd, "state", "InvalidState");
cmd.getState();
}
@Test
public void testGetEntityOwnerIdWithServer() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
DnsServer mockServer = mock(DnsServer.class);
when(mockServer.getAccountId()).thenReturn(ACCOUNT_ID);
when(entityManager.findById(DnsServer.class, ENTITY_ID)).thenReturn(mockServer);
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testGetEntityOwnerIdServerNotFound() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
when(entityManager.findById(DnsServer.class, ENTITY_ID)).thenReturn(null);
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
}
@Test
public void testExecuteSuccess() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
DnsServer mockServer = mock(DnsServer.class);
DnsServerResponse mockResponse = new DnsServerResponse();
mockResponse.setName("updated-dns");
when(dnsProviderManager.updateDnsServer(cmd)).thenReturn(mockServer);
when(dnsProviderManager.createDnsServerResponse(mockServer)).thenReturn(mockResponse);
cmd.execute();
DnsServerResponse response = (DnsServerResponse) cmd.getResponseObject();
assertNotNull(response);
assertEquals("updatednsserverresponse", response.getResponseName());
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsNull() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
when(dnsProviderManager.updateDnsServer(cmd)).thenReturn(null);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
UpdateDnsServerCmd cmd = createCmd();
when(dnsProviderManager.updateDnsServer(cmd)).thenThrow(new RuntimeException("Update failed"));
cmd.execute();
}
}

View File

@ -0,0 +1,83 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DnsZoneResponse;
import org.apache.cloudstack.dns.DnsZone;
import org.junit.Test;
public class UpdateDnsZoneCmdTest extends BaseDnsCmdTest {
private UpdateDnsZoneCmd createCmd() throws Exception {
UpdateDnsZoneCmd cmd = new UpdateDnsZoneCmd();
setField(cmd, "dnsProviderManager", dnsProviderManager);
setField(cmd, "id", ENTITY_ID);
setField(cmd, "description", "Updated description");
return cmd;
}
@Test
public void testAccessors() throws Exception {
UpdateDnsZoneCmd cmd = createCmd();
assertEquals(Long.valueOf(ENTITY_ID), cmd.getId());
assertEquals("Updated description", cmd.getDescription());
}
@Test
public void testGetEntityOwnerId() throws Exception {
UpdateDnsZoneCmd cmd = createCmd();
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
}
@Test
public void testExecuteSuccess() throws Exception {
UpdateDnsZoneCmd cmd = createCmd();
DnsZone mockZone = mock(DnsZone.class);
DnsZoneResponse mockResponse = new DnsZoneResponse();
mockResponse.setName("example.com");
when(dnsProviderManager.updateDnsZone(cmd)).thenReturn(mockZone);
when(dnsProviderManager.createDnsZoneResponse(mockZone)).thenReturn(mockResponse);
cmd.execute();
DnsZoneResponse response = (DnsZoneResponse) cmd.getResponseObject();
assertNotNull(response);
assertEquals("updatednszoneresponse", response.getResponseName());
}
@Test(expected = ServerApiException.class)
public void testExecuteReturnsNull() throws Exception {
UpdateDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.updateDnsZone(cmd)).thenReturn(null);
cmd.execute();
}
@Test(expected = ServerApiException.class)
public void testExecuteThrowsException() throws Exception {
UpdateDnsZoneCmd cmd = createCmd();
when(dnsProviderManager.updateDnsZone(cmd)).thenThrow(new RuntimeException("Update failed"));
cmd.execute();
}
}

View File

@ -0,0 +1,105 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.dns;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import com.cloud.utils.exception.CloudRuntimeException;
public class DnsRecordTest {
@Test
public void testDefaultConstructor() {
DnsRecord record = new DnsRecord();
assertNull(record.getName());
assertNull(record.getType());
assertNull(record.getContents());
assertEquals(0, record.getTtl());
}
@Test
public void testParameterizedConstructor() {
List<String> contents = Arrays.asList("192.168.1.1");
DnsRecord record = new DnsRecord("www", DnsRecord.RecordType.A, contents, 3600);
assertEquals("www", record.getName());
assertEquals(DnsRecord.RecordType.A, record.getType());
assertEquals(contents, record.getContents());
assertEquals(3600, record.getTtl());
}
@Test
public void testSettersAndGetters() {
DnsRecord record = new DnsRecord();
List<String> contents = Arrays.asList("10.0.0.1", "10.0.0.2");
record.setName("mail");
record.setType(DnsRecord.RecordType.AAAA);
record.setContents(contents);
record.setTtl(7200);
assertEquals("mail", record.getName());
assertEquals(DnsRecord.RecordType.AAAA, record.getType());
assertEquals(contents, record.getContents());
assertEquals(7200, record.getTtl());
}
// RecordType.fromString tests
@Test
public void testFromStringValid() {
assertEquals(DnsRecord.RecordType.A, DnsRecord.RecordType.fromString("A"));
assertEquals(DnsRecord.RecordType.AAAA, DnsRecord.RecordType.fromString("AAAA"));
assertEquals(DnsRecord.RecordType.CNAME, DnsRecord.RecordType.fromString("CNAME"));
assertEquals(DnsRecord.RecordType.MX, DnsRecord.RecordType.fromString("MX"));
assertEquals(DnsRecord.RecordType.TXT, DnsRecord.RecordType.fromString("TXT"));
assertEquals(DnsRecord.RecordType.SRV, DnsRecord.RecordType.fromString("SRV"));
assertEquals(DnsRecord.RecordType.PTR, DnsRecord.RecordType.fromString("PTR"));
assertEquals(DnsRecord.RecordType.NS, DnsRecord.RecordType.fromString("NS"));
}
@Test
public void testFromStringCaseInsensitive() {
assertEquals(DnsRecord.RecordType.A, DnsRecord.RecordType.fromString("a"));
assertEquals(DnsRecord.RecordType.CNAME, DnsRecord.RecordType.fromString("cname"));
assertEquals(DnsRecord.RecordType.MX, DnsRecord.RecordType.fromString("mx"));
}
@Test
public void testFromStringNull() {
assertNull(DnsRecord.RecordType.fromString(null));
}
@Test(expected = CloudRuntimeException.class)
public void testFromStringInvalid() {
DnsRecord.RecordType.fromString("INVALID");
}
@Test
public void testRecordTypeValues() {
DnsRecord.RecordType[] values = DnsRecord.RecordType.values();
assertNotNull(values);
assertEquals(8, values.length);
}
}

View File

@ -944,8 +944,6 @@ public class DnsProviderManagerImpl extends ManagerBase implements DnsProviderMa
}
}
private void handleNicEvent(long nicId, long instanceId, boolean isAddDnsRecord) {
VMInstanceVO vmInstanceVO = vmInstanceDao.findById(instanceId);
if (vmInstanceVO == null) {

View File

@ -109,20 +109,34 @@ public class DnsProviderManagerImplTest {
@InjectMocks
DnsProviderManagerImpl manager;
@Mock AccountManager accountMgr;
@Mock DnsServerDao dnsServerDao;
@Mock DnsZoneDao dnsZoneDao;
@Mock DnsZoneJoinDao dnsZoneJoinDao;
@Mock DnsServerJoinDao dnsServerJoinDao;
@Mock DnsZoneNetworkMapDao dnsZoneNetworkMapDao;
@Mock NetworkDao networkDao;
@Mock DomainDao domainDao;
@Mock NicDao nicDao;
@Mock NicDetailsDao nicDetailsDao;
@Mock MessageBus messageBus;
@Mock VMInstanceDao vmInstanceDao;
@Mock DnsProvider dnsProviderMock;
@Mock Account callerMock;
@Mock
AccountManager accountMgr;
@Mock
DnsServerDao dnsServerDao;
@Mock
DnsZoneDao dnsZoneDao;
@Mock
DnsZoneJoinDao dnsZoneJoinDao;
@Mock
DnsServerJoinDao dnsServerJoinDao;
@Mock
DnsZoneNetworkMapDao dnsZoneNetworkMapDao;
@Mock
NetworkDao networkDao;
@Mock
DomainDao domainDao;
@Mock
NicDao nicDao;
@Mock
NicDetailsDao nicDetailsDao;
@Mock
MessageBus messageBus;
@Mock
VMInstanceDao vmInstanceDao;
@Mock
DnsProvider dnsProviderMock;
@Mock
Account callerMock;
private MockedStatic<CallContext> callContextMocked;
private CallContext callContextMock;
@ -889,4 +903,211 @@ public class DnsProviderManagerImplTest {
ListResponse<DnsZoneResponse> res = manager.listDnsZones(cmd);
assertEquals(1, res.getCount().intValue());
}
@Test(expected = InvalidParameterValueException.class)
public void testAddDnsServerAlreadyExists() {
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
when(cmd.getUrl()).thenReturn("http://newpdns:8081");
when(dnsServerDao.findByUrlAndAccount(anyString(), anyLong())).thenReturn(serverVO);
manager.addDnsServer(cmd);
}
@Test
public void testAddDnsServerNormalUser() throws Exception {
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
when(callerMock.getType()).thenReturn(Account.Type.NORMAL);
when(cmd.getUrl()).thenReturn("http://newpdns:8081");
when(cmd.getProvider()).thenReturn(DnsProviderType.PowerDNS);
when(cmd.getNameServers()).thenReturn(Collections.emptyList());
when(cmd.isPublic()).thenReturn(true);
when(cmd.getPublicDomainSuffix()).thenReturn("example.com");
when(dnsServerDao.findByUrlAndAccount(anyString(), anyLong())).thenReturn(null);
when(dnsProviderMock.validateAndResolveServer(any())).thenReturn("resolved-id");
when(dnsServerDao.persist(any())).thenReturn(serverVO);
DnsServer result = manager.addDnsServer(cmd);
assertNotNull(result);
verify(dnsServerDao).persist(Mockito.argThat(s -> !((DnsServerVO) s).getPublicServer() && ((DnsServerVO) s).getPublicDomainSuffix() == null));
}
@Test(expected = CloudRuntimeException.class)
public void testAddDnsServerValidationFailure() throws Exception {
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
when(callerMock.getType()).thenReturn(Account.Type.ADMIN);
when(cmd.getUrl()).thenReturn("http://newpdns:8081");
when(cmd.getProvider()).thenReturn(DnsProviderType.PowerDNS);
when(cmd.getNameServers()).thenReturn(Collections.emptyList());
when(dnsServerDao.findByUrlAndAccount(anyString(), anyLong())).thenReturn(null);
when(dnsProviderMock.validateAndResolveServer(any())).thenThrow(new CloudRuntimeException("Validation failed"));
manager.addDnsServer(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void testUpdateDnsServerUrlDuplicate() {
org.apache.cloudstack.api.command.user.dns.UpdateDnsServerCmd cmd = mock(org.apache.cloudstack.api.command.user.dns.UpdateDnsServerCmd.class);
when(cmd.getId()).thenReturn(SERVER_ID);
when(cmd.getUrl()).thenReturn("http://duplicate:8081");
DnsServerVO existingServer = mock(DnsServerVO.class);
when(existingServer.getId()).thenReturn(SERVER_ID + 1); // Different ID implies duplicate
when(dnsServerDao.findById(SERVER_ID)).thenReturn(serverVO);
Mockito.doReturn("http://original:8081").when(serverVO).getUrl();
when(dnsServerDao.findByUrlAndAccount(anyString(), anyLong())).thenReturn(existingServer);
manager.updateDnsServer(cmd);
}
@Test
public void testUpdateDnsServerUrlValid() throws Exception {
org.apache.cloudstack.api.command.user.dns.UpdateDnsServerCmd cmd = mock(org.apache.cloudstack.api.command.user.dns.UpdateDnsServerCmd.class);
when(cmd.getId()).thenReturn(SERVER_ID);
when(cmd.getUrl()).thenReturn("http://new-url:8081");
when(dnsServerDao.findById(SERVER_ID)).thenReturn(serverVO);
Mockito.doReturn("http://original:8081").when(serverVO).getUrl();
Mockito.doReturn(DnsProviderType.PowerDNS).when(serverVO).getProviderType();
when(dnsServerDao.findByUrlAndAccount(anyString(), anyLong())).thenReturn(null);
doNothing().when(dnsProviderMock).validate(any());
when(dnsServerDao.update(anyLong(), any())).thenReturn(true);
DnsServer result = manager.updateDnsServer(cmd);
assertNotNull(result);
verify(dnsProviderMock).validate(any()); // Changing URL triggers validationRequired
}
@Test(expected = InvalidParameterValueException.class)
public void testUpdateDnsServerValidationException() throws Exception {
org.apache.cloudstack.api.command.user.dns.UpdateDnsServerCmd cmd = mock(org.apache.cloudstack.api.command.user.dns.UpdateDnsServerCmd.class);
when(cmd.getId()).thenReturn(SERVER_ID);
when(cmd.getCredentials()).thenReturn("new-api-key");
when(dnsServerDao.findById(SERVER_ID)).thenReturn(serverVO);
Mockito.doReturn("old-api-key").when(serverVO).getApiKey();
Mockito.doReturn("http://original:8081").when(serverVO).getUrl();
Mockito.doReturn(DnsProviderType.PowerDNS).when(serverVO).getProviderType();
Mockito.doThrow(new CloudRuntimeException("Validation failed")).when(dnsProviderMock).validate(any());
manager.updateDnsServer(cmd);
}
@Test
public void testVmLifecycleSubscriberStateUnchanged() {
DnsProviderManagerImpl.VmLifecycleSubscriber subscriber = manager.new VmLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.OLD_STATE, com.cloud.vm.VirtualMachine.State.Running);
event.put(org.apache.cloudstack.api.ApiConstants.NEW_STATE, com.cloud.vm.VirtualMachine.State.Running);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 10L);
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, never()).findByIdIncludingRemoved(anyLong());
}
@Test
public void testVmLifecycleSubscriberRunning() {
DnsProviderManagerImpl.VmLifecycleSubscriber subscriber = manager.new VmLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.OLD_STATE, com.cloud.vm.VirtualMachine.State.Starting);
event.put(org.apache.cloudstack.api.ApiConstants.NEW_STATE, com.cloud.vm.VirtualMachine.State.Running);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 12L);
// Expect handleVmEvent to be called, which accesses vmInstanceDao.findByIdIncludingRemoved
when(vmInstanceDao.findByIdIncludingRemoved(12L)).thenReturn(null);
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, times(1)).findByIdIncludingRemoved(12L);
}
@Test
public void testVmLifecycleSubscriberStopped() {
DnsProviderManagerImpl.VmLifecycleSubscriber subscriber = manager.new VmLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.OLD_STATE, com.cloud.vm.VirtualMachine.State.Running);
event.put(org.apache.cloudstack.api.ApiConstants.NEW_STATE, com.cloud.vm.VirtualMachine.State.Stopped);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 15L);
when(vmInstanceDao.findByIdIncludingRemoved(15L)).thenReturn(null);
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, times(1)).findByIdIncludingRemoved(15L);
}
@Test
public void testVmLifecycleSubscriberUnsupportedState() {
DnsProviderManagerImpl.VmLifecycleSubscriber subscriber = manager.new VmLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.OLD_STATE, com.cloud.vm.VirtualMachine.State.Running);
event.put(org.apache.cloudstack.api.ApiConstants.NEW_STATE, com.cloud.vm.VirtualMachine.State.Starting);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 20L);
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, never()).findByIdIncludingRemoved(anyLong());
}
@Test
public void testVmLifecycleSubscriberException() {
DnsProviderManagerImpl.VmLifecycleSubscriber subscriber = manager.new VmLifecycleSubscriber();
// Passing invalid args to trigger ClassCastException or similar
subscriber.onPublishMessage("sender", "subject", "not a map");
// Should not throw exception upstream
verify(vmInstanceDao, never()).findByIdIncludingRemoved(anyLong());
}
@Test
public void testNicLifecycleSubscriberCreate() {
DnsProviderManagerImpl.NicLifecycleSubscriber subscriber = manager.new NicLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.EVENT_TYPE, com.cloud.event.EventTypes.EVENT_NIC_CREATE);
event.put(org.apache.cloudstack.api.ApiConstants.NIC_ID, 100L);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 200L);
when(vmInstanceDao.findById(200L)).thenReturn(null); // Short circuits handleNicEvent
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, times(1)).findById(200L);
}
@Test
public void testNicLifecycleSubscriberDelete() {
DnsProviderManagerImpl.NicLifecycleSubscriber subscriber = manager.new NicLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.EVENT_TYPE, com.cloud.event.EventTypes.EVENT_NIC_DELETE);
event.put(org.apache.cloudstack.api.ApiConstants.NIC_ID, 101L);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 201L);
when(vmInstanceDao.findById(201L)).thenReturn(null);
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, times(1)).findById(201L);
}
@Test
public void testNicLifecycleSubscriberMissingData() {
DnsProviderManagerImpl.NicLifecycleSubscriber subscriber = manager.new NicLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.EVENT_TYPE, com.cloud.event.EventTypes.EVENT_NIC_CREATE);
// Missing NIC_ID and INSTANCE_ID
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, never()).findById(anyLong());
}
@Test
public void testNicLifecycleSubscriberUnsupportedEvent() {
DnsProviderManagerImpl.NicLifecycleSubscriber subscriber = manager.new NicLifecycleSubscriber();
java.util.Map<String, Object> event = new java.util.HashMap<>();
event.put(org.apache.cloudstack.api.ApiConstants.EVENT_TYPE, "unsupported-event");
event.put(org.apache.cloudstack.api.ApiConstants.NIC_ID, 102L);
event.put(org.apache.cloudstack.api.ApiConstants.INSTANCE_ID, 202L);
subscriber.onPublishMessage("sender", "subject", event);
verify(vmInstanceDao, never()).findById(anyLong());
}
@Test
public void testNicLifecycleSubscriberException() {
DnsProviderManagerImpl.NicLifecycleSubscriber subscriber = manager.new NicLifecycleSubscriber();
subscriber.onPublishMessage("sender", "subject", "not a map");
// Should catch and not throw
verify(vmInstanceDao, never()).findById(anyLong());
}
}

View File

@ -0,0 +1,91 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.dns.dao;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import org.apache.cloudstack.dns.vo.DnsZoneNetworkMapVO;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import com.cloud.utils.db.SearchCriteria;
@RunWith(MockitoJUnitRunner.class)
public class DnsZoneNetworkMapDaoImplTest {
private DnsZoneNetworkMapDaoImpl dao;
private DnsZoneNetworkMapVO mockMapping;
@Before
public void setUp() {
dao = spy(new DnsZoneNetworkMapDaoImpl());
mockMapping = new DnsZoneNetworkMapVO(10L, 20L, "dev");
}
@Test
public void testFindByNetworkId() {
doReturn(mockMapping).when(dao).findOneBy(any(SearchCriteria.class));
DnsZoneNetworkMapVO result = dao.findByNetworkId(20L);
assertNotNull(result);
assertEquals(10L, result.getDnsZoneId());
assertEquals(20L, result.getNetworkId());
assertEquals("dev", result.getSubDomain());
}
@Test
public void testFindByNetworkIdNotFound() {
doReturn(null).when(dao).findOneBy(any(SearchCriteria.class));
DnsZoneNetworkMapVO result = dao.findByNetworkId(999L);
assertNull(result);
}
@Test
public void testFindByZoneId() {
doReturn(mockMapping).when(dao).findOneBy(any(SearchCriteria.class));
DnsZoneNetworkMapVO result = dao.findByZoneId(10L);
assertNotNull(result);
assertEquals(10L, result.getDnsZoneId());
assertEquals(20L, result.getNetworkId());
}
@Test
public void testFindByZoneIdNotFound() {
doReturn(null).when(dao).findOneBy(any(SearchCriteria.class));
DnsZoneNetworkMapVO result = dao.findByZoneId(999L);
assertNull(result);
}
@Test
public void testRemoveNetworkMappingByZoneId() {
doReturn(1).when(dao).remove(any(SearchCriteria.class));
dao.removeNetworkMappingByZoneId(10L);
verify(dao).remove(any(SearchCriteria.class));
}
}

View File

@ -0,0 +1,100 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.dns.vo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import org.apache.cloudstack.dns.DnsServer;
import org.junit.Test;
public class DnsServerJoinVOTest {
private void setField(Object target, String fieldName, Object value) throws Exception {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(target, value);
}
@Test
public void testDefaultConstructor() {
DnsServerJoinVO vo = new DnsServerJoinVO();
assertEquals(0L, vo.getId());
assertNull(vo.getUuid());
assertNull(vo.getName());
}
@Test
public void testAllGetters() throws Exception {
DnsServerJoinVO vo = new DnsServerJoinVO();
setField(vo, "id", 1L);
setField(vo, "uuid", "test-uuid");
setField(vo, "name", "test-server");
setField(vo, "providerType", "PowerDNS");
setField(vo, "url", "http://pdns:8081");
setField(vo, "port", 8081);
setField(vo, "nameServers", "ns1.example.com,ns2.example.com");
setField(vo, "isPublic", true);
setField(vo, "publicDomainSuffix", "pub.example.com");
setField(vo, "state", DnsServer.State.Enabled);
setField(vo, "accountName", "admin");
setField(vo, "domainName", "ROOT");
setField(vo, "domainUuid", "domain-uuid");
setField(vo, "domainPath", "/ROOT");
assertEquals(1L, vo.getId());
assertEquals("test-uuid", vo.getUuid());
assertEquals("test-server", vo.getName());
assertEquals("PowerDNS", vo.getProviderType());
assertEquals("http://pdns:8081", vo.getUrl());
assertEquals(Integer.valueOf(8081), vo.getPort());
assertEquals(Arrays.asList("ns1.example.com", "ns2.example.com"), vo.getNameServers());
assertTrue(vo.isPublicServer());
assertEquals("pub.example.com", vo.getPublicDomainSuffix());
assertEquals(DnsServer.State.Enabled, vo.getState());
assertEquals("admin", vo.getAccountName());
assertEquals("ROOT", vo.getDomainName());
assertEquals("domain-uuid", vo.getDomainUuid());
assertEquals("/ROOT", vo.getDomainPath());
}
@Test
public void testGetNameServersEmpty() {
DnsServerJoinVO vo = new DnsServerJoinVO();
assertEquals(Collections.emptyList(), vo.getNameServers());
}
@Test
public void testGetNameServersBlank() throws Exception {
DnsServerJoinVO vo = new DnsServerJoinVO();
setField(vo, "nameServers", "");
assertEquals(Collections.emptyList(), vo.getNameServers());
}
@Test
public void testIsPublicServerDefault() {
DnsServerJoinVO vo = new DnsServerJoinVO();
assertFalse(vo.isPublicServer());
}
}

View File

@ -0,0 +1,149 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.dns.vo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.cloudstack.dns.DnsProviderType;
import org.apache.cloudstack.dns.DnsServer;
import org.junit.Test;
public class DnsServerVOTest {
@Test
public void testDefaultConstructor() {
DnsServerVO vo = new DnsServerVO();
assertNotNull(vo.getUuid());
assertNotNull(vo.getCreated());
}
@Test
public void testParameterizedConstructor() {
List<String> nameServers = Arrays.asList("ns1.example.com", "ns2.example.com");
DnsServerVO vo = new DnsServerVO("test-server", "http://pdns:8081", 8081,
"localhost", DnsProviderType.PowerDNS, "admin", "api-key-123",
true, "public.example.com", nameServers, 10L, 20L);
assertEquals("test-server", vo.getName());
assertEquals("http://pdns:8081", vo.getUrl());
assertEquals(Integer.valueOf(8081), vo.getPort());
assertEquals("localhost", vo.getExternalServerId());
assertEquals(DnsProviderType.PowerDNS, vo.getProviderType());
assertEquals("api-key-123", vo.getApiKey());
assertTrue(vo.getPublicServer());
assertEquals("public.example.com", vo.getPublicDomainSuffix());
assertEquals(nameServers, vo.getNameServers());
assertEquals(10L, vo.getAccountId());
assertEquals(20L, vo.getDomainId());
assertEquals(DnsServer.State.Enabled, vo.getState());
assertNotNull(vo.getUuid());
assertNotNull(vo.getCreated());
assertNull(vo.getRemoved());
}
@Test
public void testGetEntityType() {
DnsServerVO vo = new DnsServerVO();
assertEquals(DnsServer.class, vo.getEntityType());
}
@Test
public void testSettersAndGetters() {
DnsServerVO vo = new DnsServerVO();
vo.setName("updated");
assertEquals("updated", vo.getName());
vo.setUrl("http://new-url:8081");
assertEquals("http://new-url:8081", vo.getUrl());
vo.setPort(9090);
assertEquals(Integer.valueOf(9090), vo.getPort());
vo.setApiKey("new-key");
assertEquals("new-key", vo.getApiKey());
vo.setPublicServer(true);
assertTrue(vo.getPublicServer());
vo.setPublicDomainSuffix("new.suffix.com");
assertEquals("new.suffix.com", vo.getPublicDomainSuffix());
vo.setExternalServerId("remote-host");
assertEquals("remote-host", vo.getExternalServerId());
vo.setState(DnsServer.State.Disabled);
assertEquals(DnsServer.State.Disabled, vo.getState());
vo.setNameServers("ns1.test.com,ns2.test.com");
assertEquals(Arrays.asList("ns1.test.com", "ns2.test.com"), vo.getNameServers());
}
@Test
public void testGetNameServersEmpty() {
DnsServerVO vo = new DnsServerVO();
assertEquals(Collections.emptyList(), vo.getNameServers());
}
@Test
public void testGetNameServersBlank() {
DnsServerVO vo = new DnsServerVO();
vo.setNameServers("");
assertEquals(Collections.emptyList(), vo.getNameServers());
}
@Test
public void testGetNameServersSingle() {
DnsServerVO vo = new DnsServerVO();
vo.setNameServers("ns1.example.com");
assertEquals(Collections.singletonList("ns1.example.com"), vo.getNameServers());
}
@Test
public void testToString() {
List<String> nameServers = Collections.singletonList("ns1.example.com");
DnsServerVO vo = new DnsServerVO("test-server", "http://pdns:8081", 8081,
"localhost", DnsProviderType.PowerDNS, null, "secret-key",
false, null, nameServers, 1L, 10L);
String result = vo.toString();
assertTrue(result.contains("test-server"));
assertTrue(result.contains("http://pdns:8081"));
// API key should be masked
assertTrue(result.contains("*****"));
assertFalse(result.contains("secret-key"));
}
@Test
public void testConstructorNotPublicServer() {
List<String> nameServers = Collections.singletonList("ns1.example.com");
DnsServerVO vo = new DnsServerVO("srv", "http://url", null, null,
DnsProviderType.PowerDNS, null, "key",
false, null, nameServers, 1L, 1L);
assertFalse(vo.getPublicServer());
assertNull(vo.getPort());
}
}

View File

@ -0,0 +1,82 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.dns.vo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.lang.reflect.Field;
import org.apache.cloudstack.dns.DnsZone;
import org.junit.Test;
public class DnsZoneJoinVOTest {
private void setField(Object target, String fieldName, Object value) throws Exception {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(target, value);
}
@Test
public void testDefaultConstructor() {
DnsZoneJoinVO vo = new DnsZoneJoinVO();
assertEquals(0L, vo.getId());
assertNull(vo.getUuid());
assertNull(vo.getName());
assertNull(vo.getState());
assertNull(vo.getDescription());
}
@Test
public void testAllGetters() throws Exception {
DnsZoneJoinVO vo = new DnsZoneJoinVO();
setField(vo, "id", 1L);
setField(vo, "uuid", "zone-uuid");
setField(vo, "name", "example.com");
setField(vo, "state", DnsZone.State.Active);
setField(vo, "dnsServerUuid", "server-uuid");
setField(vo, "dnsServerName", "pdns-server");
setField(vo, "dnsServerAccountName", "server-owner");
setField(vo, "accountName", "admin");
setField(vo, "domainName", "ROOT");
setField(vo, "domainUuid", "domain-uuid");
setField(vo, "domainPath", "/ROOT");
setField(vo, "description", "Test zone");
assertEquals(1L, vo.getId());
assertEquals("zone-uuid", vo.getUuid());
assertEquals("example.com", vo.getName());
assertEquals(DnsZone.State.Active, vo.getState());
assertEquals("server-uuid", vo.getDnsServerUuid());
assertEquals("pdns-server", vo.getDnsServerName());
assertEquals("server-owner", vo.getDnsServerAccountName());
assertEquals("admin", vo.getAccountName());
assertEquals("ROOT", vo.getDomainName());
assertEquals("domain-uuid", vo.getDomainUuid());
assertEquals("/ROOT", vo.getDomainPath());
assertEquals("Test zone", vo.getDescription());
}
@Test
public void testStateInactive() throws Exception {
DnsZoneJoinVO vo = new DnsZoneJoinVO();
setField(vo, "state", DnsZone.State.Inactive);
assertEquals(DnsZone.State.Inactive, vo.getState());
}
}