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 static org.junit.Assert.assertTrue;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
25 import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
26 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
29 public class SimpleLabelRegistryTest {
30 private final short ctype = 1;
31 private final SimpleLabelRegistry simpleLabelRegistry = new SimpleLabelRegistry();
32 private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
34 private LabelParser labelParser;
36 private LabelSerializer labelSerializer;
39 public void setUp() throws RSVPParsingException {
40 MockitoAnnotations.initMocks(this);
41 this.simpleLabelRegistry.registerLabelParser(this.ctype, this.labelParser);
42 this.simpleLabelRegistry.registerLabelSerializer(MockLabel.class, this.labelSerializer);
43 Mockito.doReturn(new MockLabel()).when(this.labelParser).parseLabel(this.input);
44 final ArgumentCaptor<LabelType> tlvArg = ArgumentCaptor.forClass(LabelType.class);
45 final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
46 Mockito.doNothing().when(this.labelSerializer).serializeLabel(Mockito.anyBoolean(), Mockito.anyBoolean(),
47 tlvArg.capture(), bufArg.capture());
51 public void testParserRegistration() {
52 assertNotNull(this.simpleLabelRegistry.registerLabelParser(this.ctype, this.labelParser));
56 public void testSerializerRegistration() {
57 assertNotNull(this.simpleLabelRegistry.registerLabelSerializer(MockLabelClass.class, this.labelSerializer));
61 public void testUnrecognizedType() throws RSVPParsingException {
62 final int wrongLabelType = 99;
63 assertNull(this.simpleLabelRegistry.parseLabel(wrongLabelType, this.input));
64 final ByteBuf output = Unpooled.EMPTY_BUFFER;
65 this.simpleLabelRegistry.serializeLabel(false, false, new MockLabel(), output);
66 assertEquals(0, output.readableBytes());
69 @Test(expected = IllegalArgumentException.class)
70 public void testParseWrongType() throws RSVPParsingException {
71 final int wrongType = 65536;
72 this.simpleLabelRegistry.parseLabel(wrongType, this.input);
75 @Test(expected = IllegalArgumentException.class)
76 public void testRegisterWrongType() {
77 final int wrongType = 65536;
78 this.simpleLabelRegistry.registerLabelParser(wrongType, this.labelParser);
82 public void testParseLabel() throws RSVPParsingException {
83 final LabelType output = this.simpleLabelRegistry.parseLabel(this.ctype, this.input);
84 assertNotNull(output);
85 assertTrue(output instanceof MockLabel);
87 final ByteBuf aggregator = Unpooled.EMPTY_BUFFER;
88 this.simpleLabelRegistry.serializeLabel(false, false, output, aggregator);
89 Mockito.verify(this.labelSerializer).serializeLabel(false, false, output, aggregator);
92 private final class MockLabelClass implements LabelType {
94 public Class<? extends LabelType> implementedInterface() {
95 return MockLabelClass.class;
99 private final class MockLabel implements LabelType {
101 public Class<? extends LabelType> implementedInterface() {
102 return MockLabel.class;