2 * Copyright (c) 2014 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
8 package org.opendaylight.protocol.pcep.spi;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.only;
16 import static org.mockito.Mockito.verify;
18 import com.google.common.collect.Lists;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcrep;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcrepBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.PcrepMessageBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.RepliesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObjectBuilder;
43 @RunWith(MockitoJUnitRunner.class)
44 public class AbstractMessageParserTest {
46 private static final EnterpriseNumber EN = new EnterpriseNumber(0L);
48 private Object object;
50 private VendorInformationObject viObject;
53 private ObjectRegistry registry;
55 private class Abs extends AbstractMessageParser {
57 protected Abs(final ObjectRegistry registry) {
62 public void serializeMessage(final Message message, final ByteBuf buffer) {
66 protected Message validate(final List<Object> objects, final List<Message> errors) {
67 if (objects.get(0) instanceof VendorInformationObject) {
68 final RepliesBuilder repsBuilder = new RepliesBuilder();
69 repsBuilder.setVendorInformationObject(addVendorInformationObjects(objects));
70 return new PcrepBuilder().setPcrepMessage(
71 new PcrepMessageBuilder().setReplies(Arrays.asList(repsBuilder.build())).build())
73 } else if (objects.get(0) instanceof ErrorObject) {
74 final short errorType = ((ErrorObject) objects.get(0)).getType();
75 final short errorValue = ((ErrorObject) objects.get(0)).getValue();
76 return createErrorMsg(PCEPErrors.forValue(errorType, errorValue), Optional.empty());
83 public void setUp() throws PCEPDeserializerException {
84 this.object = new ErrorObjectBuilder().setType((short) 1).setValue((short) 1).build();
85 this.viObject = new VendorInformationObjectBuilder().setEnterpriseNumber(EN).build();
86 doNothing().when(this.registry).serializeVendorInformationObject(any(VendorInformationObject.class),
88 doReturn(Optional.of(this.viObject)).when(this.registry).parseVendorInformationObject(eq(EN),
89 eq(new ObjectHeaderImpl(true, true)), any(ByteBuf.class));
90 doNothing().when(this.registry).serializeObject(any(Object.class), any(ByteBuf.class));
91 doReturn(this.object).when(this.registry).parseObject(13, 1, new ObjectHeaderImpl(true, true),
92 Unpooled.wrappedBuffer(new byte[] { 0, 0, 1, 1 }));
96 public void testParseObjects() throws PCEPDeserializerException {
97 final Abs a = new Abs(this.registry);
98 final ByteBuf buffer = Unpooled.buffer();
99 a.serializeObject(this.object, buffer);
101 verify(this.registry, only()).serializeObject(any(Object.class), any(ByteBuf.class));
103 final Message b = a.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x0D, 0x13, 0, 0x08, 0, 0, 1, 1 }),
104 Collections.emptyList());
106 assertEquals(this.object, ((Pcerr) b).getPcerrMessage().getErrors().get(0).getErrorObject());
110 public void testParseVendorInformationObject() throws PCEPDeserializerException {
111 final Abs parser = new Abs(this.registry);
112 final ByteBuf buffer = Unpooled.buffer();
114 parser.serializeVendorInformationObjects(Lists.newArrayList(this.viObject), buffer);
115 verify(this.registry, only()).serializeVendorInformationObject(any(VendorInformationObject.class),
118 final Message msg = parser.parseMessage(
119 Unpooled.wrappedBuffer(new byte[] { 0x22, 0x13, 0x00, 0x08, 0, 0, 0, 0 }), Collections.emptyList());
121 assertEquals(this.viObject, ((Pcrep)msg).getPcrepMessage().getReplies().get(0).getVendorInformationObject()