1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ims.rcs.uce.presence.pidfparser.pidf; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertTrue; 22 23 import androidx.test.ext.junit.runners.AndroidJUnit4; 24 import androidx.test.filters.SmallTest; 25 26 import com.android.ims.ImsTestBase; 27 import com.android.ims.rcs.uce.presence.pidfparser.PidfParserConstant; 28 29 import java.io.IOException; 30 import java.io.Reader; 31 import java.io.StringReader; 32 import java.io.StringWriter; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.xmlpull.v1.XmlPullParser; 39 import org.xmlpull.v1.XmlPullParserException; 40 import org.xmlpull.v1.XmlPullParserFactory; 41 import org.xmlpull.v1.XmlSerializer; 42 43 @RunWith(AndroidJUnit4.class) 44 public class NoteTest extends ImsTestBase { 45 46 @Before setUp()47 public void setUp() throws Exception { 48 super.setUp(); 49 } 50 51 @After tearDown()52 public void tearDown() throws Exception { 53 super.tearDown(); 54 } 55 56 @Test 57 @SmallTest testElementName()58 public void testElementName() throws Exception { 59 Note note = new Note(); 60 61 assertEquals(PidfConstant.NAMESPACE, note.getNamespace()); 62 assertEquals(Note.ELEMENT_NAME, note.getElementName()); 63 } 64 65 @Test 66 @SmallTest testSerializing()67 public void testSerializing() throws Exception { 68 final String noteValue = "Note test"; 69 70 Note note = new Note(noteValue); 71 StringWriter writer = new StringWriter(); 72 XmlSerializer serializer = getXmlSerializer(writer); 73 74 // Serializing 75 serializer.startDocument(PidfParserConstant.ENCODING_UTF_8, true); 76 note.serialize(serializer); 77 serializer.endDocument(); 78 serializer.flush(); 79 80 final String result = writer.toString(); 81 82 StringBuilder verificationBuilder = new StringBuilder(); 83 verificationBuilder.append("<note xmlns=\"").append(PidfConstant.NAMESPACE).append("\">") 84 .append(noteValue).append("</note>"); 85 assertTrue(result.contains(verificationBuilder.toString())); 86 } 87 88 @Test 89 @SmallTest testParsing()90 public void testParsing() throws Exception { 91 final String noteValue = "Note test"; 92 93 StringBuilder noteExample = new StringBuilder(); 94 noteExample.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>") 95 .append("<note xmlns=\"urn:ietf:params:xml:ns:pidf\">") 96 .append(noteValue) 97 .append("</note>"); 98 99 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); 100 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 101 Reader reader = new StringReader(noteExample.toString()); 102 parser.setInput(reader); 103 104 Note note = null; 105 int nextType = parser.next(); 106 107 // Find the start tag 108 do { 109 if (nextType == XmlPullParser.START_TAG 110 && Note.ELEMENT_NAME.equals(parser.getName())) { 111 note = new Note(); 112 note.parse(parser); 113 break; 114 } 115 nextType = parser.next(); 116 } while(nextType != XmlPullParser.END_DOCUMENT); 117 118 reader.close(); 119 120 assertNotNull(note); 121 assertEquals(noteValue, note.getNote()); 122 } 123 getXmlSerializer(StringWriter writer)124 private XmlSerializer getXmlSerializer(StringWriter writer) 125 throws XmlPullParserException, IOException { 126 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 127 XmlSerializer serializer = factory.newSerializer(); 128 serializer.setOutput(writer); 129 serializer.setPrefix("", PidfConstant.NAMESPACE); 130 return serializer; 131 } 132 } 133