UDPPublishHandler extends Handler<ExtensionServiceMessage> { @Override public void handleMessage(ExtensionServiceMessage message) { Map receivedMsg = (Map) message.getObject(); Map sendMsg = new LinkedHashMap<String,Object>(); byte[] sendBytes = null; if (formatLocations != null) { sendBytes = getFormattedOutput(receivedMsg); } else if (csvSource != null) { String str = buildCsv(receivedMsg); sendBytes = str.getBytes(); } else if (bytesLocation != null) { sendBytes = ((String)receivedMsg.get(bytesLocation)).getBytes(); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<String,Object>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } try { if (sendBytes == null) { log.debug("Sending message to address {} and port {} with contents: {}" , address.getHostAddress(), port, sendMsg); sendBytes = writer.writeValueAsBytes(sendMsg); } else { log.debug("Sending message to address {} and port {} with contents: {}" , address.getHostAddress(), port, new String(sendBytes)); } DatagramPacket packet = new DatagramPacket(sendBytes, sendBytes.length, address, port); socket.send(packet); } catch (Exception e) { log.warn("Failed trying to translate and send the message.", e); } } UDPPublishHandler(Map outgoing, DatagramSocket socket, String sourceName); @Override void handleMessage(ExtensionServiceMessage message);  }
@Test public void testEmptyConfig() { pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("str", "msg"); String expectedAddress = "127.0.0.1"; int expectedPort = port; Map<String,Object> expectedData = new LinkedHashMap<>(); createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); assert fakeSocket.compareAddress(expectedAddress); assert fakeSocket.comparePort(expectedPort); }
@Test public void testPassPure() { outgoing.put("passPureMapOut", true); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("str", "msg"); String expectedAddress = "127.0.0.1"; int expectedPort = port; Map<String,Object> expectedData = new LinkedHashMap<>(); expectedData.put("str", "msg"); createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); assert fakeSocket.compareAddress(expectedAddress); assert fakeSocket.comparePort(expectedPort); }
@Test public void testTransform() { String[][] transforms = { {"str","message"}, {"str", "place.loc"}, {"str","invalid","nope"}, {"str"} }; List<List> transformations = new ArrayList<>(); for (String[] t : transforms) { List list = new ArrayList(Arrays.asList(t)); transformations.add(list); } outgoing.put("transformations", transformations); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); String expectedAddress = "127.0.0.1"; int expectedPort = port; Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("str", "msg"); Map<String,Object> expectedData = new LinkedHashMap<>(); Map<String,Object> subMap = new LinkedHashMap<>(); subMap.put("loc","msg"); expectedData.put("place",subMap); expectedData.put("message","msg"); createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); assert fakeSocket.compareAddress(expectedAddress); assert fakeSocket.comparePort(expectedPort); }
@Test public void testPassUnspecifiedWithTransform() { outgoing.put("passUnspecifiedOut", true); String[][] transforms = { {"str","message"}, {"str", "place.loc"}, {"str","invalid","nope"}, {"str"} }; List<List> transformations = new ArrayList<>(); for (String[] t : transforms) { List list = new ArrayList(Arrays.asList(t)); transformations.add(list); } outgoing.put("transformations", transformations); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); String expectedAddress = "127.0.0.1"; int expectedPort = port; Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("str", "msg"); testMap.put("stay","here"); Map<String,Object> expectedData = new LinkedHashMap<>(); Map<String,Object> subMap = new LinkedHashMap<>(); subMap.put("loc","msg"); expectedData.put("place",subMap); expectedData.put("message","msg"); expectedData.put("stay","here"); createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); assert fakeSocket.compareAddress(expectedAddress); assert fakeSocket.comparePort(expectedPort); }
@Test public void testPassBytesOutFrom() { outgoing.put("passBytesOutFrom", "byteLoc"); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("byteLoc", "msg"); testMap.put("not","relevant"); byte[] expectedData = "msg".getBytes(); createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); }
@Test public void testFormatParser() { Map<String,Object> formatParser = new LinkedHashMap<>(); formatParser.put("pattern", "str:%s num:%d"); String[] locations = {"msg", "port"}; formatParser.put("locations", Arrays.asList(locations)); formatParser.put("altPatternLocation", "altPattern"); formatParser.put("altLocations", "altLocations"); outgoing.put("formatParser", formatParser); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("msg", "string"); testMap.put("port",1253); String expectedData = "str:string num:1253"; createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); testMap = new LinkedHashMap<>(); testMap.put("message", "msg"); testMap.put("harbor", 2718); locations[0] = "message"; locations[1] = "harbor"; testMap.put("altLocations", Arrays.asList(locations)); expectedData = "str:msg num:2718"; createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); testMap = new LinkedHashMap<>(); testMap.put("newNum", 6283); testMap.put("newStr", "Hello World"); testMap.put("altPattern", "#:%d txt:%s"); locations[0] = "newNum"; locations[1] = "newStr"; testMap.put("altLocations", Arrays.asList(locations)); expectedData = "#:6283 txt:Hello World"; createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); }
@Test public void testXml() { outgoing.put("sendXmlRoot", "route"); outgoing.put("passPureMapOut", true); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); testMap.put("Hello", "World"); testMap.put("Goodbye","Cruel World"); String expectedData = "<route><Hello>World</Hello><Goodbye>Cruel World</Goodbye></route>"; createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); }
@Test public void testCsvInitialSchema() { outgoing.put("passCsvOutFrom", "csv"); String[] schema = {"name","id#"}; outgoing.put("useCsvSchema", Arrays.asList(schema)); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); Map<String,Object> obj1 = new LinkedHashMap<>(); obj1.put("name","bob newhart"); obj1.put("id#","12345"); Map<String,Object> obj2 = new LinkedHashMap<>(); obj2.put("name","raynor, jim"); obj2.put("id#","67892"); obj2.put("notInSchema","ignored"); Map[] objects = {obj1, obj2}; testMap.put("csv", Arrays.asList(objects)); String expectedData = "name,id#\nbob newhart,12345\n\"raynor, jim\",67892\n"; createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); }
@Test public void testCsvAlternateSchema() { outgoing.put("passCsvOutFrom", "csv"); outgoing.put("useCsvSchema", "schemaLoc"); pHandler = new UDPPublishHandler(outgoing, fakeSocket, sourceName); Map<String,Object> testMap = new LinkedHashMap<>(); Map<String,Object> obj1 = new LinkedHashMap<>(); obj1.put("name","bob newhart"); obj1.put("score",26); Map<String,Object> obj2 = new LinkedHashMap<>(); obj2.put("name","raynor, jim"); obj2.put("notInSchema","ignored"); Map[] objects = {obj1, obj2}; String[] schema = {"name","score"}; testMap.put("csv", Arrays.asList(objects)); testMap.put("schemaLoc", Arrays.asList(schema)); String expectedData = "name,score\nbob newhart,26\n\"raynor, jim\",\n"; createMessage(testMap); pHandler.handleMessage(extMsg); assert fakeSocket.compareData(expectedData); }