Tests for Iovisor root package
[groupbasedpolicy.git] / renderers / iovisor / src / test / java / org / opendaylight / groupbasedpolicy / renderer / iovisor / IovisorResolvedEndpointListenerTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems. 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.renderer.iovisor;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.util.List;
18 import java.util.Set;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.ImmutableSet;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.groupbasedpolicy.renderer.iovisor.test.GbpIovisorDataBrokerTest;
33 import org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorIidFactory;
34 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
35 import org.opendaylight.groupbasedpolicy.util.IidFactory;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorResolvedEndpointsByTenantByEndpointgroupId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorResolvedEndpointsByTenantByEndpointgroupIdBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.resolved.endpoints.by.tenant.by.endpointgroup.id.IovisorResolvedEndpointByTenantByEndpointgroupId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.resolved.endpoints.by.tenant.by.endpointgroup.id.IovisorResolvedEndpointByTenantByEndpointgroupIdBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.interests.followed.tenants.followed.tenant.FollowedEndpointGroup;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 public class IovisorResolvedEndpointListenerTest extends GbpIovisorDataBrokerTest {
46
47     private DataBroker dataBroker;
48     private IovisorResolvedEndpointListener iovisorResolvedEndpointListener;
49     private DataObjectModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootNode;
50     private Set<DataTreeModification<IovisorResolvedEndpointsByTenantByEndpointgroupId>> changes;
51     private InstanceIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootIdentifier;
52
53     private final TenantId tenant1 = new TenantId("tenant1");
54     private final EndpointGroupId epg1 = new EndpointGroupId("client");
55
56     @Before
57     public void iovisorInit() {
58         dataBroker = getDataBroker();
59         iovisorResolvedEndpointListener = spy(new IovisorResolvedEndpointListener(dataBroker));
60
61         rootNode = mock(DataObjectModification.class);
62         rootIdentifier = IovisorIidFactory.iovisorResolvedEndpointsByTenantIdByEndpointGroupIdWildCardIid();
63         DataTreeIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootPath =
64                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
65                         IovisorIidFactory.iovisorResolvedEndpointsByTenantIdByEndpointGroupIdWildCardIid());
66         DataTreeModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> change = mock(DataTreeModification.class);
67
68         when(change.getRootNode()).thenReturn(rootNode);
69         when(change.getRootPath()).thenReturn(rootPath);
70
71         changes = ImmutableSet.of(change);
72
73         IovisorResolvedEndpointByTenantByEndpointgroupId testElement = new IovisorResolvedEndpointByTenantByEndpointgroupIdBuilder()
74                 .setTenantId(tenant1)
75                 .setEndpointgroupId(epg1)
76                 .build();
77         List<IovisorResolvedEndpointByTenantByEndpointgroupId> list = ImmutableList.of(testElement);
78
79         IovisorResolvedEndpointsByTenantByEndpointgroupId testData = new IovisorResolvedEndpointsByTenantByEndpointgroupIdBuilder()
80                 .setIovisorResolvedEndpointByTenantByEndpointgroupId(list)
81                 .build();
82
83         when(rootNode.getDataAfter()).thenReturn(testData);
84     }
85
86     @Test
87     public void testOnWrite() {
88         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
89
90         iovisorResolvedEndpointListener.onDataTreeChanged(changes);
91
92         verify(iovisorResolvedEndpointListener).onSubtreeModified(rootNode, rootIdentifier);
93     }
94
95     @Test(expected = UnsupportedOperationException.class)
96     public void testOnDelete() {
97         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
98
99         iovisorResolvedEndpointListener.onDataTreeChanged(changes);
100     }
101
102     @Test
103     public void testOnSubtreeModified() {
104         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
105
106         iovisorResolvedEndpointListener.onDataTreeChanged(changes);
107
108         verify(iovisorResolvedEndpointListener).endpointPolicyUpdated(any(TenantId.class), any(EndpointGroupId.class), any(WriteTransaction.class));
109     }
110
111
112     @Test
113     public void endpointPolicyUpdatedTest() {
114         iovisorResolvedEndpointListener.endpointPolicyUpdated(tenant1, epg1, dataBroker.newWriteOnlyTransaction());
115         Optional<FollowedEndpointGroup> readFromDs = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
116                 IidFactory.followedEndpointgroupIid(IovisorRenderer.RENDERER_NAME, tenant1, epg1),
117                 dataBroker.newReadOnlyTransaction());
118         Assert.assertTrue(readFromDs.isPresent());
119         Assert.assertEquals(epg1, readFromDs.get().getId());
120     }
121 }