|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package com.cloud.network; |
| 19 | + |
| 20 | +import static org.mockito.Matchers.any; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | +import static org.mockito.Matchers.*; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import junit.framework.Assert; |
| 29 | + |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import com.cloud.dc.VlanVO; |
| 34 | +import com.cloud.dc.dao.VlanDao; |
| 35 | +import com.cloud.network.dao.IPAddressDao; |
| 36 | +import com.cloud.user.Account; |
| 37 | +import com.cloud.utils.db.Filter; |
| 38 | +import com.cloud.utils.db.SearchBuilder; |
| 39 | +import com.cloud.utils.db.SearchCriteria; |
| 40 | +import com.cloud.utils.net.Ip; |
| 41 | + |
| 42 | +public class NetworkModelTest { |
| 43 | + @Before |
| 44 | + public void setUp() { |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetSourceNatIpAddressForGuestNetwork() { |
| 50 | + NetworkModelImpl modelImpl = new NetworkModelImpl(); |
| 51 | + IPAddressDao ipAddressDao = mock(IPAddressDao.class); |
| 52 | + modelImpl._ipAddressDao = ipAddressDao; |
| 53 | + List<IPAddressVO> fakeList = new ArrayList<IPAddressVO>(); |
| 54 | + IPAddressVO fakeIp = new IPAddressVO(new Ip("75.75.75.75"), 1, 0xaabbccddeeffL, 10, false); |
| 55 | + fakeList.add(fakeIp); |
| 56 | + SearchBuilder<IPAddressVO> fakeSearch = mock(SearchBuilder.class); |
| 57 | + modelImpl.IpAddressSearch = fakeSearch; |
| 58 | + VlanDao fakeVlanDao = mock(VlanDao.class); |
| 59 | + when (fakeVlanDao.findById(anyLong())).thenReturn(mock(VlanVO.class)); |
| 60 | + modelImpl._vlanDao = fakeVlanDao; |
| 61 | + when(fakeSearch.create()).thenReturn(mock(SearchCriteria.class)); |
| 62 | + when( |
| 63 | + ipAddressDao.search( |
| 64 | + any(SearchCriteria.class), |
| 65 | + (Filter)org.mockito.Matchers.isNull() |
| 66 | + ) |
| 67 | + ).thenReturn(fakeList); |
| 68 | + when ( |
| 69 | + ipAddressDao.findById(anyLong()) |
| 70 | + ).thenReturn(fakeIp); |
| 71 | + Account fakeAccount = mock(Account.class); |
| 72 | + when(fakeAccount.getId()).thenReturn(1L); |
| 73 | + Network fakeNetwork = mock(Network.class); |
| 74 | + when(fakeNetwork.getId()).thenReturn(1L); |
| 75 | + PublicIpAddress answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork); |
| 76 | + Assert.assertNull(answer); |
| 77 | + IPAddressVO fakeIp2 = new IPAddressVO(new Ip("76.75.75.75"), 1, 0xaabb10ddeeffL, 10, true); |
| 78 | + fakeList.add(fakeIp2); |
| 79 | + when ( |
| 80 | + ipAddressDao.findById(anyLong()) |
| 81 | + ).thenReturn(fakeIp2); |
| 82 | + answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork); |
| 83 | + Assert.assertNotNull(answer); |
| 84 | + Assert.assertEquals(answer.getAddress().addr(), "76.75.75.75"); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments