65754a48916592136287954ecf86c166c913a8f6
[groupbasedpolicy.git] / groupbasedpolicy / src / test / java / org / opendaylight / groupbasedpolicy / resolver / PolicyResolverTest.java
1 package org.opendaylight.groupbasedpolicy.resolver;
2
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
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;
22
23 import com.google.common.base.Optional;
24
25 public class PolicyResolverTest extends GbpDataBrokerTest {
26
27     private static final TenantId TENANT_ID_1 = new TenantId("tenant_1");
28
29     private DataBroker dataProvider;
30     private PolicyResolver policyResolver;
31
32     @Before
33     public void init() {
34         dataProvider = getDataBroker();
35         policyResolver = new PolicyResolver(dataProvider);
36     }
37
38     @After
39     public void teardown() throws Exception {
40         policyResolver.close();
41     }
42
43     @Test
44     public void testConstructor() throws Exception {
45         PolicyResolver other = new PolicyResolver(dataProvider);
46         other.close();
47     }
48
49     @Test
50     public void testUpdateTenant() throws Exception {
51         PolicyResolver spyPolicyResolver = Mockito.spy(policyResolver);
52         Mockito.when(spyPolicyResolver.isPolicyValid(Mockito.any(Policy.class))).thenReturn(true);
53
54         WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
55         wTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Tenants.class),
56                 new TenantsBuilder().build());
57         wTx.submit().get();
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());
65     }
66
67     @Test
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());
75     }
76
77     @Test
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());
87     }
88
89 }