7aa5458201d1b57204740463226013a67a82d6df
[groupbasedpolicy.git] / sxp-integration / sxp-ep-provider / src / test / java / org / opendaylight / groupbasedpolicy / sxp / ep / provider / impl / dao / SimpleCachedDaoEPForwardingTemplateImplTest.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.ep.provider.impl.dao;
10
11 import com.google.common.collect.Iterables;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointForwardingTemplateBySubnet;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointForwardingTemplateBySubnetBuilder;
19
20 /**
21  * Test for {@link SimpleCachedDaoEPForwardingTemplateImpl}.
22  */
23 public class SimpleCachedDaoEPForwardingTemplateImplTest {
24
25     private static final IpPrefix IP_PREFIX_1 = buildIpPrefix("1.2.3.0/24");
26
27
28     private static final IpPrefix IP_PREFIX_2 = buildIpPrefix("1.2.3.4/32");
29
30     private SimpleCachedDaoEPForwardingTemplateImpl dao;
31
32     @Before
33     public void setUp() throws Exception {
34         dao = new SimpleCachedDaoEPForwardingTemplateImpl();
35         Assert.assertTrue(dao.isEmpty());
36     }
37
38     @Test
39     public void testUpdate() throws Exception {
40         dao.update(IP_PREFIX_1, buildValue(IP_PREFIX_1));
41         dao.update(IP_PREFIX_2, buildValue(IP_PREFIX_2));
42
43         Assert.assertEquals(2, Iterables.size(dao.values()));
44     }
45
46     private EndpointForwardingTemplateBySubnet buildValue(final IpPrefix ipPrefix) {
47         return new EndpointForwardingTemplateBySubnetBuilder()
48                 .setIpPrefix(ipPrefix)
49                 .build();
50     }
51
52     @Test
53     public void testFind() throws Exception {
54         final EndpointForwardingTemplateBySubnet value1 = buildValue(IP_PREFIX_1);
55         final EndpointForwardingTemplateBySubnet value2 = buildValue(IP_PREFIX_2);
56         dao.update(IP_PREFIX_1, value1);
57         dao.update(IP_PREFIX_2, value2);
58         Assert.assertFalse(dao.isEmpty());
59
60         Assert.assertTrue(dao.find(IP_PREFIX_1).isPresent());
61         Assert.assertEquals(value1, dao.find(IP_PREFIX_1).get());
62         Assert.assertTrue(dao.find(IP_PREFIX_2).isPresent());
63         Assert.assertEquals(value2, dao.find(IP_PREFIX_2).get());
64
65         final IpPrefix key = buildIpPrefix("1.2.3.1/32");
66         Assert.assertTrue(dao.find(key).isPresent());
67         Assert.assertEquals(value1, dao.find(key).get());
68     }
69
70     private static IpPrefix buildIpPrefix(final String ipv4PrefixValue) {
71         return new IpPrefix(new Ipv4Prefix(ipv4PrefixValue));
72     }
73
74     @Test
75     public void testInvalidateCache() throws Exception {
76         dao.update(IP_PREFIX_1, buildValue(IP_PREFIX_1));
77         dao.update(IP_PREFIX_2, buildValue(IP_PREFIX_2));
78
79         Assert.assertEquals(2, Iterables.size(dao.values()));
80         dao.invalidateCache();
81         Assert.assertTrue(dao.isEmpty());
82     }
83 }