Use DataTreeChangeListener instead of DataChangeListener
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / OvsdbDataTreeChangeListenerTest.java
1 /*
2  * Copyright © 2016 Red Hat, Inc. and others.
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 package org.opendaylight.ovsdb.southbound;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.concurrent.ExecutionException;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.ovsdb.lib.OvsdbConnection;
23 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
24 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes
29         .ConnectionInfoBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology
31         .Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33
34 /**
35  * Unit tests for the data-tree change listener.
36  */
37 public class OvsdbDataTreeChangeListenerTest extends AbstractDataBrokerTest {
38     private final OvsdbConnection ovsdbConnection = Mockito.mock(OvsdbConnection.class);
39     private DataBroker dataBroker;
40     private OvsdbDataTreeChangeListener listener;
41
42     @Before
43     public void setupListener() {
44         dataBroker = getDataBroker();
45         EntityOwnershipService entityOwnershipService = Mockito.mock(EntityOwnershipService.class);
46         listener = new OvsdbDataTreeChangeListener(dataBroker,
47                 new OvsdbConnectionManager(dataBroker, new TransactionInvokerImpl(dataBroker), entityOwnershipService,
48                         ovsdbConnection));
49     }
50
51     @Test
52     public void testConnect() throws UnknownHostException, InterruptedException, ExecutionException {
53         // Given ...
54
55         // When we request a connection ...
56         InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
57         int port = 6640;
58         IpAddress ipAddress = SouthboundMapper.createIpAddress(inetAddress);
59         PortNumber portNumber = new PortNumber(port);
60
61         final ConnectionInfo connectionInfo = new ConnectionInfoBuilder()
62                 .setRemoteIp(ipAddress)
63                 .setRemotePort(portNumber)
64                 .build();
65         final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
66         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
67         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, SouthboundUtils.createNode(connectionInfo),
68                 WriteTransaction.CREATE_MISSING_PARENTS);
69         transaction.submit().get();
70
71         // Then the listener tries to open a connection
72         Mockito.verify(ovsdbConnection).connect(inetAddress, port);
73     }
74 }