2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao;
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;
21 * Test for {@link SimpleCachedDaoEPForwardingTemplateImpl}.
23 public class SimpleCachedDaoEPForwardingTemplateImplTest {
25 private static final IpPrefix IP_PREFIX_1 = buildIpPrefix("1.2.3.0/24");
28 private static final IpPrefix IP_PREFIX_2 = buildIpPrefix("1.2.3.4/32");
30 private SimpleCachedDaoEPForwardingTemplateImpl dao;
33 public void setUp() throws Exception {
34 dao = new SimpleCachedDaoEPForwardingTemplateImpl();
35 Assert.assertTrue(dao.isEmpty());
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));
43 Assert.assertEquals(2, Iterables.size(dao.values()));
46 private EndpointForwardingTemplateBySubnet buildValue(final IpPrefix ipPrefix) {
47 return new EndpointForwardingTemplateBySubnetBuilder()
48 .setIpPrefix(ipPrefix)
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());
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());
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());
70 private static IpPrefix buildIpPrefix(final String ipv4PrefixValue) {
71 return new IpPrefix(new Ipv4Prefix(ipv4PrefixValue));
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));
79 Assert.assertEquals(2, Iterables.size(dao.values()));
80 dao.invalidateCache();
81 Assert.assertTrue(dao.isEmpty());