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;
42 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.opendaylight.yangtools.yang.common.Uint8;
45 @RunWith(MockitoJUnitRunner.class)
46 public class AbstractMessageParserTest {
48 private static final EnterpriseNumber EN = new EnterpriseNumber(Uint32.ZERO);
50 private Object object;
52 private VendorInformationObject viObject;
55 private ObjectRegistry registry;
57 private class Abs extends AbstractMessageParser {
59 protected Abs(final ObjectRegistry registry) {
64 public void serializeMessage(final Message message, final ByteBuf buffer) {
68 protected Message validate(final List<Object> objects, final List<Message> errors) {
69 if (objects.get(0) instanceof VendorInformationObject) {
70 final RepliesBuilder repsBuilder = new RepliesBuilder();
71 repsBuilder.setVendorInformationObject(addVendorInformationObjects(objects));
72 return new PcrepBuilder().setPcrepMessage(
73 new PcrepMessageBuilder().setReplies(Arrays.asList(repsBuilder.build())).build())
75 } else if (objects.get(0) instanceof ErrorObject) {
76 final Uint8 errorType = ((ErrorObject) objects.get(0)).getType();
77 final Uint8 errorValue = ((ErrorObject) objects.get(0)).getValue();
78 return createErrorMsg(PCEPErrors.forValue(errorType, errorValue), Optional.empty());
85 public void setUp() throws PCEPDeserializerException {
86 this.object = new ErrorObjectBuilder().setType(Uint8.ONE).setValue(Uint8.ONE).build();
87 this.viObject = new VendorInformationObjectBuilder().setEnterpriseNumber(EN).build();
88 doNothing().when(this.registry).serializeVendorInformationObject(any(VendorInformationObject.class),
90 doReturn(Optional.of(this.viObject)).when(this.registry).parseVendorInformationObject(eq(EN),
91 eq(new ObjectHeaderImpl(true, true)), any(ByteBuf.class));
92 doNothing().when(this.registry).serializeObject(any(Object.class), any(ByteBuf.class));
93 doReturn(this.object).when(this.registry).parseObject(13, 1, new ObjectHeaderImpl(true, true),
94 Unpooled.wrappedBuffer(new byte[] { 0, 0, 1, 1 }));
98 public void testParseObjects() throws PCEPDeserializerException {
99 final Abs a = new Abs(this.registry);
100 final ByteBuf buffer = Unpooled.buffer();
101 a.serializeObject(this.object, buffer);
103 verify(this.registry, only()).serializeObject(any(Object.class), any(ByteBuf.class));
105 final Message b = a.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x0D, 0x13, 0, 0x08, 0, 0, 1, 1 }),
106 Collections.emptyList());
108 assertEquals(this.object, ((Pcerr) b).getPcerrMessage().getErrors().get(0).getErrorObject());
112 public void testParseVendorInformationObject() throws PCEPDeserializerException {
113 final Abs parser = new Abs(this.registry);
114 final ByteBuf buffer = Unpooled.buffer();
116 parser.serializeVendorInformationObjects(Lists.newArrayList(this.viObject), buffer);
117 verify(this.registry, only()).serializeVendorInformationObject(any(VendorInformationObject.class),
120 final Message msg = parser.parseMessage(
121 Unpooled.wrappedBuffer(new byte[] { 0x22, 0x13, 0x00, 0x08, 0, 0, 0, 0 }), Collections.emptyList());
123 assertEquals(this.viObject, ((Pcrep)msg).getPcrepMessage().getReplies().get(0).getVendorInformationObject()