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