273965e2ee2d9fb70eb4f039e7d317864ff8f0f2
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleRROSubobjectRegistryTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.rsvp.parser.spi.pojo;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
25 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
26 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
31
32 public class SimpleRROSubobjectRegistryTest {
33
34     private final int subObjectTypeOne = 1;
35     private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
36     private final SimpleRROSubobjectRegistry simpleRROSubobjectRegistry = new SimpleRROSubobjectRegistry();
37     @Mock
38     private RROSubobjectParser rroSubobjectParser;
39     @Mock
40     private RROSubobjectSerializer rroSubobjectSerializer;
41
42
43     @Before
44     public void setUp() throws RSVPParsingException {
45         MockitoAnnotations.initMocks(this);
46         this.simpleRROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this.rroSubobjectParser);
47         Mockito.doReturn(new SubobjectContainerBuilder().build()).when(this.rroSubobjectParser).parseSubobject(this.input);
48         final ArgumentCaptor<SubobjectContainer> arg = ArgumentCaptor.forClass(SubobjectContainer.class);
49         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
50         Mockito.doAnswer(invocation -> {
51             final Object[] args = invocation.getArguments();
52             ((ByteBuf) args[1]).writeBoolean(Boolean.TRUE);
53             return null;
54         }).when(this.rroSubobjectSerializer).serializeSubobject(arg.capture(), bufArg.capture());
55     }
56
57     @Test
58     public void testUnrecognizedType() throws RSVPParsingException {
59         final int wrongType = 99;
60         assertNull(this.simpleRROSubobjectRegistry.parseSubobject(wrongType, this.input));
61         final ByteBuf output = Unpooled.EMPTY_BUFFER;
62         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
63             LabelCaseBuilder().build()).build();
64         this.simpleRROSubobjectRegistry.serializeSubobject(container, output);
65         assertEquals(0, output.readableBytes());
66     }
67
68     @Test(expected = IllegalArgumentException.class)
69     public void testParseWrongType() throws RSVPParsingException {
70         final int wrongType = 65536;
71         this.simpleRROSubobjectRegistry.parseSubobject(wrongType, this.input);
72     }
73
74     @Test(expected = IllegalArgumentException.class)
75     public void testRegisterWrongType() {
76         final int wrongType = 65536;
77         this.simpleRROSubobjectRegistry.registerSubobjectParser(wrongType, this.rroSubobjectParser);
78     }
79
80     @Test
81     public void testParserRegistration() throws RSVPParsingException {
82         assertNotNull(this.simpleRROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this
83             .rroSubobjectParser));
84         assertNotNull(this.simpleRROSubobjectRegistry.parseSubobject(this.subObjectTypeOne, this.input));
85     }
86
87     @Test
88     public void testSerializerRegistration() {
89         assertNotNull(this.simpleRROSubobjectRegistry.registerSubobjectSerializer(LabelCase.class, this
90             .rroSubobjectSerializer));
91         final ByteBuf output = Unpooled.buffer();
92         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
93             LabelCaseBuilder().build()).build();
94         this.simpleRROSubobjectRegistry.serializeSubobject(container, output);
95         assertEquals(1, output.readableBytes());
96     }
97
98 }