+ Assert.assertNotNull(message);
+ final String expectedMsg = "404 Not Found - CCAP " + ccapId + " failed to connect @ " + cmtsAddr.getValue()
+ + ':' + port + " - ";
+ Assert.assertTrue(expectedMsg, message.startsWith(expectedMsg));
+ }
+
+ @Test
+ public void testAddValidUpGateTwice() throws Exception {
+ connectToCmts(service);
+ final String expectedMsg1 = "200 OK - sendGateSet for " + ccapId + '/' + gatePath + " returned GateId";
+ addAndValidateGate(service, "extrm_up", srcAddr, dstAddr, ServiceFlowDirection.Us, cmAddrInet, gatePath,
+ expectedMsg1);
+
+ final String expectedMsg2 = "404 Not Found - sendGateSet for " + ccapId + '/' + gatePath + " already exists";
+ addAndValidateGate(service, "extrm_up", srcAddr, dstAddr, ServiceFlowDirection.Us, cmAddrInet, gatePath,
+ expectedMsg2);
+
+ Assert.assertTrue(deleteGate(service, gatePath));
+ }
+
+ @Test
+ public void testAddTwoValidUpGates() throws Exception {
+ connectToCmts(service);
+
+ final String gatePath1 = "gatePath1";
+ final String expectedMsg1 = "200 OK - sendGateSet for " + ccapId + '/' + gatePath1 + " returned GateId";
+ addAndValidateGate(service, "extrm_up", srcAddr, dstAddr, ServiceFlowDirection.Us, cmAddrInet, gatePath1,
+ expectedMsg1);
+
+ final String gatePath2 = "gatePath2";
+ final String expectedMsg2 = "200 OK - sendGateSet for " + ccapId + '/' + gatePath2 + " returned GateId";
+ addAndValidateGate(service, "extrm_up", srcAddr, dstAddr, ServiceFlowDirection.Us, cmAddrInet, gatePath2,
+ expectedMsg2);
+
+ Assert.assertTrue(deleteGate(service, gatePath1));
+ Assert.assertTrue(deleteGate(service, gatePath2));
+ }
+
+ @Test
+ public void testAddValidDownGateTwice() throws Exception {
+ connectToCmts(service);
+ final String expectedMsg1 = "200 OK - sendGateSet for " + ccapId + '/' + gatePath + " returned GateId";
+ addAndValidateGate(service, "extrm_dn", srcAddr, dstAddr, ServiceFlowDirection.Ds, cmAddrInet, gatePath,
+ expectedMsg1);
+
+ final String expectedMsg2 = "404 Not Found - sendGateSet for " + ccapId + '/' + gatePath + " already exists";
+ addAndValidateGate(service, "extrm_dn", srcAddr, dstAddr, ServiceFlowDirection.Ds, cmAddrInet, gatePath,
+ expectedMsg2);
+
+ Assert.assertTrue(deleteGate(service, gatePath));
+ }
+
+ @Test
+ public void testDeleteNonExistentGate() throws Exception {
+ connectToCmts(service);
+ Assert.assertFalse(deleteGate(service, gatePath));
+ }
+
+ @Test
+ public void testAddAndRemoveValidUpGate() throws Exception {
+ final String expectedMsgStart = "200 OK - sendGateSet for " + ccapId + '/' + gatePath + " returned GateId";
+ addRemoveValidateGate(service, "extrm_up", srcAddr, dstAddr, ServiceFlowDirection.Us, cmAddrInet, gatePath,
+ expectedMsgStart);
+ }
+
+ @Test
+ public void testAddAndRemoveValidDownGate() throws Exception {
+ final String expectedMsgStart = "200 OK - sendGateSet for " + ccapId + '/' + gatePath + " returned GateId";
+ addRemoveValidateGate(service, "extrm_dn", srcAddr, dstAddr, ServiceFlowDirection.Ds, cmAddrInet, gatePath,
+ expectedMsgStart);
+ }
+
+ @Test
+ public void testAddAndRemoveInvalidCmAddrUpGate() throws Exception {
+ // TODO - fix cmts emulator
+ final String expectedMsgStart = "404 Not Found - sendGateSet for " + ccapId + '/' + gatePath
+ + " returned error - Error Code: 13 Subcode: 0 Invalid SubscriberID";
+ addInvalidGate(service, "extrm_up", srcAddr, dstAddr, ServiceFlowDirection.Us, invalidCmAddrInet, gatePath,
+ expectedMsgStart);
+ }
+
+ @Test
+ public void testAddInvalidScnUpGate() throws Exception {
+ final String expectedMsgStart = "404 Not Found - sendGateSet for " + ccapId + '/' + gatePath
+ + " returned error - Error Code: 11 Subcode: 0 Undefined Service Class Name";
+ addInvalidGate(service, "extrm_up_invalid", srcAddr, dstAddr, ServiceFlowDirection.Us, cmAddrInet, gatePath,
+ expectedMsgStart);
+ }
+
+ @Test
+ public void testAddInvalidScnDownGate() throws Exception {
+ final String expectedMsgStart = "404 Not Found - sendGateSet for " + ccapId + '/' + gatePath
+ + " returned error - Error Code: 11 Subcode: 0 Undefined Service Class Name";
+ addInvalidGate(service, "extrm_dn_invalid", srcAddr, dstAddr, ServiceFlowDirection.Ds, cmAddrInet, gatePath,
+ expectedMsgStart);
+ }
+
+ /**
+ * This tests the instantiation of a COPSDecisionMsg object that is responsible for setting a gate request,
+ * streams it over a mock Socket object and parses the bytes into a new COPSDecisionMsg object which should
+ * be equivalent
+ * @throws Exception - test will fail should any exception be thrown during execution
+ */
+ @Test
+ public void testGateRequestDecisionMsg() throws Exception {
+ final Socket socket = new MockSocket();
+
+ final ServiceFlowDirection direction = ServiceFlowDirection.Us;
+ final Gate gate = makeGateObj("extrm_up", cmtsAddr, direction, new Ipv4Address("127.0.0.1"));
+ final IPCMMGate gateReq = makeGateRequest(ccap, gate, InetAddress.getByName("localhost"), direction);
+ final byte[] data = gateReq.getData();
+
+ final Set<COPSDecision> decisionSet = new HashSet<>();
+ decisionSet.add(new COPSDecision(CType.DEF, Command.INSTALL, DecisionFlag.REQERROR));
+ final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
+ decisionMap.put(new COPSContext(RType.CONFIG, (short) 0), decisionSet);
+
+ final COPSClientSI clientSD = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData(data, 0, data.length));
+ final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(IPCMMClient.CLIENT_TYPE, new COPSHandle(new COPSData("123")),
+ decisionMap, null, clientSD);
+ decisionMsg.writeData(socket);
+
+ final COPSMsg msg = COPSMsgParser.parseMessage(socket);
+ Assert.assertNotNull(msg);
+ Assert.assertEquals(decisionMsg, msg);
+ }
+
+ /**
+ * Attempts to create a gate against a CMTS, validates the results then attempts to delete it.
+ * @param service - the service used to connect to a CMTS for issuing requests
+ * @param scnName - the service class name (aka. gate name)
+ * @param srcAddr - the address to the CMTS subnet?
+ * @param dstAddr - the destination address
+ * @param direction - the gate direction
+ * @param cmAddrInet - the address to the cable modem to which the gate will be assigned
+ * @param gatePath - the path to the gate
+ * @param expGateSetMsgStart - the expected start of the gate set return message to be validated against
+ */
+ private void addRemoveValidateGate(final PCMMService service, final String scnName, final Ipv4Address srcAddr,
+ final Ipv4Address dstAddr, final ServiceFlowDirection direction,
+ final InetAddress cmAddrInet, final String gatePath,
+ final String expGateSetMsgStart) {
+ connectToCmts(service);
+ addAndValidateGate(service, scnName, srcAddr, dstAddr, direction, cmAddrInet, gatePath, expGateSetMsgStart);
+ deleteGate(service, gatePath);
+ }
+
+ private void addInvalidGate(final PCMMService service, final String scnName, final Ipv4Address srcAddr,
+ final Ipv4Address dstAddr, final ServiceFlowDirection direction,
+ final InetAddress cmAddrInet, final String gatePath,
+ final String expGateSetMsgStart) {
+ connectToCmts(service);
+ final int numRequestsBefore = service.gateRequests.size();
+ addAndValidateGate(service, scnName, srcAddr, dstAddr, direction, cmAddrInet, gatePath, expGateSetMsgStart);
+ assertEquals(numRequestsBefore, service.gateRequests.size());
+ }
+
+ private void connectToCmts(final PCMMService service) {
+ final String message = service.addCcap();
+ Assert.assertNotNull(message);
+ final String expectedMsg = "200 OK - CCAP " + ccapId + " connected @ "
+ + ccap.getConnection().getIpAddress().getIpv4Address().getValue()
+ + ":" + ccap.getConnection().getPort().getValue();
+ Assert.assertEquals(expectedMsg, message);