MgcpRequestedEventsParser { public static MgcpRequestedEvent[] parse(int requestId, String requestedEvents, MgcpPackageManager packageManager) throws UnrecognizedMgcpPackageException, UnrecognizedMgcpEventException, UnrecognizedMgcpActionException, MgcpParseException { String[] tokens = requestedEvents.split("(?<=\\)),"); MgcpRequestedEvent[] events = new MgcpRequestedEvent[tokens.length]; for (int i = 0; i < events.length; i++) { events[i] = parseSingle(requestId, tokens[i], packageManager); } return events; }  static MgcpRequestedEvent[] parse(int requestId, String requestedEvents, MgcpPackageManager packageManager);  }
@Test public void testParseEndpointEventWithoutParameters() throws Exception { final int requestId = 16; final String requestedEvents = "AU/oc(N)"; final MgcpEventType eventType = mock(MgcpEventType.class); final MgcpPackage mgcpPackage = mock(MgcpPackage.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("AU")).thenReturn(mgcpPackage); when(mgcpPackage.getEventDetails("oc")).thenReturn(eventType); when(eventType.parameterized()).thenReturn(false); MgcpRequestedEvent[] events = MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); assertEquals(1, events.length); final MgcpRequestedEvent oc = events[0]; assertNotNull(oc); assertEquals(requestId, oc.getRequestId()); assertEquals("AU", oc.getPackageName()); assertEquals("oc", oc.getEventType()); assertEquals(0, oc.getConnectionId()); assertEquals(0, oc.getParameters().length); assertEquals(MgcpActionType.NOTIFY, oc.getAction()); }
@Test public void testParseEndpointEventWithParameters() throws Exception { final int requestId = 16; final String requestedEvents = "AU/oc(N)(param1=value1,param2=value2)"; final MgcpEventType eventType = mock(MgcpEventType.class); final MgcpPackage mgcpPackage = mock(MgcpPackage.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("AU")).thenReturn(mgcpPackage); when(mgcpPackage.getEventDetails("oc")).thenReturn(eventType); when(eventType.parameterized()).thenReturn(true); MgcpRequestedEvent[] events = MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); assertEquals(1, events.length); final MgcpRequestedEvent event = events[0]; assertNotNull(event); assertEquals(requestId, event.getRequestId()); assertEquals("AU", event.getPackageName()); assertEquals("oc", event.getEventType()); assertEquals(0, event.getConnectionId()); assertEquals(2, event.getParameters().length); assertEquals(MgcpActionType.NOTIFY, event.getAction()); }
@Test public void testParseConnectionEventWithoutParameters() throws Exception { final int requestId = 16; final String requestedEvents = "R/rto@364823(N)"; final MgcpEventType eventType = mock(MgcpEventType.class); final MgcpPackage mgcpPackage = mock(MgcpPackage.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("R")).thenReturn(mgcpPackage); when(mgcpPackage.getEventDetails("rto")).thenReturn(eventType); when(eventType.parameterized()).thenReturn(false); MgcpRequestedEvent[] events = MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); assertEquals(1, events.length); final MgcpRequestedEvent event = events[0]; assertNotNull(event); assertEquals(requestId, event.getRequestId()); assertEquals("R", event.getPackageName()); assertEquals("rto", event.getEventType()); assertEquals(Integer.parseInt("364823", 16), event.getConnectionId()); assertEquals(MgcpActionType.NOTIFY, event.getAction()); }
@Test public void testParseMulitpleEvents() throws Exception { final int requestId = 16; final String requestedEvents = "AU/oc(N),R/rto@AB23F(N)(100,st=im),AU/of(N)"; final MgcpEventType audioEventType = mock(MgcpEventType.class); final MgcpPackage audioPackage = mock(MgcpPackage.class); final MgcpPackage rtpPackage = mock(MgcpPackage.class); final MgcpEventType rtpEventType = mock(MgcpEventType.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("AU")).thenReturn(audioPackage); when(audioPackage.getEventDetails(any(String.class))).thenReturn(audioEventType); when(audioEventType.parameterized()).thenReturn(false); when(packageManager.getPackage("R")).thenReturn(rtpPackage); when(rtpPackage.getEventDetails("rto")).thenReturn(rtpEventType); when(rtpEventType.parameterized()).thenReturn(true); MgcpRequestedEvent[] events = MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); assertEquals(3, events.length); final MgcpRequestedEvent oc = events[0]; assertNotNull(oc); assertEquals(requestId, oc.getRequestId()); assertEquals("AU", oc.getPackageName()); assertEquals("oc", oc.getEventType()); assertEquals(0, oc.getConnectionId()); assertEquals(0, oc.getParameters().length); assertEquals(MgcpActionType.NOTIFY, oc.getAction()); final MgcpRequestedEvent rto = events[1]; assertNotNull(rto); assertEquals(requestId, rto.getRequestId()); assertEquals("R", rto.getPackageName()); assertEquals("rto", rto.getEventType()); assertEquals(Integer.parseInt("AB23F", 16), rto.getConnectionId()); assertEquals(2, rto.getParameters().length); assertEquals(MgcpActionType.NOTIFY, rto.getAction()); final MgcpRequestedEvent of = events[2]; assertNotNull(of); assertEquals(requestId, of.getRequestId()); assertEquals("AU", of.getPackageName()); assertEquals("of", of.getEventType()); assertEquals(0, of.getConnectionId()); assertEquals(0, of.getParameters().length); assertEquals(MgcpActionType.NOTIFY, of.getAction()); }
@Test(expected = UnrecognizedMgcpPackageException.class) public void testParseUnrecognizedPackage() throws Exception { final int requestId = 16; final String requestedEvents = "XYZ/oc(N),AU/of(N)"; final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("XYZ")).thenReturn(null); MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); }
@Test(expected = UnrecognizedMgcpEventException.class) public void testParseUnrecognizedEvent() throws Exception { final int requestId = 16; final String requestedEvents = "AU/xyz(N),AU/of(N)"; final MgcpPackage mgcpPackage = mock(MgcpPackage.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("AU")).thenReturn(mgcpPackage); when(mgcpPackage.getEventDetails("xyz")).thenReturn(null); MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); }
@Test(expected = UnrecognizedMgcpActionException.class) public void testParseUnrecognizedAction() throws Exception { final int requestId = 16; final String requestedEvents = "AU/oc(XYZ),AU/of(N)"; final MgcpPackage mgcpPackage = mock(MgcpPackage.class); final MgcpEventType eventType = mock(MgcpEventType.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("AU")).thenReturn(mgcpPackage); when(mgcpPackage.getEventDetails(any(String.class))).thenReturn(eventType); when(eventType.parameterized()).thenReturn(false); MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); }
@Test(expected = MgcpParseException.class) public void testParseMalformedRequest() throws Exception { final int requestId = 16; final String requestedEvents = "AU/oc(XYZ"; final MgcpPackage mgcpPackage = mock(MgcpPackage.class); final MgcpEventType eventType = mock(MgcpEventType.class); final MgcpPackageManager packageManager = mock(MgcpPackageManager.class); when(packageManager.getPackage("AU")).thenReturn(mgcpPackage); when(mgcpPackage.getEventDetails(any(String.class))).thenReturn(eventType); when(eventType.parameterized()).thenReturn(false); MgcpRequestedEventsParser.parse(requestId, requestedEvents, packageManager); }