summaryrefslogtreecommitdiffstats
blob: a334285d504a7cc45aebc6e0b940c7a3f9d00b4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (c) 2012, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2011, Kevin Smith
 * All rights reserved.
 */
package com.isode.stroke.parser.payloadparsers;

import static org.junit.Assert.*;

import org.junit.Test;
import com.isode.stroke.elements.MUCDestroyPayload;
import com.isode.stroke.elements.MUCItem;
import com.isode.stroke.elements.MUCOccupant;
import com.isode.stroke.elements.MUCUserPayload;
import com.isode.stroke.elements.Payload;
import com.isode.stroke.eventloop.DummyEventLoop;
import com.isode.stroke.jid.JID;

public class MUCUserPayloadParserTest {
    
    private static MUCUserPayload parse(String xmlString) {
        DummyEventLoop eventLoop = new DummyEventLoop();
        PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
        assertTrue(parser.parse(xmlString));

        Payload payload = null;
        do {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            eventLoop.processEvents();
            payload = parser.getPayload();
        } while (payload == null);

        return (MUCUserPayload) payload;
    }

    @Test
    public void testParse() {
        boolean found110 = false;
        boolean found210 = false;

        MUCUserPayload payload = parse("<x xmlns=\"http://jabber.org/protocol/muc#user\"><status code='110'/>" +
                "<item affiliation=\"owner\" role=\"visitor\"><actor jid=\"kev@tester.lit\"/>" +
        "<reason>malice</reason></item><status code='210'/></x>");

        for (MUCUserPayload.StatusCode status : payload.getStatusCodes()) {
            if (status.code == 110) found110 = true;
            if (status.code == 210) found210 = true;
        }

        MUCItem item = payload.getItems().get(0);
        assertEquals(MUCOccupant.Affiliation.Owner, item.affiliation);
        assertEquals(MUCOccupant.Role.Visitor, item.role);
        assertEquals(new JID("kev@tester.lit"), item.actor);
        assertEquals("malice", item.reason);
        assertTrue(found110);
        assertTrue(found210);
    }

    @Test
    public void testParseEmpty() {        
        MUCUserPayload payload = parse("<x xmlns=\"http://jabber.org/protocol/muc#user\">" +
    "<destroy jid='alice@wonderland.lit'><reason>bert</reason></destroy></x>");
        assertTrue(payload != null);
        assertTrue(payload.getItems().isEmpty());
    }
    
    @Test
    public void testParseDestroy() {
        MUCUserPayload payload = parse("<x xmlns=\"http://jabber.org/protocol/muc#user\">" +
        "<destroy jid='alice@wonderland.lit'><reason>bert</reason></destroy></x>");
        assertTrue(payload!= null);
        MUCDestroyPayload destroy = (MUCDestroyPayload)(payload.getPayload());
        assertTrue(destroy != null);
        assertEquals("bert", destroy.getReason());
        assertEquals(new JID("alice@wonderland.lit"), destroy.getNewVenue());
    }

    @Test
    public void testParseInvite() {
        MUCUserPayload payload = parse("<x xmlns=\"http://jabber.org/protocol/muc#user\">" +
                "<invite from='crone1@shakespeare.lit/desktop' to='alice@wonderland.lit/xxx'>      " +
                "<reason>Hey Hecate, this is the place for all good witches!</reason>    " +
        "</invite>    <password>cauldronburn</password></x>");
        assertTrue(payload != null);
        assertTrue(payload.getInvite() != null);
        assertTrue(payload.getPassword() != null);
        assertEquals("cauldronburn", payload.getPassword());
        MUCUserPayload.Invite invite = payload.getInvite();
        assertEquals("Hey Hecate, this is the place for all good witches!", invite.reason);
        assertEquals(new JID("crone1@shakespeare.lit/desktop"), invite.from);
        assertEquals(new JID("alice@wonderland.lit/xxx"), invite.to);
    }

}