From ae2177abc4fc9d9aa0f71dfe9b949194520b80e9 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Tue, 3 Oct 2023 10:40:11 +0200 Subject: [PATCH] VNF: add more unit tests --- .../api/response/VnfNicResponse.java | 28 +++ .../api/response/VnfNicResponseTest.java | 51 +++++ .../api/response/VnfTemplateResponseTest.java | 44 +++++ .../query/dao/TemplateJoinDaoImplTest.java | 2 +- .../template/TemplateManagerImplTest.java | 22 +++ .../template/VnfTemplateManagerImplTest.java | 176 ++++++++++++++++++ 6 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/org/apache/cloudstack/api/response/VnfNicResponseTest.java create mode 100644 api/src/test/java/org/apache/cloudstack/api/response/VnfTemplateResponseTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/template/VnfTemplateManagerImplTest.java diff --git a/api/src/main/java/org/apache/cloudstack/api/response/VnfNicResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/VnfNicResponse.java index 82fce602cacf..51ebae9aa245 100644 --- a/api/src/main/java/org/apache/cloudstack/api/response/VnfNicResponse.java +++ b/api/src/main/java/org/apache/cloudstack/api/response/VnfNicResponse.java @@ -88,4 +88,32 @@ public VnfNicResponse(long deviceId, String name, Boolean required, Boolean mana this.management = management; this.description = description; } + + public long getDeviceId() { + return deviceId; + } + + public String getName() { + return name; + } + + public Boolean getRequired() { + return required; + } + + public Boolean getManagement() { + return management; + } + + public String getDescription() { + return description; + } + + public String getNetworkId() { + return networkId; + } + + public String getNetworkName() { + return networkName; + } } diff --git a/api/src/test/java/org/apache/cloudstack/api/response/VnfNicResponseTest.java b/api/src/test/java/org/apache/cloudstack/api/response/VnfNicResponseTest.java new file mode 100644 index 000000000000..e43dee121fc4 --- /dev/null +++ b/api/src/test/java/org/apache/cloudstack/api/response/VnfNicResponseTest.java @@ -0,0 +1,51 @@ +// 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.response; + +import org.junit.Assert; +import org.junit.Test; + +public final class VnfNicResponseTest { + + static long deviceId = 0L; + static String deviceName = "eth0"; + static boolean required = true; + static boolean management = false; + static String description = "description of vnf nic"; + + static String networkUuid = "networkuuid"; + static String networkName = "networkname"; + + @Test + public void testNewVnfNicResponse() { + final VnfNicResponse response = new VnfNicResponse(deviceId, deviceName, required, management, description); + Assert.assertEquals(deviceId, response.getDeviceId()); + Assert.assertEquals(deviceName, response.getName()); + Assert.assertEquals(required, response.getRequired()); + Assert.assertEquals(management, response.getManagement()); + Assert.assertEquals(description, response.getDescription()); + } + + @Test + public void testSetVnfNicResponse() { + final VnfNicResponse response = new VnfNicResponse(); + response.setNetworkId(networkUuid); + response.setNetworkName(networkName); + Assert.assertEquals(networkUuid, response.getNetworkId()); + Assert.assertEquals(networkName, response.getNetworkName()); + } +} diff --git a/api/src/test/java/org/apache/cloudstack/api/response/VnfTemplateResponseTest.java b/api/src/test/java/org/apache/cloudstack/api/response/VnfTemplateResponseTest.java new file mode 100644 index 000000000000..c8ceb7597ff9 --- /dev/null +++ b/api/src/test/java/org/apache/cloudstack/api/response/VnfTemplateResponseTest.java @@ -0,0 +1,44 @@ +// 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.response; + +import org.junit.Assert; +import org.junit.Test; + +public final class VnfTemplateResponseTest { + + @Test + public void testAddVnfNicToResponse() { + final VnfTemplateResponse response = new VnfTemplateResponse(); + + response.addVnfNic(new VnfNicResponse()); + response.addVnfNic(new VnfNicResponse()); + + Assert.assertEquals(2, response.getVnfNics().size()); + } + + @Test + public void testAddVnfDetailToResponse() { + final VnfTemplateResponse response = new VnfTemplateResponse(); + + response.addVnfDetail("key1", "value1"); + response.addVnfDetail("key2", "value2"); + response.addVnfDetail("key3", "value3"); + + Assert.assertEquals(3, response.getVnfDetails().size()); + } +} diff --git a/server/src/test/java/com/cloud/api/query/dao/TemplateJoinDaoImplTest.java b/server/src/test/java/com/cloud/api/query/dao/TemplateJoinDaoImplTest.java index b66c57fbef7d..94d6722a7c5d 100755 --- a/server/src/test/java/com/cloud/api/query/dao/TemplateJoinDaoImplTest.java +++ b/server/src/test/java/com/cloud/api/query/dao/TemplateJoinDaoImplTest.java @@ -150,4 +150,4 @@ public void testNewUpdateResponseForVnf() { Assert.assertEquals(3, ((VnfTemplateResponse)response).getVnfDetails().size()); } -} \ No newline at end of file +} diff --git a/server/src/test/java/com/cloud/template/TemplateManagerImplTest.java b/server/src/test/java/com/cloud/template/TemplateManagerImplTest.java index 6740fc25b8a2..519771e67590 100755 --- a/server/src/test/java/com/cloud/template/TemplateManagerImplTest.java +++ b/server/src/test/java/com/cloud/template/TemplateManagerImplTest.java @@ -645,6 +645,28 @@ public void testUpdateTemplateWithoutTemplateType() { Assert.assertNull(type); } + @Test(expected = InvalidParameterValueException.class) + public void testUpdateTemplateWithInvalidTemplateType() { + UpdateTemplateCmd cmd = Mockito.mock(UpdateTemplateCmd.class); + when(cmd.getTemplateType()).thenReturn("invalidtype"); + Storage.TemplateType type = templateManager.validateTemplateType(cmd, true, true); + } + + @Test(expected = InvalidParameterValueException.class) + public void testUpdateTemplateWithInvalidTemplateTypeForRouting() { + UpdateTemplateCmd cmd = Mockito.mock(UpdateTemplateCmd.class); + when(cmd.getTemplateType()).thenReturn(Storage.TemplateType.USER.toString()); + when(cmd.isRoutingType()).thenReturn(true); + Storage.TemplateType type = templateManager.validateTemplateType(cmd, true, true); + } + + @Test(expected = InvalidParameterValueException.class) + public void testUpdateTemplateWithInvalidCrossZonesForSystem() { + UpdateTemplateCmd cmd = Mockito.mock(UpdateTemplateCmd.class); + when(cmd.getTemplateType()).thenReturn(Storage.TemplateType.SYSTEM.toString()); + Storage.TemplateType type = templateManager.validateTemplateType(cmd, true, false); + } + @Test(expected = InvalidParameterValueException.class) public void testUpdateTemplateWithSystemTemplateTypeByUser() { UpdateVnfTemplateCmd cmd = Mockito.mock(UpdateVnfTemplateCmd.class); diff --git a/server/src/test/java/org/apache/cloudstack/storage/template/VnfTemplateManagerImplTest.java b/server/src/test/java/org/apache/cloudstack/storage/template/VnfTemplateManagerImplTest.java new file mode 100644 index 000000000000..d2f489843c96 --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/template/VnfTemplateManagerImplTest.java @@ -0,0 +1,176 @@ +// 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.storage.template; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.storage.VnfTemplateNicVO; +import com.cloud.storage.dao.VnfTemplateDetailsDao; +import com.cloud.storage.dao.VnfTemplateNicDao; +import com.cloud.template.VirtualMachineTemplate; +import org.apache.cloudstack.api.command.user.template.RegisterVnfTemplateCmd; +import org.apache.cloudstack.api.command.user.template.UpdateVnfTemplateCmd; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; + +@RunWith(MockitoJUnitRunner.class) +public class VnfTemplateManagerImplTest { + + @Spy + @InjectMocks + VnfTemplateManagerImpl vnfTemplateManagerImpl; + + @Mock + VnfTemplateDetailsDao vnfTemplateDetailsDao; + @Mock + VnfTemplateNicDao vnfTemplateNicDao; + + @Mock + VirtualMachineTemplate template; + + static long templateId = 100L; + final Map vnfNics = new HashMap<>(); + final Map vnfDetails = new HashMap<>(); + + @Before + public void setUp() { + vnfNics.put("0", new HashMap<>(Map.ofEntries( + Map.entry("deviceid", "1"), + Map.entry("name", "eth1"), + Map.entry("required", "true"), + Map.entry("description", "The second NIC of VNF appliance") + ))); + vnfNics.put("1", new HashMap<>(Map.ofEntries( + Map.entry("deviceid", "2"), + Map.entry("name", "eth2"), + Map.entry("required", "false"), + Map.entry("description", "The third NIC of VNF appliance") + ))); + vnfNics.put("2", new HashMap<>(Map.ofEntries( + Map.entry("deviceid", "0"), + Map.entry("name", "eth0"), + Map.entry("description", "The first NIC of VNF appliance") + ))); + + vnfDetails.put("0", new HashMap<>(Map.ofEntries( + Map.entry("accessMethods", "console,http,https"), + Map.entry("username", "admin"), + Map.entry("password", "password"), + Map.entry("version", "4.19.0"), + Map.entry("vendor", "cloudstack") + ))); + + VnfTemplateNicVO vnfNic1 = new VnfTemplateNicVO(templateId, 0L, "eth0", true, true, "first"); + VnfTemplateNicVO vnfNic2 = new VnfTemplateNicVO(templateId, 1L, "eth1", true, true, "second"); + VnfTemplateNicVO vnfNic3 = new VnfTemplateNicVO(templateId, 2L, "eth2", false, true, "second"); + Mockito.doReturn(Arrays.asList(vnfNic1, vnfNic2, vnfNic3)).when(vnfTemplateNicDao).listByTemplateId(templateId); + + Mockito.when(template.getId()).thenReturn(templateId); + } + + @Test + public void testPersistVnfTemplateRegister() { + RegisterVnfTemplateCmd cmd = new RegisterVnfTemplateCmd(); + ReflectionTestUtils.setField(cmd,"vnfNics", vnfNics); + ReflectionTestUtils.setField(cmd,"vnfDetails", vnfDetails); + + vnfTemplateManagerImpl.persistVnfTemplate(templateId, cmd); + + Mockito.verify(vnfTemplateNicDao, Mockito.times(vnfNics.size())).persist(any(VnfTemplateNicVO.class)); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(0)).removeDetails(templateId); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(5)).addDetail(eq(templateId), anyString(), anyString(), eq(true)); + } + + @Test + public void testPersistVnfTemplateUpdate() { + UpdateVnfTemplateCmd cmd = new UpdateVnfTemplateCmd(); + ReflectionTestUtils.setField(cmd,"vnfNics", vnfNics); + ReflectionTestUtils.setField(cmd,"vnfDetails", vnfDetails); + + vnfTemplateManagerImpl.updateVnfTemplate(templateId, cmd); + + Mockito.verify(vnfTemplateNicDao, Mockito.times(vnfNics.size())).persist(any(VnfTemplateNicVO.class)); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(1)).removeDetails(templateId); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(5)).addDetail(eq(templateId), anyString(), anyString(), eq(true)); + } + + @Test + public void testPersistVnfTemplateUpdateWithoutNics() { + UpdateVnfTemplateCmd cmd = new UpdateVnfTemplateCmd(); + ReflectionTestUtils.setField(cmd,"vnfDetails", vnfDetails); + + vnfTemplateManagerImpl.updateVnfTemplate(templateId, cmd); + + Mockito.verify(vnfTemplateNicDao, Mockito.times(0)).persist(any(VnfTemplateNicVO.class)); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(1)).removeDetails(templateId); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(5)).addDetail(eq(templateId), anyString(), anyString(), eq(true)); + } + + @Test + public void testPersistVnfTemplateUpdateWithoutDetails() { + UpdateVnfTemplateCmd cmd = new UpdateVnfTemplateCmd(); + ReflectionTestUtils.setField(cmd,"vnfNics", vnfNics); + ReflectionTestUtils.setField(cmd,"cleanupVnfDetails", true); + + vnfTemplateManagerImpl.updateVnfTemplate(templateId, cmd); + + Mockito.verify(vnfTemplateNicDao, Mockito.times(vnfNics.size())).persist(any(VnfTemplateNicVO.class)); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(1)).removeDetails(templateId); + Mockito.verify(vnfTemplateDetailsDao, Mockito.times(0)).addDetail(eq(templateId), anyString(), anyString(), eq(true)); + } + + @Test + public void testValidateVnfApplianceNicsWithRequiredNics() { + List networkIds = Arrays.asList(200L, 201L); + vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); + } + + @Test + public void testValidateVnfApplianceNicsWithAllNics() { + List networkIds = Arrays.asList(200L, 201L, 202L); + vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); + } + + @Test(expected = InvalidParameterValueException.class) + public void testValidateVnfApplianceNicsWithEmptyList() { + List networkIds = new ArrayList<>(); + vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); + } + + @Test(expected = InvalidParameterValueException.class) + public void testValidateVnfApplianceNicsWithMissingNetworkId() { + List networkIds = Arrays.asList(200L); + vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); + } +}