201665afd3b59a8e0d2fca7d828213821070511f
[bgpcep.git] / pcep / spi / src / test / java / org / opendaylight / protocol / pcep / spi / AbstractObjectWithTlvsTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.protocol.pcep.spi;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.only;
15 import static org.mockito.Mockito.verify;
16
17 import com.google.common.collect.Lists;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.OfId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlvBuilder;
38
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class AbstractObjectWithTlvsTest {
41
42     private static final EnterpriseNumber EN = new EnterpriseNumber(0L);
43
44     private Tlv tlv;
45
46     private VendorInformationTlv viTlv;
47
48     @Mock
49     private TlvRegistry tlvRegistry;
50
51     @Mock
52     private VendorInformationTlvRegistry viTlvRegistry;
53
54     private class Abs extends AbstractObjectWithTlvsParser<TlvsBuilder> {
55
56         protected Abs(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
57             super(tlvReg, viTlvReg, 0, 0);
58         }
59
60         @Override
61         public Object parseObject(final ObjectHeader header, final ByteBuf buffer) {
62             return null;
63         }
64
65         @Override
66         public void serializeObject(final Object object, final ByteBuf buffer) {
67         }
68
69         @Override
70         public void addTlv(final TlvsBuilder builder, final Tlv newTlv) {
71             builder.setOfList((OfList) newTlv);
72         }
73
74         @Override
75         protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
76             builder.setVendorInformationTlv(tlvs);
77         }
78     }
79
80     @Before
81     public void setUp() throws PCEPDeserializerException {
82         this.tlv = new OfListBuilder().setCodes(Collections.singletonList(new OfId(10))).build();
83         this.viTlv = new VendorInformationTlvBuilder().setEnterpriseNumber(EN).build();
84         doNothing().when(this.viTlvRegistry).serializeVendorInformationTlv(any(VendorInformationTlv.class),
85             any(ByteBuf.class));
86         doReturn(Optional.of(this.viTlv)).when(this.viTlvRegistry).parseVendorInformationTlv(EN,
87             Unpooled.wrappedBuffer(new byte[0]));
88         doNothing().when(this.tlvRegistry).serializeTlv(any(Tlv.class), any(ByteBuf.class));
89         doReturn(this.tlv).when(this.tlvRegistry).parseTlv(4, Unpooled.wrappedBuffer(new byte[] { 5, 6 }));
90     }
91
92     @Test
93     public void testParseTlvs() throws PCEPDeserializerException {
94         Abs abs = new Abs(this.tlvRegistry, this.viTlvRegistry);
95         ByteBuf buffer = Unpooled.buffer();
96         abs.serializeTlv(this.tlv, buffer);
97
98         verify(this.tlvRegistry, only()).serializeTlv(any(Tlv.class), any(ByteBuf.class));
99
100         TlvsBuilder builder = new TlvsBuilder();
101         abs.parseTlvs(builder, Unpooled.wrappedBuffer(new byte[] { 0, 4, 0, 2, 5, 6, 0, 0 }));
102
103         assertEquals(this.tlv, builder.getOfList());
104     }
105
106     @Test
107     public void testParseVendorInformationTlv() throws PCEPDeserializerException {
108         final Abs parser = new Abs(this.tlvRegistry, this.viTlvRegistry);
109         final ByteBuf buffer = Unpooled.buffer();
110
111         parser.serializeVendorInformationTlvs(Lists.newArrayList(this.viTlv), buffer);
112         verify(this.viTlvRegistry, only()).serializeVendorInformationTlv(any(VendorInformationTlv.class),
113             any(ByteBuf.class));
114
115         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
116         parser.parseTlvs(tlvsBuilder, Unpooled.wrappedBuffer(
117             new byte[] { 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00 }));
118         assertEquals(this.viTlv, tlvsBuilder.getVendorInformationTlv().get(0));
119     }
120 }