2 * Copyright (c) 2015 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
9 package org.opendaylight.protocol.rsvp.parser.spi.pojo;
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;
32 public class SimpleRROSubobjectRegistryTest {
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();
38 private RROSubobjectParser rroSubobjectParser;
40 private RROSubobjectSerializer rroSubobjectSerializer;
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(new Answer<Object>() {
52 public Object answer(final InvocationOnMock invocation) throws Throwable {
53 final Object[] args = invocation.getArguments();
54 ((ByteBuf) args[1]).writeBoolean(Boolean.TRUE);
57 }).when(this.rroSubobjectSerializer).serializeSubobject(arg.capture(), bufArg.capture());
61 public void testUnrecognizedType() throws RSVPParsingException {
62 final int wrongType = 99;
63 assertNull(this.simpleRROSubobjectRegistry.parseSubobject(wrongType, this.input));
64 final ByteBuf output = Unpooled.EMPTY_BUFFER;
65 final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
66 LabelCaseBuilder().build()).build();
67 this.simpleRROSubobjectRegistry.serializeSubobject(container, output);
68 assertEquals(0, output.readableBytes());
71 @Test(expected = IllegalArgumentException.class)
72 public void testParseWrongType() throws RSVPParsingException {
73 final int wrongType = 65536;
74 this.simpleRROSubobjectRegistry.parseSubobject(wrongType, this.input);
77 @Test(expected = IllegalArgumentException.class)
78 public void testRegisterWrongType() {
79 final int wrongType = 65536;
80 this.simpleRROSubobjectRegistry.registerSubobjectParser(wrongType, this.rroSubobjectParser);
84 public void testParserRegistration() throws RSVPParsingException {
85 assertNotNull(this.simpleRROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this
86 .rroSubobjectParser));
87 assertNotNull(this.simpleRROSubobjectRegistry.parseSubobject(this.subObjectTypeOne, this.input));
91 public void testSerializerRegistration() {
92 assertNotNull(this.simpleRROSubobjectRegistry.registerSubobjectSerializer(LabelCase.class, this
93 .rroSubobjectSerializer));
94 final ByteBuf output = Unpooled.buffer();
95 final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
96 LabelCaseBuilder().build()).build();
97 this.simpleRROSubobjectRegistry.serializeSubobject(container, output);
98 assertEquals(1, output.readableBytes());