Upgrade ietf-{inet,yang}-types to 2013-07-15
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasSubnetManagerListenerCovrgTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.faas;
2
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.eq;
5 import static org.mockito.Mockito.doNothing;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ScheduledExecutorService;
13
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
26 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
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.MappedSubnet;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnetBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.Gateways;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.GatewaysBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.Prefixes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.PrefixesBuilder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest(UlnDatastoreApi.class)
50 public class FaasSubnetManagerListenerCovrgTest {
51
52     private InstanceIdentifier<Subnet> subnetIid;
53     private FaasSubnetManagerListener listener;
54     private TenantId gbpTenantId = new TenantId("gbpTenantId");
55     private SubnetId subnetId = new SubnetId("subnetId");
56     private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
57     private Uuid faasSubnetId = new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f");
58     private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
59     private DataChangeListenerTester tester;
60     private DataBroker dataProvider;
61
62     @SuppressWarnings("unchecked")
63     @Before
64     public void init() {
65         dataProvider = mock(DataBroker.class);
66         subnetIid = mock(InstanceIdentifier.class);
67         listener = new FaasSubnetManagerListener(dataProvider, gbpTenantId, faasTenantId, executor);
68         tester = new DataChangeListenerTester(listener);
69         tester.setRemovedPath(subnetIid);
70     }
71
72     @SuppressWarnings("unchecked")
73     @Test
74     public void testOnDataChanged() throws ReadFailedException {
75         PowerMockito.mockStatic(UlnDatastoreApi.class);
76         PowerMockito.doNothing().when(UlnDatastoreApi.class);
77         UlnDatastoreApi.submitSubnetToDs(any(
78                 org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.Subnet.class));
79         PowerMockito.doNothing().when(UlnDatastoreApi.class);
80         UlnDatastoreApi.removeSubnetFromDsIfExists(any(Uuid.class), any(Uuid.class));
81
82         ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
83         WriteTransaction woTx = mock(WriteTransaction.class);
84         CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
85         when(rwTx.submit()).thenReturn(futureVoid);
86         when(woTx.submit()).thenReturn(futureVoid);
87
88         CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet = mock(CheckedFuture.class);
89         Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
90         when(optMappedSubnet.isPresent()).thenReturn(true);
91         when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
92         when(rwTx.read(LogicalDatastoreType.OPERATIONAL, FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId)))
93             .thenReturn(futureMappedSubnet);
94         doNothing().when(woTx).put(eq(LogicalDatastoreType.OPERATIONAL),
95                 eq(FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId)), any(MappedSubnet.class));
96
97         when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
98         when(dataProvider.newWriteOnlyTransaction()).thenReturn(woTx);
99
100         Subnet subnet = new SubnetBuilder().setId(subnetId).build();
101         tester.setDataObject(subnetIid, subnet);
102         tester.callOnDataChanged();
103         listener.executeEvent(tester.getChangeMock());
104     }
105
106     @Test
107     public void testLoadAll() {
108         PowerMockito.mockStatic(UlnDatastoreApi.class);
109         PowerMockito.doNothing().when(UlnDatastoreApi.class);
110         UlnDatastoreApi.submitSubnetToDs(any(
111                 org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.Subnet.class));
112
113         List<Subnet> subnets = new ArrayList<>();
114         List<MappedSubnet> mpSubnets = new ArrayList<>();
115
116         Subnet subnet = new SubnetBuilder().setId(subnetId).build();
117         subnets.add(subnet);
118         MappedSubnet mappedSubnet =
119                 new MappedSubnetBuilder().setGbpSubnetId(subnetId).setFaasSubnetId(faasSubnetId).build();
120         mpSubnets.add(mappedSubnet);
121
122         listener.loadAll(subnets, mpSubnets);
123     }
124
125     @SuppressWarnings("unchecked")
126     @Test
127     public void testInitSubnetBuilder() {
128         WriteTransaction wTx = mock(WriteTransaction.class);
129         CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
130         when(wTx.submit()).thenReturn(futureVoid);
131         doNothing().when(wTx).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
132                 any(DataObject.class));
133         when(dataProvider.newWriteOnlyTransaction()).thenReturn(wTx);
134
135         List<Gateways> gatewaysList = new ArrayList<>();
136         List<Prefixes> prefixesList = new ArrayList<>();
137         Prefixes prefixes = new PrefixesBuilder().setPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8"))).build();
138         prefixesList.add(prefixes);
139         Gateways gateways = new GatewaysBuilder().setGateway(new IpAddress(new Ipv4Address("10.0.0.55")))
140             .setPrefixes(prefixesList)
141             .build();
142         gatewaysList.add(gateways);
143
144         Subnet subnet = new SubnetBuilder().setId(subnetId).setGateways(gatewaysList).build();
145         listener.initSubnetBuilder(subnet);
146     }
147
148 }