Bug-479: Implementation of Vendor-Information TLV
[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
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
29
30 public class AbstractObjectWithTlvsTest {
31
32     private Tlv tlv;
33
34     @Mock
35     private TlvRegistry tlvRegistry;
36
37     @Mock
38     private VendorInformationTlvRegistry viTlvRegistry;
39
40     private class Abs extends AbstractObjectWithTlvsParser<TlvsBuilder> {
41
42         protected Abs(TlvRegistry tlvReg, VendorInformationTlvRegistry viTlvReg) {
43             super(tlvReg, viTlvReg);
44         }
45
46         @Override
47         public Object parseObject(ObjectHeader header, ByteBuf buffer) throws PCEPDeserializerException {
48             return null;
49         }
50
51         @Override
52         public void serializeObject(Object object, ByteBuf buffer) {
53         }
54
55         @Override
56         public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
57             builder.setOfList((OfList) tlv);
58         }
59
60         @Override
61         protected void addVendorInformationTlvs(TlvsBuilder builder, List<VendorInformationTlv> tlvs) {
62             builder.setVendorInformationTlv(tlvs);
63         }
64     };
65
66     @Before
67     public void setUp() throws PCEPDeserializerException {
68         MockitoAnnotations.initMocks(this);
69         this.tlv = new OfListBuilder().setCodes(Lists.newArrayList(new OfId(10))).build();
70         Mockito.doNothing().when(this.tlvRegistry).serializeTlv(Mockito.any(Tlv.class), Mockito.any(ByteBuf.class));
71         Mockito.doReturn(this.tlv).when(this.tlvRegistry).parseTlv(4, Unpooled.wrappedBuffer(new byte[] { 5, 6 }));
72     }
73
74     @Test
75     public void testParseTlvs() throws PCEPDeserializerException {
76         Abs a = new Abs(this.tlvRegistry, this.viTlvRegistry);
77         ByteBuf buffer = Unpooled.buffer();
78         a.serializeTlv(this.tlv, buffer);
79
80         Mockito.verify(this.tlvRegistry, Mockito.only()).serializeTlv(Mockito.any(Tlv.class), Mockito.any(ByteBuf.class));;
81
82         TlvsBuilder b = new TlvsBuilder();
83         a.parseTlvs(b, Unpooled.wrappedBuffer(new byte[] { 0, 4, 0, 2, 5, 6, 0, 0 }));
84
85         assertEquals(this.tlv, b.getOfList());
86     }
87 }