use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageRoutesExternalRoutedTest method test_group4__conversation.
@Test
// @Rollback would't work as camel still commits
@Commit
public void test_group4__conversation() throws Exception {
URI atomURI = newAtomURI();
URI socketURI = URI.create(atomURI.toString() + "#socket1");
URI atomURI2 = newAtomURI();
URI socketURI2 = URI.create(atomURI2.toString() + "#socket1");
URI atomURI3 = newAtomURI();
URI socketURI3 = URI.create(atomURI3.toString() + "#socket1");
URI atomURI4 = newAtomURI();
URI socketURI4 = URI.create(atomURI4.toString() + "#socket1");
URI groupAtomURI = newAtomURI();
URI groupSocketURI = URI.create(groupAtomURI.toString() + "#groupSocket");
prepareMockitoStubs(atomURI, socketURI, atomURI2, socketURI2);
Mockito.when(linkedDataSource.getDataForPublicResource(groupAtomURI)).then(x -> linkedDataService.getAtomDataset(groupAtomURI, false, null, WonAclEvalContext.allowAll()));
Mockito.when(linkedDataSource.getDataForPublicResource(atomURI3)).then(x -> linkedDataService.getAtomDataset(atomURI3, false, null, WonAclEvalContext.allowAll()));
Mockito.when(linkedDataSource.getDataForPublicResource(atomURI4)).then(x -> linkedDataService.getAtomDataset(atomURI4, false, null, WonAclEvalContext.allowAll()));
WonMessage createAtom1Msg = prepareFromOwner(makeCreateAtomMessage(atomURI, "/won/node/WonMessageRoutesTest/data/test-atom1.ttl"));
WonMessage createAtom2Msg = prepareFromOwner(makeCreateAtomMessage(atomURI2, "/won/node/WonMessageRoutesTest/data/test-atom1.ttl"));
WonMessage createAtom3Msg = prepareFromOwner(makeCreateAtomMessage(atomURI3, "/won/node/WonMessageRoutesTest/data/test-atom1.ttl"));
WonMessage createAtom4Msg = prepareFromOwner(makeCreateAtomMessage(atomURI4, "/won/node/WonMessageRoutesTest/data/test-atom1.ttl"));
WonMessage createGroupAtomMsg = prepareFromOwner(makeCreateAtomMessage(groupAtomURI, "/won/node/WonMessageRoutesTest/data/test-atom1__groupsocket.ttl"));
// set minimal expectations just so we can expect something and subsequently
// reset expectations
toOwnerMockEndpoint.expectedMessageCount(1);
toMatcherMockEndpoint.expectedMessageCount(1);
sendFromOwner(createAtom1Msg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
toOwnerMockEndpoint.expectedMessageCount(1);
toMatcherMockEndpoint.expectedMessageCount(1);
sendFromOwner(createAtom2Msg, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
toOwnerMockEndpoint.expectedMessageCount(1);
toMatcherMockEndpoint.expectedMessageCount(1);
sendFromOwner(createAtom3Msg, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
toOwnerMockEndpoint.expectedMessageCount(1);
toMatcherMockEndpoint.expectedMessageCount(1);
sendFromOwner(createAtom4Msg, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
toOwnerMockEndpoint.expectedMessageCount(1);
toMatcherMockEndpoint.expectedMessageCount(1);
sendFromOwner(createGroupAtomMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
// start connecting
WonMessage connectFromAtomMsg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(socketURI).recipient(groupSocketURI).content().text("Unittest connect with group, atom 1").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(connectFromAtomMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
WonMessage connectFromAtom2Msg = prepareFromExternalOwner(WonMessageBuilder.connect().sockets().sender(socketURI2).recipient(groupSocketURI).content().text("Unittest connect with group, atom 2").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(connectFromAtom2Msg, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
WonMessage connectFromAtom3Msg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(socketURI3).recipient(groupSocketURI).content().text("Unittest connect with group, atom 3").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(connectFromAtom3Msg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
WonMessage connectFromAtom4Msg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(socketURI4).recipient(groupSocketURI).content().text("Unittest connect with group, atom 4").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(connectFromAtom4Msg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
// group: accept connects
WonMessage connectFromGroupMsg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(groupSocketURI).recipient(socketURI).content().text("Unittest connect (completing the group handshake with atom 1)").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(2);
sendFromOwner(connectFromGroupMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
connectFromGroupMsg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(groupSocketURI).recipient(socketURI2).content().text("Unittest connect (completing the group handshake with atom 2)").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(2);
sendFromOwner(connectFromGroupMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
connectFromGroupMsg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(groupSocketURI).recipient(socketURI3).content().text("Unittest connect (completing the group handshake with atom 3)").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(2);
sendFromOwner(connectFromGroupMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
connectFromGroupMsg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(groupSocketURI).recipient(socketURI4).content().text("Unittest connect (completing the group handshake with atom 4)").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(2);
sendFromOwner(connectFromGroupMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
Optional<Connection> con = connectionRepository.findOneBySocketURIAndTargetSocketURI(socketURI, groupSocketURI);
Connection expected = new Connection();
expected.setState(ConnectionState.CONNECTED);
expected.setSocketURI(socketURI);
expected.setTargetSocketURI(groupSocketURI);
assertConnectionAsExpected(expected, con);
con = connectionRepository.findOneBySocketURIAndTargetSocketURI(socketURI2, groupSocketURI);
expected = new Connection();
expected.setState(ConnectionState.CONNECTED);
expected.setSocketURI(socketURI2);
expected.setTargetSocketURI(groupSocketURI);
assertConnectionAsExpected(expected, con);
// send a message to the group
WonMessage textMsgToGroup = prepareFromOwner(WonMessageBuilder.connectionMessage().sockets().sender(socketURI).recipient(groupSocketURI).content().text("Message to the group from atom1").build());
// TODO this includes two responses to the group's owner, which
toOwnerMockEndpoint.expectedMessageCount(5);
// should not be delivered
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(textMsgToGroup, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(toOwnerMockEndpoint, toMatcherMockEndpoint);
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageRoutesExternalRoutedTest method test_conversation__proposal_accepted__then_canceled.
@Test
// @Rollback would't work as camel still commits
@Commit
public void test_conversation__proposal_accepted__then_canceled() throws Exception {
URI atomURI = newAtomURI();
URI socketURI = URI.create(atomURI.toString() + "#socket1");
URI atomURI2 = newAtomURI();
URI socketURI2 = URI.create(atomURI2.toString() + "#socket1");
prepareMockitoStubs(atomURI, socketURI, atomURI2, socketURI2);
MessageCollector collector = new MessageCollector();
WonMessage createAtom1Msg = prepareFromOwner(makeCreateAtomMessage(atomURI, "/won/node/WonMessageRoutesTest/data/test-atom1.ttl"));
WonMessage createAtom2Msg = prepareFromOwner(makeCreateAtomMessage(atomURI2, "/won/node/WonMessageRoutesTest/data/test-atom1.ttl"));
// set minimal expectations just so we can expect something and subsequently
// reset expectations
toOwnerMockEndpoint.expectedMessageCount(2);
toMatcherMockEndpoint.expectedMessageCount(2);
sendFromOwner(createAtom1Msg, OWNERAPPLICATION_ID_OWNER1);
sendFromOwner(createAtom2Msg, OWNERAPPLICATION_ID_OWNER2);
WonMessage socketHintMessage = prepareFromMatcher(WonMessageBuilder.socketHint().recipientSocket(socketURI).hintTargetSocket(socketURI2).hintScore(0.5).build());
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
toOwnerMockEndpoint.expectedMessageCount(1);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromMatcher(socketHintMessage);
// start connecting
WonMessage connectFromExternalMsg = prepareFromExternalOwner(WonMessageBuilder.connect().sockets().sender(socketURI2).recipient(socketURI).content().text("Unittest connect").build());
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
toMatcherMockEndpoint.expectedMessageCount(0);
toOwnerMockEndpoint.expectedMessageCount(3);
sendFromOwner(connectFromExternalMsg, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
WonMessage connectFromOwnerMsg = prepareFromOwner(WonMessageBuilder.connect().sockets().sender(socketURI).recipient(socketURI2).content().text("Unittest connect (completing the handshake)").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(2);
sendFromOwner(connectFromOwnerMsg, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
// // connect done
Optional<Connection> con = connectionRepository.findOneBySocketURIAndTargetSocketURI(socketURI, socketURI2);
// // send one message, which will be the content of the agreement
WonMessage contentForProposal = prepareFromOwner(WonMessageBuilder.connectionMessage().sockets().sender(con.get().getSocketURI()).recipient(con.get().getTargetSocketURI()).content().text(// something important
"We are best friends forever!").build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(contentForProposal, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
// now propose the previous message formally
WonMessage proposal = prepareFromOwner(WonMessageBuilder.connectionMessage().sockets().sender(con.get().getSocketURI()).recipient(con.get().getTargetSocketURI()).content().text("I am hereby proposing to be best friends forever").agreement().proposes(contentForProposal.getMessageURI()).build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(proposal, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
// now accept the proposal from the other side
WonMessage acceptMessage = prepareFromExternalOwner(WonMessageBuilder.connectionMessage().sockets().sender(con.get().getTargetSocketURI()).recipient(con.get().getSocketURI()).content().text("Thank you so much, of course I accept!").agreement().accepts(proposal.getMessageURI()).build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(acceptMessage, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
// now realize we made a mistake: propose to cancel
WonMessage proposalToCancel = prepareFromOwner(WonMessageBuilder.connectionMessage().sockets().sender(con.get().getSocketURI()).recipient(con.get().getTargetSocketURI()).content().text("Hey, I realized I did not want this. Can we cancel?").agreement().proposesToCancel(// remember, the uri of the agreement is the uri of the
proposal).build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(proposalToCancel, OWNERAPPLICATION_ID_OWNER1);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
// the other side is gentle and accepts the cancellation
WonMessage acceptCancelMessage = prepareFromExternalOwner(WonMessageBuilder.connectionMessage().sockets().sender(con.get().getTargetSocketURI()).recipient(con.get().getSocketURI()).content().text("Bummer. Sure.").agreement().accepts(proposalToCancel).build());
toOwnerMockEndpoint.expectedMessageCount(3);
toMatcherMockEndpoint.expectedMessageCount(0);
sendFromOwner(acceptCancelMessage, OWNERAPPLICATION_ID_OWNER2);
assertMockEndpointsSatisfiedAndReset(collector, toOwnerMockEndpoint, toMatcherMockEndpoint);
printConversationIfDebugging(collector);
assertCorrectAgreements("Wrong agreement content", collector.getCollected(), DatasetFactory.createGeneral());
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class AtomService method replaceAtom.
public Atom replaceAtom(final WonMessage wonMessage, WonAclEvaluator evaluator, OperationRequest request) throws NoSuchAtomException {
Dataset newAtomContent = wonMessage.getMessageContent();
List<WonMessage.AttachmentHolder> attachmentHolders = wonMessage.getAttachments();
// remove attachment and its signature from the atomContent
removeAttachmentsFromAtomContent(newAtomContent, attachmentHolders);
final AtomModelWrapper atomModelWrapper = new AtomModelWrapper(newAtomContent);
URI atomURI = getAtomURIAndCheck(atomModelWrapper);
if (evaluator != null) {
checkReplaceAllowed(newAtomContent, atomURI, evaluator, request);
}
checkCanThisMessageCreateOrModifyThisAtom(wonMessage, atomURI);
checkResourcesInAtomContent(atomModelWrapper);
URI messageURI = wonMessage.getMessageURI();
atomModelWrapper.renameResourceWithPrefix(messageURI.toString(), atomURI.toString());
final Atom atom = getAtomRequired(atomURI);
DatasetHolder datasetHolder = atom.getDatatsetHolder();
Dataset oldAtomDataset = atom.getDatatsetHolder().getDataset();
URI keyGraphUri = WonRelativeUriHelper.createKeyGraphURIForAtomURI(atom.getAtomURI());
// store the atom content
Iterator<String> oldGraphNames = oldAtomDataset.listNames();
String derivedDataName = atom.getAtomURI().toString() + "#derivedData";
while (oldGraphNames.hasNext()) {
String oldGraphName = oldGraphNames.next();
if (oldGraphName.equals(derivedDataName)) {
newAtomContent.addNamedModel(oldGraphName, oldAtomDataset.getNamedModel(oldGraphName));
continue;
}
if (!newAtomContent.containsNamedModel(oldGraphName)) {
// copy graphs not contained in the message from old to new state
// ie, graphs with same name are overwritten
newAtomContent.addNamedModel(oldGraphName, oldAtomDataset.getNamedModel(oldGraphName));
}
}
if (!newAtomContent.containsNamedModel(keyGraphUri.toString())) {
throw new WonProtocolException(String.format("Cannot replace %s, operation results in atom without key graph", atomURI));
}
// replace attachments
List<DatasetHolder> attachments = new ArrayList<>(attachmentHolders.size());
for (WonMessage.AttachmentHolder attachmentHolder : attachmentHolders) {
datasetHolder = new DatasetHolder(attachmentHolder.getDestinationUri(), attachmentHolder.getAttachmentDataset());
attachments.add(datasetHolder);
}
// rename the content graphs and signature graphs so they start with the atom
// uri
// analyzed change in socket data
Collection<String> sockets = getSocketsAndCheck(atomModelWrapper, atomURI);
List<Socket> existingSockets = socketRepository.findByAtomURI(atomURI);
Set<Socket> newSocketEntities = determineNewSockets(atomURI, existingSockets, atomModelWrapper);
Set<Socket> removedSockets = determineRemovedSockets(atomURI, existingSockets, atomModelWrapper);
Set<Socket> changedSockets = determineAndModifyChangedSockets(atomURI, existingSockets, atomModelWrapper);
// close connections for changed or removed sockets
removedSockets.stream().filter(socket -> connectionService.existOpenConnections(atomURI, socket.getSocketURI())).findFirst().ifPresent(socket -> new IllegalSocketModificationException("Cannot remove socket " + socket.getSocketURI() + ": socket has connections in state " + ConnectionState.CONNECTED));
changedSockets.stream().filter(socket -> connectionService.existOpenConnections(atomURI, socket.getSocketURI())).findFirst().ifPresent(socket -> new IllegalSocketModificationException("Cannot change socket " + socket.getSocketURI() + ": socket has connections in state " + ConnectionState.CONNECTED));
// add everything to the atom model class and save it
socketRepository.saveAll(newSocketEntities);
socketRepository.saveAll(changedSockets);
socketRepository.deleteAll(removedSockets);
socketAclService.addLocalSocketAcls(atomURI, newSocketEntities, newAtomContent);
socketAclService.removeLocalSocketAcls(atomURI, removedSockets, newAtomContent);
datasetHolder.setDataset(newAtomContent);
atom.setDatatsetHolder(datasetHolder);
atom.setAttachmentDatasetHolders(attachments);
dataDerivationService.deriveDataIfNecessary(atom);
return atomRepository.save(atom);
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageSlipComputerTests method makeCreateAtomMessage.
private WonMessage makeCreateAtomMessage(URI atomURI, String filename) throws IOException {
Dataset atom1Content = loadDatasetAndReplaceAtomURI(filename, atomURI);
WonMessage createAtom1Msg = WonMessageBuilder.createAtom().atom(atomURI).content().dataset(atom1Content).build();
return createAtom1Msg;
}
use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.
the class WonMessageRoutesExternalInterceptedTest method test_create_atom__error_contains_subpath.
@Test
// @Rollback would't work as camel still commits
@Commit
public void test_create_atom__error_contains_subpath() throws Exception {
URI atomURI = newAtomURI();
Dataset atom1Content = loadDatasetAndReplaceAtomURI("/won/node/WonMessageRoutesTest/data/test-atom1__error_contains_subpath.ttl", atomURI);
WonMessage createAtom1Msg = prepareFromOwner(WonMessageBuilder.createAtom().atom(atomURI).content().dataset(atom1Content).build());
// set expectations
toOwnerMockEndpoint.expectedMessageCount(1);
toOwnerMockEndpoint.expectedMessagesMatches(isFailureResponseTo(createAtom1Msg.getMessageURI()));
// send message
sendFromOwner(createAtom1Msg, OWNERAPPLICATION_ID_OWNER1);
toOwnerMockEndpoint.assertIsSatisfied();
Assert.assertFalse("No atom should have been created", atomService.getAtom(atomURI).isPresent());
}
Aggregations