BUG-730 : added tests for pcep-spi.
[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 org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
27
28 public class AbstractObjectWithTlvsTest {
29
30     private Tlv tlv;
31
32     @Mock
33     private TlvRegistry tlvRegistry;
34
35     private class Abs extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36
37         protected Abs(TlvRegistry tlvReg) {
38             super(tlvReg);
39         }
40
41         @Override
42         public Object parseObject(ObjectHeader header, ByteBuf buffer) throws PCEPDeserializerException {
43             return null;
44         }
45
46         @Override
47         public void serializeObject(Object object, ByteBuf buffer) {
48         }
49
50         @Override
51         public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
52             builder.setOfList((OfList) tlv);
53         }
54     };
55
56     @Before
57     public void setUp() throws PCEPDeserializerException {
58         MockitoAnnotations.initMocks(this);
59         this.tlv = new OfListBuilder().setCodes(Lists.newArrayList(new OfId(10))).build();
60         Mockito.doNothing().when(this.tlvRegistry).serializeTlv(Mockito.any(Tlv.class), Mockito.any(ByteBuf.class));
61         Mockito.doReturn(this.tlv).when(this.tlvRegistry).parseTlv(4, Unpooled.wrappedBuffer(new byte[] { 5, 6 }));
62     }
63
64     @Test
65     public void testParseTlvs() throws PCEPDeserializerException {
66         Abs a = new Abs(this.tlvRegistry);
67         ByteBuf buffer = Unpooled.buffer();
68         a.serializeTlv(this.tlv, buffer);
69
70         Mockito.verify(this.tlvRegistry, Mockito.only()).serializeTlv(Mockito.any(Tlv.class), Mockito.any(ByteBuf.class));;
71
72         TlvsBuilder b = new TlvsBuilder();
73         a.parseTlvs(b, Unpooled.wrappedBuffer(new byte[] { 0, 4, 0, 2, 5, 6, 0, 0 }));
74
75         assertEquals(this.tlv, b.getOfList());
76     }
77 }