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_ise_adapter.impl;
11 import com.google.common.collect.Lists;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collections;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Captor;
20 import org.mockito.InOrder;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.IseHarvestStatus;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.IseSourceConfig;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 * Test for {@link GbpIseConfigListenerImpl}.
38 @RunWith(MockitoJUnitRunner.class)
39 public class GbpIseConfigListenerImplTest {
41 private static final SgtInfo SGT_INFO = new SgtInfo(new Sgt(42), "ultimate_group", "uuidOf42");
43 private DataBroker dataBroker;
45 private GbpIseSgtHarvester harvester;
47 private DataTreeModification<IseSourceConfig> treeModification;
49 private DataObjectModification<IseSourceConfig> dataModification;
51 private WriteTransaction wTx;
53 private IseSourceConfig config;
55 private EPPolicyTemplateProviderFacade templateProviderFacade;
57 private ArgumentCaptor<IseContext> iseContextCpt;
59 private GbpIseConfigListenerImpl listener;
62 public void setUp() throws Exception {
63 listener = new GbpIseConfigListenerImpl(dataBroker, harvester, templateProviderFacade);
67 public void testOnDataTreeChanged_noop() throws Exception {
68 Mockito.when(dataModification.getDataAfter()).thenReturn(null);
69 Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
71 listener.onDataTreeChanged(Collections.singleton(treeModification));
72 Mockito.verifyNoMoreInteractions(harvester, dataBroker);
76 public void testOnDataTreeChanged_succeeded() throws Exception {
77 Mockito.when(dataModification.getDataAfter()).thenReturn(config);
78 Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
80 Mockito.when(harvester.harvestAll(iseContextCpt.capture())).thenReturn(Futures.immediateFuture(Lists.newArrayList(SGT_INFO)));
82 Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
83 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
85 listener.onDataTreeChanged(Collections.singleton(treeModification));
88 Assert.assertSame(config, iseContextCpt.getValue().getIseSourceConfig());
89 final InOrder inOrder = Mockito.inOrder(harvester, dataBroker, wTx);
90 inOrder.verify(harvester).harvestAll(Matchers.<IseContext>any());
91 inOrder.verify(dataBroker).newWriteOnlyTransaction();
92 inOrder.verify(wTx).put(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
93 Matchers.<InstanceIdentifier<IseHarvestStatus>>any(),
94 Matchers.<IseHarvestStatus>any(),
96 inOrder.verify(wTx).submit();
97 inOrder.verifyNoMoreInteractions();
101 public void testOnDataTreeChanged_failed() throws Exception {
102 Mockito.when(dataModification.getDataAfter()).thenReturn(config);
103 Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
105 Mockito.when(harvester.harvestAll(iseContextCpt.capture())).thenReturn(Futures.immediateFailedFuture(
106 new Exception("extremely poor harvestAll occurred")));
108 Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
109 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
111 listener.onDataTreeChanged(Collections.singleton(treeModification));
114 Assert.assertSame(config, iseContextCpt.getValue().getIseSourceConfig());
115 final InOrder inOrder = Mockito.inOrder(harvester, dataBroker, wTx);
116 inOrder.verify(harvester).harvestAll(Matchers.<IseContext>any());
117 inOrder.verify(dataBroker).newWriteOnlyTransaction();
118 inOrder.verify(wTx).put(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
119 Matchers.<InstanceIdentifier<IseHarvestStatus>>any(),
120 Matchers.<IseHarvestStatus>any(),
122 inOrder.verify(wTx).submit();
123 inOrder.verifyNoMoreInteractions();