Add new revision for pcep types model
[bgpcep.git] / pcep / spi / src / test / java / org / opendaylight / protocol / pcep / spi / AbstractMessageParserTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.protocol.pcep.spi;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
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.rev181109.Pcerr;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcrep;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcrepBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.PcrepMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.RepliesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObjectBuilder;
35
36 public class AbstractMessageParserTest {
37
38     private static final EnterpriseNumber EN = new EnterpriseNumber(0L);
39
40     private Object object;
41
42     private VendorInformationObject viObject;
43
44     @Mock
45     private ObjectRegistry registry;
46
47     private class Abs extends AbstractMessageParser {
48
49         protected Abs(final ObjectRegistry registry) {
50             super(registry);
51         }
52
53         @Override
54         public void serializeMessage(final Message message, final ByteBuf buffer) {
55         }
56
57         @Override
58         protected Message validate(final List<Object> objects, final List<Message> errors) {
59             if (objects.get(0) instanceof VendorInformationObject) {
60                 final RepliesBuilder repsBuilder = new RepliesBuilder();
61                 repsBuilder.setVendorInformationObject(addVendorInformationObjects(objects));
62                 final PcrepBuilder builder = new PcrepBuilder();
63                 builder.setPcrepMessage(new PcrepMessageBuilder().setReplies(Lists.newArrayList(repsBuilder.build())).build());
64                 return builder.build();
65             } else if (objects.get(0) instanceof ErrorObject) {
66                 final short errorType = ((ErrorObject) objects.get(0)).getType();
67                 final short errorValue = ((ErrorObject) objects.get(0)).getValue();
68                 return createErrorMsg(PCEPErrors.forValue(errorType, errorValue), Optional.empty());
69             }
70             return null;
71         }
72     }
73
74     @Before
75     public void setUp() throws PCEPDeserializerException {
76         MockitoAnnotations.initMocks(this);
77         this.object = new ErrorObjectBuilder().setType((short) 1).setValue((short) 1).build();
78         this.viObject = new VendorInformationObjectBuilder().setEnterpriseNumber(EN).build();
79         Mockito.doNothing().when(this.registry).serializeVendorInformationObject(Mockito.any(VendorInformationObject.class), Mockito.any(ByteBuf.class));
80         Mockito.doReturn(Optional.of(this.viObject)).when(this.registry).parseVendorInformationObject(Mockito.eq(EN), Mockito.eq(new ObjectHeaderImpl(true, true)), Mockito.any(ByteBuf.class));
81         Mockito.doNothing().when(this.registry).serializeObject(Mockito.any(Object.class), Mockito.any(ByteBuf.class));
82         Mockito.doReturn(this.object).when(this.registry).parseObject(13, 1, new ObjectHeaderImpl(true, true), Unpooled.wrappedBuffer(new byte[] { 0, 0, 1, 1 }));
83     }
84
85     @Test
86     public void testParseObjects() throws PCEPDeserializerException {
87         final Abs a = new Abs(this.registry);
88         final ByteBuf buffer = Unpooled.buffer();
89         a.serializeObject(this.object, buffer);
90
91         Mockito.verify(this.registry, Mockito.only()).serializeObject(Mockito.any(Object.class), Mockito.any(ByteBuf.class));
92
93         final Message b = a.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x0D, 0x13, 0, 0x08, 0, 0, 1, 1 }), Collections.emptyList());
94
95         assertEquals(this.object, ((Pcerr) b).getPcerrMessage().getErrors().get(0).getErrorObject());
96     }
97
98     @Test
99     public void testParseVendorInformationObject() throws PCEPDeserializerException {
100         final Abs parser = new Abs(this.registry);
101         final ByteBuf buffer = Unpooled.buffer();
102
103         parser.serializeVendorInformationObjects(Lists.newArrayList(this.viObject), buffer);
104         Mockito.verify(this.registry, Mockito.only()).serializeVendorInformationObject(Mockito.any(VendorInformationObject.class), Mockito.any(ByteBuf.class));
105
106         final Message msg = parser.parseMessage(Unpooled.wrappedBuffer(new byte[] {0x22, 0x13, 0x00, 0x08, 0, 0, 0, 0 }), Collections.emptyList());
107
108         assertEquals(this.viObject, ((Pcrep)msg).getPcrepMessage().getReplies().get(0).getVendorInformationObject().get(0));
109     }
110 }