2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.spi;
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;
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;
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class AbstractObjectWithTlvsTest {
42 private static final EnterpriseNumber EN = new EnterpriseNumber(0L);
46 private VendorInformationTlv viTlv;
49 private TlvRegistry tlvRegistry;
52 private VendorInformationTlvRegistry viTlvRegistry;
54 private class Abs extends AbstractObjectWithTlvsParser<TlvsBuilder> {
56 protected Abs(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
57 super(tlvReg, viTlvReg, 0, 0);
61 public Object parseObject(final ObjectHeader header, final ByteBuf buffer) {
66 public void serializeObject(final Object object, final ByteBuf buffer) {
70 public void addTlv(final TlvsBuilder builder, final Tlv newTlv) {
71 builder.setOfList((OfList) newTlv);
75 protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
76 builder.setVendorInformationTlv(tlvs);
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),
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 }));
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);
98 verify(this.tlvRegistry, only()).serializeTlv(any(Tlv.class), any(ByteBuf.class));
100 TlvsBuilder builder = new TlvsBuilder();
101 abs.parseTlvs(builder, Unpooled.wrappedBuffer(new byte[] { 0, 4, 0, 2, 5, 6, 0, 0 }));
103 assertEquals(this.tlv, builder.getOfList());
107 public void testParseVendorInformationTlv() throws PCEPDeserializerException {
108 final Abs parser = new Abs(this.tlvRegistry, this.viTlvRegistry);
109 final ByteBuf buffer = Unpooled.buffer();
111 parser.serializeVendorInformationTlvs(Lists.newArrayList(this.viTlv), buffer);
112 verify(this.viTlvRegistry, only()).serializeVendorInformationTlv(any(VendorInformationTlv.class),
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));