417f62764c06f43aa8ef59c7cfa63984c47c0f73
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleEROSubobjectRegistryTest.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.EROSubobjectParser;
25 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
26 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
31
32 public class SimpleEROSubobjectRegistryTest {
33     private final int subObjectTypeOne = 1;
34     private final ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
35     private final SimpleEROSubobjectRegistry simpleEROSubobjectRegistry = new SimpleEROSubobjectRegistry();
36     @Mock
37     private EROSubobjectParser rroSubobjectParser;
38     @Mock
39     private EROSubobjectSerializer rroSubobjectSerializer;
40
41
42     @Before
43     public void setUp() throws RSVPParsingException {
44         MockitoAnnotations.initMocks(this);
45         this.simpleEROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this.rroSubobjectParser);
46         Mockito.doReturn(new SubobjectContainerBuilder().build()).when(this.rroSubobjectParser).parseSubobject(this.input, false);
47         final ArgumentCaptor<SubobjectContainer> arg = ArgumentCaptor.forClass(SubobjectContainer.class);
48         final ArgumentCaptor<ByteBuf> bufArg = ArgumentCaptor.forClass(ByteBuf.class);
49         Mockito.doAnswer(invocation -> {
50             final Object[] args = invocation.getArguments();
51             ((ByteBuf) args[1]).writeBoolean(Boolean.TRUE);
52             return null;
53         }).when(this.rroSubobjectSerializer).serializeSubobject(arg.capture(), bufArg.capture());
54     }
55
56     @Test
57     public void testUnrecognizedType() throws RSVPParsingException {
58         final int wrongType = 99;
59         assertNull(this.simpleEROSubobjectRegistry.parseSubobject(wrongType, this.input, false));
60         final ByteBuf output = Unpooled.EMPTY_BUFFER;
61         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
62             LabelCaseBuilder().build()).build();
63         this.simpleEROSubobjectRegistry.serializeSubobject(container, output);
64         assertEquals(0, output.readableBytes());
65     }
66
67     @Test(expected = IllegalArgumentException.class)
68     public void testParseWrongType() throws RSVPParsingException {
69         final int wrongType = 65536;
70         this.simpleEROSubobjectRegistry.parseSubobject(wrongType, this.input, false);
71     }
72
73     @Test(expected = IllegalArgumentException.class)
74     public void testRegisterWrongType() {
75         final int wrongType = 65536;
76         this.simpleEROSubobjectRegistry.registerSubobjectParser(wrongType, this.rroSubobjectParser);
77     }
78
79     @Test
80     public void testParserRegistration() throws RSVPParsingException {
81         assertNotNull(this.simpleEROSubobjectRegistry.registerSubobjectParser(this.subObjectTypeOne, this
82             .rroSubobjectParser));
83         assertNotNull(this.simpleEROSubobjectRegistry.parseSubobject(this.subObjectTypeOne, this.input, false));
84     }
85
86     @Test
87     public void testSerializerRegistration() {
88         assertNotNull(this.simpleEROSubobjectRegistry.registerSubobjectSerializer(LabelCase.class, this
89             .rroSubobjectSerializer));
90         final SubobjectContainer container = new SubobjectContainerBuilder().setSubobjectType(new
91             LabelCaseBuilder().build()).build();
92         final ByteBuf output = Unpooled.buffer();
93         this.simpleEROSubobjectRegistry.serializeSubobject(container, output);
94         assertEquals(1, output.readableBytes());
95     }
96
97 }