Search in sources :

Example 1 with BulkAction

use of com.zimbra.soap.mail.type.BulkAction in project zm-mailbox by Zimbra.

the class SearchActionTest method testSearchActionRead.

@Test
public void testSearchActionRead() throws Exception {
    Account acct = Provisioning.getInstance().getAccountByName("testRead@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // Add two messages to inbox, one with search match and other with no match
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setFlags(Flag.BITMASK_UNREAD | Flag.BITMASK_MUTED);
    Message message1 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), dopt, null);
    Message message2 = mbox.addMessage(null, MailboxTestUtil.generateMessage("unmatched subject"), dopt, null);
    TypedIdList ids = mbox.getItemIds(null, 2);
    Assert.assertEquals(2, ids.size());
    Assert.assertEquals(true, message1.isUnread());
    Assert.assertEquals(true, message2.isUnread());
    SearchRequest sRequest = new SearchRequest();
    sRequest.setSearchTypes("conversation");
    // search with query 'test'
    sRequest.setQuery("test");
    BulkAction bAction = new BulkAction();
    // search action - mark search result with 'read'
    bAction.setOp(BulkAction.Operation.read);
    Map<String, Object> context = ServiceTestUtil.getRequestContext(acct);
    ZimbraSoapContext zsc = (ZimbraSoapContext) context.get(SoapEngine.ZIMBRA_CONTEXT);
    Element searchResponse = new Search().handle(zsc.jaxbToElement(sRequest), ServiceTestUtil.getRequestContext(acct));
    com.zimbra.soap.mail.message.SearchResponse sResponse = zsc.elementToJaxb(searchResponse);
    List<SearchHit> searchHits = sResponse.getSearchHits();
    ConvActionRequest req = SearchAction.getConvActionRequest(searchHits, "read");
    ConvAction convAction = new ConvAction();
    PowerMockito.stub(PowerMockito.method(SoapHttpTransport.class, "invokeWithoutSession")).toReturn(convAction.handle(zsc.jaxbToElement(req), ServiceTestUtil.getRequestContext(acct)));
    SearchAction.performAction(bAction, sRequest, searchHits, mbox, null);
    // check search result message is marked read
    Assert.assertEquals(false, message1.isUnread());
    Assert.assertEquals(true, message2.isUnread());
}
Also used : Account(com.zimbra.cs.account.Account) SearchRequest(com.zimbra.soap.mail.message.SearchRequest) Message(com.zimbra.cs.mailbox.Message) SearchHit(com.zimbra.soap.type.SearchHit) ConvActionRequest(com.zimbra.soap.mail.message.ConvActionRequest) Element(com.zimbra.common.soap.Element) BulkAction(com.zimbra.soap.mail.type.BulkAction) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with BulkAction

use of com.zimbra.soap.mail.type.BulkAction in project zm-mailbox by Zimbra.

the class SearchAction method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    SearchActionRequest req = zsc.elementToJaxb(request);
    SearchRequest searchRequest = req.getSearchRequest();
    Account acct = mbox.getAccount();
    Server server = Provisioning.getInstance().getServer(acct);
    String url = URLUtil.getSoapURL(server, false);
    com.zimbra.soap.mail.message.SearchResponse resp = null;
    Element searchResponse;
    SoapHttpTransport transport = new SoapHttpTransport(url);
    transport.setTargetAcctId(acct.getId());
    try {
        transport.setAuthToken(octxt.getAuthToken().getEncoded());
        searchResponse = transport.invokeWithoutSession(JaxbUtil.jaxbToElement(searchRequest));
        resp = (com.zimbra.soap.mail.message.SearchResponse) JaxbUtil.elementToJaxb(searchResponse);
        List<SearchHit> searchHits = resp.getSearchHits();
        BulkAction action = req.getBulkAction();
        performAction(action, searchRequest, searchHits, mbox, octxt);
    } catch (AuthTokenException | IOException | HttpException e) {
        throw ServiceException.FAILURE("Failed to execute search request", e);
    }
    SearchActionResponse searchActionResponse = new SearchActionResponse();
    return zsc.jaxbToElement(searchActionResponse);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) SearchRequest(com.zimbra.soap.mail.message.SearchRequest) Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) SearchHit(com.zimbra.soap.type.SearchHit) Element(com.zimbra.common.soap.Element) IOException(java.io.IOException) BulkAction(com.zimbra.soap.mail.type.BulkAction) SearchActionRequest(com.zimbra.soap.mail.message.SearchActionRequest) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) AuthTokenException(com.zimbra.cs.account.AuthTokenException) HttpException(org.apache.http.HttpException) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) SearchActionResponse(com.zimbra.soap.mail.message.SearchActionResponse)

Example 3 with BulkAction

use of com.zimbra.soap.mail.type.BulkAction in project zm-mailbox by Zimbra.

the class SearchActionTest method testSearchActionMove.

@Test
public void testSearchActionMove() throws Exception {
    Account acct = Provisioning.getInstance().getAccountByName("testMove@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // Add two messages to inbox, one with search match and other with no match
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setFlags(Flag.BITMASK_UNREAD | Flag.BITMASK_MUTED);
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), dopt, null);
    mbox.addMessage(null, MailboxTestUtil.generateMessage("unmatched subject"), dopt, null);
    TypedIdList ids = mbox.getItemIds(null, 2);
    Assert.assertEquals(2, ids.size());
    SearchRequest sRequest = new SearchRequest();
    sRequest.setSearchTypes("conversation");
    // search with query 'test'
    sRequest.setQuery("test");
    BulkAction bAction = new BulkAction();
    // search action - move search result to 'Trash'
    bAction.setOp(BulkAction.Operation.move);
    bAction.setFolder("Trash");
    Map<String, Object> context = ServiceTestUtil.getRequestContext(acct);
    ZimbraSoapContext zsc = (ZimbraSoapContext) context.get(SoapEngine.ZIMBRA_CONTEXT);
    Element searchResponse = new Search().handle(zsc.jaxbToElement(sRequest), ServiceTestUtil.getRequestContext(acct));
    com.zimbra.soap.mail.message.SearchResponse sResponse = zsc.elementToJaxb(searchResponse);
    List<SearchHit> searchHits = sResponse.getSearchHits();
    SearchAction.performAction(bAction, sRequest, searchHits, mbox, null);
    // check inbox contains only 1 unmatched mail item after move
    List<MailItem> mailItems = mbox.getItemList(null, MailItem.Type.MESSAGE, 2, com.zimbra.cs.index.SortBy.DATE_DESC);
    Assert.assertEquals(1, mailItems.size());
    Assert.assertEquals("unmatched subject", mailItems.get(0).getSubject());
    // check trash contains mail item having 'test subject' after move
    mailItems = mbox.getItemList(null, MailItem.Type.MESSAGE, 3, com.zimbra.cs.index.SortBy.DATE_DESC);
    Assert.assertEquals(1, mailItems.size());
    Assert.assertEquals("test subject", mailItems.get(0).getSubject());
}
Also used : Account(com.zimbra.cs.account.Account) SearchRequest(com.zimbra.soap.mail.message.SearchRequest) SearchHit(com.zimbra.soap.type.SearchHit) Element(com.zimbra.common.soap.Element) BulkAction(com.zimbra.soap.mail.type.BulkAction) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Element (com.zimbra.common.soap.Element)3 Account (com.zimbra.cs.account.Account)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 SearchRequest (com.zimbra.soap.mail.message.SearchRequest)3 BulkAction (com.zimbra.soap.mail.type.BulkAction)3 SearchHit (com.zimbra.soap.type.SearchHit)3 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)2 TypedIdList (com.zimbra.cs.mailbox.util.TypedIdList)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)1 AuthTokenException (com.zimbra.cs.account.AuthTokenException)1 Server (com.zimbra.cs.account.Server)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 Message (com.zimbra.cs.mailbox.Message)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ConvActionRequest (com.zimbra.soap.mail.message.ConvActionRequest)1 SearchActionRequest (com.zimbra.soap.mail.message.SearchActionRequest)1 SearchActionResponse (com.zimbra.soap.mail.message.SearchActionResponse)1