/** * * Copyright 2017 Fernando Ramirez * * 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 org.jivesoftware.smackx.avatar; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smackx.avatar.element.MetadataExtension; import org.jivesoftware.smackx.avatar.provider.MetadataProvider; import org.junit.Assert; import org.junit.Test; public class MetadataExtensionTest { private static final String metadataExtensionExample = "" + "" + ""; private static final String emptyMetadataExtensionExample = ""; private static final String metadataWithSeveralInfos = "" + "" + "" + "" + ""; private static final String metadataWithInfoAndPointers = "" + "" + "" + "" + "Ancapistan" + "Kropotkin" + "" + "" + "" + "" + "hard" + "2" + "" + "" + ""; @Test public void checkMetadataExtensionParse() throws Exception { String id = "357a8123a30844a3aa99861b6349264ba67a5694"; URL url = new URL("http://avatars.example.org/happy.gif"); long bytes = 23456; String type = "image/gif"; int pixelsHeight = 64; int pixelsWidth = 128; MetadataInfo info = new MetadataInfo(id, url, bytes, type, pixelsHeight, pixelsWidth); List infos = new ArrayList<>(); infos.add(info); MetadataExtension metadataExtension = new MetadataExtension(infos); Assert.assertEquals(metadataExtensionExample, metadataExtension.toXML().toString()); XmlPullParser parser = PacketParserUtils.getParserFor(metadataExtensionExample); MetadataExtension metadataExtensionFromProvider = new MetadataProvider().parse(parser); Assert.assertEquals(id, metadataExtensionFromProvider.getInfoElements().get(0).getId()); Assert.assertEquals(url, metadataExtensionFromProvider.getInfoElements().get(0).getUrl()); Assert.assertEquals(bytes, metadataExtensionFromProvider.getInfoElements().get(0).getBytes().intValue()); Assert.assertEquals(type, metadataExtensionFromProvider.getInfoElements().get(0).getType()); Assert.assertEquals(pixelsHeight, metadataExtensionFromProvider.getInfoElements().get(0).getHeight().intValue()); Assert.assertEquals(pixelsWidth, metadataExtensionFromProvider.getInfoElements().get(0).getWidth().intValue()); } @Test public void checkEmptyMetadataExtensionParse() throws Exception { MetadataExtension metadataExtension = new MetadataExtension(null); Assert.assertEquals(emptyMetadataExtensionExample, metadataExtension.toXML().toString()); } @Test public void checkSeveralInfosInMetadataExtension() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor(metadataWithSeveralInfos); MetadataExtension metadataExtensionFromProvider = new MetadataProvider().parse(parser); MetadataInfo info1 = metadataExtensionFromProvider.getInfoElements().get(0); MetadataInfo info2 = metadataExtensionFromProvider.getInfoElements().get(1); MetadataInfo info3 = metadataExtensionFromProvider.getInfoElements().get(2); Assert.assertEquals("111f4b3c50d7b0df729d299bc6f8e9ef9066971f", info1.getId()); Assert.assertNull(info1.getUrl()); Assert.assertEquals(12345, info1.getBytes().intValue()); Assert.assertEquals("image/png", info1.getType()); Assert.assertEquals(64, info1.getHeight().intValue()); Assert.assertEquals(64, info1.getWidth().intValue()); Assert.assertEquals("e279f80c38f99c1e7e53e262b440993b2f7eea57", info2.getId()); Assert.assertEquals(new URL("http://avatars.example.org/happy.png"), info2.getUrl()); Assert.assertEquals(12345, info2.getBytes().intValue()); Assert.assertEquals("image/png", info2.getType()); Assert.assertEquals(64, info2.getHeight().intValue()); Assert.assertEquals(128, info2.getWidth().intValue()); Assert.assertEquals("357a8123a30844a3aa99861b6349264ba67a5694", info3.getId()); Assert.assertEquals(new URL("http://avatars.example.org/happy.gif"), info3.getUrl()); Assert.assertEquals(23456, info3.getBytes().intValue()); Assert.assertEquals("image/gif", info3.getType()); Assert.assertEquals(64, info3.getHeight().intValue()); Assert.assertEquals(64, info3.getWidth().intValue()); } @Test public void checkInfosAndPointersParse() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor(metadataWithInfoAndPointers); MetadataExtension metadataExtensionFromProvider = new MetadataProvider().parse(parser); MetadataInfo info = metadataExtensionFromProvider.getInfoElements().get(0); Assert.assertEquals("111f4b3c50d7b0df729d299bc6f8e9ef9066971f", info.getId()); Assert.assertNull(info.getUrl()); Assert.assertEquals(12345, info.getBytes().intValue()); Assert.assertEquals("image/png", info.getType()); Assert.assertEquals(64, info.getHeight().intValue()); Assert.assertEquals(64, info.getWidth().intValue()); MetadataPointer pointer1 = metadataExtensionFromProvider.getPointerElements().get(0); Map fields1 = pointer1.getFields(); Assert.assertEquals("http://example.com/virtualworlds", pointer1.getNamespace()); Assert.assertEquals("Ancapistan", fields1.get("game")); Assert.assertEquals("Kropotkin", fields1.get("character")); MetadataPointer pointer2 = metadataExtensionFromProvider.getPointerElements().get(1); Map fields2 = pointer2.getFields(); Assert.assertEquals("http://sample.com/game", pointer2.getNamespace()); Assert.assertEquals("hard", fields2.get("level")); Assert.assertEquals("2", fields2.get("players")); } @Test public void createMetadataExtensionWithInfoAndPointer() { String id = "111f4b3c50d7b0df729d299bc6f8e9ef9066971f"; long bytes = 12345; String type = "image/png"; int pixelsHeight = 64; int pixelsWidth = 64; MetadataInfo info = new MetadataInfo(id, null, bytes, type, pixelsHeight, pixelsWidth); HashMap fields1 = new HashMap<>(); fields1.put("game", "Ancapistan"); fields1.put("character", "Kropotkin"); MetadataPointer pointer1 = new MetadataPointer("http://example.com/virtualworlds", fields1); HashMap fields2 = new HashMap<>(); fields2.put("level", "hard"); fields2.put("players", 2); MetadataPointer pointer2 = new MetadataPointer("http://sample.com/game", fields2); List infos = new ArrayList<>(); infos.add(info); List pointers = new ArrayList<>(); pointers.add(pointer1); pointers.add(pointer2); MetadataExtension metadataExtension = new MetadataExtension(infos, pointers); Assert.assertEquals(metadataWithInfoAndPointers, metadataExtension.toXML().toString()); } }