Tests for neutron-ovsdb
[groupbasedpolicy.git] / neutron-ovsdb / src / test / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / util / NeutronHelperTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.neutron.ovsdb.util;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.UniqueId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.endpoints.by.ports.EndpointByPort;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 public class NeutronHelperTest {
32
33     private UniqueId externalId;
34     private DataBroker dataBroker;
35     private Optional<EndpointByPort> optionalEp;
36
37     @SuppressWarnings("unchecked")
38     @Before
39     public void init() throws Exception {
40         externalId = mock(UniqueId.class);
41         dataBroker = mock(DataBroker.class);
42
43         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
44         when(dataBroker.newReadWriteTransaction()).thenReturn(transaction);
45         CheckedFuture<Optional<EndpointByPort>, ReadFailedException> resultFuture = mock(CheckedFuture.class);
46         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(resultFuture);
47         optionalEp = mock(Optional.class);
48         when(resultFuture.checkedGet()).thenReturn(optionalEp);
49     }
50
51     @Test
52     public void testGetEpKeyFromNeutronMapper() throws Exception {
53         when(optionalEp.isPresent()).thenReturn(true);
54         EndpointByPort epByPort = mock(EndpointByPort.class);
55         when(optionalEp.get()).thenReturn(epByPort);
56         when(epByPort.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
57         when(epByPort.getMacAddress()).thenReturn(mock(MacAddress.class));
58
59         assertNotNull(NeutronHelper.getEpKeyFromNeutronMapper(externalId, dataBroker));
60     }
61
62     @Test
63     public void testGetEpKeyFromNeutronMapper_PresentFalse() throws Exception {
64         when(optionalEp.isPresent()).thenReturn(false);
65
66         assertNull(NeutronHelper.getEpKeyFromNeutronMapper(externalId, dataBroker));
67     }
68 }