public void sendMessage(Message message) { sentMessages.add(message); logger.info("Sending message " + message.getContent()); String receiver = message.getTo(); Agent agentTo = agents.get(receiver); if (agentTo != null) { agentTo.receive(message); } else { logger.info( "Could not deliver message <" + message.getContent() + " to unknown agent " + receiver); } }
@Test public void testUnhandledMessage() { Message msg = new Message(); msg.setFrom("SantaClaus"); msg.setTo(AGENT_NAME); msg.setRequestId("SomeRequest"); msg.setContent(new RegisterAsk("")); agent.receive(msg); sleep(1 * WAITMS); Assert.assertEquals("No message should have been sent", 0, agent.getMessagesSent().size()); }
@Override protected boolean isConditionMet() { List<Message> toHttp = mta.getMessagesToHttp(); for (Message msg : toHttp) { MessageContent content = msg.getContent(); if (content instanceof ErrorNotify) { if (((ErrorNotify) content).getError().equals(error)) { return true; } } } return false; }
@Override protected Message askUserAgentForAuthentication(Agent agent, Message loginMsg) throws TimeoutException { String login = ((LoginAsk) loginMsg.getContent()).getLogin(); MessageContent tell = null; if (login.equals(TIMEOUT_LOGIN)) { throw new TimeoutException(); } else if (login.equals(WRONG_LOGIN)) { tell = new ErrorNotify(ErrorConstants.LOGIN_WRONG_PASSWORD); } else { tell = getLoginTell(login); } Message message = loginMsg.tell(tell); return message; }
private void handleSynonymousTermsAsk(Message message, SynonymousTermsAsk content) { TermInfos termInfos = relatedTermsService.getTermInfos(Arrays.asList(content.getTerm())); List<TermInfo> l = termInfos.getTermInfos(); List<Term> terms = new ArrayList<Term>(); for (TermInfo termInfo : l) { terms.add(termInfo.getTerm()); } send(message.tell(new SynonymousTermsTell(terms))); }
@Test public void testReceiveAliveAsk() { Message msg = new Message(); msg.setFrom("SantaClaus"); msg.setTo(AGENT_NAME); msg.setRequestId("SomeRequest"); msg.setContent(new AliveAsk()); agent.receive(msg); (new AssertWaiter() { @Override protected boolean isConditionMet() { System.out.println(agent.getMessagesSent().toString()); return agent.getMessagesSent().toString().contains("AliveTell"); } }) .assertGetsOkay("Tell sent", 1 * WAITMS, 50); }
@Override protected boolean work(Message message) { boolean handled = true; MessageContent content = message.getContent(); if (content instanceof DeleteLibraryNotify) { DeleteLibraryNotify n = (DeleteLibraryNotify) content; handleDeleteLibrary(message, n); } else { handled = false; } return handled; }
@Test public void testReceiveCancelRequestNotify() { String initialRequestId = "SomeRequest"; Message longRunningMsg = new Message(); longRunningMsg.setFrom("Eve"); longRunningMsg.setTo("SantaClaus"); longRunningMsg.setRequestId(initialRequestId); DocumentQuery query = getQuery(40000, "Dummy"); DocumentQueryAsk ask = new DocumentQueryAsk(query, null); longRunningMsg.setContent(ask); getLogger().info("First message"); agent.receive(longRunningMsg); // Wait a bit until the request is created. new RunningHandlerCountCheck(agent, 1).assertStaysOkay("1", 2000, 10); // Now let the LongRunningMockRequestHandler run some time sleep(2000); // Now send the cancel message Message cancelMsg = new Message(); cancelMsg.setFrom("Eve"); cancelMsg.setTo("SantaClaus"); cancelMsg.setRequestId("SomeRequest2"); cancelMsg.setContent(new CancelRequestNotify(initialRequestId)); getLogger().info("Second message"); agent.receive(cancelMsg); // Wait until no handlers new RunningHandlerCountCheck(agent, 0).assertGetsOkay("2", 2000, 10); Assert.assertEquals( "[From: SantaClaus / To: Eve / Request: SomeRequest / Message: {DocumentQueryStoredTell {DataList []}}]", // agent.getMessagesSent().toString()); }
@Override protected boolean work(Message message) { boolean handled = true; MessageContent content = message.getContent(); if (content instanceof SynonymousTermsAsk) { handleSynonymousTermsAsk(message, (SynonymousTermsAsk) content); } else { handled = false; } return handled; }
@Override protected boolean isConditionMet() { boolean contains = false; List<Message> sent = orb.getSentMessages(); for (Message recMsg : sent) { if (recMsg != null) { boolean toOk = recMsg.getTo().equals(message.getTo()); boolean fromOk = recMsg.getFrom().equals(message.getFrom()); boolean contOk = recMsg.getContent().equals(message.getContent()); if (toOk && fromOk && contOk) { contains = true; break; } } } return contains; }