1 package org.opendaylight.groupbasedpolicy.resolver;
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.mockito.Mockito;
8 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
9 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
10 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.groupbasedpolicy.test.GbpDataBrokerTest;
13 import org.opendaylight.groupbasedpolicy.util.IidFactory;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.TenantsBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Policy;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.PolicyBuilder;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import com.google.common.base.Optional;
25 public class PolicyResolverTest extends GbpDataBrokerTest {
27 private static final TenantId TENANT_ID_1 = new TenantId("tenant_1");
29 private DataBroker dataProvider;
30 private PolicyResolver policyResolver;
34 dataProvider = getDataBroker();
35 policyResolver = new PolicyResolver(dataProvider);
39 public void teardown() throws Exception {
40 policyResolver.close();
44 public void testConstructor() throws Exception {
45 PolicyResolver other = new PolicyResolver(dataProvider);
50 public void testUpdateTenant() throws Exception {
51 PolicyResolver spyPolicyResolver = Mockito.spy(policyResolver);
52 Mockito.when(spyPolicyResolver.isPolicyValid(Mockito.any(Policy.class))).thenReturn(true);
54 WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
55 wTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Tenants.class),
56 new TenantsBuilder().build());
58 Tenant tenant = new TenantBuilder().setId(TENANT_ID_1).setPolicy(new PolicyBuilder().build()).build();
59 spyPolicyResolver.updateTenant(TENANT_ID_1, tenant);
60 ReadOnlyTransaction rTx = getDataBroker().newReadOnlyTransaction();
61 Optional<Tenant> potentialTenant =
62 rTx.read(LogicalDatastoreType.OPERATIONAL, IidFactory.tenantIid(TENANT_ID_1)).get();
63 Assert.assertTrue(potentialTenant.isPresent());
64 Assert.assertEquals(tenant.getId(), potentialTenant.get().getId());
68 public void testUpdateTenant_noPolicy() throws Exception {
69 Tenant tenant = new TenantBuilder().setId(TENANT_ID_1).build();
70 policyResolver.updateTenant(TENANT_ID_1, tenant);
71 ReadOnlyTransaction rTx = getDataBroker().newReadOnlyTransaction();
72 Optional<Tenant> potentialTenant =
73 rTx.read(LogicalDatastoreType.OPERATIONAL, IidFactory.tenantIid(TENANT_ID_1)).get();
74 Assert.assertFalse(potentialTenant.isPresent());
78 public void testUpdateTenant_nullTenant() throws Exception {
79 Tenant tenant = new TenantBuilder().setId(TENANT_ID_1).build();
80 InstanceIdentifier<Tenant> tenantIid = IidFactory.tenantIid(TENANT_ID_1);
81 WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
82 wTx.put(LogicalDatastoreType.OPERATIONAL, tenantIid, tenant);
83 policyResolver.updateTenant(TENANT_ID_1, null);
84 ReadOnlyTransaction rTx = getDataBroker().newReadOnlyTransaction();
85 Optional<Tenant> potentialTenant = rTx.read(LogicalDatastoreType.OPERATIONAL, tenantIid).get();
86 Assert.assertFalse(potentialTenant.isPresent());