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;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.Collections;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcrep;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcrepBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.RepliesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObjectBuilder;
37 public class AbstractMessageParserTest {
39 private static final EnterpriseNumber EN = new EnterpriseNumber(0L);
41 private Object object;
43 private VendorInformationObject viObject;
46 private ObjectRegistry registry;
48 private class Abs extends AbstractMessageParser {
50 protected Abs(final ObjectRegistry registry) {
55 public void serializeMessage(final Message message, final ByteBuf buffer) {
59 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
60 if (objects.get(0) instanceof VendorInformationObject) {
61 final RepliesBuilder repsBuilder = new RepliesBuilder();
62 repsBuilder.setVendorInformationObject(addVendorInformationObjects(objects));
63 final PcrepBuilder builder = new PcrepBuilder();
64 builder.setPcrepMessage(new PcrepMessageBuilder().setReplies(Lists.newArrayList(repsBuilder.build())).build());
65 return builder.build();
66 } else if (objects.get(0) instanceof ErrorObject) {
67 final short errorType = ((ErrorObject) objects.get(0)).getType();
68 final short errorValue = ((ErrorObject) objects.get(0)).getValue();
69 return createErrorMsg(PCEPErrors.forValue(errorType, errorValue), Optional.<Rp>absent());
76 public void setUp() throws PCEPDeserializerException {
77 MockitoAnnotations.initMocks(this);
78 this.object = new ErrorObjectBuilder().setType((short) 1).setValue((short) 1).build();
79 this.viObject = new VendorInformationObjectBuilder().setEnterpriseNumber(EN).build();
80 Mockito.doNothing().when(this.registry).serializeVendorInformationObject(Mockito.any(VendorInformationObject.class), Mockito.any(ByteBuf.class));
81 Mockito.doReturn(Optional.of(this.viObject)).when(this.registry).parseVendorInformationObject(Mockito.eq(EN), Mockito.eq(new ObjectHeaderImpl(true, true)), Mockito.any(ByteBuf.class));
82 Mockito.doNothing().when(this.registry).serializeObject(Mockito.any(Object.class), Mockito.any(ByteBuf.class));
83 Mockito.doReturn(this.object).when(this.registry).parseObject(13, 1, new ObjectHeaderImpl(true, true), Unpooled.wrappedBuffer(new byte[] { 0, 0, 1, 1 }));
87 public void testParseObjects() throws PCEPDeserializerException {
88 final Abs a = new Abs(this.registry);
89 final ByteBuf buffer = Unpooled.buffer();
90 a.serializeObject(this.object, buffer);
92 Mockito.verify(this.registry, Mockito.only()).serializeObject(Mockito.any(Object.class), Mockito.any(ByteBuf.class));
94 final Message b = a.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x0D, 0x13, 0, 0x08, 0, 0, 1, 1 }), Collections.<Message> emptyList());
96 assertEquals(this.object, ((Pcerr) b).getPcerrMessage().getErrors().get(0).getErrorObject());
100 public void testParseVendorInformationObject() throws PCEPDeserializerException {
101 final Abs parser = new Abs(this.registry);
102 final ByteBuf buffer = Unpooled.buffer();
104 parser.serializeVendorInformationObjects(Lists.newArrayList(this.viObject), buffer);
105 Mockito.verify(this.registry, Mockito.only()).serializeVendorInformationObject(Mockito.any(VendorInformationObject.class), Mockito.any(ByteBuf.class));
107 final Message msg = parser.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x22, 0x13, 0x00, 0x08, 0, 0, 0, 0 }), Collections.<Message> emptyList());
109 assertEquals(this.viObject, ((Pcrep)msg).getPcrepMessage().getReplies().get(0).getVendorInformationObject().get(0));