BUG-6650: ep-ip/sgt, move groupbasedpolicy-ise-adapter into sxp-integration
[groupbasedpolicy.git] / sxp-integration / groupbasedpolicy-ise-adapter / src / test / java / org / opendaylight / groupbasedpolicy / gbp_ise_adapter / impl / GbpIseConfigListenerImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.gbp_ise_adapter.impl;
10
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.ise.adapter.model.rev160630.gbp.ise.adapter.IseHarvestConfig;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.gbp.ise.adapter.model.rev160630.gbp.ise.adapter.IseHarvestStatus;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29
30 /**
31  * Test for {@link GbpIseConfigListenerImpl}.
32  */
33 @RunWith(MockitoJUnitRunner.class)
34 public class GbpIseConfigListenerImplTest {
35
36     @Mock
37     private DataBroker dataBroker;
38     @Mock
39     private GbpIseSgtHarvester harvester;
40     @Mock
41     private DataTreeModification<IseHarvestConfig> treeModification;
42     @Mock
43     private DataObjectModification<IseHarvestConfig> dataModification;
44     @Mock
45     private WriteTransaction wTx;
46     @Mock
47     private IseHarvestConfig config;
48
49     private GbpIseConfigListenerImpl listener;
50
51     @Before
52     public void setUp() throws Exception {
53         listener = new GbpIseConfigListenerImpl(dataBroker, harvester);
54     }
55
56     @Test
57     public void testOnDataTreeChanged_noop() throws Exception {
58         Mockito.when(dataModification.getDataAfter()).thenReturn(null);
59         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
60
61         listener.onDataTreeChanged(Collections.singleton(treeModification));
62         Mockito.verifyNoMoreInteractions(harvester, dataBroker);
63     }
64
65     @Test
66     public void testOnDataTreeChanged_succeeded() throws Exception {
67         Mockito.when(dataModification.getDataAfter()).thenReturn(config);
68         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
69
70         Mockito.when(harvester.harvest(config)).thenReturn(Futures.immediateFuture(42));
71
72         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
73         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
74
75         listener.onDataTreeChanged(Collections.singleton(treeModification));
76         listener.close();
77
78         final InOrder inOrder = Mockito.inOrder(harvester, dataBroker, wTx);
79         inOrder.verify(harvester).harvest(config);
80         inOrder.verify(dataBroker).newWriteOnlyTransaction();
81         inOrder.verify(wTx).put(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
82                 Matchers.<InstanceIdentifier<IseHarvestStatus>>any(),
83                 Matchers.<IseHarvestStatus>any(),
84                 Matchers.eq(true));
85         inOrder.verify(wTx).submit();
86         inOrder.verifyNoMoreInteractions();
87     }
88
89     @Test
90     public void testOnDataTreeChanged_failed() throws Exception {
91         Mockito.when(dataModification.getDataAfter()).thenReturn(config);
92         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
93
94         Mockito.when(harvester.harvest(config)).thenReturn(Futures.immediateFailedFuture(
95                 new Exception("extremely poor harvest occurred")));
96
97         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
98         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
99
100         listener.onDataTreeChanged(Collections.singleton(treeModification));
101         listener.close();
102
103         final InOrder inOrder = Mockito.inOrder(harvester, dataBroker, wTx);
104         inOrder.verify(harvester).harvest(config);
105         inOrder.verify(dataBroker).newWriteOnlyTransaction();
106         inOrder.verify(wTx).put(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
107                 Matchers.<InstanceIdentifier<IseHarvestStatus>>any(),
108                 Matchers.<IseHarvestStatus>any(),
109                 Matchers.eq(true));
110         inOrder.verify(wTx).submit();
111         inOrder.verifyNoMoreInteractions();
112     }
113 }