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