Migrate tests away from Optional.get()
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / MatchBgpNeighborSetTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.openconfig.routing.policy.statement;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
14
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
21 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
22 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
23 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
26
27 public class MatchBgpNeighborSetTest extends AbstractStatementRegistryConsumerTest {
28     @Mock
29     private BGPRouteEntryExportParameters exportParameters;
30     private List<Statement> basicStatements;
31     private PolicyRIBBaseParametersImpl baseAttributes;
32
33     @Before
34     @Override
35     public void setUp() throws Exception {
36         super.setUp();
37         basicStatements = loadStatement("bgp-neighbor-statements-test");
38         baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
39     }
40
41     @Test
42     public void testMatchFromBgpNeighborAny() {
43         Statement statement = basicStatements.stream()
44                 .filter(st -> st.getName().equals("reject-from-neighbor-test")).findFirst().orElseThrow();
45         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
46                 new AttributesBuilder().build());
47
48         doReturn(new PeerId("bgp://42.42.42.42")).when(exportParameters).getFromPeerId();
49
50         RouteAttributeContainer result = statementRegistry.applyExportStatement(
51                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
52         assertNotNull(result.getAttributes());
53
54
55         doReturn(new PeerId("bgp://127.0.0.1")).when(exportParameters).getFromPeerId();
56         result = statementRegistry.applyExportStatement(
57                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
58         assertNull(result.getAttributes());
59     }
60
61     @Test
62     public void testMatchFromBgpNeighborInvert() {
63         Statement statement = basicStatements.stream()
64                 .filter(st -> st.getName().equals("reject-from-neighbor-invert-test")).findFirst().orElseThrow();
65         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
66                 new AttributesBuilder().build());
67
68         doReturn(new PeerId("bgp://42.42.42.42")).when(exportParameters).getFromPeerId();
69
70         RouteAttributeContainer result = statementRegistry.applyExportStatement(
71                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
72         assertNull(result.getAttributes());
73
74         doReturn(new PeerId("bgp://127.0.0.1")).when(exportParameters).getFromPeerId();
75         result = statementRegistry.applyExportStatement(
76                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
77         assertNotNull(result.getAttributes());
78     }
79
80     @Test
81     public void testMatchToBgpNeighborAny() {
82         Statement statement = basicStatements.stream()
83                 .filter(st -> st.getName().equals("reject-to-neighbor-test")).findFirst().orElseThrow();
84         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
85                 new AttributesBuilder().build());
86
87         doReturn(new PeerId("bgp://127.0.0.2")).when(exportParameters).getFromPeerId();
88         doReturn(new PeerId("bgp://42.42.42.42")).when(exportParameters).getToPeerId();
89
90         RouteAttributeContainer result = statementRegistry.applyExportStatement(
91                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
92         assertNotNull(result.getAttributes());
93
94
95         doReturn(new PeerId("bgp://127.0.0.1")).when(exportParameters).getToPeerId();
96         result = statementRegistry.applyExportStatement(
97                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
98         assertNull(result.getAttributes());
99     }
100
101     @Test
102     public void testMatchToBgpNeighborInvert() {
103         Statement statement = basicStatements.stream()
104                 .filter(st -> st.getName().equals("reject-to-neighbor-invert-test")).findFirst().orElseThrow();
105         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
106                 new AttributesBuilder().build());
107
108         doReturn(new PeerId("bgp://127.0.0.2")).when(exportParameters).getFromPeerId();
109         doReturn(new PeerId("bgp://42.42.42.42")).when(exportParameters).getToPeerId();
110
111         RouteAttributeContainer result = statementRegistry.applyExportStatement(
112                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
113         assertNull(result.getAttributes());
114
115         doReturn(new PeerId("bgp://127.0.0.1")).when(exportParameters).getToPeerId();
116         result = statementRegistry.applyExportStatement(
117                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
118         assertNotNull(result.getAttributes());
119     }
120 }