BUG-6650: ep-ip/sgt, move groupbasedpolicy-ise-adapter into sxp-integration
[groupbasedpolicy.git] / sxp-integration / sxp-ise-adapter / src / test / java / org / opendaylight / groupbasedpolicy / sxp_ise_adapter / impl / SgtToEpgGeneratorImplTest.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.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.InOrder;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.groupbasedpolicy.util.IidFactory;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 /**
37  * Test for {@link SgtToEpgGeneratorImpl}.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class SgtToEpgGeneratorImplTest {
41
42     public static final TenantId TENANT_ID = new TenantId("tenant-unit-1");
43     public static final Sgt SGT_1 = new Sgt(1);
44     public static final String EPG_NAME_1 = "epg-unit-1";
45     public static final Sgt SGT_2 = new Sgt(2);
46     public static final String EPG_NAME_2 = "epg-unit-2";
47
48     @Mock
49     private DataBroker dataBroker;
50     @Mock
51     private WriteTransaction wTx;
52
53     private SgtToEpgGeneratorImpl generator;
54
55     @Before
56     public void setUp() throws Exception {
57         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
58         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wTx);
59         generator = new SgtToEpgGeneratorImpl(dataBroker);
60     }
61
62     @Test
63     public void testProcessSgtInfo() throws Exception {
64         final SgtInfo sgtInfo1 = new SgtInfo(SGT_1, EPG_NAME_1);
65         final SgtInfo sgtInfo2 = new SgtInfo(SGT_2, EPG_NAME_2);
66
67         final CheckedFuture<Void, TransactionCommitFailedException> outcome =
68                 generator.processSgtInfo(TENANT_ID, Lists.newArrayList(sgtInfo1, sgtInfo2));
69
70         outcome.get(10, TimeUnit.SECONDS);
71
72         final EndpointGroupId epgId1 = new EndpointGroupId(EPG_NAME_1);
73         final InstanceIdentifier<EndpointGroup> epgPath1 = IidFactory.endpointGroupIid(TENANT_ID, epgId1);
74         final EndpointGroup epg1 = createEpg(epgId1, SGT_1.getValue());
75
76         final EndpointGroupId epgId2 = new EndpointGroupId(EPG_NAME_2);
77         final InstanceIdentifier<EndpointGroup> epgPath2 = IidFactory.endpointGroupIid(TENANT_ID, epgId2);
78         final EndpointGroup epg2 = createEpg(epgId2, SGT_2.getValue());
79
80         final InOrder inOrder = Mockito.inOrder(wTx);
81         inOrder.verify(wTx, Mockito.calls(1)).put(LogicalDatastoreType.CONFIGURATION, epgPath1, epg1, true);
82         inOrder.verify(wTx, Mockito.calls(1)).put(LogicalDatastoreType.CONFIGURATION, epgPath2, epg2, false);
83         inOrder.verify(wTx).submit();
84
85         Mockito.verifyZeroInteractions(wTx);
86     }
87
88     private EndpointGroup createEpg(final EndpointGroupId epgId1, final int sgt) {
89         final String epgIdValue = epgId1.getValue();
90         return new EndpointGroupBuilder()
91                 .setId(epgId1)
92                 .setName(new Name(epgIdValue + "--"+sgt))
93                 .setDescription(new Description("imported from ISE for sgt="+sgt))
94                 .build();
95     }
96 }