Bump MDSAL to 4.0.0
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / ServiceLoaderRSVPExtensionProviderContextTest.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
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.InvocationTargetException;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectParser;
22 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
23 import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
24 import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
25 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
26 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
27 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
28 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectParser;
29 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectSerializer;
30 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
31 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
36
37 public class ServiceLoaderRSVPExtensionProviderContextTest {
38
39     private final SimpleRSVPExtensionProviderContext context = (SimpleRSVPExtensionProviderContext)
40         ServiceLoaderRSVPExtensionProviderContext.getSingletonInstance();
41
42     private final RSVPTeObjectParser rsvpTeParser = Mockito.mock(RSVPTeObjectParser.class);
43     private final RSVPTeObjectSerializer rsvpTeSerializer = Mockito.mock(RSVPTeObjectSerializer.class);
44     private final RsvpTeObject parsedRsvpTeObj = Mockito.mock(RsvpTeObject.class);
45
46     private final XROSubobjectParser xroObjParser = Mockito.mock(XROSubobjectParser.class);
47     private final XROSubobjectSerializer xroObjSerializer = Mockito.mock(XROSubobjectSerializer.class);
48     private final SubobjectContainer subObj = Mockito.mock(SubobjectContainer.class);
49     private final SubobjectType subObjType = Mockito.mock(SubobjectType.class);
50
51     private final RROSubobjectParser rroParser = Mockito.mock(RROSubobjectParser.class);
52     private final RROSubobjectSerializer rroSerializer = Mockito.mock(RROSubobjectSerializer.class);
53     private final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route
54         .subobjects.list.SubobjectContainer rroSubObj = Mockito.mock(org.opendaylight.yang.gen.v1.urn.opendaylight
55         .params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer.class);
56     private final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route
57         .subobjects.SubobjectType rroSubObjType = Mockito.mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params
58         .xml.ns.yang.rsvp.rev150820.record.route.subobjects.SubobjectType.class);
59
60     private final EROSubobjectParser eroParser = Mockito.mock(EROSubobjectParser.class);
61     private final EROSubobjectSerializer eroSerializer = Mockito.mock(EROSubobjectSerializer.class);
62     private final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
63         .subobjects.list.SubobjectContainer eroSubObj = Mockito.mock(org.opendaylight.yang.gen.v1.urn.opendaylight
64         .params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer.class);
65
66     private final LabelParser labelParser = Mockito.mock(LabelParser.class);
67     private final LabelSerializer labelSerializer = Mockito.mock(LabelSerializer.class);
68     private final LabelType labelType = Mockito.mock(LabelType.class);
69
70     @Before
71     public void setUp() throws RSVPParsingException {
72         Mockito.doReturn(this.parsedRsvpTeObj).when(this.rsvpTeParser).parseObject(Mockito.any(ByteBuf.class));
73         Mockito.doReturn(RsvpTeObject.class).when(this.parsedRsvpTeObj).implementedInterface();
74         Mockito.doReturn("parsedRsvpTeObj").when(this.parsedRsvpTeObj).toString();
75         Mockito.doNothing().when(this.rsvpTeSerializer).serializeObject(Mockito.any(RsvpTeObject.class),
76             Mockito.any(ByteBuf.class));
77
78         Mockito.doReturn(this.subObj).when(this.xroObjParser).parseSubobject(Mockito.any(ByteBuf.class),
79             Mockito.any(Boolean.class));
80         Mockito.doReturn(this.subObjType).when(this.subObj).getSubobjectType();
81         Mockito.doReturn("SubobjectContainer").when(this.subObj).toString();
82         Mockito.doReturn(SubobjectType.class).when(this.subObjType).implementedInterface();
83         Mockito.doNothing().when(this.xroObjSerializer).serializeSubobject(Mockito.any(SubobjectContainer.class),
84             Mockito.any(ByteBuf.class));
85
86         Mockito.doReturn(this.rroSubObj).when(this.rroParser).parseSubobject(Mockito.any(ByteBuf.class));
87         Mockito.doReturn(this.rroSubObjType).when(this.rroSubObj).getSubobjectType();
88         Mockito.doReturn("SubobjectContainer").when(this.rroSubObj).toString();
89         Mockito.doReturn(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route
90             .subobjects.SubobjectType.class).when(this.rroSubObjType).implementedInterface();
91         Mockito.doNothing().when(this.rroSerializer).serializeSubobject(Mockito.any(org.opendaylight.yang.gen.v1.urn
92                 .opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer.class),
93             Mockito.any(ByteBuf.class));
94
95         Mockito.doReturn(this.eroSubObj).when(this.eroParser).parseSubobject(Mockito.any(ByteBuf.class),
96             Mockito.any(Boolean.class));
97         Mockito.doReturn(this.subObjType).when(this.eroSubObj).getSubobjectType();
98         Mockito.doReturn("EROSubobjectContainer").when(this.eroSubObj).toString();
99         Mockito.doReturn(SubobjectType.class).when(this.subObjType).implementedInterface();
100         Mockito.doNothing().when(this.eroSerializer).serializeSubobject(Mockito.any(org.opendaylight.yang.gen.v1.urn
101             .opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer.class),
102             Mockito.any(ByteBuf.class));
103
104         Mockito.doReturn(this.labelType).when(this.labelParser).parseLabel(Mockito.any(ByteBuf.class));
105         Mockito.doReturn(LabelType.class).when(this.labelType).implementedInterface();
106         Mockito.doReturn("LabelType").when(this.labelType).toString();
107         Mockito.doNothing().when(this.labelSerializer).serializeLabel(Mockito.anyBoolean(), Mockito.anyBoolean(),
108             Mockito.any(LabelType.class), Mockito.any(ByteBuf.class));
109     }
110
111     @Test
112     public void testReferenceCache() {
113         assertNotNull(this.context.getReferenceCache());
114     }
115
116     @Test
117     public void testServiceForRsvpObject() throws RSVPParsingException {
118         this.context.registerRsvpObjectParser(1, 1, this.rsvpTeParser);
119         final ByteBuf buffer = Unpooled.buffer();
120         assertEquals(this.parsedRsvpTeObj, this.context.getRsvpRegistry().parseRSPVTe(1, 1, buffer));
121         this.context.registerRsvpObjectSerializer(RsvpTeObject.class, this.rsvpTeSerializer);
122         this.context.getRsvpRegistry().serializeRSPVTe(this.parsedRsvpTeObj, buffer);
123         Mockito.verify(this.rsvpTeSerializer).serializeObject(Mockito.any(RsvpTeObject.class),
124             Mockito.any(ByteBuf.class));
125     }
126
127     @Test
128     public void testServiceForXROSubobject() throws RSVPParsingException {
129         this.context.registerXROSubobjectParser(2, this.xroObjParser);
130         final ByteBuf buffer = Unpooled.buffer();
131         assertEquals(this.subObj, this.context.getXROSubobjectHandlerRegistry().parseSubobject(2, buffer, false));
132         this.context.registerXROSubobjectSerializer(SubobjectType.class, this.xroObjSerializer);
133         this.context.getXROSubobjectHandlerRegistry().serializeSubobject(this.subObj, buffer);
134         Mockito.verify(this.xroObjSerializer).serializeSubobject(this.subObj, buffer);
135     }
136
137     @Test
138     public void testServiceForRROSubobject() throws RSVPParsingException {
139         this.context.registerRROSubobjectParser(3, this.rroParser);
140         final ByteBuf buffer = Unpooled.buffer();
141         assertEquals(this.rroSubObj, this.context.getRROSubobjectHandlerRegistry().parseSubobject(3, buffer));
142         this.context.registerRROSubobjectSerializer(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
143             .rsvp.rev150820.record.route.subobjects.SubobjectType.class, this.rroSerializer);
144         this.context.getRROSubobjectHandlerRegistry().serializeSubobject(this.rroSubObj, buffer);
145         Mockito.verify(this.rroSerializer).serializeSubobject(this.rroSubObj, buffer);
146     }
147
148     @Test
149     public void testServiceForEROSubobject() throws RSVPParsingException {
150         this.context.registerEROSubobjectParser(4, this.eroParser);
151         final ByteBuf buffer = Unpooled.buffer();
152         assertEquals(this.eroSubObj, this.context.getEROSubobjectHandlerRegistry().parseSubobject(4, buffer, false));
153         this.context.registerEROSubobjectSerializer(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
154             .rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType.class, this.eroSerializer);
155         this.context.getEROSubobjectHandlerRegistry().serializeSubobject(this.eroSubObj, buffer);
156         Mockito.verify(this.eroSerializer).serializeSubobject(this.eroSubObj, buffer);
157     }
158
159     @Test
160     public void testServiceForLabel() throws RSVPParsingException {
161         this.context.registerLabelParser(5, this.labelParser);
162         final ByteBuf buffer = Unpooled.buffer();
163         assertEquals(this.labelType, this.context.getLabelHandlerRegistry().parseLabel(5, buffer));
164         this.context.registerLabelSerializer(LabelType.class, this.labelSerializer);
165         this.context.getLabelHandlerRegistry().serializeLabel(false, false, this.labelType, buffer);
166         Mockito.verify(this.labelSerializer).serializeLabel(false, false, this.labelType, buffer);
167     }
168
169     @Test(expected = UnsupportedOperationException.class)
170     @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
171     public void testPrivateConstructor() throws Throwable {
172         final Constructor<ServiceLoaderRSVPExtensionProviderContext> c =
173             ServiceLoaderRSVPExtensionProviderContext.class.getDeclaredConstructor();
174         c.setAccessible(true);
175         try {
176             c.newInstance();
177         } catch (final InvocationTargetException e) {
178             throw e.getCause();
179         }
180     }
181
182 }