Migrate tests away from Optional.get()
[bgpcep.git] / bgp / extensions / route-target / src / test / java / org / opendaylight / protocol / bgp / route / targetcontrain / impl / route / policy / ClientAttributePrependHandlerTest.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.route.targetcontrain.impl.route.policy;
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.Collections;
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.spi.AbstractStatementRegistryTest;
20 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
21 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
22 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
23 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.activators.StatementActivator;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint32;
35
36 public class ClientAttributePrependHandlerTest extends AbstractStatementRegistryTest {
37     @Mock
38     private BGPRouteEntryExportParameters exportParameters;
39     private List<Statement> basicStatements;
40     @Mock
41     private RouteEntryBaseAttributes baseAttributes;
42
43     @Before
44     @Override
45     public void setUp() throws Exception {
46         super.setUp();
47         final StatementActivator act = new StatementActivator();
48         act.start(statementRegistry);
49         basicStatements = loadStatement("basic-statements-test");
50         doReturn(CLUSTER).when(baseAttributes).getClusterId();
51         doReturn(LOCAL_AS).when(baseAttributes).getLocalAs();
52         doReturn(IPV4).when(baseAttributes).getOriginatorId();
53     }
54
55
56     @Test
57     public void testPreprendClientAttribute() {
58         Statement statement = basicStatements.stream()
59                 .filter(st -> st.getName().equals("client-attribute-append-test")).findFirst().orElseThrow();
60         final Attributes att = new AttributesBuilder()
61                 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
62                         .setGlobal(IPV4).build()).build())
63                 .build();
64         final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(att);
65         doReturn(Collections.emptyList()).when(exportParameters).getClientRouteTargetContrainCache();
66
67         RouteAttributeContainer result = statementRegistry.applyExportStatement(
68                 baseAttributes,
69                 IPV4UNICAST.VALUE,
70                 exportParameters,
71                 attributeContainer,
72                 statement);
73         assertEquals(att, result.getAttributes());
74
75         final Attributes expected = new AttributesBuilder()
76                 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
77                         .setGlobal(new Ipv4AddressNoZone("2.2.2.2")).build()).build())
78                 .build();
79         final String rk = "testRoute";
80         final Route rtRoute = new RouteTargetConstrainRouteBuilder()
81                 .setRouteKey(rk)
82                 .setPathId(new PathId(Uint32.ZERO))
83                 .setAttributes(expected)
84                 .build();
85         doReturn(Collections.singletonList(rtRoute)).when(exportParameters).getClientRouteTargetContrainCache();
86         doReturn(rk).when(exportParameters).getRouteKey();
87
88         result = statementRegistry.applyExportStatement(
89                 baseAttributes,
90                 IPV4UNICAST.VALUE,
91                 exportParameters,
92                 attributeContainer,
93                 statement);
94         assertEquals(expected, result.getAttributes());
95     }
96
97 }