BUG-6650: ep-ip/sgt, implement and wire template provider
[groupbasedpolicy.git] / sxp-integration / sxp-ise-adapter / src / test / java / org / opendaylight / groupbasedpolicy / sxp_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.sxp_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.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;
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<IseSourceConfig> treeModification;
42     @Mock
43     private DataObjectModification<IseSourceConfig> dataModification;
44     @Mock
45     private WriteTransaction wTx;
46     @Mock
47     private IseSourceConfig config;
48     @Mock
49     private EPPolicyTemplateProviderFacade templateProviderFacade;
50
51     private GbpIseConfigListenerImpl listener;
52
53     @Before
54     public void setUp() throws Exception {
55         listener = new GbpIseConfigListenerImpl(dataBroker, harvester, templateProviderFacade);
56     }
57
58     @Test
59     public void testOnDataTreeChanged_noop() throws Exception {
60         Mockito.when(dataModification.getDataAfter()).thenReturn(null);
61         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
62
63         listener.onDataTreeChanged(Collections.singleton(treeModification));
64         Mockito.verifyNoMoreInteractions(harvester, dataBroker);
65     }
66
67     @Test
68     public void testOnDataTreeChanged_succeeded() throws Exception {
69         Mockito.when(dataModification.getDataAfter()).thenReturn(config);
70         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
71
72         Mockito.when(harvester.harvest(config)).thenReturn(Futures.immediateFuture(42));
73
74         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
75         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
76
77         listener.onDataTreeChanged(Collections.singleton(treeModification));
78         listener.close();
79
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(),
86                 Matchers.eq(true));
87         inOrder.verify(wTx).submit();
88         inOrder.verifyNoMoreInteractions();
89     }
90
91     @Test
92     public void testOnDataTreeChanged_failed() throws Exception {
93         Mockito.when(dataModification.getDataAfter()).thenReturn(config);
94         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
95
96         Mockito.when(harvester.harvest(config)).thenReturn(Futures.immediateFailedFuture(
97                 new Exception("extremely poor harvest occurred")));
98
99         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
100         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
101
102         listener.onDataTreeChanged(Collections.singleton(treeModification));
103         listener.close();
104
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(),
111                 Matchers.eq(true));
112         inOrder.verify(wTx).submit();
113         inOrder.verifyNoMoreInteractions();
114     }
115 }