Skip to content

Commit

Permalink
refactor MockNetworkVO
Browse files Browse the repository at this point in the history
  • Loading branch information
gzhao9 committed Oct 24, 2023
1 parent 543c54c commit 4cb6c4f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@ public void testCreateInstanceIp() throws IOException {
when(controller.getManager()).thenReturn(contrailMgr);

// Create Virtual-Network (VN)
NetworkVO network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(Network.State.Implemented);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
NetworkVO network = new MockNetworkVO(Network.State.Implemented).getNetwork();
NetworkDao networkDao = mock(NetworkDao.class);
when(networkDao.findById(anyLong())).thenReturn(network);
when(controller.getNetworkDao()).thenReturn(networkDao);
Expand Down
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.network.contrail.model;

import com.cloud.network.Network;
import com.cloud.network.dao.NetworkVO;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MockNetworkVO {

private NetworkVO network;
MockNetworkVO(Network.State state){
network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(state);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
}

public NetworkVO getNetwork() {
return network;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ public void testCreateVMInterface() throws IOException {
when(controller.getApiAccessor()).thenReturn(api);

// Create Virtual-Network (VN)
NetworkVO network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(Network.State.Implemented);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
NetworkVO network = new MockNetworkVO(Network.State.Implemented).getNetwork();
NetworkDao networkDao = mock(NetworkDao.class);
when(networkDao.findById(anyLong())).thenReturn(network);
when(controller.getNetworkDao()).thenReturn(networkDao);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,7 @@ public void testCreateVirtualMachine() throws IOException {
when(controller.getApiAccessor()).thenReturn(api);

// Create Virtual-Network (VN)
NetworkVO network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(Network.State.Allocated);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
NetworkVO network = new MockNetworkVO(Network.State.Allocated).getNetwork();

when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.UUID;

import com.cloud.network.Network;
import junit.framework.TestCase;
import net.juniper.contrail.api.ApiConnector;
import net.juniper.contrail.api.ApiConnectorMock;
Expand Down Expand Up @@ -115,34 +116,13 @@ public void setUp() throws IOException {
when(vn3.getNetworkPolicy()).thenReturn(policyRefs3);

//Virtual-Network 1
NetworkVO network1 = mock(NetworkVO.class);
when(network1.getName()).thenReturn("testnetwork");
when(network1.getState()).thenReturn(State.Allocated);
when(network1.getGateway()).thenReturn("10.1.1.1");
when(network1.getCidr()).thenReturn("10.1.1.0/24");
when(network1.getPhysicalNetworkId()).thenReturn(42L);
when(network1.getDomainId()).thenReturn(10L);
when(network1.getAccountId()).thenReturn(42L);
NetworkVO network1 = new MockNetworkVO(State.Allocated).getNetwork();

//Virtual-Network 2
NetworkVO network2 = mock(NetworkVO.class);
when(network2.getName()).thenReturn("Testnetwork");
when(network2.getState()).thenReturn(State.Allocated);
when(network2.getGateway()).thenReturn("10.1.1.1");
when(network2.getCidr()).thenReturn("10.1.1.0/24");
when(network2.getPhysicalNetworkId()).thenReturn(42L);
when(network2.getDomainId()).thenReturn(10L);
when(network2.getAccountId()).thenReturn(42L);
NetworkVO network2 = new MockNetworkVO(State.Allocated).getNetwork();

//Virtual-Network 3
NetworkVO network3 = mock(NetworkVO.class);
when(network3.getName()).thenReturn("Testnetwork");
when(network3.getState()).thenReturn(State.Allocated);
when(network3.getGateway()).thenReturn("10.1.1.1");
when(network3.getCidr()).thenReturn("10.1.1.0/24");
when(network3.getPhysicalNetworkId()).thenReturn(42L);
when(network3.getDomainId()).thenReturn(10L);
when(network3.getAccountId()).thenReturn(42L);
NetworkVO network3 = new MockNetworkVO(State.Allocated).getNetwork();

when(contrailMgr.getCanonicalName(network1)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network1.getDomainId(), network1.getAccountId())).thenReturn("testProjectId");
Expand Down Expand Up @@ -185,14 +165,7 @@ public void testCreateVirtualNetwork() throws IOException {
when(controller.getApiAccessor()).thenReturn(api);

// Create Virtual-Network (VN)
NetworkVO network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(State.Allocated);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
NetworkVO network = new MockNetworkVO(State.Allocated).getNetwork();

when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
Expand Down

0 comments on commit 4cb6c4f

Please sign in to comment.