Skip to content

Commit

Permalink
test: add test for Ipv4SubnetForGuestNetworkCmd and BgpPeerCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Aug 28, 2024
1 parent b53fed8 commit 56a9bd7
Show file tree
Hide file tree
Showing 21 changed files with 830 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public String getEventType() {

@Override
public String getEventDescription() {
return "Dedicating guest IPv4 subnet " + getId();
return "Dedicating zone IPv4 subnet " + getId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public String getEventType() {

@Override
public String getEventDescription() {
return "Deleting guest IPv4 subnet " + getId();
return "Deleting zone IPv4 subnet " + getId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public String getEventType() {

@Override
public String getEventDescription() {
return "Releasing a dedicated guest IPv4 subnet " + getId();
return "Releasing a dedicated zone IPv4 subnet " + getId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String getEventType() {

@Override
public String getEventDescription() {
return "Updating guest IPv4 subnet " + getId();
return "Updating zone IPv4 subnet " + getId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public interface RoutedIpv4Manager extends PluggableService, Configurable {

void releaseIpv4SubnetForVpc(long vpcId);

List<? extends Ipv4GuestSubnetNetworkMap> listIpv4GuestSubnetsForGuestNetwork(ListIpv4SubnetsForGuestNetworkCmd listIpv4SubnetsForGuestNetworkCmd);
List<Ipv4GuestSubnetNetworkMap> listIpv4GuestSubnetsForGuestNetwork(ListIpv4SubnetsForGuestNetworkCmd listIpv4SubnetsForGuestNetworkCmd);

Ipv4SubnetForGuestNetworkResponse createIpv4SubnetForGuestNetworkResponse(Ipv4GuestSubnetNetworkMap subnet);

Expand Down Expand Up @@ -165,7 +165,7 @@ public interface RoutedIpv4Manager extends PluggableService, Configurable {

BgpPeer releaseDedicatedBgpPeer(ReleaseDedicatedBgpPeerCmd releaseDedicatedBgpPeerCmd);

List<? extends BgpPeer> listBgpPeers(ListBgpPeersCmd listBgpPeersCmd);
List<BgpPeer> listBgpPeers(ListBgpPeersCmd listBgpPeersCmd);

Network changeBgpPeersForNetwork(ChangeBgpPeersForNetworkCmd changeBgpPeersForNetworkCmd);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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.admin.network;

import com.cloud.event.EventTypes;

import org.apache.cloudstack.api.response.Ipv4SubnetForGuestNetworkResponse;
import org.apache.cloudstack.network.Ipv4GuestSubnetNetworkMap;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public class CreateIpv4SubnetForGuestNetworkCmdTest {

RoutedIpv4Manager routedIpv4Manager = Mockito.spy(RoutedIpv4Manager.class);

@Test
public void testCreateIpv4SubnetForGuestNetworkCmd() {
Long parentId = 1L;
String subnet = "192.168.1.0/24";
Integer cidrSize = 26;

CreateIpv4SubnetForGuestNetworkCmd cmd = new CreateIpv4SubnetForGuestNetworkCmd();
ReflectionTestUtils.setField(cmd, "parentId", parentId);
ReflectionTestUtils.setField(cmd, "subnet", subnet);
ReflectionTestUtils.setField(cmd, "cidrSize", cidrSize);
ReflectionTestUtils.setField(cmd,"routedIpv4Manager", routedIpv4Manager);

Assert.assertEquals(parentId, cmd.getParentId());
Assert.assertEquals(subnet, cmd.getSubnet());
Assert.assertEquals(cidrSize, cmd.getCidrSize());
Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_IP4_GUEST_SUBNET_CREATE, cmd.getEventType());
Assert.assertEquals(String.format("Creating guest IPv4 subnet %s in zone subnet=%s", subnet, parentId), cmd.getEventDescription());

Ipv4GuestSubnetNetworkMap ipv4GuestSubnetNetworkMap = Mockito.mock(Ipv4GuestSubnetNetworkMap.class);
Mockito.when(routedIpv4Manager.createIpv4SubnetForGuestNetwork(cmd)).thenReturn(ipv4GuestSubnetNetworkMap);

Ipv4SubnetForGuestNetworkResponse response = Mockito.mock(Ipv4SubnetForGuestNetworkResponse.class);
Mockito.when(routedIpv4Manager.createIpv4SubnetForGuestNetworkResponse(ipv4GuestSubnetNetworkMap)).thenReturn(response);

try {
cmd.execute();
} catch (Exception ignored) {
}

Assert.assertEquals(response, cmd.getResponseObject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testDedicateIpv4SubnetForZoneCmd() {

Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_ZONE_IP4_SUBNET_DEDICATE, cmd.getEventType());
Assert.assertEquals(String.format("Dedicating guest IPv4 subnet %s", id), cmd.getEventDescription());
Assert.assertEquals(String.format("Dedicating zone IPv4 subnet %s", id), cmd.getEventDescription());

DataCenterIpv4GuestSubnet zoneSubnet = Mockito.mock(DataCenterIpv4GuestSubnet.class);
Mockito.when(routedIpv4Manager.dedicateDataCenterIpv4GuestSubnet(cmd)).thenReturn(zoneSubnet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.admin.network;

import com.cloud.event.EventTypes;

import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public class DeleteIpv4SubnetForGuestNetworkCmdTest {

RoutedIpv4Manager routedIpv4Manager = Mockito.spy(RoutedIpv4Manager.class);

@Test
public void testDeleteIpv4SubnetForGuestNetworkCmd() {
Long id = 1L;

DeleteIpv4SubnetForGuestNetworkCmd cmd = new DeleteIpv4SubnetForGuestNetworkCmd();
ReflectionTestUtils.setField(cmd, "id", id);
ReflectionTestUtils.setField(cmd,"routedIpv4Manager", routedIpv4Manager);

Assert.assertEquals(id, cmd.getId());
Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_IP4_GUEST_SUBNET_DELETE, cmd.getEventType());
Assert.assertEquals(String.format("Deleting guest IPv4 subnet %s", id), cmd.getEventDescription());

Mockito.when(routedIpv4Manager.deleteIpv4SubnetForGuestNetwork(cmd)).thenReturn(true);

try {
cmd.execute();
} catch (Exception ignored) {
}

Assert.assertTrue(cmd.getResponseObject() instanceof SuccessResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.cloud.event.EventTypes;

import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.datacenter.DataCenterIpv4GuestSubnet;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -45,9 +44,8 @@ public void testDeleteIpv4SubnetForZoneCmd() {
Assert.assertEquals(id, cmd.getId());
Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_ZONE_IP4_SUBNET_DELETE, cmd.getEventType());
Assert.assertEquals(String.format("Deleting guest IPv4 subnet %s", id), cmd.getEventDescription());
Assert.assertEquals(String.format("Deleting zone IPv4 subnet %s", id), cmd.getEventDescription());

DataCenterIpv4GuestSubnet zoneSubnet = Mockito.mock(DataCenterIpv4GuestSubnet.class);
Mockito.when(routedIpv4Manager.deleteDataCenterIpv4GuestSubnet(cmd)).thenReturn(true);

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.admin.network;

import org.apache.cloudstack.api.response.Ipv4SubnetForGuestNetworkResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.network.Ipv4GuestSubnetNetworkMap;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Arrays;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class ListIpv4SubnetsForGuestNetworkCmdTest {

RoutedIpv4Manager routedIpv4Manager = Mockito.spy(RoutedIpv4Manager.class);

@Test
public void testListIpv4SubnetsForGuestNetworkCmd() {
Long id = 1L;
Long zoneId = 2L;
Long parentId = 2L;
String subnet = "192.168.1.0/24";
Long networkId = 10L;
Long vpcId = 11L;

ListIpv4SubnetsForGuestNetworkCmd cmd = new ListIpv4SubnetsForGuestNetworkCmd();
ReflectionTestUtils.setField(cmd, "id", id);
ReflectionTestUtils.setField(cmd, "zoneId", zoneId);
ReflectionTestUtils.setField(cmd, "subnet", subnet);
ReflectionTestUtils.setField(cmd, "parentId", parentId);
ReflectionTestUtils.setField(cmd,"networkId", networkId);
ReflectionTestUtils.setField(cmd,"vpcId", vpcId);
ReflectionTestUtils.setField(cmd,"routedIpv4Manager", routedIpv4Manager);

Assert.assertEquals(id, cmd.getId());
Assert.assertEquals(zoneId, cmd.getZoneId());
Assert.assertEquals(subnet, cmd.getSubnet());
Assert.assertEquals(networkId, cmd.getNetworkId());
Assert.assertEquals(vpcId, cmd.getVpcId());
Assert.assertEquals(parentId, cmd.getParentId());

Assert.assertEquals(0L, cmd.getEntityOwnerId());

Ipv4GuestSubnetNetworkMap ipv4GuestSubnetNetworkMap = Mockito.mock(Ipv4GuestSubnetNetworkMap.class);
List<Ipv4GuestSubnetNetworkMap> ipv4GuestSubnetNetworkMaps = Arrays.asList(ipv4GuestSubnetNetworkMap);
Mockito.when(routedIpv4Manager.listIpv4GuestSubnetsForGuestNetwork(cmd)).thenReturn(ipv4GuestSubnetNetworkMaps);

Ipv4SubnetForGuestNetworkResponse response = Mockito.mock(Ipv4SubnetForGuestNetworkResponse.class);
Mockito.when(routedIpv4Manager.createIpv4SubnetForGuestNetworkResponse(ipv4GuestSubnetNetworkMap)).thenReturn(response);

try {
cmd.execute();
} catch (Exception ignored) {
}

Assert.assertTrue(cmd.getResponseObject() instanceof ListResponse);
ListResponse listResponse = (ListResponse) cmd.getResponseObject();
Assert.assertEquals(1, (int) listResponse.getCount());
Assert.assertEquals(response, listResponse.getResponses().get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testReleaseDedicatedIpv4SubnetForZoneCmd() {
Assert.assertEquals(id, cmd.getId());
Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_ZONE_IP4_SUBNET_RELEASE, cmd.getEventType());
Assert.assertEquals(String.format("Releasing a dedicated guest IPv4 subnet %s", id), cmd.getEventDescription());
Assert.assertEquals(String.format("Releasing a dedicated zone IPv4 subnet %s", id), cmd.getEventDescription());

DataCenterIpv4GuestSubnet zoneSubnet = Mockito.mock(DataCenterIpv4GuestSubnet.class);
Mockito.when(routedIpv4Manager.releaseDedicatedDataCenterIpv4GuestSubnet(cmd)).thenReturn(zoneSubnet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testUpdateIpv4SubnetForZoneCmd() {
Assert.assertEquals(subnet, cmd.getSubnet());
Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_ZONE_IP4_SUBNET_UPDATE, cmd.getEventType());
Assert.assertEquals(String.format("Updating guest IPv4 subnet %s", id), cmd.getEventDescription());
Assert.assertEquals(String.format("Updating zone IPv4 subnet %s", id), cmd.getEventDescription());

DataCenterIpv4GuestSubnet zoneSubnet = Mockito.mock(DataCenterIpv4GuestSubnet.class);
Mockito.when(routedIpv4Manager.updateDataCenterIpv4GuestSubnet(cmd)).thenReturn(zoneSubnet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.admin.network.bgp;

import com.cloud.event.EventTypes;

import com.cloud.network.Network;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.response.NetworkResponse;
import org.apache.cloudstack.network.RoutedIpv4Manager;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Arrays;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class ChangeBgpPeersForNetworkCmdTest {

RoutedIpv4Manager routedIpv4Manager = Mockito.spy(RoutedIpv4Manager.class);

ResponseGenerator _responseGenerator = Mockito.spy(ResponseGenerator.class);

@Test
public void testChangeBgpPeersForNetworkCmd() {
Long networkId = 10L;
List<Long> bgpPeerIds = Arrays.asList(20L, 21L);

ChangeBgpPeersForNetworkCmd cmd = new ChangeBgpPeersForNetworkCmd();
ReflectionTestUtils.setField(cmd, "networkId", networkId);
ReflectionTestUtils.setField(cmd, "bgpPeerIds", bgpPeerIds);
ReflectionTestUtils.setField(cmd,"routedIpv4Manager", routedIpv4Manager);
ReflectionTestUtils.setField(cmd,"_responseGenerator", _responseGenerator);

Assert.assertEquals(networkId, cmd.getNetworkId());
Assert.assertEquals(bgpPeerIds, cmd.getBgpPeerIds());
Assert.assertEquals(1L, cmd.getEntityOwnerId());
Assert.assertEquals(EventTypes.EVENT_NETWORK_BGP_PEER_UPDATE, cmd.getEventType());
Assert.assertEquals(String.format("Changing Bgp Peers for network %s", networkId), cmd.getEventDescription());

Network network = Mockito.mock(Network.class);
Mockito.when(routedIpv4Manager.changeBgpPeersForNetwork(cmd)).thenReturn(network);

NetworkResponse response = Mockito.mock(NetworkResponse.class);
Mockito.when(_responseGenerator.createNetworkResponse(ResponseObject.ResponseView.Full, network)).thenReturn(response);

try {
cmd.execute();
} catch (Exception ignored) {
}

Assert.assertEquals(response, cmd.getResponseObject());
}
}
Loading

0 comments on commit 56a9bd7

Please sign in to comment.