@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); service.serialize(event).ifPresent(json -> { try { sns.publish(topic, json); } catch (final Exception ex) { LOGGER.error("Error writing to SNS topic {}: {}", topic, ex.getMessage()); } }); } }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); service.serialize(event).ifPresent(message -> { LOGGER.debug("Sending message to Kafka topic: {}", topic); producer.send( new ProducerRecord<>(topic, event.getTarget().map(IRI::getIRIString).orElse(null), message)); }); }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); service.serialize(event).ifPresent(json -> { try { final Message message = session.createTextMessage(json); message.setStringProperty("Content-Type", "application/ld+json"); producer.send(message); } catch (final JMSException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); service.serialize(event).ifPresent(message -> { LOGGER.debug("Sending message to Kafka topic: {}", topic); producer.send( new ProducerRecord<>(topic, event.getTarget().map(IRI::getIRIString).orElse(null), message)); }); }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); final BasicProperties props = new BasicProperties().builder() .contentType("application/ld+json").contentEncoding("UTF-8").build(); service.serialize(event).ifPresent(message -> { try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, props, message.getBytes()); } catch (final IOException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); final BasicProperties props = new BasicProperties().builder() .contentType("application/ld+json").contentEncoding("UTF-8").build(); service.serialize(event).ifPresent(message -> { try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, props, message.getBytes()); } catch (final IOException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
@Test public void testSerialization() { final Optional<String> json = svc.serialize(mockEvent); assertTrue(json.isPresent(), "Serialization not present!"); assertTrue(json.get().contains("\"inbox\":\"info:ldn/inbox\""), "ldp:inbox not in serialization!"); }
@Test public void testError() { when(mockEvent.getIdentifier()).thenAnswer(inv -> { sneakyJsonException(); return rdf.createIRI("info:event/12456"); }); final Optional<String> json = svc.serialize(mockEvent); assertFalse(json.isPresent(), "exception didn't prevent serialization!"); }
@Test public void testSerializationStructureNoEmptyElements() throws Exception { when(mockEvent.getInbox()).thenReturn(empty()); when(mockEvent.getAgents()).thenReturn(emptyList()); when(mockEvent.getTargetTypes()).thenReturn(emptyList()); final Optional<String> json = svc.serialize(mockEvent); assertTrue(json.isPresent(), "Serialization not present!"); final ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") final Map<String, Object> map = mapper.readValue(json.get(), Map.class); assertTrue(map.containsKey("@context"), "@context property not in JSON structure!"); assertTrue(map.containsKey("id"), "id property not in JSON structure!"); assertTrue(map.containsKey("type"), "type property not in JSON strucutre!"); assertFalse(map.containsKey("inbox"), "inbox property unexpectedly in JSON structure!"); assertFalse(map.containsKey("actor"), "actor property unexpectedly in JSON structure!"); assertTrue(map.containsKey("object"), "object property not in JSON structure!"); assertTrue(map.containsKey("published"), "published property not in JSON structure!"); final List<?> types = (List<?>) map.get("type"); assertTrue(types.contains("Create"), "as:Create type not in type list!"); @SuppressWarnings("unchecked") final Map<String, Object> obj = (Map<String, Object>) map.get("object"); assertTrue(obj.containsKey("id"), "object id property not in JSON structure!"); assertFalse(obj.containsKey("type"), "empty object type unexpectedly in JSON structure!"); assertTrue(AS.getNamespace().contains((String) map.get("@context")), "AS namespace not in @context!"); assertTrue(map.get("id").equals("info:event/12345"), "id property has incorrect value!"); assertTrue(map.get("published").equals(time.toString()), "published property has incorrect value!"); }
@Test public void testSerializationStructure() throws Exception { when(mockEvent.getTypes()).thenReturn(asList(Create, Activity)); final Optional<String> json = svc.serialize(mockEvent); assertTrue(json.isPresent(), "Serialization not present!"); final ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") final Map<String, Object> map = mapper.readValue(json.get(), Map.class); assertTrue(map.containsKey("@context"), "@context property not in JSON structure!"); assertTrue(map.containsKey("id"), "id property not in JSON structure!"); assertTrue(map.containsKey("type"), "type property not in JSON structure!"); assertTrue(map.containsKey("inbox"), "inbox property not in JSON structure!"); assertTrue(map.containsKey("actor"), "actor property not in JSON structure!"); assertTrue(map.containsKey("object"), "object property not in JSON structure!"); assertTrue(map.containsKey("published"), "published property not in JSON structure!"); final List<?> types = (List<?>) map.get("type"); assertTrue(types.contains("Create"), "as:Create not in type list!"); assertTrue(types.contains(Activity.getIRIString()), "prov:Activity not in type list!"); assertTrue(AS.getNamespace().contains((String) map.get("@context")), "AS namespace not in @context!"); final List<?> actor = (List<?>) map.get("actor"); assertTrue(actor.contains("info:user/test"), "actor property has incorrect value!"); assertTrue(map.get("id").equals("info:event/12345"), "id property has incorrect value!"); assertTrue(map.get("inbox").equals("info:ldn/inbox"), "inbox property has incorrect value!"); assertTrue(map.get("published").equals(time.toString()), "published property has incorrect value!"); }