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