1 /* 2 * Copyright (C) 2021 Ignite Realtime Foundation. All rights reserved. 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 package org.jivesoftware.openfire.muc; 17 18 import org.dom4j.Element; 19 import org.junit.Before; 20 import org.junit.Test; 21 import org.junit.runner.RunWith; 22 import org.mockito.Mock; 23 import org.mockito.junit.MockitoJUnitRunner; 24 import org.xmpp.packet.JID; 25 import org.xmpp.packet.Presence; 26 27 import javax.xml.namespace.QName; 28 import java.io.ByteArrayInputStream; 29 import java.io.ByteArrayOutputStream; 30 import java.io.ObjectInputStream; 31 import java.io.ObjectOutputStream; 32 import java.lang.reflect.Field; 33 import java.util.ArrayList; 34 35 import static org.junit.Assert.*; 36 import static org.mockito.Mockito.doReturn; 37 38 /** 39 * Unit tests that verify the implementation of {@link MUCRole}. 40 * 41 * @author Guus der Kinderen, guus.der.kinderen@gmail.com 42 */ 43 @RunWith(MockitoJUnitRunner.class) 44 public class MUCRoleTest { 45 46 @Mock 47 private MUCRoom mockRoom; 48 49 @Before setup()50 public void setup() throws Exception { 51 doReturn(new JID("testroom@conference.example.org")).when(mockRoom).getJID(); 52 } 53 54 /** 55 * Asserts that when a populated instance of MUCRole is serialized and the resulting data deserialized again, an 56 * instance results that is equal to the original input. 57 */ 58 @Test testExternalizedEquals()59 public void testExternalizedEquals() throws Exception 60 { 61 // Setup test fixture. 62 final JID userAddress = new JID("unittest@example.org"); 63 final String nickname = "nickname for test"; 64 final MUCRole.Role role = MUCRole.Role.participant; 65 final MUCRole.Affiliation affiliation = MUCRole.Affiliation.admin; 66 final Presence presence = new Presence(); 67 presence.setFrom(userAddress); 68 presence.setTo(mockRoom.getJID()); 69 presence.addChildElement("x", "http://jabber.org/protocol/muc"); 70 presence.addChildElement("x", "http://jivesoftware.org/protocol/muc").addElement("deaf-occupant"); 71 final MUCRole input = new MUCRole(mockRoom, nickname, role, affiliation, userAddress, presence); // Set all fields to a non-default value, for a more specific test! 72 73 // Execute system under test. 74 final byte[] serialized; 75 try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 76 final ObjectOutputStream oos = new ObjectOutputStream(baos) ) { 77 oos.writeObject(input); 78 serialized = baos.toByteArray(); 79 } 80 81 final Object result; 82 try (final ByteArrayInputStream bais = new ByteArrayInputStream(serialized); 83 final ObjectInputStream ois = new ObjectInputStream(bais)) { 84 result = ois.readObject(); 85 } 86 87 // Verify results. 88 assertNotNull(result); 89 assertTrue(result instanceof MUCRole); 90 assertEquals(input, result); 91 assertEquals(input.getAffiliation(), ((MUCRole) result).getAffiliation()); 92 assertEquals(input.getNickname(), ((MUCRole) result).getNickname()); 93 assertEquals(input.getPresence().toXML(), ((MUCRole) result).getPresence().toXML()); 94 assertEquals(input.getReportedFmucAddress(), ((MUCRole) result).getReportedFmucAddress()); 95 assertEquals(input.getRole(), ((MUCRole) result).getRole()); 96 assertEquals(input.getRoleAddress(), ((MUCRole) result).getRoleAddress()); 97 assertEquals(input.getUserAddress(), ((MUCRole) result).getUserAddress()); 98 assertEquals(input.isRemoteFmuc(), ((MUCRole) result).isRemoteFmuc()); 99 assertEquals(input.isVoiceOnly(), ((MUCRole) result).isVoiceOnly()); 100 101 final Field extendedInformationField = MUCRole.class.getDeclaredField("extendedInformation"); 102 extendedInformationField.setAccessible(true); 103 final Element expectedExtendedInformation = (Element) extendedInformationField.get(input); 104 final Element actualExtendedInformation = (Element) extendedInformationField.get(result); 105 extendedInformationField.setAccessible(false); 106 107 assertEquals(expectedExtendedInformation.asXML(), actualExtendedInformation.asXML()); 108 109 // TODO Worry why the cached sizes differ (it's caused by the presence and extendedInformation element sizes). 110 // assertEquals(input.getCachedSize(), ((MUCRole) result).getCachedSize()); 111 } 112 } 113