Skip to content

Commit

Permalink
VNF: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Oct 3, 2023
1 parent b2038f7 commit a8f8075
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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());
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ public void testNewUpdateResponseForVnf() {
Assert.assertEquals(3, ((VnfTemplateResponse)response).getVnfDetails().size());

}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
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);
}
}

0 comments on commit a8f8075

Please sign in to comment.