Fix NPE message asserts of JDK15+
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / NextHopAttributeParserTest.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.parser.impl.message.update;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ServiceLoader;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
21 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
31
32 public class NextHopAttributeParserTest {
33
34     private static final byte[] IPV4_NEXT_HOP_BYTES = {
35         (byte) 0x40, (byte) 0x03, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
36     };
37     private static final byte[] IPV6_NEXT_HOP_BYTES = {
38         (byte) 0x40, (byte) 0x03, (byte) 0x20, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00,
39         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
40         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xFF, (byte) 0xFF,
41         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
42         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
43     };
44
45     private static final Attributes IPV4_RESULT = new AttributesBuilder()
46             .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
47                     .setGlobal(new Ipv4AddressNoZone("255.255.255.255"))
48                     .build()).build()).build();
49     private static final Attributes IPV6_RESULT = new AttributesBuilder()
50             .setCNextHop(new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
51                     .setGlobal(new Ipv6AddressNoZone("ffff::1"))
52                     .setLinkLocal(new Ipv6AddressNoZone("ffff::2"))
53                     .build()).build()).build();
54
55     private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class)
56         .findFirst().orElseThrow().getAttributeRegistry();
57
58     @Test
59     public void testIpv4AttributeParser() throws BGPParsingException, BGPDocumentedException {
60         final ByteBuf actual = Unpooled.buffer();
61         registry.serializeAttribute(IPV4_RESULT, actual);
62         assertArrayEquals(IPV4_NEXT_HOP_BYTES, ByteArray.getAllBytes(actual));
63
64         final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
65         assertEquals(IPV4_RESULT.getCNextHop(), attributeOut.getCNextHop());
66     }
67
68     @Test
69     public void testIpv6AttributeParser() throws BGPParsingException, BGPDocumentedException {
70         final ByteBuf actual = Unpooled.buffer();
71         registry.serializeAttribute(IPV6_RESULT, actual);
72         assertArrayEquals(IPV6_NEXT_HOP_BYTES, ByteArray.getAllBytes(actual));
73
74         final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
75         assertEquals(IPV6_RESULT.getCNextHop(), attributeOut.getCNextHop());
76     }
77
78     @Test
79     public void testParseEmptyIpv4Attribute() {
80         final String message = assertThrows(NullPointerException.class,
81             () -> registry.serializeAttribute(new AttributesBuilder()
82                 .setCNextHop(new Ipv4NextHopCaseBuilder().build())
83                 .build(), Unpooled.buffer()))
84             .getMessage();
85         assertEquals(npeString("Cannot invoke \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp"
86             + ".types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHop.getGlobal()\" because the return "
87             + "value of \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop"
88             + ".c.next.hop.Ipv4NextHopCase.getIpv4NextHop()\" is null"), message);
89     }
90
91     @Test
92     public void testParseEmptyIpv6Attribute() {
93         final String message = assertThrows(NullPointerException.class,
94             () -> registry.serializeAttribute(new AttributesBuilder()
95                 .setCNextHop(new Ipv6NextHopCaseBuilder().build())
96                 .build(), Unpooled.buffer()))
97             .getMessage();
98         assertEquals(npeString("Cannot invoke \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp"
99             + ".types.rev200120.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHop.getGlobal()\" because \"nextHop\" "
100             + "is null"), message);
101     }
102
103     // FIXME: remove this method once we require JDK17+
104     private static String npeString(final String helpfulString) {
105         return Runtime.getRuntime().version().feature() >= 15 ? helpfulString : null;
106     }
107 }