BUG-6858: adapt to ise api, change lookup from ise
[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.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;
34
35 /**
36  * Test for {@link GbpIseConfigListenerImpl}.
37  */
38 @RunWith(MockitoJUnitRunner.class)
39 public class GbpIseConfigListenerImplTest {
40
41     private static final SgtInfo SGT_INFO = new SgtInfo(new Sgt(42), "ultimate_group", "uuidOf42");
42     @Mock
43     private DataBroker dataBroker;
44     @Mock
45     private GbpIseSgtHarvester harvester;
46     @Mock
47     private DataTreeModification<IseSourceConfig> treeModification;
48     @Mock
49     private DataObjectModification<IseSourceConfig> dataModification;
50     @Mock
51     private WriteTransaction wTx;
52     @Mock
53     private IseSourceConfig config;
54     @Mock
55     private EPPolicyTemplateProviderFacade templateProviderFacade;
56     @Captor
57     private ArgumentCaptor<IseContext> iseContextCpt;
58
59     private GbpIseConfigListenerImpl listener;
60
61     @Before
62     public void setUp() throws Exception {
63         listener = new GbpIseConfigListenerImpl(dataBroker, harvester, templateProviderFacade);
64     }
65
66     @Test
67     public void testOnDataTreeChanged_noop() throws Exception {
68         Mockito.when(dataModification.getDataAfter()).thenReturn(null);
69         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
70
71         listener.onDataTreeChanged(Collections.singleton(treeModification));
72         Mockito.verifyNoMoreInteractions(harvester, dataBroker);
73     }
74
75     @Test
76     public void testOnDataTreeChanged_succeeded() throws Exception {
77         Mockito.when(dataModification.getDataAfter()).thenReturn(config);
78         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
79
80         Mockito.when(harvester.harvestAll(iseContextCpt.capture())).thenReturn(Futures.immediateFuture(Lists.newArrayList(SGT_INFO)));
81
82         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
83         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
84
85         listener.onDataTreeChanged(Collections.singleton(treeModification));
86         listener.close();
87
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(),
95                 Matchers.eq(true));
96         inOrder.verify(wTx).submit();
97         inOrder.verifyNoMoreInteractions();
98     }
99
100     @Test
101     public void testOnDataTreeChanged_failed() throws Exception {
102         Mockito.when(dataModification.getDataAfter()).thenReturn(config);
103         Mockito.when(treeModification.getRootNode()).thenReturn(dataModification);
104
105         Mockito.when(harvester.harvestAll(iseContextCpt.capture())).thenReturn(Futures.immediateFailedFuture(
106                 new Exception("extremely poor harvestAll occurred")));
107
108         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
109         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
110
111         listener.onDataTreeChanged(Collections.singleton(treeModification));
112         listener.close();
113
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(),
121                 Matchers.eq(true));
122         inOrder.verify(wTx).submit();
123         inOrder.verifyNoMoreInteractions();
124     }
125 }