|
| 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 org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.mockito.InjectMocks; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.MockitoAnnotations; |
| 26 | + |
| 27 | +import com.cloud.network.dao.IPAddressDao; |
| 28 | +import com.cloud.network.dao.IPAddressVO; |
| 29 | +import com.cloud.network.rules.StaticNat; |
| 30 | +import com.cloud.network.rules.StaticNatImpl; |
| 31 | +import com.cloud.utils.net.Ip; |
| 32 | + |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +import static org.mockito.Mockito.anyLong; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | + |
| 41 | +public class IpAddressManagerTest { |
| 42 | + |
| 43 | + @Mock |
| 44 | + IPAddressDao _ipAddrDao; |
| 45 | + |
| 46 | + @InjectMocks |
| 47 | + IpAddressManagerImpl _ipManager; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setup() { |
| 51 | + MockitoAnnotations.initMocks(this); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testGetStaticNatSourceIps() { |
| 56 | + String publicIpAddress = "192.168.1.3"; |
| 57 | + IPAddressVO vo = mock(IPAddressVO.class); |
| 58 | + when(vo.getAddress()).thenReturn(new Ip(publicIpAddress)); |
| 59 | + when(vo.getId()).thenReturn(1l); |
| 60 | + |
| 61 | + when(_ipAddrDao.findById(anyLong())).thenReturn(vo); |
| 62 | + StaticNat snat = new StaticNatImpl(1, 1, 1, 1, publicIpAddress, false); |
| 63 | + |
| 64 | + List<IPAddressVO> ips = _ipManager.getStaticNatSourceIps(Collections.singletonList(snat)); |
| 65 | + Assert.assertNotNull(ips); |
| 66 | + Assert.assertEquals(1, ips.size()); |
| 67 | + |
| 68 | + IPAddressVO returnedVO = ips.get(0); |
| 69 | + Assert.assertEquals(vo, returnedVO); |
| 70 | + } |
| 71 | +} |
0 commit comments