49cec621aebc32240cea39ee3fe59a39d1aadf54
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ClusterIdAttributeParserTest.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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ClusterIdBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
26
27 public class ClusterIdAttributeParserTest {
28     private static final byte[] clusterIdBytes = {(byte) 0x80, (byte) 0x0A, (byte) 0x08,
29         (byte) 0xC0, (byte) 0xA8, (byte) 0x1, (byte) 0x1,
30         (byte) 0xC0, (byte) 0xA8, (byte) 0x1, (byte) 0x2};
31     ClusterIdAttributeParser parser;
32
33     @Before
34     public void setUp() {
35         this.parser = new ClusterIdAttributeParser();
36     }
37
38     @Test
39     public void testParserAttribute() throws Exception {
40         final List<ClusterIdentifier> list = Lists.newArrayList();
41         final Ipv4Address ip1 = new Ipv4Address("192.168.1.1");
42         final Ipv4Address ip2 = new Ipv4Address("192.168.1.2");
43         list.add(new ClusterIdentifier(ip1));
44         list.add(new ClusterIdentifier(ip2));
45         final Attributes clusterId = new AttributesBuilder().setClusterId(new ClusterIdBuilder().setCluster
46             (list).build()).build();
47
48
49         final ByteBuf output = Unpooled.buffer();
50         this.parser.serializeAttribute(clusterId, output);
51
52         assertArrayEquals(clusterIdBytes, ByteArray.getAllBytes(output));
53
54         AttributesBuilder clusterIdOutput = new AttributesBuilder();
55         this.parser.parseAttribute(Unpooled.wrappedBuffer(ByteArray.cutBytes(clusterIdBytes, 3)), clusterIdOutput);
56         assertEquals(clusterId, clusterIdOutput.build());
57     }
58
59     @Test
60     public void testParseEmptyListAttribute() {
61         final List<ClusterIdentifier> list = Lists.newArrayList();
62         final Attributes clusterId = new AttributesBuilder().setClusterId(new ClusterIdBuilder().setCluster
63             (list).build()).build();
64         final ByteBuf output = Unpooled.buffer();
65         this.parser.serializeAttribute(clusterId, output);
66         assertEquals(Unpooled.buffer(), output);
67     }
68
69     @Test
70     public void testParseEmptyAttribute() {
71         final Attributes clusterId = new AttributesBuilder().setClusterId(new ClusterIdBuilder().build()).build();
72         final ByteBuf output = Unpooled.buffer();
73         this.parser.serializeAttribute(clusterId, output);
74         assertEquals(Unpooled.buffer(), output);
75     }
76 }