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
8 package org.opendaylight.groupbasedpolicy.sxp.mapper.impl.dao;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Iterables;
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.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Captor;
24 import org.mockito.InOrder;
25 import org.mockito.Matchers;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.Spy;
29 import org.mockito.runners.MockitoJUnitRunner;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.groupbasedpolicy.sxp.mapper.api.SimpleCachedDao;
35 import org.opendaylight.groupbasedpolicy.sxp.mapper.impl.util.EPTemplateUtil;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.SxpMapper;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgtBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgtKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 * Test for {@link EPPolicyTemplateDaoImpl}.
50 @RunWith(MockitoJUnitRunner.class)
51 public class EPPolicyTemplateDaoImplTest {
53 private static final Sgt KEY_1 = new Sgt(1);
54 private final EndpointPolicyTemplateBySgt EP_POLICY_TEMPLATE_VALUE;
56 private DataBroker dataBroker;
58 private SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
60 private ReadOnlyTransaction rTx;
62 private EpPolicyTemplateValueKeyFactory keyFactory = new EpPolicyTemplateValueKeyFactory(
63 EPTemplateUtil.createEndpointGroupIdOrdering(), EPTemplateUtil.createConditionNameOrdering());
65 ArgumentCaptor<Sgt> sgtCapt;
67 ArgumentCaptor<EndpointPolicyTemplateBySgt> epPolicyTemplateCapt;
69 private EPPolicyTemplateDaoImpl dao;
71 public EPPolicyTemplateDaoImplTest() {
72 EP_POLICY_TEMPLATE_VALUE = new EndpointPolicyTemplateBySgtBuilder()
78 public void setUp() throws Exception {
79 dao = new EPPolicyTemplateDaoImpl(dataBroker, cachedDao, keyFactory);
83 public void testRead_absent() throws Exception {
84 Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(Optional.<EndpointPolicyTemplateBySgt>absent());
85 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
86 Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
87 Matchers.<InstanceIdentifier<EndpointPolicyTemplateBySgt>>any())).thenReturn(
88 Futures.<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException>immediateCheckedFuture(
89 Optional.<EndpointPolicyTemplateBySgt>absent()));
92 final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read = dao.read(KEY_1);
93 Assert.assertTrue(read.isDone());
94 Assert.assertFalse(read.get().isPresent());
98 public void testRead_presentCached() throws Exception {
99 Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(Optional.of(EP_POLICY_TEMPLATE_VALUE));
101 final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read = dao.read(KEY_1);
102 Assert.assertTrue(read.isDone());
103 Assert.assertTrue(read.get().isPresent());
104 Assert.assertEquals(KEY_1, read.get().get().getSgt());
108 public void testRead_presentDS() throws Exception {
109 Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(
110 Optional.<EndpointPolicyTemplateBySgt>absent());
111 Mockito.when(cachedDao.isEmpty()).thenReturn(true);
112 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
113 Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
114 Matchers.<InstanceIdentifier<EndpointPolicyTemplateBySgt>>any())).thenReturn(
115 Futures.<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException>immediateCheckedFuture(
116 Optional.of(EP_POLICY_TEMPLATE_VALUE)));
117 Mockito.doCallRealMethod().when(keyFactory).sortValueKeyLists(Matchers.<EndpointPolicyTemplateBySgt>any());
119 final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read = dao.read(KEY_1);
120 Assert.assertTrue(read.isDone());
121 Assert.assertTrue(read.get().isPresent());
122 Assert.assertEquals(KEY_1, read.get().get().getSgt());
124 final InOrder inOrder = Mockito.inOrder(cachedDao);
125 inOrder.verify(cachedDao).update(KEY_1, EP_POLICY_TEMPLATE_VALUE);
126 inOrder.verifyNoMoreInteractions();
130 public void testBuildReadPath() throws Exception {
131 final KeyedInstanceIdentifier<EndpointPolicyTemplateBySgt, EndpointPolicyTemplateBySgtKey> expectedPath =
132 InstanceIdentifier.create(SxpMapper.class)
133 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(KEY_1));
135 final InstanceIdentifier<EndpointPolicyTemplateBySgt> readPath = dao.buildReadPath(KEY_1);
136 Assert.assertEquals(expectedPath, readPath);
140 public void testReadBy_single() throws Exception {
141 final EpPolicyTemplateValueKey key = new EpPolicyTemplateValueKey(new TenantId("tn1"),
142 buildEndpointGroupIds(new String[]{"epg1", "epg2"}),
143 buildConditions(new String[]{"cn1", "cn2"}));
145 Mockito.doCallRealMethod().when(keyFactory).createKeyWithDefaultOrdering(Matchers.<EndpointPolicyTemplateBySgt>any());
147 Mockito.when(cachedDao.values()).thenReturn(Lists.newArrayList(
148 createEpPolicytemplate(new Sgt(1), new String[]{"cn2", "cn1"}, new String[]{"epg1", "epg2"}, "tn1"),
149 createEpPolicytemplate(new Sgt(2), new String[]{"cn1", "cn2"}, new String[]{"epg2", "epg1"}, "tn1"),
150 createEpPolicytemplate(new Sgt(3), new String[]{"cn2", "cn1"}, new String[]{"epg2", "epg1"}, "tn1"),
151 createEpPolicytemplate(new Sgt(4), new String[]{"cn1", "cn2"}, new String[]{"epg1", "epg2"}, "tn1")
154 final Collection<EndpointPolicyTemplateBySgt> policyTemplates = dao.readBy(key);
155 Assert.assertEquals(1, policyTemplates.size());
156 Assert.assertEquals(4, Iterables.getFirst(policyTemplates, null).getSgt().getValue().intValue());
160 public void testRead_unsortedLists() throws Exception {
161 final EndpointPolicyTemplateBySgt epPolicytemplateUnsorted = createEpPolicytemplate(new Sgt(1),
162 new String[]{"cn2", "cn1"}, new String[]{"epg2", "epg1"}, "tn1");
164 Mockito.doCallRealMethod().when(keyFactory).createKeyWithDefaultOrdering(Matchers.<EndpointPolicyTemplateBySgt>any());
166 Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(
167 Optional.<EndpointPolicyTemplateBySgt>absent());
168 Mockito.when(cachedDao.isEmpty()).thenReturn(true);
169 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
171 Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
172 Matchers.<InstanceIdentifier<EndpointPolicyTemplateBySgt>>any())).thenReturn(
173 Futures.<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException>immediateCheckedFuture(
174 Optional.of(epPolicytemplateUnsorted)));
176 dao.read(new Sgt(1));
178 Mockito.verify(cachedDao).update(sgtCapt.capture(), epPolicyTemplateCapt.capture());
179 Mockito.verify(cachedDao).find(sgtCapt.capture());
181 Assert.assertEquals(1, sgtCapt.getValue().getValue().intValue());
182 final EndpointPolicyTemplateBySgt template = epPolicyTemplateCapt.getValue();
183 Assert.assertEquals(1, template.getSgt().getValue().intValue());
184 Assert.assertEquals("tn1", template.getTenant().getValue());
185 Assert.assertEquals(buildEndpointGroupIds(new String[]{"epg1", "epg2"}), template.getEndpointGroups());
186 Assert.assertEquals(buildConditions(new String[]{"cn1", "cn2"}), template.getConditions());
190 private EndpointPolicyTemplateBySgt createEpPolicytemplate(final Sgt sgt, final String[] conditionNames,
191 final String[] epgIds, final String tenant) {
192 return new EndpointPolicyTemplateBySgtBuilder()
194 .setEndpointGroups(buildEndpointGroupIds(epgIds))
195 .setConditions(buildConditions(conditionNames))
196 .setTenant(new TenantId(tenant))
200 private static List<EndpointGroupId> buildEndpointGroupIds(final String[] names) {
201 final List<EndpointGroupId> endpointGroupIds = new ArrayList<>();
202 for (String epgId : names) {
203 endpointGroupIds.add(new EndpointGroupId(epgId));
205 return endpointGroupIds;
208 private static List<ConditionName> buildConditions(final String[] names) {
209 final List<ConditionName> conditions = new ArrayList<>();
210 for (String condition : names) {
211 conditions.add(new ConditionName(condition));