Reduce number of parameters for path selection
[bgpcep.git] / bgp / path-selection-mode / src / test / java / org / opendaylight / protocol / bgp / mode / impl / add / n / paths / SimpleRouteEntryTest.java
1 /*
2  * Copyright (c) 2015 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.protocol.bgp.mode.impl.add.n.paths;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14
15 import java.util.Map;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.mode.impl.AbstractRouteEntryTest;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20
21 public final class SimpleRouteEntryTest extends AbstractRouteEntryTest {
22     private static final long N_PATHS = 2;
23     private SimpleRouteEntry testBARE;
24
25     @Before
26     public void setUp() {
27         super.setUp();
28     }
29
30     @Test
31     public void testSimpleRouteEntry() throws Exception {
32         this.testBARE = (SimpleRouteEntry) new AddPathBestNPathSelection(N_PATHS)
33                 .createRouteEntry(false);
34         testWriteEmptyBestPath();
35         testAddRouteSelectBestAndWriteOnDS();
36         testRewriteSameRoute();
37         testInitializePeerWithExistentRoute();
38         testRemoveRoute();
39     }
40
41     /**
42      * Add non Add Path Route.
43      */
44     @Test(expected = NullPointerException.class)
45     public void testAddRouteSelectBestAndWriteOnDSs() {
46         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
47     }
48
49     private void testWriteEmptyBestPath() {
50         doReturn(ROUTE_ID_PA).when(this.entryInfo).getRouteId();
51
52         this.testBARE.initializeBestPaths(this.entryDep, this.entryInfo, this.peg, this.tx);
53         assertEquals(0, this.yiichanges.size());
54     }
55
56     /**
57      * Add AddPath Route.
58      */
59     private void testAddRouteSelectBestAndWriteOnDS() {
60         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(),
61                 this.attributes);
62         assertFalse(this.testBARE.isEmpty());
63         assertTrue(this.testBARE.selectBest(AS));
64         this.testBARE.updateBestPaths(this.entryDep, ROUTE_ID_PA_ADD_PATH, this.tx);
65         final Map<YangInstanceIdentifier, Long> yiiCount = collectInfo();
66         assertEquals(3, yiiCount.size());
67         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
68         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYii));
69         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutAttYii));
70     }
71
72     private void testRewriteSameRoute() {
73         this.testBARE.addRoute(ROUTER_ID, REMOTE_PATH_ID, this.ribSupport.routeAttributesIdentifier(), this.attributes);
74         assertFalse(this.testBARE.selectBest(AS));
75     }
76
77     private void testInitializePeerWithExistentRoute() {
78         assertEquals(3, this.yiichanges.size());
79         doReturn(ROUTE_ID_PA_ADD_PATH).when(this.entryInfo).getRouteId();
80
81         this.testBARE.initializeBestPaths(this.entryDep, this.entryInfo, this.peg, this.tx);
82         assertEquals(5, this.yiichanges.size());
83         final Map<YangInstanceIdentifier, Long> yiiCount = collectInfo();
84         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYiiPeer2));
85         assertEquals(1, (long) yiiCount.get(this.routeAddRiboutYiiPeer2));
86     }
87
88     private void testRemoveRoute() {
89         Map<YangInstanceIdentifier, Long> yiiCount = collectInfo();
90         assertEquals(5, yiiCount.size());
91         assertEquals(1, (long) yiiCount.get(this.routePaAddPathYii));
92         assertTrue(this.testBARE.removeRoute(ROUTER_ID, REMOTE_PATH_ID));
93         assertTrue(this.testBARE.selectBest(AS));
94         this.testBARE.updateBestPaths(this.entryDep, ROUTE_ID_PA_ADD_PATH, this.tx);
95         yiiCount = collectInfo();
96         assertEquals(2, yiiCount.size());
97         assertFalse(yiiCount.containsKey(this.routePaAddPathYii));
98         assertFalse(yiiCount.containsKey(this.routeAddRiboutYii));
99     }
100 }