aboutsummaryrefslogtreecommitdiff
path: root/tests/src/com/android/ims/rcs/uce/presence/pidfparser/pidf/PresenceTest.java
blob: 99606f906b010617c653298b1363ae07b3c86f40 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ims.rcs.uce.presence.pidfparser.pidf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import android.net.Uri;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.ims.ImsTestBase;
import com.android.ims.rcs.uce.presence.pidfparser.PidfParserConstant;
import com.android.ims.rcs.uce.presence.pidfparser.PidfParserUtils;
import com.android.ims.rcs.uce.presence.pidfparser.capabilities.CapsConstant;
import com.android.ims.rcs.uce.presence.pidfparser.omapres.Description;
import com.android.ims.rcs.uce.presence.pidfparser.omapres.OmaPresConstant;
import com.android.ims.rcs.uce.presence.pidfparser.omapres.ServiceDescription;
import com.android.ims.rcs.uce.presence.pidfparser.omapres.ServiceId;
import com.android.ims.rcs.uce.presence.pidfparser.omapres.Version;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

@RunWith(AndroidJUnit4.class)
public class PresenceTest extends ImsTestBase {

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    @SmallTest
    public void testElementName() throws Exception {
        Presence presence = new Presence();

        assertEquals(PidfConstant.NAMESPACE, presence.getNamespace());
        assertEquals(Presence.ELEMENT_NAME, presence.getElementName());
    }

    @Test
    @SmallTest
    public void testSerializing() throws Exception {
        String contact = Uri.fromParts("sip", "test", null).toString();

        String serviceId1 = "service_id_01";
        String description1 = "description_test1";
        Tuple tuple1 = getTuple(Basic.OPEN, serviceId1, description1, contact);

        String serviceId2 = "service_id_02";
        String description2 = "description_test2";
        Tuple tuple2 = getTuple(Basic.OPEN, serviceId2, description2, contact);

        Presence presence = new Presence();
        presence.setEntity(contact);
        presence.addTuple(tuple1);
        presence.addTuple(tuple2);

        StringWriter writer = new StringWriter();
        XmlSerializer serializer = getXmlSerializer(writer);

        // Serializing
        serializer.startDocument(PidfParserConstant.ENCODING_UTF_8, true);
        presence.serialize(serializer);
        serializer.endDocument();
        serializer.flush();

        String result = writer.toString();

        String verificationServiceId1 = "<op:service-id>service_id_01</op:service-id>";
        String verificationDescription1 = "<op:description>description_test1</op:description>";
        String verificationServiceId2 = "<op:service-id>service_id_02</op:service-id>";
        String verificationDescription2 = "<op:description>description_test2</op:description>";
        String verificationContact = "<contact>sip:test</contact>";

        assertTrue(result.contains(verificationServiceId1));
        assertTrue(result.contains(verificationDescription1));
        assertTrue(result.contains(verificationServiceId2));
        assertTrue(result.contains(verificationDescription2));
        assertTrue(result.contains(verificationContact));
    }

    @Test
    @SmallTest
    public void testParsing() throws Exception {
        final String contact = Uri.fromParts("sip", "test", null).toString();
        final String serviceId = "service_id_01";
        final String version = "1.0";
        final String description = "description_test";

        StringBuilder presenceExample = new StringBuilder();
        presenceExample.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>")
                .append("<presence entity=\"").append(contact).append("\"")
                .append(" xmlns=\"urn:ietf:params:xml:ns:pidf\"")
                .append(" xmlns:op=\"urn:oma:xml:prs:pidf:oma-pres\"")
                .append(" xmlns:caps=\"urn:ietf:params:xml:ns:pidf:caps\">")
                .append("<tuple id=\"tid0\"><status><basic>open</basic></status>")
                .append("<op:service-description>")
                .append("<op:service-id>").append(serviceId).append("</op:service-id>")
                .append("<op:version>").append(version).append("</op:version>")
                .append("<op:description>").append(description).append("</op:description>")
                .append("</op:service-description>")
                .append("<contact>sip:test</contact></tuple></presence>");

        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        Reader reader = new StringReader(presenceExample.toString());
        parser.setInput(reader);

        Presence presence = null;
        int nextType = parser.next();

        // Find the start tag
        do {
            if (nextType == XmlPullParser.START_TAG
                    && Presence.ELEMENT_NAME.equals(parser.getName())) {
                presence = new Presence();
                presence.parse(parser);
                break;
            }
            nextType = parser.next();
        } while(nextType != XmlPullParser.END_DOCUMENT);

        reader.close();

        assertNotNull(presence);
        assertEquals(contact, presence.getEntity());

        List<Tuple> tupleList = presence.getTupleList();
        assertNotNull(tupleList);
        assertEquals(1, tupleList.size());

        Tuple tuple = tupleList.get(0);
        assertEquals(serviceId, PidfParserUtils.getTupleServiceId(tuple));
        assertEquals(version, PidfParserUtils.getTupleServiceVersion(tuple));
        assertEquals(description, PidfParserUtils.getTupleServiceDescription(tuple));
        assertEquals(contact, PidfParserUtils.getTupleContact(tuple));
    }

    private Tuple getTuple(String statusValue, String serviceIdValue, String descValue,
            String contactValue) {
        Basic basic = new Basic(statusValue);
        Status status = new Status();
        status.setBasic(basic);

        ServiceId serviceId = new ServiceId(serviceIdValue);
        Version version = new Version(1, 0);
        Description description = new Description(descValue);
        ServiceDescription serviceDescription = new ServiceDescription();
        serviceDescription.setServiceId(serviceId);
        serviceDescription.setVersion(version);
        serviceDescription.setDescription(description);

        Contact contact = new Contact();
        contact.setContact(contactValue);

        Tuple tuple = new Tuple();
        tuple.setStatus(status);
        tuple.setServiceDescription(serviceDescription);
        tuple.setContact(contact);

        return tuple;
    }

    private XmlSerializer getXmlSerializer(StringWriter writer)
            throws XmlPullParserException, IOException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlSerializer serializer = factory.newSerializer();
        serializer.setOutput(writer);
        serializer.setPrefix("", PidfConstant.NAMESPACE);
        serializer.setPrefix("op", OmaPresConstant.NAMESPACE);
        serializer.setPrefix("caps", CapsConstant.NAMESPACE);
        return serializer;
    }
}