Skip to content

Commit

Permalink
OF-2612: Migrate unit test implementation to JUnit 5
Browse files Browse the repository at this point in the history
Most unit tests are refactored to use JUnit 5 instead of 4.

Notable exceptions:
- FlattenNestedGroupsTest, which uses org.zapodot:embedded-ldap-junit which currently is based on JUnit 4 (JUnit 5 support being tracked in zapodot/embedded-ldap-junit#57 )
- DBUnit-based tests
  • Loading branch information
guusdk committed Jun 26, 2023
1 parent fe41fa5 commit dfb7d9d
Show file tree
Hide file tree
Showing 66 changed files with 1,188 additions and 656 deletions.
52 changes: 26 additions & 26 deletions xmppserver/src/test/java/dom/io/XMPPPacketReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2017-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -12,22 +12,22 @@
* 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 dom.io;

import org.dom4j.Document;
import org.dom4j.Namespace;
import org.dom4j.io.XMPPPacketReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xmlpull.v1.XmlPullParser;

import java.io.StringReader;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

/**
* Unit tests that verify the functionality of {@link XMPPPacketReader}
*
Expand All @@ -37,7 +37,7 @@ public class XMPPPacketReaderTest
{
private XMPPPacketReader packetReader;

@Before
@BeforeEach
public void setup()
{
packetReader = new XMPPPacketReader();
Expand All @@ -61,7 +61,7 @@ public void testStripContentNamespace() throws Exception
final Document result = packetReader.read( new StringReader( input ) );

// Verify result.
Assert.assertFalse( result.asXML().contains( "jabber:client" ) );
assertFalse( result.asXML().contains( "jabber:client" ) );
}

/**
Expand All @@ -82,7 +82,7 @@ public void testStripPrefixFreeCanonicalization() throws Exception
final Document result = packetReader.read( new StringReader( input ) );

// Verify result.
Assert.assertFalse( result.asXML().contains( "jabber:client" ) );
assertFalse( result.asXML().contains( "jabber:client" ) );
}

/**
Expand Down Expand Up @@ -111,8 +111,8 @@ public void testAvoidStrippingInternalContentNamespace() throws Exception
final Document result = packetReader.read( new StringReader( input ) );

// Verify result.
Assert.assertFalse( "'jabber:client' should not occur before 'something:else'", result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
Assert.assertTrue( "'jabber:client' should occur after 'something:else'", result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
assertFalse( result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ), "'jabber:client' should not occur before 'something:else'" );
assertTrue( result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ), "'jabber:client' should occur after 'something:else'" );
}

/**
Expand Down Expand Up @@ -141,8 +141,8 @@ public void testAvoidStrippingPrefixFreeCanonicalization() throws Exception
final Document result = packetReader.read( new StringReader( input ) );

// Verify result.
Assert.assertFalse( "'jabber:client' should not occur before 'something:else'", result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
Assert.assertTrue( "'jabber:client' should occur after 'something:else'", result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
assertFalse( result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ), "'jabber:client' should not occur before 'something:else'" );
assertTrue( result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ), "'jabber:client' should occur after 'something:else'" );
}

/**
Expand All @@ -161,8 +161,8 @@ public void testStripNamespacesForWebsocket() throws Exception
final Document result = packetReader.read( new StringReader( input ) );

// Verify result.
Assert.assertFalse( "'jabber:client' should not occur before 'something:else'", result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
Assert.assertTrue( "'jabber:client' should occur after 'something:else'", result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ) );
assertFalse( result.asXML().substring( 0, result.asXML().indexOf("something:else") ).contains( "jabber:client" ), "'jabber:client' should not occur before 'something:else'" );
assertTrue( result.asXML().substring( result.asXML().indexOf("something:else") ).contains( "jabber:client" ), "'jabber:client' should occur after 'something:else'" );
}

/**
Expand All @@ -186,8 +186,8 @@ public void test_GetNamespacesOnCurrentElement_CustomNamespace() throws Exceptio
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(1, namespacesOnCurrentElement.size());
Assert.assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
assertEquals(1, namespacesOnCurrentElement.size());
assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
}

/**
Expand All @@ -211,7 +211,7 @@ public void test_GetNamespacesOnCurrentElement_XmlLang() throws Exception
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(0, namespacesOnCurrentElement.size());
assertEquals(0, namespacesOnCurrentElement.size());
}

/**
Expand All @@ -235,7 +235,7 @@ public void test_GetNamespacesOnCurrentElement_DefaultNamespace() throws Excepti
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(0, namespacesOnCurrentElement.size());
assertEquals(0, namespacesOnCurrentElement.size());
}

/**
Expand All @@ -260,7 +260,7 @@ public void test_GetNamespacesOnCurrentElement_IgnoredNamespace() throws Excepti
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(0, namespacesOnCurrentElement.size());
assertEquals(0, namespacesOnCurrentElement.size());
}

/**
Expand All @@ -285,7 +285,7 @@ public void test_GetNamespacesOnCurrentElement_IgnoredStreamNamespace() throws E
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(0, namespacesOnCurrentElement.size());
assertEquals(0, namespacesOnCurrentElement.size());
}

/**
Expand All @@ -310,8 +310,8 @@ public void test_GetNamespacesOnCurrentElement_MixedNamespace() throws Exception
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(1, namespacesOnCurrentElement.size());
Assert.assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
assertEquals(1, namespacesOnCurrentElement.size());
assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
}

/**
Expand All @@ -337,8 +337,8 @@ public void test_GetNamespacesOnCurrentElement_MixedNamespace_WithChildElement()
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(1, namespacesOnCurrentElement.size());
Assert.assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
assertEquals(1, namespacesOnCurrentElement.size());
assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
}

/**
Expand All @@ -364,7 +364,7 @@ public void test_GetNamespacesOnCurrentElement_MixedNamespace_OnChildElement() t
final Set<Namespace> namespacesOnCurrentElement = XMPPPacketReader.getPrefixedNamespacesOnCurrentElement(packetReader.getXPPParser());

// Verify results.
Assert.assertEquals(1, namespacesOnCurrentElement.size());
Assert.assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
assertEquals(1, namespacesOnCurrentElement.size());
assertEquals(Namespace.get("unittest", "custom:namespace"), namespacesOnCurrentElement.iterator().next());
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
package org.jivesoftware.admin;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/*
* Copyright (C) 2023 Ignite Realtime Foundation. All rights reserved.
*
* 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.
*/

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
package org.jivesoftware.admin;

import org.jivesoftware.Fixtures;
import org.jivesoftware.openfire.admin.AdminManager;
import org.jivesoftware.openfire.auth.AuthToken;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@RunWith(MockitoJUnitRunner.class)
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class AuthCheckFilterTest {

private static final String adminUser = "test-admin-user";
Expand All @@ -39,22 +55,22 @@ public class AuthCheckFilterTest {
@Mock private AdminManager adminManager;
@Mock private LoginLimitManager loginLimitManager;

@BeforeClass
@BeforeAll
public static void setUpClass() throws Exception {
Fixtures.reconfigureOpenfireHome();
}

@Before
@BeforeEach
public void setUp() {

Fixtures.clearExistingProperties();

doReturn("/uri/to/page").when(request).getRequestURI();
doReturn(httpSession).when(request).getSession();
doReturn(remoteAddr).when(request).getRemoteAddr();
lenient().doReturn("/uri/to/page").when(request).getRequestURI();
lenient().doReturn(httpSession).when(request).getSession();
lenient().doReturn(remoteAddr).when(request).getRemoteAddr();

doReturn(true).when(adminManager).isUserAdmin(adminUser, true);
doReturn(false).when(adminManager).isUserAdmin(normalUser, true);
lenient().doReturn(true).when(adminManager).isUserAdmin(adminUser, true);
lenient().doReturn(false).when(adminManager).isUserAdmin(normalUser, true);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
/*
* Copyright (C) 2023 Ignite Realtime Foundation. All rights reserved.
*
* 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.admin;

import org.jivesoftware.openfire.security.SecurityAuditManager;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.StringUtils;
import org.jivesoftware.util.SystemProperty;
import org.jivesoftware.util.TaskEngine;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Random;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class LoginLimitManagerTest {

private LoginLimitManager loginLimitManager;

@Mock private SecurityAuditManager securityAuditManager;
@Mock private TaskEngine taskEngine;

@Before
@BeforeEach
public void setUp() {
loginLimitManager = new LoginLimitManager(securityAuditManager, taskEngine);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
/*
* Copyright (C) 2023 Ignite Realtime Foundation. All rights reserved.
*
* 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.admin;


import org.jivesoftware.Fixtures;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.servlet.http.HttpServletRequest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.doReturn;

import javax.servlet.http.HttpServletRequest;

import org.jivesoftware.Fixtures;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class SiteMinderServletRequestAuthenticatorTest {

private SiteMinderServletRequestAuthenticator authenticator;

@Mock private HttpServletRequest request;

@Before
@BeforeEach
public void setUp() throws Exception {
Fixtures.reconfigureOpenfireHome();
authenticator = new SiteMinderServletRequestAuthenticator();
Expand Down
Loading

0 comments on commit dfb7d9d

Please sign in to comment.