Skip to content

Commit 9d4e141

Browse files
committed
unit tests
1 parent f524bbe commit 9d4e141

22 files changed

+2274
-16
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.assertNotNull;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.when;
23+
24+
import org.apache.cloudstack.api.ServerApiException;
25+
import org.apache.cloudstack.api.response.DnsZoneNetworkMapResponse;
26+
import org.junit.Test;
27+
28+
import com.cloud.network.Network;
29+
import com.cloud.user.Account;
30+
31+
public class AssociateDnsZoneToNetworkCmdTest extends BaseDnsCmdTest {
32+
33+
private static final long NETWORK_ID = 200L;
34+
35+
private AssociateDnsZoneToNetworkCmd createCmd() throws Exception {
36+
AssociateDnsZoneToNetworkCmd cmd = new AssociateDnsZoneToNetworkCmd();
37+
setField(cmd, "dnsProviderManager", dnsProviderManager);
38+
setField(cmd, "_entityMgr", entityManager);
39+
setField(cmd, "dnsZoneId", ENTITY_ID);
40+
setField(cmd, "networkId", NETWORK_ID);
41+
setField(cmd, "subDomain", "dev");
42+
return cmd;
43+
}
44+
45+
@Test
46+
public void testAccessors() throws Exception {
47+
AssociateDnsZoneToNetworkCmd cmd = createCmd();
48+
assertEquals(Long.valueOf(ENTITY_ID), cmd.getDnsZoneId());
49+
assertEquals(Long.valueOf(NETWORK_ID), cmd.getNetworkId());
50+
assertEquals("dev", cmd.getSubDomain());
51+
}
52+
53+
@Test
54+
public void testGetEntityOwnerIdWithNetwork() throws Exception {
55+
AssociateDnsZoneToNetworkCmd cmd = createCmd();
56+
Network mockNetwork = mock(Network.class);
57+
when(mockNetwork.getAccountId()).thenReturn(ACCOUNT_ID);
58+
when(entityManager.findById(Network.class, NETWORK_ID))
59+
.thenReturn(mockNetwork);
60+
61+
assertEquals(ACCOUNT_ID, cmd.getEntityOwnerId());
62+
}
63+
64+
@Test
65+
public void testGetEntityOwnerIdNetworkNotFound() throws Exception {
66+
AssociateDnsZoneToNetworkCmd cmd = createCmd();
67+
when(entityManager.findById(Network.class, NETWORK_ID))
68+
.thenReturn(null);
69+
70+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
71+
}
72+
73+
@Test
74+
public void testExecuteSuccess() throws Exception {
75+
AssociateDnsZoneToNetworkCmd cmd = createCmd();
76+
DnsZoneNetworkMapResponse mockResponse =
77+
new DnsZoneNetworkMapResponse();
78+
when(dnsProviderManager.associateZoneToNetwork(cmd))
79+
.thenReturn(mockResponse);
80+
81+
cmd.execute();
82+
83+
DnsZoneNetworkMapResponse response =
84+
(DnsZoneNetworkMapResponse) cmd.getResponseObject();
85+
assertNotNull(response);
86+
assertEquals("associatednszonetonetworkresponse",
87+
response.getResponseName());
88+
}
89+
90+
@Test(expected = ServerApiException.class)
91+
public void testExecuteThrowsException() throws Exception {
92+
AssociateDnsZoneToNetworkCmd cmd = createCmd();
93+
when(dnsProviderManager.associateZoneToNetwork(cmd))
94+
.thenThrow(new RuntimeException("Error"));
95+
cmd.execute();
96+
}
97+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
22+
import java.lang.reflect.Field;
23+
24+
import org.apache.cloudstack.context.CallContext;
25+
import org.apache.cloudstack.dns.DnsProviderManager;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.mockito.MockedStatic;
29+
import org.mockito.Mockito;
30+
31+
import com.cloud.user.Account;
32+
import com.cloud.utils.db.EntityManager;
33+
34+
/**
35+
* Shared setup for all DNS command unit tests.
36+
*/
37+
public abstract class BaseDnsCmdTest {
38+
39+
protected static final long ACCOUNT_ID = 42L;
40+
protected static final long ENTITY_ID = 100L;
41+
42+
protected DnsProviderManager dnsProviderManager;
43+
protected EntityManager entityManager;
44+
protected Account callingAccount;
45+
46+
private MockedStatic<CallContext> callContextMock;
47+
48+
@Before
49+
public void setUp() {
50+
dnsProviderManager = mock(DnsProviderManager.class);
51+
entityManager = mock(EntityManager.class);
52+
53+
callingAccount = mock(Account.class);
54+
when(callingAccount.getId()).thenReturn(ACCOUNT_ID);
55+
56+
CallContext callContext = mock(CallContext.class);
57+
when(callContext.getCallingAccount()).thenReturn(callingAccount);
58+
59+
callContextMock = Mockito.mockStatic(CallContext.class);
60+
callContextMock.when(CallContext::current).thenReturn(callContext);
61+
}
62+
63+
@After
64+
public void tearDown() {
65+
callContextMock.close();
66+
}
67+
68+
/**
69+
* Sets a private/inherited field value via reflection.
70+
*/
71+
protected void setField(Object target, String fieldName, Object value) throws Exception {
72+
Field field = null;
73+
Class<?> clazz = target.getClass();
74+
while (clazz != null) {
75+
try {
76+
field = clazz.getDeclaredField(fieldName);
77+
break;
78+
} catch (NoSuchFieldException e) {
79+
clazz = clazz.getSuperclass();
80+
}
81+
}
82+
if (field == null) {
83+
throw new NoSuchFieldException(fieldName + " not found in hierarchy of " + target.getClass().getName());
84+
}
85+
field.setAccessible(true);
86+
field.set(target, value);
87+
}
88+
}

0 commit comments

Comments
 (0)