2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Collection;
16 import java.util.Collections;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.InOrder;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
31 import org.opendaylight.sxp.core.Configuration;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.master.database.fields.MasterDatabaseBinding;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.master.database.fields.MasterDatabaseBindingBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.node.rev160308.SxpNodeIdentity;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.node.rev160308.SxpNodeIdentityBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.node.rev160308.network.topology.topology.node.SxpDomainsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.node.rev160308.network.topology.topology.node.sxp.domains.SxpDomainBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.node.rev160308.sxp.databases.fields.MasterDatabaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 * Test for {@link MasterDatabaseBindingDaoImpl}.
52 @RunWith(MockitoJUnitRunner.class)
53 public class MasterDatabaseBindingDaoImplTest {
55 private static final Sgt KEY_1 = new Sgt(1);
56 private static final IpPrefix IP_PREFIX = new IpPrefix(new Ipv4Prefix("1.2.3.4/32"));
57 private final Topology TOPOLOGY_VALUE;
58 private final MasterDatabaseBinding MASTER_DB_BINDING_VALUE;
61 private DataBroker dataBroker;
63 private SimpleCachedDao<IpPrefix, MasterDatabaseBinding> cachedDao;
65 private ReadOnlyTransaction rTx;
67 private MasterDatabaseBindingDaoImpl dao;
69 public MasterDatabaseBindingDaoImplTest() {
70 MASTER_DB_BINDING_VALUE = new MasterDatabaseBindingBuilder()
71 .setSecurityGroupTag(KEY_1)
72 .setIpPrefix(IP_PREFIX)
75 TOPOLOGY_VALUE = new TopologyBuilder()
76 .setTopologyId(new TopologyId(Configuration.TOPOLOGY_NAME))
77 .setNode(Lists.newArrayList(new NodeBuilder()
78 .setNodeId(new NodeId("utNodeId"))
79 .addAugmentation(SxpNodeIdentity.class, new SxpNodeIdentityBuilder()
80 .setSxpDomains(new SxpDomainsBuilder()
81 .setSxpDomain(Collections.singletonList(new SxpDomainBuilder()
82 .setDomainName("global")
83 .setMasterDatabase(new MasterDatabaseBuilder()
84 .setMasterDatabaseBinding(Lists.newArrayList(MASTER_DB_BINDING_VALUE))
95 public void setUp() throws Exception {
96 dao = new MasterDatabaseBindingDaoImpl(dataBroker, cachedDao);
100 public void testRead_absent() throws Exception {
101 Mockito.when(cachedDao.find(Matchers.<IpPrefix>any())).thenReturn(Optional.<MasterDatabaseBinding>absent());
102 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
103 Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
104 Matchers.<InstanceIdentifier<Topology>>any())).thenReturn(
105 Futures.<Optional<Topology>, ReadFailedException>immediateCheckedFuture(
106 Optional.<Topology>absent()));
109 final ListenableFuture<Optional<MasterDatabaseBinding>> read = dao.read(IP_PREFIX);
110 Assert.assertTrue(read.isDone());
111 Assert.assertFalse(read.get().isPresent());
115 public void testRead_presentCached() throws Exception {
116 Mockito.when(cachedDao.find(Matchers.<IpPrefix>any())).thenReturn(Optional.of(MASTER_DB_BINDING_VALUE));
118 final ListenableFuture<Optional<MasterDatabaseBinding>> read = dao.read(IP_PREFIX);
119 Assert.assertTrue(read.isDone());
120 Assert.assertTrue(read.get().isPresent());
121 Assert.assertEquals(KEY_1, read.get().get().getSecurityGroupTag());
125 public void testRead_presentDS() throws Exception {
126 Mockito.when(cachedDao.find(Matchers.<IpPrefix>any())).thenReturn(
127 Optional.<MasterDatabaseBinding>absent(),
128 Optional.of(MASTER_DB_BINDING_VALUE));
129 Mockito.when(cachedDao.isEmpty()).thenReturn(true);
130 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
131 Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
132 Matchers.<InstanceIdentifier<Topology>>any())).thenReturn(
133 Futures.<Optional<Topology>, ReadFailedException>immediateCheckedFuture(
134 Optional.of(TOPOLOGY_VALUE)));
136 final ListenableFuture<Optional<MasterDatabaseBinding>> read = dao.read(IP_PREFIX);
137 Assert.assertTrue(read.isDone());
138 Assert.assertTrue(read.get().isPresent());
139 Assert.assertEquals(KEY_1, read.get().get().getSecurityGroupTag());
141 final InOrder inOrder = Mockito.inOrder(cachedDao);
142 inOrder.verify(cachedDao).invalidateCache();
143 inOrder.verify(cachedDao).update(IP_PREFIX, MASTER_DB_BINDING_VALUE);
144 inOrder.verify(cachedDao).find(IP_PREFIX);
145 inOrder.verifyNoMoreInteractions();
149 public void testReadBy() throws Exception {
150 Mockito.when(cachedDao.isEmpty()).thenReturn(false);
151 Mockito.when(cachedDao.values()).thenReturn(Collections.singleton(MASTER_DB_BINDING_VALUE));
153 final ListenableFuture<Collection<MasterDatabaseBinding>> readByFt = dao.readBy(KEY_1);
154 Assert.assertTrue(readByFt.isDone());
155 Assert.assertEquals(1, readByFt.get().size());