cd82d2ad6896de03d91c75a321b0bd155b0d0ecf
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasSubnetManagerListenerTest.java
1 /*
2  * Copyright (c) 2016 Huawei Technologies 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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
31 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.Subnet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42
43 import com.google.common.util.concurrent.CheckedFuture;
44
45 @PrepareForTest(UlnDatastoreApi.class)
46 @RunWith(PowerMockRunner.class)
47 public class FaasSubnetManagerListenerTest {
48
49     private InstanceIdentifier<DataObject> subnetId;
50     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
51     private MockFaasSubnetManagerListener subnetManagerListener;
52     private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime()
53         .availableProcessors());
54     private TenantId gbpTenantId = new TenantId("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
55     private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
56
57     @SuppressWarnings("unchecked")
58     @Before
59     public void init() {
60         subnetId = mock(InstanceIdentifier.class);
61         change = mock(AsyncDataChangeEvent.class);
62         subnetId = mock(InstanceIdentifier.class);
63         DataBroker dataProvider = mock(DataBroker.class);
64         PowerMockito.mockStatic(UlnDatastoreApi.class);
65         WriteTransaction writeTransaction = mock(WriteTransaction.class);
66         when(dataProvider.newWriteOnlyTransaction()).thenReturn(writeTransaction);
67         CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = mock(CheckedFuture.class);
68         when(writeTransaction.submit()).thenReturn(checkedFuture);
69         subnetManagerListener = new MockFaasSubnetManagerListener(dataProvider, gbpTenantId, faasTenantId, executor);
70
71         Set<InstanceIdentifier<?>> removedPaths = new HashSet<>();
72         removedPaths.add(subnetId);
73         when(change.getRemovedPaths()).thenReturn(removedPaths);
74     }
75
76     @Test
77     public void testOnDataChangeSubnet() {
78         // prepare input test data
79         ArgumentCaptor<Subnet> captor = ArgumentCaptor.forClass(Subnet.class);
80         try {
81             PowerMockito.doNothing().when(UlnDatastoreApi.class, "submitSubnetToDs", captor.capture());
82         } catch (Exception e) {
83             fail("testOnDataChangeSubnet: Exception = " + e.toString());
84         }
85         Uuid expectedFaasSubnetId = new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f");
86         subnetManagerListener.setExpectedFaasSubnetId(expectedFaasSubnetId);
87         DataObject testSubnet = makeTestSubnet();
88         subnetManagerListener.setExpectedGbpSubnet(testSubnet);
89         Map<InstanceIdentifier<?>, DataObject> testData = new HashMap<>();
90         testData.put(subnetId, testSubnet);
91         when(change.getCreatedData()).thenReturn(testData);
92         when(change.getOriginalData()).thenReturn(testData);
93         when(change.getUpdatedData()).thenReturn(testData);
94         // invoke event -- expected data is verified in mocked classes
95         subnetManagerListener.onDataChanged(change);
96
97         // make sure internal threads have completed
98         try {
99             executor.shutdown();
100             executor.awaitTermination(10, TimeUnit.SECONDS);
101         } catch (InterruptedException e) {
102             fail("testOnDataChangeSubnet: Exception = " + e.toString());
103         }
104         // Verify passed in values to fabric mapping engine
105         assertTrue("testOnDataChangeSubnet: Actual Faas SubnetId is NOT as expected",
106                 expectedFaasSubnetId.equals(captor.getValue().getUuid()));
107     }
108
109     private DataObject makeTestSubnet() {
110         SubnetBuilder builder = new SubnetBuilder();
111         builder.setId(new SubnetId("b4511aac-ae43-11e5-bf7f-feff819cdc9f"));
112         return builder.build();
113     }
114 }