/** * Populate a ActivityStreamMessage from an Event. * * @param event The event * @return an ActivityStreamMessage */ public static ActivityStreamMessage from(final Event event) { final ActivityStreamMessage msg = new ActivityStreamMessage(); msg.id = event.getIdentifier().getIRIString(); msg.type = event.getTypes().stream().map(IRI::getIRIString) .map(type -> type.startsWith(AS.getNamespace()) ? type.substring(AS.getNamespace().length()) : type) .collect(toList()); msg.published = event.getCreated().toString(); final List<String> actors = event.getAgents().stream().map(IRI::getIRIString).collect(toList()); msg.actor = actors.isEmpty() ? null : actors; event.getInbox().map(IRI::getIRIString).ifPresent(inbox -> msg.inbox = inbox); event.getTarget().map(IRI::getIRIString).ifPresent(target -> msg.object = new EventResource(target, event.getTargetTypes().stream().map(IRI::getIRIString).collect(toList()))); return msg; } }
@Test public void testEmptyEvent() { final IRI resource = rdf.createIRI(identifier); final Event event = new SimpleEvent(identifier, agent, emptyList(), emptyList()); assertEquals(of(resource), event.getTarget(), "Incorrect target resource!"); assertTrue(event.getAgents().contains(agent), "Unexpected agent list!"); assertTrue(event.getTargetTypes().isEmpty(), "Unexpected target types!"); assertTrue(event.getTypes().isEmpty(), "Unexpected event types!"); assertFalse(event.getInbox().isPresent()); } }
@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!"); }
@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)); }); }
@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 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!"); }
@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)); }); }
/** * Populate a ActivityStreamMessage from an Event. * * @param event The event * @return an ActivityStreamMessage */ public static ActivityStreamMessage from(final Event event) { final ActivityStreamMessage msg = new ActivityStreamMessage(); msg.id = event.getIdentifier().getIRIString(); msg.type = event.getTypes().stream().map(IRI::getIRIString) .map(type -> type.startsWith(AS.getNamespace()) ? type.substring(AS.getNamespace().length()) : type) .collect(toList()); msg.published = event.getCreated().toString(); final List<String> actors = event.getAgents().stream().map(IRI::getIRIString).collect(toList()); msg.actor = actors.isEmpty() ? null : actors; event.getInbox().map(IRI::getIRIString).ifPresent(inbox -> msg.inbox = inbox); event.getTarget().map(IRI::getIRIString).ifPresent(target -> msg.object = new EventResource(target, event.getTargetTypes().stream().map(IRI::getIRIString).collect(toList()))); return msg; } }
@BeforeEach public void setUp() { initMocks(this); when(mockEvent.getTarget()).thenReturn(of(rdf.createIRI("trellis:data/resource"))); when(mockEvent.getAgents()).thenReturn(singleton(Trellis.AdministratorAgent)); when(mockEvent.getIdentifier()).thenReturn(rdf.createIRI("urn:test")); when(mockEvent.getCreated()).thenReturn(time); when(mockEvent.getTypes()).thenReturn(singleton(AS.Update)); when(mockEvent.getTargetTypes()).thenReturn(singleton(LDP.RDFSource)); when(mockEvent.getInbox()).thenReturn(empty()); }
@BeforeEach public void setUp() throws IOException { initMocks(this); when(mockEvent.getAgents()).thenReturn(singleton(Trellis.AdministratorAgent)); when(mockEvent.getCreated()).thenReturn(time); when(mockEvent.getIdentifier()).thenReturn(rdf.createIRI("urn:amqp:test")); when(mockEvent.getTypes()).thenReturn(singleton(AS.Update)); when(mockEvent.getTarget()).thenReturn(of(rdf.createIRI(TRELLIS_DATA_PREFIX + "resource"))); when(mockEvent.getTargetTypes()).thenReturn(singleton(LDP.RDFSource)); when(mockEvent.getInbox()).thenReturn(empty()); doNothing().when(mockChannel).basicPublish(eq(exchangeName), eq(queueName), anyBoolean(), anyBoolean(), any(BasicProperties.class), any(byte[].class)); }
@BeforeEach public void setUp() { initMocks(this); when(mockEvent.getIdentifier()).thenReturn(rdf.createIRI("info:event/12345")); when(mockEvent.getAgents()).thenReturn(singleton(rdf.createIRI("info:user/test"))); when(mockEvent.getTarget()).thenReturn(of(rdf.createIRI("trellis:data/resource"))); when(mockEvent.getTypes()).thenReturn(singleton(Create)); when(mockEvent.getTargetTypes()).thenReturn(singleton(Container)); when(mockEvent.getInbox()).thenReturn(of(rdf.createIRI("info:ldn/inbox"))); when(mockEvent.getCreated()).thenReturn(time); }
@BeforeEach public void setUp() throws JMSException { initMocks(this); when(mockEvent.getAgents()).thenReturn(singleton(Trellis.AdministratorAgent)); when(mockEvent.getCreated()).thenReturn(time); when(mockEvent.getIdentifier()).thenReturn(rdf.createIRI("urn:jms:test")); when(mockEvent.getTypes()).thenReturn(singleton(AS.Update)); when(mockEvent.getTarget()).thenReturn(of(rdf.createIRI(TRELLIS_DATA_PREFIX + "a-resource"))); when(mockEvent.getTargetTypes()).thenReturn(singleton(LDP.RDFSource)); when(mockEvent.getInbox()).thenReturn(empty()); when(mockConnection.createSession(anyBoolean(), eq(AUTO_ACKNOWLEDGE))).thenReturn(mockSession); when(mockSession.createQueue(eq(queueName))).thenReturn(mockQueue); when(mockSession.createTextMessage(anyString())).thenReturn(mockMessage); when(mockSession.createProducer(any(Queue.class))).thenReturn(mockProducer); doNothing().when(mockProducer).send(any(TextMessage.class)); }
@Test public void testSimpleEvent() { final IRI resource = rdf.createIRI(identifier); final Instant time = now(); final Event event = new SimpleEvent(identifier, agent, asList(PROV.Activity, AS.Create), asList(LDP.RDFSource, SKOS.Concept)); assertFalse(time.isAfter(event.getCreated()), "Non-sequential events!"); assertTrue(event.getIdentifier().getIRIString().startsWith("urn:uuid:"), "Incorrect ID prefix for event!"); assertEquals(of(resource), event.getTarget(), "Incorrect target resource!"); assertEquals(1L, event.getAgents().size(), "Incorrect agent count!"); assertTrue(event.getAgents().contains(agent), "Incorrect agent value!"); final Collection<IRI> targetTypes = event.getTargetTypes(); assertEquals(2L, targetTypes.size(), "Incorrect target type size!"); assertTrue(targetTypes.contains(LDP.RDFSource), "Missing ldp:RDFSource type!"); assertTrue(targetTypes.contains(SKOS.Concept), "Missing skos:Concept type!"); final Collection<IRI> eventTypes = event.getTypes(); assertEquals(2L, eventTypes.size(), "Incorrect event type size!"); assertTrue(eventTypes.contains(AS.Create), "Missing as:Create from event type!"); assertTrue(eventTypes.contains(PROV.Activity), "Missing prov:Activity from event type!"); assertFalse(event.getInbox().isPresent()); }