0f44b276ac597beb2c6a9152543f9ca5f44d3674
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasContractManagerListenerCovrgTest.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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.ScheduledExecutorService;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
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.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
29 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.SecurityRuleGroups;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedContract;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
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 @RunWith(PowerMockRunner.class)
44 @PrepareForTest(UlnDatastoreApi.class)
45 public class FaasContractManagerListenerCovrgTest {
46
47     private InstanceIdentifier<Contract> contractIid;
48     private ContractId contractId = new ContractId("contractId");
49     private FaasContractManagerListener listener;
50     private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
51     private TenantId gbpTenantId = new TenantId("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
52     private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
53     private DataChangeListenerTester tester;
54     private DataBroker dataProvider;
55
56     @SuppressWarnings("unchecked")
57     @Before
58     public void init() {
59         contractIid = mock(InstanceIdentifier.class);
60         dataProvider = mock(DataBroker.class);
61
62         listener = new FaasContractManagerListener(dataProvider, gbpTenantId, faasTenantId, executor);
63         tester = new DataChangeListenerTester(listener);
64         tester.setRemovedPath(contractIid);
65     }
66
67     @Test
68     public void testT() throws ReadFailedException {
69         PowerMockito.mockStatic(UlnDatastoreApi.class);
70         PowerMockito.doNothing().when(UlnDatastoreApi.class);
71         UlnDatastoreApi.submitSecurityGroupsToDs(any(SecurityRuleGroups.class));
72
73         ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
74         WriteTransaction woTx = mock(WriteTransaction.class);
75         CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
76         when(rwTx.submit()).thenReturn(futureVoid);
77         when(woTx.submit()).thenReturn(futureVoid);
78         when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
79         when(dataProvider.newWriteOnlyTransaction()).thenReturn(woTx);
80
81         CheckedFuture<Optional<MappedContract>, ReadFailedException> futureMappedContract = mock(CheckedFuture.class);
82         when(rwTx.read(LogicalDatastoreType.OPERATIONAL, FaasIidFactory.mappedContractIid(gbpTenantId, contractId)))
83             .thenReturn(futureMappedContract);
84         Optional<MappedContract> optMappedContract = mock(Optional.class);
85         when(optMappedContract.isPresent()).thenReturn(true);
86         when(futureMappedContract.checkedGet()).thenReturn(optMappedContract);
87
88         Contract contract = new ContractBuilder().setId(contractId).build();
89         tester.setDataObject(contractIid, contract);
90         listener.executeEvent(tester.getChangeMock());
91     }
92
93     private DataObject makeTestContract() {
94         ContractBuilder builder = new ContractBuilder();
95         builder.setId(new ContractId("b4511aac-ae43-11e5-bf7f-feff819cdc9f"));
96         return builder.build();
97     }
98 }