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.util.concurrent.Futures;
12 import java.util.Collections;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.InOrder;
17 import org.mockito.Matchers;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.gbp.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.IseHarvestStatus;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.gbp.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.IseSourceConfig;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 * Test for {@link GbpIseConfigListenerImpl}.
33 @RunWith(MockitoJUnitRunner.class)
34 public class GbpIseConfigListenerImplTest {
37 private DataBroker dataBroker;
39 private GbpIseSgtHarvester harvester;
41 private DataTreeModification<IseSourceConfig> treeModification;
43 private DataObjectModification<IseSourceConfig> dataModification;
45 private WriteTransaction wTx;
47 private IseSourceConfig config;
49 private EPPolicyTemplateProviderFacade templateProviderFacade;
51 private GbpIseConfigListenerImpl listener;
54 public void setUp() throws Exception {
55 listener = new GbpIseConfigListenerImpl(dataBroker, harvester, templateProviderFacade);
59 public void testOnDataTreeChanged_noop() throws Exception {
60 Mockito.when(dataModification.getDataAfter()).thenReturn(null);
61 Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
63 listener.onDataTreeChanged(Collections.singleton(treeModification));
64 Mockito.verifyNoMoreInteractions(harvester, dataBroker);
68 public void testOnDataTreeChanged_succeeded() throws Exception {
69 Mockito.when(dataModification.getDataAfter()).thenReturn(config);
70 Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
72 Mockito.when(harvester.harvest(config)).thenReturn(Futures.immediateFuture(42));
74 Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
75 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
77 listener.onDataTreeChanged(Collections.singleton(treeModification));
80 final InOrder inOrder = Mockito.inOrder(harvester, dataBroker, wTx);
81 inOrder.verify(harvester).harvest(config);
82 inOrder.verify(dataBroker).newWriteOnlyTransaction();
83 inOrder.verify(wTx).put(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
84 Matchers.<InstanceIdentifier<IseHarvestStatus>>any(),
85 Matchers.<IseHarvestStatus>any(),
87 inOrder.verify(wTx).submit();
88 inOrder.verifyNoMoreInteractions();
92 public void testOnDataTreeChanged_failed() throws Exception {
93 Mockito.when(dataModification.getDataAfter()).thenReturn(config);
94 Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
96 Mockito.when(harvester.harvest(config)).thenReturn(Futures.immediateFailedFuture(
97 new Exception("extremely poor harvest occurred")));
99 Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
100 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
102 listener.onDataTreeChanged(Collections.singleton(treeModification));
105 final InOrder inOrder = Mockito.inOrder(harvester, dataBroker, wTx);
106 inOrder.verify(harvester).harvest(config);
107 inOrder.verify(dataBroker).newWriteOnlyTransaction();
108 inOrder.verify(wTx).put(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
109 Matchers.<InstanceIdentifier<IseHarvestStatus>>any(),
110 Matchers.<IseHarvestStatus>any(),
112 inOrder.verify(wTx).submit();
113 inOrder.verifyNoMoreInteractions();