Add missing license headers
[packetcable.git] / packetcable-driver / src / test / java / org / pcmm / gates / impl / PCMMErrorTest.java
1 /*
2  * Copyright (c) 2015 Cable Television Laboratories, 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.pcmm.gates.impl;
9
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.pcmm.gates.IPCMMError.ErrorCode;
13 import org.umu.cops.stack.COPSMsgParser;
14
15 /**
16  * Tests the data holder class PCMMError to ensure both construction and byte parsing result in correct object
17  * creation.
18  */
19 public class PCMMErrorTest {
20
21     @Test(expected = IllegalArgumentException.class)
22     public void nullErrorAndSubCodes() {
23         new PCMMError(null, (short)0);
24     }
25
26     @Test(expected = IllegalArgumentException.class)
27     public void nullErrorCode() {
28         new PCMMError(null, (short)0);
29     }
30
31     @Test(expected = IllegalArgumentException.class)
32     public void naErrorCode() {
33         new PCMMError(ErrorCode.NA);
34     }
35
36     @Test
37     public void construction() {
38         final PCMMError error = new PCMMError(ErrorCode.TRANSPORT_ERROR, (short)0);
39         Assert.assertEquals(ErrorCode.TRANSPORT_ERROR, error.getErrorCode());
40         Assert.assertEquals(ErrorCode.NA.getCode(), error.getErrorSubcode());
41
42         final byte[] dataBytes = error.getBytes();
43         Assert.assertEquals(4, dataBytes.length);
44         Assert.assertEquals(ErrorCode.TRANSPORT_ERROR,
45                 ErrorCode.valueOf(COPSMsgParser.bytesToShort(dataBytes[0], dataBytes[1])));
46         final short subCodeVal = COPSMsgParser.bytesToShort(dataBytes[2], dataBytes[3]);
47         Assert.assertEquals(ErrorCode.NA,
48                 ErrorCode.valueOf(COPSMsgParser.bytesToShort(dataBytes[2], dataBytes[3])));
49     }
50
51     @Test
52     public void byteParsing() {
53         final PCMMError error = new PCMMError(ErrorCode.INVALID_FIELD, ErrorCode.INVALID_SUB_ID.getCode());
54         final PCMMError parsed = PCMMError.parse(error.getBytes());
55         Assert.assertEquals(error, parsed);
56     }
57
58 }