JUnit Test - Neutron plugin tests 18/42318/6
authorMiroslav Toth <mirtoth@cisco.com>
Fri, 22 Jul 2016 12:54:15 +0000 (14:54 +0200)
committerMiroslav Toth <mirtoth@cisco.com>
Thu, 28 Jul 2016 15:37:38 +0000 (17:37 +0200)
Change-Id: If11132e6df768eef5b58d18dbe7747c0efe7397f
Signed-off-by: Miroslav Toth <mirtoth@cisco.com>
mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/DelegatingDataTreeListenerTest.java [new file with mode: 0644]
mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/LispNeutronServiceTest.java [new file with mode: 0644]
mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/LispUtilTest.java [new file with mode: 0644]
mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/ListenerTest.java [new file with mode: 0644]
mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/PortDataProcessorTest.java [new file with mode: 0644]
mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/SubnetDataProcessorTest.java [new file with mode: 0644]

diff --git a/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/DelegatingDataTreeListenerTest.java b/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/DelegatingDataTreeListenerTest.java
new file mode 100644 (file)
index 0000000..cfc6804
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.neutron;
+
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.collect.Lists;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.Networks;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+@SuppressWarnings("unchecked")
+public class DelegatingDataTreeListenerTest {
+    private static final DataProcessor<Network> DATA_PROCESSOR_NETWORK_MOCK = Mockito.mock(DataProcessor.class);
+
+    private static ILispNeutronService iLispNeutronServiceMock = Mockito.mock(ILispNeutronService.class);
+    private static DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
+    private static final DataTreeIdentifier<Network> NETWORK_ID =
+            new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
+                    .child(Networks.class).child(Network.class));
+
+    private final DelegatingDataTreeListener<Network> networkDelegatingDataTreeListener =
+            new DelegatingDataTreeListener<>(DATA_PROCESSOR_NETWORK_MOCK, dataBrokerMock, NETWORK_ID);
+
+    private static final Network DUMMY_NETWORK = Mockito.mock(Network.class);
+
+    private static final DataTreeModification<Network> DATA_TREE_MODIFICATION =
+            Mockito.mock(DataTreeModification.class);
+    private static final DataObjectModification<Network> DATA_OBJECT_MODIFICATION =
+            Mockito.mock(DataObjectModification.class);
+
+    /**
+     * Tests {@link DelegatingDataTreeListener#initiateListener} method.
+     */
+    @Test
+    public void initiateListenerTest() {
+        assertTrue(DelegatingDataTreeListener
+                .initiateListener(Network.class, iLispNeutronServiceMock, dataBrokerMock) instanceof NetworkListener);
+        assertTrue(DelegatingDataTreeListener
+                .initiateListener(Port.class, iLispNeutronServiceMock, dataBrokerMock) instanceof PortListener);
+        assertTrue(DelegatingDataTreeListener
+                .initiateListener(Subnet.class, iLispNeutronServiceMock, dataBrokerMock) instanceof SubnetListener);
+    }
+
+    /**
+     * Tests {@link DelegatingDataTreeListener#onDataTreeChanged} method with WRITE modification type.
+     */
+    @Test
+    public void onDataTreeChangedTest_write() {
+        Mockito.when(DATA_TREE_MODIFICATION.getRootNode()).thenReturn(DATA_OBJECT_MODIFICATION);
+        Mockito.when(DATA_OBJECT_MODIFICATION.getDataAfter()).thenReturn(DUMMY_NETWORK);
+        Mockito.when(DATA_OBJECT_MODIFICATION.getModificationType())
+                .thenReturn(DataObjectModification.ModificationType.WRITE);
+
+        networkDelegatingDataTreeListener.onDataTreeChanged(Lists.newArrayList(DATA_TREE_MODIFICATION));
+        Mockito.verify(DATA_PROCESSOR_NETWORK_MOCK).create(DUMMY_NETWORK);
+    }
+
+    /**
+     * Tests {@link DelegatingDataTreeListener#onDataTreeChanged} method with DELETE modification type.
+     */
+    @Test
+    public void onDataTreeChangedTest_delete() {
+        Mockito.when(DATA_TREE_MODIFICATION.getRootNode()).thenReturn(DATA_OBJECT_MODIFICATION);
+        Mockito.when(DATA_OBJECT_MODIFICATION.getDataBefore()).thenReturn(DUMMY_NETWORK);
+        Mockito.when(DATA_OBJECT_MODIFICATION.getModificationType())
+                .thenReturn(DataObjectModification.ModificationType.DELETE);
+
+        networkDelegatingDataTreeListener.onDataTreeChanged(Lists.newArrayList(DATA_TREE_MODIFICATION));
+        Mockito.verify(DATA_PROCESSOR_NETWORK_MOCK).delete(DUMMY_NETWORK);
+    }
+
+    /**
+     * Tests {@link DelegatingDataTreeListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type.
+     */
+    @Test
+    public void onDataTreeChangedTest_subtreeModified() {
+        Mockito.when(DATA_TREE_MODIFICATION.getRootNode()).thenReturn(DATA_OBJECT_MODIFICATION);
+        Mockito.when(DATA_OBJECT_MODIFICATION.getDataAfter()).thenReturn(DUMMY_NETWORK);
+        Mockito.when(DATA_OBJECT_MODIFICATION.getModificationType())
+                .thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
+
+        networkDelegatingDataTreeListener.onDataTreeChanged(Lists.newArrayList(DATA_TREE_MODIFICATION));
+        Mockito.verify(DATA_PROCESSOR_NETWORK_MOCK).update(DUMMY_NETWORK);
+    }
+}
diff --git a/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/LispNeutronServiceTest.java b/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/LispNeutronServiceTest.java
new file mode 100644 (file)
index 0000000..c6d3953
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.neutron;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService;
+
+public class LispNeutronServiceTest {
+    private static BindingAwareBroker.ProviderContext session = Mockito.mock(BindingAwareBroker.ProviderContext.class);
+    private static RpcProviderRegistry rpcRegistry = Mockito.mock(RpcProviderRegistry.class);
+    private static DataBroker dataBroker = Mockito.mock(DataBroker.class);
+    private IFlowMapping mappingService = Mockito.mock(IFlowMapping.class);
+    private BindingAwareBroker bindingAwareBroker = Mockito.mock(BindingAwareBroker.class);
+
+    private LispNeutronService lispNeutronService = new LispNeutronService(mappingService, bindingAwareBroker);
+
+    /**
+     * Tests {@link LispNeutronService#onSessionInitiated} method.
+     */
+    @Test
+    public void onSessionInitiatedTest() {
+        Mockito.when(session.getSALService(RpcProviderRegistry.class)).thenReturn(rpcRegistry);
+        Mockito.when(session.getSALService(DataBroker.class)).thenReturn(dataBroker);
+
+        lispNeutronService.onSessionInitiated(session);
+        Mockito.verify(rpcRegistry).getRpcService(OdlMappingserviceService.class);
+        Mockito.verify(session).getSALService(DataBroker.class);
+    }
+
+    /**
+     * Tests {@link LispNeutronService#close} method.
+     */
+    @Test
+    public void closeTest() {
+        lispNeutronService.close();
+        assertEquals(null, lispNeutronService.getMappingService());
+    }
+}
diff --git a/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/LispUtilTest.java b/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/LispUtilTest.java
new file mode 100644 (file)
index 0000000..5c080b1
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.neutron;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.Lists;
+import org.junit.Test;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInputBuilder;
+
+public class LispUtilTest {
+    private static final int RECORD_TTL = 1440;
+    private static final String IPV4_1 = "192.168.0.1";
+    private static final String IPV4_2 = "192.168.0.2";
+    private static final String KEY = "key";
+    private static final Eid EID = LispAddressUtil.asIpv4Eid(IPV4_1);
+    private static final LocatorRecord LOCATOR_RECORD = getDefaultLocatorRecord();
+
+    /**
+     * Tests {@link LispUtil#buildAddMappingInput} method.
+     */
+    @Test
+    public void buildAddMappingInputTest() {
+        final MappingRecord mappingRecord = new MappingRecordBuilder()
+                .setAction(MappingRecord.Action.NoAction)
+                .setAuthoritative(true).setEid(EID)
+                .setLocatorRecord(Lists.newArrayList(getDefaultLocatorRecord()))
+                .setMapVersion((short) 0)
+                .setRecordTtl(RECORD_TTL).build();
+
+        final AddMappingInput expectedResult = new AddMappingInputBuilder().setMappingRecord(mappingRecord).build();
+        final AddMappingInput result = LispUtil.buildAddMappingInput(EID, Lists.newArrayList(LOCATOR_RECORD));
+
+        assertEquals(expectedResult, result);
+    }
+
+    /**
+     * Tests {@link LispUtil#buildAddKeyInput} method.
+     */
+    @Test
+    public void buildAddKeyInputTest() {
+        final AddKeyInput expectedResult = new AddKeyInputBuilder()
+                .setEid(EID)
+                .setMappingAuthkey(new MappingAuthkeyBuilder()
+                        .setKeyString(KEY)
+                        .setKeyType(1).build()).build();
+        final AddKeyInput result = LispUtil.buildAddKeyInput(EID, KEY);
+
+        assertEquals(expectedResult, result);
+    }
+
+    /**
+     * Tests {@link LispUtil#buildGetMappingInput} method.
+     */
+    @Test
+    public void buildGetMappingInputTest() {
+        final GetMappingInput expectedResult = new GetMappingInputBuilder().setEid(EID).build();
+        final GetMappingInput result = LispUtil.buildGetMappingInput(EID);
+
+        assertEquals(expectedResult, result);
+    }
+
+    /**
+     * Tests {@link LispUtil#buildRemoveMappingInput} method.
+     */
+    @Test
+    public void buildRemoveMappingInputTest() {
+        final RemoveMappingInput expectedResult = new RemoveMappingInputBuilder().setEid(EID).build();
+        final RemoveMappingInput result = LispUtil.buildRemoveMappingInput(EID);
+
+        assertEquals(expectedResult, result);
+    }
+
+    /**
+     * Tests {@link LispUtil#buildRemoveKeyInput} method.
+     */
+    @Test
+    public void buildRemoveKeyInputTest() {
+        final RemoveKeyInput expectedResult = new RemoveKeyInputBuilder().setEid(EID).build();
+        final RemoveKeyInput result = LispUtil.buildRemoveKeyInput(EID);
+
+        assertEquals(expectedResult, result);
+    }
+
+    private static LocatorRecord getDefaultLocatorRecord() {
+        return new LocatorRecordBuilder()
+                .setRloc(new RlocBuilder()
+                        .setAddress(new Ipv4Builder()
+                                .setIpv4(new Ipv4Address(IPV4_2)).build()).build()).build();
+    }
+}
diff --git a/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/ListenerTest.java b/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/ListenerTest.java
new file mode 100644 (file)
index 0000000..0d15206
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.neutron;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+
+@SuppressWarnings("unchecked")
+public class ListenerTest {
+
+    /**
+     * Tests {@link NetworkListener#toString}, {@link PortListener#toString}, {@link SubnetListener#toString} method.
+     */
+    @Test
+    public void toStringTest() {
+        final DataProcessor<Network> networkDataProcessor = Mockito.mock(DataProcessor.class);
+        final DataBroker dataBroker = Mockito.mock(DataBroker.class);
+        final NetworkListener networkListener = new NetworkListener(networkDataProcessor, dataBroker);
+
+        final DataProcessor<Port> portDataProcessor = Mockito.mock(DataProcessor.class);
+        final PortListener portListener = new PortListener(portDataProcessor, dataBroker);
+
+        final DataProcessor<Subnet> subnetDataProcessor = Mockito.mock(DataProcessor.class);
+        final SubnetListener subnetListener = new SubnetListener(subnetDataProcessor, dataBroker);
+
+        assertEquals("NetworkListener", networkListener.toString());
+        assertEquals("PortListener", portListener.toString());
+        assertEquals("SubnetListener", subnetListener.toString());
+    }
+}
diff --git a/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/PortDataProcessorTest.java b/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/PortDataProcessorTest.java
new file mode 100644 (file)
index 0000000..a1609b4
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.neutron;
+
+import com.google.common.collect.Lists;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.DistinguishedNameType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedNameBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.PortBindingExtension;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIpsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+public class PortDataProcessorTest {
+
+    private static ILispNeutronService iLispNeutronServiceMock = Mockito.mock(ILispNeutronService.class);
+    private OdlMappingserviceService odlMappingserviceServiceMock;
+    private Port portMock;
+    private PortBindingExtension augmentationMock;
+    private Future<RpcResult<GetMappingOutput>> future;
+    private RpcResult<GetMappingOutput> rpcResult;
+
+    private static final String HOST_ID = "host-id";
+    private static final String IPV4 = "192.168.0.1";
+    private static final Address ADDRESS = new DistinguishedNameBuilder()
+            .setDistinguishedName(new DistinguishedNameType(HOST_ID)).build();
+
+    private PortDataProcessor portDataProcessor = new PortDataProcessor(iLispNeutronServiceMock);
+
+    @Before
+    @SuppressWarnings("unchecked")
+    public void init() {
+        portMock = Mockito.mock(Port.class);
+        odlMappingserviceServiceMock = Mockito.mock(OdlMappingserviceService.class);
+        augmentationMock = Mockito.mock(PortBindingExtension.class);
+        future = Mockito.mock(Future.class);
+        rpcResult = Mockito.mock(RpcResult.class);
+        commonStubbing(); // common stubbing is called before invocation of each test
+    }
+
+    /**
+     * Tests {@link PortDataProcessor#create} method.
+     */
+    @Test
+    public void createTest() throws ExecutionException, InterruptedException {
+        final GetMappingInput getMappingInput =
+                new GetMappingInputBuilder().setEid(LispAddressUtil.asDistinguishedNameEid(HOST_ID)).build();
+        final GetMappingOutput getMappingOutput = new GetMappingOutputBuilder()
+                .setMappingRecord(getDefaultMappingRecord()).build();
+
+        Mockito.when(odlMappingserviceServiceMock.getMapping(getMappingInput)).thenReturn(future);
+        Mockito.when(future.get()).thenReturn(rpcResult);
+        Mockito.when(rpcResult.getResult()).thenReturn(getMappingOutput);
+
+        // expected result
+        final AddMappingInput expectedResult = LispUtil
+                .buildAddMappingInput(LispAddressUtil.asIpv4PrefixEid(IPV4 + "/32"),
+                        Lists.newArrayList(getDefaultLocatorRecord()));
+        portDataProcessor.create(portMock);
+        Mockito.verify(odlMappingserviceServiceMock).addMapping(expectedResult);
+    }
+
+    /**
+     * Tests {@link PortDataProcessor#create} method with null host Id.
+     */
+    @Test
+    public void createTest_nullHostId() throws ExecutionException, InterruptedException {
+        Mockito.when(augmentationMock.getHostId()).thenReturn(null); // overriding default stubbing
+
+        portDataProcessor.create(portMock);
+        Mockito.verifyNoMoreInteractions(odlMappingserviceServiceMock);
+    }
+
+    /**
+     * Tests {@link PortDataProcessor#create} method with null mapping service.
+     */
+    @Test
+    public void createTest_nullOdlMappingService() throws ExecutionException, InterruptedException {
+        Mockito.when(iLispNeutronServiceMock.getMappingDbService()).thenReturn(null); // overriding default stubbing
+
+        portDataProcessor.create(portMock);
+        Mockito.verifyNoMoreInteractions(odlMappingserviceServiceMock);
+    }
+
+    /**
+     * Tests {@link PortDataProcessor#delete} method.
+     */
+    @Test
+    public void deleteTest() {
+        // expected result
+        final RemoveMappingInput expectedResult = LispUtil
+                .buildRemoveMappingInput(LispAddressUtil.asIpv4PrefixEid(IPV4 + "/32"));
+        portDataProcessor.delete(portMock);
+        Mockito.verify(odlMappingserviceServiceMock).removeMapping(expectedResult);
+    }
+
+    /**
+     * Stubbing common for all test methods.
+     */
+    public void commonStubbing() {
+        Mockito.when(portMock.getAugmentation(PortBindingExtension.class)).thenReturn(augmentationMock);
+        Mockito.when(iLispNeutronServiceMock.getMappingDbService()).thenReturn(odlMappingserviceServiceMock);
+        Mockito.when(portMock.getFixedIps()).thenReturn(getDefaultListOfFixedIps());
+        Mockito.when(augmentationMock.getHostId()).thenReturn(HOST_ID);
+    }
+
+    private static LocatorRecord getDefaultLocatorRecord() {
+        return new LocatorRecordBuilder()
+                .setRloc(new RlocBuilder().setAddress(ADDRESS).build())
+                .build();
+    }
+
+    private static MappingRecord getDefaultMappingRecord() {
+        return new MappingRecordBuilder().setLocatorRecord(Lists.newArrayList(getDefaultLocatorRecord())).build();
+    }
+
+    private static List<FixedIps> getDefaultListOfFixedIps() {
+        FixedIps fixedIps = new FixedIpsBuilder().setIpAddress(new IpAddress(new Ipv4Address(IPV4))).build();
+
+        return Lists.newArrayList(fixedIps);
+    }
+}
diff --git a/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/SubnetDataProcessorTest.java b/mappingservice/neutron/src/test/java/org/opendaylight/lispflowmapping/neutron/SubnetDataProcessorTest.java
new file mode 100644 (file)
index 0000000..2d531f9
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.neutron;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+public class SubnetDataProcessorTest {
+
+    private static ILispNeutronService iLispNeutronServiceMock = Mockito.mock(ILispNeutronService.class);
+    private static Subnet subnet = Mockito.mock(Subnet.class);
+    private OdlMappingserviceService odlMappingserviceServiceMock;
+    private Future<RpcResult<Void>> future;
+    private RpcResult<Void> rpcResult;
+
+    private static SubnetDataProcessor subnetDataProcessor = new SubnetDataProcessor(iLispNeutronServiceMock);
+
+    private static final String UUID_STRING = "123e4567-e89b-12d3-a456-426655440000";
+    private static final String IPV4 = "192.168.0.1";
+    private static final String MASK = "/32";
+    private static final Eid EID = LispAddressUtil.asIpv4PrefixEid(IPV4 + MASK);
+    private static final IpPrefix IP_PREFIX = new IpPrefix(new Ipv4Prefix(IPV4 + MASK));
+
+    @Before
+    @SuppressWarnings("unchecked")
+    public void init() {
+        odlMappingserviceServiceMock = Mockito.mock(OdlMappingserviceService.class);
+        future = Mockito.mock(Future.class);
+        rpcResult = Mockito.mock(RpcResult.class);
+    }
+
+    /**
+     * Tests {@link SubnetDataProcessor#create} method.
+     */
+    @Test
+    public void createTest() throws ExecutionException, InterruptedException {
+        final MappingAuthkey mappingAuthkey = new MappingAuthkeyBuilder()
+                .setKeyString(UUID_STRING).setKeyType(1).build();
+        final AddKeyInput addKeyInput = new AddKeyInputBuilder()
+                .setEid(EID).setMappingAuthkey(mappingAuthkey).build();
+
+        commonStubbing();
+        Mockito.when(odlMappingserviceServiceMock.addKey(addKeyInput)).thenReturn(future);
+
+        subnetDataProcessor.create(subnet);
+        assertEquals(true, rpcResult.isSuccessful());
+    }
+
+    /**
+     * Tests {@link SubnetDataProcessor#create} method with null mapping service.
+     */
+    @Test
+    public void createTest_withNullMappingService() {
+        Mockito.when(subnet.getCidr()).thenReturn(IP_PREFIX);
+        Mockito.when(iLispNeutronServiceMock.getMappingDbService()).thenReturn(null);
+
+        subnetDataProcessor.create(subnet);
+        Mockito.verifyZeroInteractions(odlMappingserviceServiceMock);
+    }
+
+    /**
+    * Tests {@link SubnetDataProcessor#delete} method.
+    */
+    @Test
+    public void deleteTest() throws ExecutionException, InterruptedException {
+        final RemoveKeyInput removeKeyInput = new RemoveKeyInputBuilder().setEid(EID).build();
+
+        commonStubbing();
+        Mockito.when(odlMappingserviceServiceMock.removeKey(removeKeyInput)).thenReturn(future);
+
+        subnetDataProcessor.delete(subnet);
+        assertEquals(true, rpcResult.isSuccessful());
+    }
+
+    /**
+     * Tests {@link SubnetDataProcessor#delete} method with null mapping service.
+     */
+    @Test
+    public void deleteTest_withNullMappingService() {
+        Mockito.when(subnet.getCidr()).thenReturn(IP_PREFIX);
+        Mockito.when(iLispNeutronServiceMock.getMappingDbService()).thenReturn(null);
+
+        subnetDataProcessor.delete(subnet);
+        Mockito.verifyZeroInteractions(odlMappingserviceServiceMock);
+    }
+
+    public void commonStubbing() throws ExecutionException, InterruptedException {
+        Mockito.when(subnet.getCidr()).thenReturn(IP_PREFIX);
+        Mockito.when(iLispNeutronServiceMock.getMappingDbService()).thenReturn(odlMappingserviceServiceMock);
+        Mockito.when(subnet.getUuid()).thenReturn(new Uuid(UUID_STRING));
+        Mockito.when(future.get()).thenReturn(rpcResult);
+        Mockito.when(rpcResult.isSuccessful()).thenReturn(true);
+    }
+}