Added new constructor. May need to tighten down interfaces in the future. 16/19316/2
authorSteven Pisarski <s.pisarski@cablelabs.com>
Wed, 29 Apr 2015 16:04:50 +0000 (10:04 -0600)
committerThomas Kee <xsited@yahoo.com>
Fri, 8 May 2015 19:51:56 +0000 (12:51 -0700)
Change-Id: I56bbcc65514f9eb2f7ff9aad804780449968e4fb
Signed-off-by: Steven Pisarski <s.pisarski@cablelabs.com>
packetcable-driver/src/main/java/org/umu/cops/stack/COPSClientSI.java
packetcable-driver/src/test/java/org/umu/cops/stack/COPSClientSITest.java

index e25fb91624924f15aee678956a9f439163b2ffc4..293332951c22c2356d9dcd09bcd7beeba6a003b7 100644 (file)
@@ -7,6 +7,7 @@
 package org.umu.cops.stack;
 
 import org.umu.cops.stack.COPSObjHeader.CNum;
+import org.umu.cops.stack.COPSObjHeader.CType;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -62,6 +63,19 @@ public class COPSClientSI extends COPSObjBase {
      */
     private final COPSData _padding;
 
+    /**
+     * Constructor generally used for sending messages
+     * @param ctype - the CSIType
+     * @param data - the data
+     * @throws java.lang.IllegalArgumentException
+     */
+    public COPSClientSI(final CNum cnum, final CType ctype, final COPSData data) {
+        /* The CSIType does not map directly to the CType, therefore the hook to map to a CType below is
+           required to ensure the header value outputs the correct value when streamed
+         */
+        this(new COPSObjHeader(cnum, ctype), data);
+    }
+
     /**
      * Constructor generally used for sending messages
      * @param csitype - the CSIType
@@ -84,14 +98,17 @@ public class COPSClientSI extends COPSObjBase {
      */
     protected COPSClientSI(final COPSObjHeader hdr, final COPSData data) {
         super(hdr);
-        _csiType = VAL_TO_CSI.get(hdr.getCType().ordinal());
+        if (VAL_TO_CSI.get(hdr.getCType().ordinal()) == null) {
+            // TODO - determine if this is a good default value???
+            _csiType = CSIType.NAMED;
+        } else {
+            _csiType = VAL_TO_CSI.get(hdr.getCType().ordinal());
+        }
 
-        if (!hdr.getCNum().equals(CNum.CSI))
-            throw new IllegalArgumentException("CNum must be equal to " + CNum.CSI);
+        if (!hdr.getCNum().equals(CNum.CSI) && !hdr.getCNum().equals(CNum.DEC))
+            throw new IllegalArgumentException("CNum must be equal to " + CNum.CSI + " or " + CNum.DEC);
         if (_csiType == null || _csiType.equals(CSIType.NA))
             throw new IllegalArgumentException("Invalid CSIType");
-        if (_csiType.ordinal() != hdr.getCType().ordinal())
-            throw new IllegalArgumentException("Error mapping CSIType " + _csiType + " to CType" + hdr.getCType());
         if (data == null) throw new IllegalArgumentException("Data must not be null");
 
         _data = data;
@@ -125,7 +142,7 @@ public class COPSClientSI extends COPSObjBase {
     @Override
     public void writeBody(final Socket socket) throws IOException {
         COPSUtil.writeData(socket, _data.getData(), _data.length());
-        COPSUtil.writeData(socket, _padding.getData(), _padding.length());
+        if (_padding.length() > 0) COPSUtil.writeData(socket, _padding.getData(), _padding.length());
     }
 
     @Override
index 6f9e7cb58ba096788b2ec636905e3c6fcb0f0e00..3b67dd725ee6231b3e5e643f48de907a16c3b545 100644 (file)
@@ -15,6 +15,31 @@ import java.io.ByteArrayOutputStream;
  */
 public class COPSClientSITest {
 
+    @Test(expected = IllegalArgumentException.class)
+    public void nullCNumConstructor1() {
+        new COPSClientSI(null, CType.CSI, new COPSData());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidCNumConstructor1() {
+        new COPSClientSI(CNum.ACCT_TIMER, CType.CSI, new COPSData());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void nullCTypeConstructor1() {
+        new COPSClientSI(CNum.DEC, null, new COPSData());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidCTypeConstructor1() {
+        new COPSClientSI(CNum.DEC, CType.NA, new COPSData());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void nullDataConstructor1() {
+        new COPSClientSI(CNum.DEC, CType.CSI, null);
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void invalidCSItype() {
         new COPSClientSI(CSIType.NA, new COPSData());
@@ -35,6 +60,15 @@ public class COPSClientSITest {
         new COPSClientSI(new COPSObjHeader(CNum.ACCT_TIMER, CType.CSI), new COPSData());
     }
 
+    @Test
+    public void validConstructor1() {
+        final COPSClientSI clientSI = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData());
+        Assert.assertEquals(CSIType.NAMED, clientSI.getCsiType());
+        Assert.assertEquals(CNum.DEC, clientSI.getHeader().getCNum());
+        Assert.assertEquals(CType.CSI, clientSI.getHeader().getCType());
+        Assert.assertEquals(new COPSData(), clientSI.getData());
+    }
+
     @Test
     public void csiSignaledEmptyData() {
         final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData());