forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2038f7
commit a8f8075
Showing
6 changed files
with
323 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
api/src/test/java/org/apache/cloudstack/api/response/VnfNicResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
api/src/test/java/org/apache/cloudstack/api/response/VnfTemplateResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
server/src/test/java/org/apache/cloudstack/storage/template/VnfTemplateManagerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> vnfNics = new HashMap<>(); | ||
final Map<String, Object> 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<Long> networkIds = Arrays.asList(200L, 201L); | ||
vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); | ||
} | ||
|
||
@Test | ||
public void testValidateVnfApplianceNicsWithAllNics() { | ||
List<Long> networkIds = Arrays.asList(200L, 201L, 202L); | ||
vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); | ||
} | ||
|
||
@Test(expected = InvalidParameterValueException.class) | ||
public void testValidateVnfApplianceNicsWithEmptyList() { | ||
List<Long> networkIds = new ArrayList<>(); | ||
vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); | ||
} | ||
|
||
@Test(expected = InvalidParameterValueException.class) | ||
public void testValidateVnfApplianceNicsWithMissingNetworkId() { | ||
List<Long> networkIds = Arrays.asList(200L); | ||
vnfTemplateManagerImpl.validateVnfApplianceNics(template, networkIds); | ||
} | ||
} |