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