Migrate tests away from Optional.get()
[bgpcep.git] / bgp / openconfig-rp-statement / src / test / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / SetCommunityTest.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.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
13
14 import java.util.List;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
19 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
20 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
21 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
22 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.CommunitiesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
28 import org.opendaylight.yangtools.yang.common.Uint16;
29
30 public class SetCommunityTest extends AbstractStatementRegistryConsumerTest {
31     private final Attributes multipleCom = new AttributesBuilder().setCommunities(List.of(
32             new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("65")).setSemantics(Uint16.valueOf(10))
33                 .build(),
34             new CommunitiesBuilder().setAsNumber(AsNumber.getDefaultInstance("66")).setSemantics(Uint16.valueOf(11))
35                 .build()
36     )).build();
37     private final Attributes emptyCom = new AttributesBuilder().setCommunities(List.of()).build();
38     @Mock
39     private BGPRouteEntryExportParameters exportParameters;
40     private List<Statement> basicStatements;
41     private PolicyRIBBaseParametersImpl baseAttributes;
42
43     @Before
44     @Override
45     public void setUp() throws Exception {
46         super.setUp();
47         basicStatements = loadStatement("set-community-statements-test");
48         baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
49         doReturn(PeerRole.Ibgp).when(exportParameters).getFromPeerRole();
50     }
51
52     @Test
53     public void testInlineAdd() {
54         Statement statement = basicStatements.stream()
55                 .filter(st -> st.getName().equals("set-community-inline-add-test")).findFirst().orElseThrow();
56         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
57                 new AttributesBuilder().build());
58         RouteAttributeContainer result = statementRegistry.applyExportStatement(
59                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
60
61         assertEquals(multipleCom, result.getAttributes());
62     }
63
64     @Test
65     public void testInlineReplace() {
66         Statement statement = basicStatements.stream()
67                 .filter(st -> st.getName().equals("set-community-inline-replace-test")).findFirst().orElseThrow();
68         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
69                 new AttributesBuilder().build());
70         RouteAttributeContainer result = statementRegistry.applyExportStatement(
71                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
72
73         assertEquals(multipleCom, result.getAttributes());
74     }
75
76     @Test
77     public void testInlineRemove() {
78         Statement statement = basicStatements.stream()
79                 .filter(st -> st.getName().equals("set-community-inline-remove-test")).findFirst().orElseThrow();
80
81         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(multipleCom);
82         RouteAttributeContainer result = statementRegistry.applyExportStatement(
83                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
84
85         assertEquals(emptyCom, result.getAttributes());
86     }
87
88     @Test
89     public void testReferenceAdd() {
90         Statement statement = basicStatements.stream()
91                 .filter(st -> st.getName().equals("set-community-reference-add-test")).findFirst().orElseThrow();
92         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
93                 new AttributesBuilder().build());
94         RouteAttributeContainer result = statementRegistry.applyExportStatement(
95                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
96
97         assertEquals(multipleCom, result.getAttributes());
98     }
99
100     @Test
101     public void testReferenceReplace() {
102         Statement statement = basicStatements.stream()
103                 .filter(st -> st.getName().equals("set-community-reference-replace-test")).findFirst().orElseThrow();
104         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
105                 new AttributesBuilder().build());
106         RouteAttributeContainer result = statementRegistry.applyExportStatement(
107                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
108
109         assertEquals(multipleCom, result.getAttributes());
110     }
111
112     @Test
113     public void testReferenceRemove() {
114         Statement statement = basicStatements.stream()
115                 .filter(st -> st.getName().equals("set-community-reference-remove-test")).findFirst().orElseThrow();
116
117         RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(multipleCom);
118         RouteAttributeContainer result = statementRegistry.applyExportStatement(
119                 baseAttributes, IPV4UNICAST.VALUE, exportParameters, attributeContainer, statement);
120
121         assertEquals(emptyCom, result.getAttributes());
122     }
123 }