From 88d55be96c00392328a80fdea176e7f2c9ad66f1 Mon Sep 17 00:00:00 2001 From: Patrick Hobusch Date: Mon, 18 Mar 2024 20:17:40 +0800 Subject: [PATCH] Update JUnit and Mockito to versions 5.x, replace PowerMock --- pom.xml | 30 +++-- .../SysadminOnlyResourceFilterTest.java | 39 +++--- .../util/ApplicationLinkBeanUtilTest.java | 28 ++-- .../model/util/DirectoryBeanUtilTest.java | 18 +-- .../jira/model/util/LicenseBeanUtilTest.java | 16 +-- .../model/util/MailServerPopBeanUtilTest.java | 16 +-- .../util/MailServerSmtpBeanUtilTest.java | 16 +-- .../SettingsColourSchemeBeanUtilTest.java | 16 +-- .../service/ApplicationLinkServiceTest.java | 56 ++++---- .../jira/service/DirectoryServiceTest.java | 123 +++++++++++------- .../jira/service/LicensesServiceTest.java | 22 ++-- .../jira/service/MailServerServiceTest.java | 59 +++++---- .../service/SettingsBrandingServiceTest.java | 102 ++++++++------- .../jira/service/SettingsServiceTest.java | 40 +++--- .../jira/util/MailProtocolUtilTest.java | 20 +-- 15 files changed, 333 insertions(+), 268 deletions(-) diff --git a/pom.xml b/pom.xml index 3eeaa42..bd740cc 100644 --- a/pom.xml +++ b/pom.xml @@ -60,11 +60,14 @@ 8.0.2 ${project.groupId}.${project.artifactId} 2.1.5 - 0.0.32-SNAPSHOT + 0.5.0 2.2.4 6.1.5.Final + 0.8.5 3.0.0 + 4.0.1 7.13.0 + 5.10.0 UTF-8 UTF-8 @@ -230,12 +233,6 @@ provided - - javax.ws.rs - jsr311-api - provided - - javax.xml.bind jaxb-api @@ -251,6 +248,7 @@ javax.servlet javax.servlet-api + ${javax.servlet-api.version} provided @@ -277,14 +275,22 @@ - junit - junit + org.junit.jupiter + junit-jupiter test org.mockito mockito-core + ${mockito.version} + test + + + + org.mockito + mockito-junit-jupiter + ${mockito.version} test @@ -410,6 +416,12 @@ + + + org.jacoco + jacoco-maven-plugin + ${jacoco.plugin.version} + diff --git a/src/test/java/de/aservo/confapi/jira/filter/SysadminOnlyResourceFilterTest.java b/src/test/java/de/aservo/confapi/jira/filter/SysadminOnlyResourceFilterTest.java index a5496b6..79204d7 100644 --- a/src/test/java/de/aservo/confapi/jira/filter/SysadminOnlyResourceFilterTest.java +++ b/src/test/java/de/aservo/confapi/jira/filter/SysadminOnlyResourceFilterTest.java @@ -5,52 +5,55 @@ import com.atlassian.sal.api.user.UserKey; import com.atlassian.sal.api.user.UserManager; import com.atlassian.sal.api.user.UserProfile; -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 org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.mockito.Matchers.any; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -@RunWith(MockitoJUnitRunner.class) -public class SysadminOnlyResourceFilterTest { +@ExtendWith(MockitoExtension.class) +class SysadminOnlyResourceFilterTest { @Mock private UserManager userManager; private SysadminOnlyResourceFilter sysadminOnlyResourceFilter; - @Before + @BeforeEach public void setup() { sysadminOnlyResourceFilter = new SysadminOnlyResourceFilter(userManager); } @Test - public void testFilterDefaults() { + void testFilterDefaults() { assertNull(sysadminOnlyResourceFilter.getResponseFilter()); assertEquals(sysadminOnlyResourceFilter, sysadminOnlyResourceFilter.getRequestFilter()); } - @Test(expected = AuthenticationRequiredException.class) - public void testAdminAccessNoUser() { - sysadminOnlyResourceFilter.filter(null); + @Test + void testAdminAccessNoUser() { + assertThrows(AuthenticationRequiredException.class, () -> { + sysadminOnlyResourceFilter.filter(null); + }); } - @Test(expected = AuthorisationException.class) - public void testNonSysadminAccess() { + @Test + void testNonSysadminAccess() { final UserProfile userProfile = mock(UserProfile.class); doReturn(userProfile).when(userManager).getRemoteUser(); - sysadminOnlyResourceFilter.filter(null); + assertThrows(AuthorisationException.class, () -> { + sysadminOnlyResourceFilter.filter(null); + }); } @Test - public void testSysadminAccess() { + void testSysadminAccess() { final UserProfile userProfile = mock(UserProfile.class); doReturn(new UserKey("user")).when(userProfile).getUserKey(); doReturn(userProfile).when(userManager).getRemoteUser(); diff --git a/src/test/java/de/aservo/confapi/jira/model/util/ApplicationLinkBeanUtilTest.java b/src/test/java/de/aservo/confapi/jira/model/util/ApplicationLinkBeanUtilTest.java index 91db92b..a43b67f 100644 --- a/src/test/java/de/aservo/confapi/jira/model/util/ApplicationLinkBeanUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/model/util/ApplicationLinkBeanUtilTest.java @@ -16,23 +16,22 @@ import de.aservo.confapi.commons.model.ApplicationLinkBean; import de.aservo.confapi.commons.model.ApplicationLinkBean.ApplicationLinkType; import org.apache.commons.lang3.NotImplementedException; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import java.net.URI; import java.net.URISyntaxException; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; -@RunWith(MockitoJUnitRunner.class) -public class ApplicationLinkBeanUtilTest { +@ExtendWith(MockitoExtension.class) +class ApplicationLinkBeanUtilTest { @Test - public void testToApplicationLinkBean() throws URISyntaxException { + void testToApplicationLinkBean() throws URISyntaxException { final ApplicationId applicationId = new ApplicationId(UUID.randomUUID().toString()); final URI displayUri = new URI("http://localhost"); final URI rpcUri = new URI("http://rpc.example.com"); @@ -48,7 +47,7 @@ public void testToApplicationLinkBean() throws URISyntaxException { } @Test - public void testToApplicationLinkDetails() { + void testToApplicationLinkDetails() { final ApplicationLinkBean bean = ApplicationLinkBean.EXAMPLE_1; final ApplicationLinkDetails linkDetails = ApplicationLinkBeanUtil.toApplicationLinkDetails(bean); @@ -60,7 +59,7 @@ public void testToApplicationLinkDetails() { } @Test - public void testLinkTypeGenerator() throws URISyntaxException { + void testLinkTypeGenerator() throws URISyntaxException { for (ApplicationLinkType linkType : ApplicationLinkType.values()) { ApplicationType applicationType = null; switch (linkType) { @@ -92,14 +91,17 @@ public void testLinkTypeGenerator() throws URISyntaxException { } } - @Test(expected = NotImplementedException.class) - public void testNonImplementedLinkTypeGenerator() throws URISyntaxException { + @Test + void testNonImplementedLinkTypeGenerator() throws URISyntaxException { ApplicationType applicationType = mock(RefAppApplicationType.class); ApplicationId applicationId = new ApplicationId(UUID.randomUUID().toString()); URI uri = new URI("http://localhost"); ApplicationLink applicationLink = new DefaultApplicationLink( applicationId, applicationType, "test", uri, uri, false, false); - ApplicationLinkBeanUtil.toApplicationLinkBean(applicationLink); + + assertThrows(NotImplementedException.class, () -> { + ApplicationLinkBeanUtil.toApplicationLinkBean(applicationLink); + }); } } diff --git a/src/test/java/de/aservo/confapi/jira/model/util/DirectoryBeanUtilTest.java b/src/test/java/de/aservo/confapi/jira/model/util/DirectoryBeanUtilTest.java index 32ff9a8..bbff61e 100644 --- a/src/test/java/de/aservo/confapi/jira/model/util/DirectoryBeanUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/model/util/DirectoryBeanUtilTest.java @@ -4,22 +4,22 @@ import com.atlassian.crowd.embedded.api.DirectoryType; import com.atlassian.crowd.model.directory.DirectoryImpl; import de.aservo.confapi.commons.model.DirectoryCrowdBean; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import java.util.Map; import static com.atlassian.crowd.directory.RemoteCrowdDirectory.*; import static com.atlassian.crowd.directory.SynchronisableDirectoryProperties.INCREMENTAL_SYNC_ENABLED; import static com.atlassian.crowd.model.directory.DirectoryImpl.ATTRIBUTE_KEY_USE_NESTED_GROUPS; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; -@RunWith(MockitoJUnitRunner.class) -public class DirectoryBeanUtilTest { +@ExtendWith(MockitoExtension.class) +class DirectoryBeanUtilTest { @Test - public void testToDirectoryWithoutProxy() { + void testToDirectoryWithoutProxy() { final DirectoryCrowdBean bean = DirectoryCrowdBean.EXAMPLE_1; final Directory directory = DirectoryBeanUtil.toDirectory(bean); @@ -32,7 +32,7 @@ public void testToDirectoryWithoutProxy() { } @Test - public void testToDirectoryWithProxy() { + void testToDirectoryWithProxy() { final DirectoryCrowdBean bean = DirectoryCrowdBean.EXAMPLE_1_WITH_PROXY; final Directory directory = DirectoryBeanUtil.toDirectory(bean); @@ -50,7 +50,7 @@ public void testToDirectoryWithProxy() { } @Test - public void testToDirectoryBeanWithProxy() { + void testToDirectoryBeanWithProxy() { final DirectoryImpl directory = new DirectoryImpl("test", DirectoryType.CROWD, "test.class"); directory.setAttribute(CROWD_SERVER_URL, "http://localhost"); directory.setAttribute(APPLICATION_PASSWORD, "test"); diff --git a/src/test/java/de/aservo/confapi/jira/model/util/LicenseBeanUtilTest.java b/src/test/java/de/aservo/confapi/jira/model/util/LicenseBeanUtilTest.java index 8639fec..13c224b 100644 --- a/src/test/java/de/aservo/confapi/jira/model/util/LicenseBeanUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/model/util/LicenseBeanUtilTest.java @@ -4,22 +4,22 @@ import com.atlassian.jira.license.LicenseDetails; import com.atlassian.jira.license.LicensedApplications; import de.aservo.confapi.commons.model.LicenseBean; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collections; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -@RunWith(MockitoJUnitRunner.class) -public class LicenseBeanUtilTest { +@ExtendWith(MockitoExtension.class) +class LicenseBeanUtilTest { @Test - public void testToLicenseBean() { + void testToLicenseBean() { final LicenseBean exampleLicenseBean = LicenseBean.EXAMPLE_1; final LicensedApplications licensedApplications = mock(LicensedApplications.class); diff --git a/src/test/java/de/aservo/confapi/jira/model/util/MailServerPopBeanUtilTest.java b/src/test/java/de/aservo/confapi/jira/model/util/MailServerPopBeanUtilTest.java index 48fef6b..2822e0a 100644 --- a/src/test/java/de/aservo/confapi/jira/model/util/MailServerPopBeanUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/model/util/MailServerPopBeanUtilTest.java @@ -3,17 +3,17 @@ import atlassian.mail.server.DefaultTestPopMailServerImpl; import com.atlassian.mail.server.PopMailServer; import de.aservo.confapi.commons.model.MailServerPopBean; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; -@RunWith(MockitoJUnitRunner.class) -public class MailServerPopBeanUtilTest { +@ExtendWith(MockitoExtension.class) +class MailServerPopBeanUtilTest { @Test - public void testToMailServerPopBean() { + void testToMailServerPopBean() { final PopMailServer server = new DefaultTestPopMailServerImpl(); final MailServerPopBean bean = MailServerPopBeanUtil.toMailServerPopBean(server); @@ -29,7 +29,7 @@ public void testToMailServerPopBean() { } @Test - public void testToMailServerPopBeanHideEmptyDescription() { + void testToMailServerPopBeanHideEmptyDescription() { final PopMailServer server = new DefaultTestPopMailServerImpl(); server.setDescription(""); final MailServerPopBean bean = MailServerPopBeanUtil.toMailServerPopBean(server); diff --git a/src/test/java/de/aservo/confapi/jira/model/util/MailServerSmtpBeanUtilTest.java b/src/test/java/de/aservo/confapi/jira/model/util/MailServerSmtpBeanUtilTest.java index 42b138f..ed4105b 100644 --- a/src/test/java/de/aservo/confapi/jira/model/util/MailServerSmtpBeanUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/model/util/MailServerSmtpBeanUtilTest.java @@ -3,17 +3,17 @@ import atlassian.mail.server.DefaultTestSmtpMailServerImpl; import com.atlassian.mail.server.SMTPMailServer; import de.aservo.confapi.commons.model.MailServerSmtpBean; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; -@RunWith(MockitoJUnitRunner.class) -public class MailServerSmtpBeanUtilTest { +@ExtendWith(MockitoExtension.class) +class MailServerSmtpBeanUtilTest { @Test - public void testToMailServerSmtpBean() { + void testToMailServerSmtpBean() { final SMTPMailServer server = new DefaultTestSmtpMailServerImpl(); final MailServerSmtpBean bean = MailServerSmtpBeanUtil.toMailServerSmtpBean(server); @@ -32,7 +32,7 @@ public void testToMailServerSmtpBean() { } @Test - public void testToMailServerSmtpBeanHideEmptyDescription() { + void testToMailServerSmtpBeanHideEmptyDescription() { final SMTPMailServer server = new DefaultTestSmtpMailServerImpl(); server.setDescription(""); final MailServerSmtpBean bean = MailServerSmtpBeanUtil.toMailServerSmtpBean(server); diff --git a/src/test/java/de/aservo/confapi/jira/model/util/SettingsColourSchemeBeanUtilTest.java b/src/test/java/de/aservo/confapi/jira/model/util/SettingsColourSchemeBeanUtilTest.java index 81898a0..00f6b48 100644 --- a/src/test/java/de/aservo/confapi/jira/model/util/SettingsColourSchemeBeanUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/model/util/SettingsColourSchemeBeanUtilTest.java @@ -3,27 +3,27 @@ import com.atlassian.jira.config.properties.APKeys; import com.atlassian.jira.config.properties.ApplicationProperties; import de.aservo.confapi.commons.model.SettingsBrandingColorSchemeBean; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class SettingsColourSchemeBeanUtilTest { @Mock private ApplicationProperties applicationProperties; @Test - public void testToGlobalColorScheme() { + void testToGlobalColorScheme() { SettingsBrandingColorSchemeBean schemeBean = SettingsBrandingColorSchemeBean.EXAMPLE_1; SettingsBrandingColorSchemeBeanUtil.setGlobalColorScheme(schemeBean, true, applicationProperties); @@ -34,7 +34,7 @@ public void testToGlobalColorScheme() { } @Test - public void testToSettingsBrandingColorSchemeBean() { + void testToSettingsBrandingColorSchemeBean() { Map dummyBaseColourScheme = getDummyBaseColourScheme(); doReturn(dummyBaseColourScheme).when(applicationProperties).asMap(); diff --git a/src/test/java/de/aservo/confapi/jira/service/ApplicationLinkServiceTest.java b/src/test/java/de/aservo/confapi/jira/service/ApplicationLinkServiceTest.java index 2160938..9ee3776 100644 --- a/src/test/java/de/aservo/confapi/jira/service/ApplicationLinkServiceTest.java +++ b/src/test/java/de/aservo/confapi/jira/service/ApplicationLinkServiceTest.java @@ -25,11 +25,11 @@ import de.aservo.confapi.commons.model.ApplicationLinksBean; import de.aservo.confapi.jira.model.type.DefaultAuthenticationScenario; import de.aservo.confapi.jira.model.util.ApplicationLinkBeanUtil; -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 org.mockito.junit.jupiter.MockitoExtension; import java.net.URI; import java.net.URISyntaxException; @@ -42,12 +42,12 @@ import static de.aservo.confapi.commons.model.ApplicationLinkBean.ApplicationLinkStatus.AVAILABLE; import static de.aservo.confapi.commons.model.ApplicationLinkBean.ApplicationLinkStatus.CONFIGURATION_ERROR; import static de.aservo.confapi.commons.model.ApplicationLinkBean.ApplicationLinkType.CROWD; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; -@RunWith(MockitoJUnitRunner.class) -public class ApplicationLinkServiceTest { +@ExtendWith(MockitoExtension.class) +class ApplicationLinkServiceTest { @Mock private MutatingApplicationLinkService mutatingApplicationLinkService; @@ -60,20 +60,20 @@ public class ApplicationLinkServiceTest { private ApplicationLinkServiceImpl applicationLinkService; - @Before + @BeforeEach public void setup() { applicationLinkService = new ApplicationLinkServiceImpl(mutatingApplicationLinkService, typeAccessor, applinkStatusService); } @Test - public void testDefaultDefaultAuthenticationScenarioImpl() { + void testDefaultDefaultAuthenticationScenarioImpl() { DefaultAuthenticationScenario defaultAuthenticationScenario = new DefaultAuthenticationScenario(); assertTrue(defaultAuthenticationScenario.isCommonUserBase()); assertTrue(defaultAuthenticationScenario.isTrusted()); } @Test - public void testGetApplicationLinks() throws URISyntaxException, NoAccessException, NoSuchApplinkException { + void testGetApplicationLinks() throws URISyntaxException, NoAccessException, NoSuchApplinkException { ApplicationLink applicationLink = createApplicationLink(); doReturn(Collections.singletonList(applicationLink)).when(mutatingApplicationLinkService).getApplicationLinks(); doReturn(createApplinkStatus(applicationLink, AVAILABLE)).when(applinkStatusService).getApplinkStatus(any()); @@ -86,7 +86,7 @@ public void testGetApplicationLinks() throws URISyntaxException, NoAccessExcepti } @Test - public void testGetApplicationLink() throws URISyntaxException, NoAccessException, NoSuchApplinkException, TypeNotInstalledException { + void testGetApplicationLink() throws URISyntaxException, NoAccessException, NoSuchApplinkException, TypeNotInstalledException { ApplicationLink applicationLink = createApplicationLink(); doReturn(applicationLink).when(mutatingApplicationLinkService).getApplicationLink(any()); doReturn(createApplinkStatus(applicationLink, AVAILABLE)).when(applinkStatusService).getApplinkStatus(any()); @@ -99,7 +99,7 @@ public void testGetApplicationLink() throws URISyntaxException, NoAccessExceptio } @Test - public void testSetApplicationLinks() + void testSetApplicationLinks() throws URISyntaxException, NoAccessException, NoSuchApplinkException, TypeNotInstalledException { ApplicationLink applicationLink = createApplicationLink(); @@ -118,7 +118,7 @@ public void testSetApplicationLinks() } @Test - public void testSetApplicationLink() + void testSetApplicationLink() throws URISyntaxException, NoAccessException, NoSuchApplinkException, TypeNotInstalledException { ApplicationLink applicationLink = createApplicationLink(); @@ -135,7 +135,7 @@ public void testSetApplicationLink() } @Test - public void testSetApplicationLinkUpdate() + void testSetApplicationLinkUpdate() throws URISyntaxException, NoAccessException, NoSuchApplinkException, TypeNotInstalledException { ApplicationLink applicationLink = createApplicationLink(); @@ -153,7 +153,7 @@ public void testSetApplicationLinkUpdate() } @Test - public void testAddApplicationLinkWithoutExistingTargetLink() + void testAddApplicationLinkWithoutExistingTargetLink() throws URISyntaxException, ManifestNotFoundException, NoAccessException, NoSuchApplinkException { ApplicationLink applicationLink = createApplicationLink(); @@ -171,7 +171,7 @@ public void testAddApplicationLinkWithoutExistingTargetLink() } @Test - public void testAddApplicationLinkWithExistingTargetLink() throws URISyntaxException, ManifestNotFoundException, NoAccessException, NoSuchApplinkException { + void testAddApplicationLinkWithExistingTargetLink() throws URISyntaxException, ManifestNotFoundException, NoAccessException, NoSuchApplinkException { ApplicationLink applicationLink = createApplicationLink(); ApplicationLinkBean applicationLinkBean = createApplicationLinkBean(); @@ -188,7 +188,7 @@ public void testAddApplicationLinkWithExistingTargetLink() throws URISyntaxExcep } @Test - public void testAddApplicationLinkWithAuthenticatorErrorIgnored() throws URISyntaxException, ManifestNotFoundException, AuthenticationConfigurationException, NoAccessException, NoSuchApplinkException { + void testAddApplicationLinkWithAuthenticatorErrorIgnored() throws URISyntaxException, ManifestNotFoundException, AuthenticationConfigurationException, NoAccessException, NoSuchApplinkException { ApplicationLink applicationLink = createApplicationLink(); ApplicationLinkBean applicationLinkBean = createApplicationLinkBean(); @@ -205,8 +205,8 @@ public void testAddApplicationLinkWithAuthenticatorErrorIgnored() throws URISynt assertNotEquals(applicationLinkResponse, applicationLinkBean); } - @Test(expected = BadRequestException.class) - public void testAddApplicationLinkWithAuthenticatorErrorNOTIgnored() throws URISyntaxException, ManifestNotFoundException, AuthenticationConfigurationException { + @Test + void testAddApplicationLinkWithAuthenticatorErrorNOTIgnored() throws URISyntaxException, ManifestNotFoundException, AuthenticationConfigurationException { ApplicationLink applicationLink = createApplicationLink(); ApplicationLinkBean applicationLinkBean = createApplicationLinkBean(); @@ -216,11 +216,13 @@ public void testAddApplicationLinkWithAuthenticatorErrorNOTIgnored() throws URIS doReturn(new DefaultApplicationType()).when(typeAccessor).getApplicationType(any()); doThrow(new AuthenticationConfigurationException("")).when(mutatingApplicationLinkService).configureAuthenticationForApplicationLink(any(), any(), any(), any()); - applicationLinkService.addApplicationLink(applicationLinkBean, false); + assertThrows(BadRequestException.class, () -> { + applicationLinkService.addApplicationLink(applicationLinkBean, false); + }); } @Test - public void testApplicationLinkTypeConverter() throws URISyntaxException, ManifestNotFoundException, NoAccessException, NoSuchApplinkException { + void testApplicationLinkTypeConverter() throws URISyntaxException, ManifestNotFoundException, NoAccessException, NoSuchApplinkException { for (ApplicationLinkType linkType : ApplicationLinkType.values()) { ApplicationLink applicationLink = createApplicationLink(); ApplicationLinkBean applicationLinkBean = createApplicationLinkBean(); @@ -238,7 +240,7 @@ public void testApplicationLinkTypeConverter() throws URISyntaxException, Manife } @Test - public void testDeleteApplicationLinks() throws URISyntaxException { + void testDeleteApplicationLinks() throws URISyntaxException { ApplicationLink applicationLink = createApplicationLink(); doReturn(Collections.singletonList(applicationLink)).when(mutatingApplicationLinkService).getApplicationLinks(); @@ -247,13 +249,15 @@ public void testDeleteApplicationLinks() throws URISyntaxException { verify(mutatingApplicationLinkService).deleteApplicationLink(any()); } - @Test(expected = BadRequestException.class) - public void testDeleteApplicationLinksWithoutForceParameter() { - applicationLinkService.deleteApplicationLinks(false); + @Test + void testDeleteApplicationLinksWithoutForceParameter() { + assertThrows(BadRequestException.class, () -> { + applicationLinkService.deleteApplicationLinks(false); + }); } @Test - public void testDeleteApplicationLink() throws URISyntaxException, TypeNotInstalledException { + void testDeleteApplicationLink() throws URISyntaxException, TypeNotInstalledException { ApplicationLink applicationLink = createApplicationLink(); doReturn(applicationLink).when(mutatingApplicationLinkService).getApplicationLink(any()); diff --git a/src/test/java/de/aservo/confapi/jira/service/DirectoryServiceTest.java b/src/test/java/de/aservo/confapi/jira/service/DirectoryServiceTest.java index ea5e56c..8b9a63d 100644 --- a/src/test/java/de/aservo/confapi/jira/service/DirectoryServiceTest.java +++ b/src/test/java/de/aservo/confapi/jira/service/DirectoryServiceTest.java @@ -14,11 +14,11 @@ import de.aservo.confapi.commons.model.DirectoryCrowdBean; import de.aservo.confapi.commons.model.DirectoryLdapBean; import de.aservo.confapi.jira.model.util.DirectoryBeanUtil; -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 org.mockito.junit.jupiter.MockitoExtension; import java.util.Collections; import java.util.Date; @@ -29,26 +29,25 @@ import static com.atlassian.crowd.directory.SynchronisableDirectoryProperties.*; import static com.atlassian.crowd.directory.SynchronisableDirectoryProperties.SyncGroupMembershipsAfterAuth.WHEN_AUTHENTICATION_CREATED_THE_USER; import static com.atlassian.crowd.model.directory.DirectoryImpl.ATTRIBUTE_KEY_USE_NESTED_GROUPS; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; -@RunWith(MockitoJUnitRunner.class) -public class DirectoryServiceTest { +@ExtendWith(MockitoExtension.class) +class DirectoryServiceTest { @Mock private CrowdDirectoryService crowdDirectoryService; private DirectoryServiceImpl directoryService; - @Before + @BeforeEach public void setup() { directoryService = new DirectoryServiceImpl(crowdDirectoryService); } @Test - public void testGetDirectories() { + void testGetDirectories() { Directory directory = createDirectory(); doReturn(Collections.singletonList(directory)).when(crowdDirectoryService).findAllDirectories(); @@ -57,15 +56,18 @@ public void testGetDirectories() { assertEquals(directories.getDirectories().iterator().next(), DirectoryBeanUtil.toDirectoryBean(directory)); } - @Test(expected = IllegalArgumentException.class) - public void testGetDirectoriesUriException() { + @Test + void testGetDirectoriesUriException() { Directory directory = createDirectory("öäöää://uhveuehvde"); doReturn(Collections.singletonList(directory)).when(crowdDirectoryService).findAllDirectories(); - directoryService.getDirectories(); + + assertThrows(IllegalArgumentException.class, () -> { + directoryService.getDirectories(); + }); } @Test - public void testGetDirectory() { + void testGetDirectory() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).findDirectoryById(1L); @@ -74,13 +76,15 @@ public void testGetDirectory() { assertEquals(DirectoryBeanUtil.toDirectoryBean(directory), directoryBean); } - @Test(expected = NotFoundException.class) - public void testGetDirectoryNotExisting() { - directoryService.getDirectory(1L); + @Test + void testGetDirectoryNotExisting() { + assertThrows(NotFoundException.class, () -> { + directoryService.getDirectory(1L); + }); } @Test - public void testSetDirectoriesWithoutExistingDirectory() { + void testSetDirectoriesWithoutExistingDirectory() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).addDirectory(any()); @@ -90,11 +94,11 @@ public void testSetDirectoriesWithoutExistingDirectory() { directoryBean.getServer().setAppPassword("test"); directoryService.setDirectories(new DirectoriesBean(Collections.singletonList(directoryBean)), false); - assertTrue("Update Successful", true); + assertTrue(true, "Update Successful"); } @Test - public void testSetDirectoriesWithExistingDirectory() { + void testSetDirectoriesWithExistingDirectory() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).findDirectoryById(1L); @@ -109,7 +113,7 @@ public void testSetDirectoriesWithExistingDirectory() { } @Test - public void testSetDirectoriesWithConnectionTest() { + void testSetDirectoriesWithConnectionTest() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).findDirectoryById(1L); @@ -124,7 +128,7 @@ public void testSetDirectoriesWithConnectionTest() { } @Test - public void testSetDirectoryDefault() { + void testSetDirectoryDefault() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).findDirectoryById(1L); @@ -142,7 +146,7 @@ public void testSetDirectoryDefault() { } @Test - public void testSetDirectoryWithConnectionTest() { + void testSetDirectoryWithConnectionTest() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).findDirectoryById(1L); @@ -156,30 +160,40 @@ public void testSetDirectoryWithConnectionTest() { assertEquals(directoryBean.getId(), directoryAdded.getId()); } - @Test(expected = BadRequestException.class) - public void testSetDirectoryUnsupportedType() { - directoryService.setDirectory(1L, new DirectoryLdapBean(), false); + @Test + void testSetDirectoryUnsupportedType() { + final DirectoryLdapBean directoryLdapBean = new DirectoryLdapBean(); + + assertThrows(BadRequestException.class, () -> { + directoryService.setDirectory(1L, directoryLdapBean, false); + }); } - @Test(expected = NotFoundException.class) - public void testSetDirectoryNotExisting() { - Directory directory = createDirectory(); - directoryService.setDirectory(1L, DirectoryBeanUtil.toDirectoryBean(directory), false); + @Test + void testSetDirectoryNotExisting() { + final Directory directory = createDirectory(); + final AbstractDirectoryBean directoryBean = DirectoryBeanUtil.toDirectoryBean(directory); + + assertThrows(NotFoundException.class, () -> { + directoryService.setDirectory(1L, directoryBean, false); + }); } - @Test(expected = IllegalArgumentException.class) - public void testAddDirectoryUriException() { + @Test + void testAddDirectoryUriException() { Directory responseDirectory = createDirectory("öäöää://uhveuehvde"); doReturn(responseDirectory).when(crowdDirectoryService).addDirectory(any()); Directory directory = createDirectory(); DirectoryCrowdBean directoryBean = (DirectoryCrowdBean) DirectoryBeanUtil.toDirectoryBean(directory); - directoryService.addDirectory(directoryBean, false); + assertThrows(IllegalArgumentException.class, () -> { + directoryService.addDirectory(directoryBean, false); + }); } @Test - public void testAddDirectory() { + void testAddDirectory() { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).addDirectory(any(Directory.class)); @@ -191,18 +205,24 @@ public void testAddDirectory() { assertEquals(directoryAdded.getId(), directoryBean.getId()); } - @Test(expected = BadRequestException.class) - public void testAddDirectoryUnsupportedType() { - directoryService.addDirectory(new DirectoryLdapBean(), false); + @Test + void testAddDirectoryUnsupportedType() { + final DirectoryLdapBean directoryLdapBean = new DirectoryLdapBean(); + + assertThrows(BadRequestException.class, () -> { + directoryService.addDirectory(directoryLdapBean, false); + }); } - @Test(expected = BadRequestException.class) - public void testDeleteDirectoriesWithoutForceParameter() { - directoryService.deleteDirectories(false); + @Test + void testDeleteDirectoriesWithoutForceParameter() { + assertThrows(BadRequestException.class, () -> { + directoryService.deleteDirectories(false); + }); } @Test - public void testDeleteDirectories() throws DirectoryCurrentlySynchronisingException { + void testDeleteDirectories() throws DirectoryCurrentlySynchronisingException { Directory directory = createDirectory(); doReturn(directory).when(crowdDirectoryService).findDirectoryById(1L); doReturn(Collections.singletonList(directory)).when(crowdDirectoryService).findAllDirectories(); @@ -213,7 +233,7 @@ public void testDeleteDirectories() throws DirectoryCurrentlySynchronisingExcept } @Test - public void testDeleteDirectoriesWithoutInternalDirectory() { + void testDeleteDirectoriesWithoutInternalDirectory() { Directory directory = createDirectory("http://localhost", DirectoryType.INTERNAL); doReturn(Collections.singletonList(directory)).when(crowdDirectoryService).findAllDirectories(); @@ -223,7 +243,7 @@ public void testDeleteDirectoriesWithoutInternalDirectory() { } @Test - public void testDeleteDirectory() throws DirectoryCurrentlySynchronisingException { + void testDeleteDirectory() throws DirectoryCurrentlySynchronisingException { doReturn(createDirectory()).when(crowdDirectoryService).findDirectoryById(1L); directoryService.deleteDirectory(1L); @@ -231,16 +251,21 @@ public void testDeleteDirectory() throws DirectoryCurrentlySynchronisingExceptio verify(crowdDirectoryService).removeDirectory(1L); } - @Test(expected = NotFoundException.class) - public void testDeleteDirectoryNotExisting() { - directoryService.deleteDirectory(1L); + @Test + void testDeleteDirectoryNotExisting() { + assertThrows(NotFoundException.class, () -> { + directoryService.deleteDirectory(1L); + }); } - @Test(expected = ServiceUnavailableException.class) - public void testDeleteDirectoryCurrentlySynchronisingException() throws DirectoryCurrentlySynchronisingException { + @Test + void testDeleteDirectoryCurrentlySynchronisingException() throws DirectoryCurrentlySynchronisingException { doReturn(createDirectory()).when(crowdDirectoryService).findDirectoryById(1L); doThrow(new DirectoryCurrentlySynchronisingException(1L)).when(crowdDirectoryService).removeDirectory(1L); - directoryService.deleteDirectory(1L); + + assertThrows(ServiceUnavailableException.class, () -> { + directoryService.deleteDirectory(1L); + }); } private Directory createDirectory() { diff --git a/src/test/java/de/aservo/confapi/jira/service/LicensesServiceTest.java b/src/test/java/de/aservo/confapi/jira/service/LicensesServiceTest.java index 6150a6a..b47c660 100644 --- a/src/test/java/de/aservo/confapi/jira/service/LicensesServiceTest.java +++ b/src/test/java/de/aservo/confapi/jira/service/LicensesServiceTest.java @@ -6,21 +6,21 @@ import com.atlassian.jira.license.LicensedApplications; import de.aservo.confapi.commons.model.LicenseBean; import de.aservo.confapi.commons.model.LicensesBean; -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 org.mockito.junit.jupiter.MockitoExtension; import java.util.Collections; import static com.atlassian.extras.api.LicenseType.TESTING; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.*; -@RunWith(MockitoJUnitRunner.class) -public class LicensesServiceTest { +@ExtendWith(MockitoExtension.class) +class LicensesServiceTest { private static final String LICENSE_KEY = "Aaa..."; @@ -29,13 +29,13 @@ public class LicensesServiceTest { private LicensesServiceImpl licensesService; - @Before + @BeforeEach public void setup() { licensesService = new LicensesServiceImpl(licenseManager); } @Test - public void testGetLicenses() { + void testGetLicenses() { final LicensedApplications licensedApplications = mock(LicensedApplications.class); doReturn(Collections.singleton(ApplicationKey.valueOf("jira"))).when(licensedApplications).getKeys(); @@ -53,7 +53,7 @@ public void testGetLicenses() { } @Test - public void testSetLicenses() { + void testSetLicenses() { final LicenseBean licenseBean = new LicenseBean(); final LicensesServiceImpl spy = spy(licensesService); diff --git a/src/test/java/de/aservo/confapi/jira/service/MailServerServiceTest.java b/src/test/java/de/aservo/confapi/jira/service/MailServerServiceTest.java index b0b4074..3d4073e 100644 --- a/src/test/java/de/aservo/confapi/jira/service/MailServerServiceTest.java +++ b/src/test/java/de/aservo/confapi/jira/service/MailServerServiceTest.java @@ -4,43 +4,40 @@ import atlassian.mail.server.DefaultTestSmtpMailServerImpl; import atlassian.mail.server.OtherTestPopMailServerImpl; import atlassian.mail.server.OtherTestSmtpMailServerImpl; -import com.atlassian.crowd.manager.mail.MailConfiguration; import com.atlassian.mail.MailException; -import com.atlassian.mail.server.MailServer; import com.atlassian.mail.server.MailServerManager; import com.atlassian.mail.server.PopMailServer; import com.atlassian.mail.server.SMTPMailServer; -import com.atlassian.mail.server.impl.SMTPMailServerImpl; import de.aservo.confapi.commons.exception.BadRequestException; import de.aservo.confapi.commons.model.MailServerPopBean; import de.aservo.confapi.commons.model.MailServerSmtpBean; import de.aservo.confapi.jira.model.util.MailServerPopBeanUtil; import de.aservo.confapi.jira.model.util.MailServerSmtpBeanUtil; -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.ArgumentCaptor; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -@RunWith(MockitoJUnitRunner.class) -public class MailServerServiceTest { +@ExtendWith(MockitoExtension.class) +class MailServerServiceTest { @Mock private MailServerManager mailServerManager; private MailServerServiceImpl mailServerService; - @Before + @BeforeEach public void setup() { mailServerService = new MailServerServiceImpl(mailServerManager); } @Test - public void testGetSmtpMailServer() { + void testGetSmtpMailServer() { final SMTPMailServer smtpMailServer = new DefaultTestSmtpMailServerImpl(); doReturn(smtpMailServer).when(mailServerManager).getDefaultSMTPMailServer(); @@ -60,13 +57,13 @@ public void testGetSmtpMailServer() { } @Test - public void testGetSmtpMailServerIsNull() { + void testGetSmtpMailServerIsNull() { final MailServerSmtpBean response = mailServerService.getMailServerSmtp(); assertNull(response); } @Test - public void testPutSmtpMaiLServerUpdate() throws Exception { + void testPutSmtpMaiLServerUpdate() throws Exception { final SMTPMailServer defaultSmtpMailServer = new DefaultTestSmtpMailServerImpl(); doReturn(true).when(mailServerManager).isDefaultSMTPMailServerDefined(); doReturn(defaultSmtpMailServer).when(mailServerManager).getDefaultSMTPMailServer(); @@ -85,7 +82,7 @@ public void testPutSmtpMaiLServerUpdate() throws Exception { } @Test - public void testPutSmtpMaiLServerCreate() throws Exception { + void testPutSmtpMaiLServerCreate() throws Exception { final SMTPMailServer createSmtpMailServer = new DefaultTestSmtpMailServerImpl(); final MailServerSmtpBean requestMailServerSmtpBean = MailServerSmtpBeanUtil.toMailServerSmtpBean(createSmtpMailServer); assertNotNull(requestMailServerSmtpBean); @@ -100,7 +97,7 @@ public void testPutSmtpMaiLServerCreate() throws Exception { } @Test - public void testPutMailServerSmtpDefaultConfig() throws MailException { + void testPutMailServerSmtpDefaultConfig() throws MailException { final MailServerSmtpBean mailServerSmtpBean = new MailServerSmtpBean(); mailServerService.setMailServerSmtp(mailServerSmtpBean); @@ -119,7 +116,7 @@ public void testPutMailServerSmtpDefaultConfig() throws MailException { } @Test - public void testPutSmtpMaiLServerWithoutPort() throws Exception { + void testPutSmtpMaiLServerWithoutPort() throws Exception { final SMTPMailServer createSmtpMailServer = new DefaultTestSmtpMailServerImpl(); createSmtpMailServer.setPort(null); @@ -135,18 +132,21 @@ public void testPutSmtpMaiLServerWithoutPort() throws Exception { assertEquals(requestMailServerSmtpBean, responseMailServerSmtpBean); } - @Test(expected = BadRequestException.class) - public void testPutSmtpMaiLServerException() throws MailException { + @Test + void testPutSmtpMaiLServerException() throws MailException { doThrow(new MailException("SMTP test exception")).when(mailServerManager).create(any()); final SMTPMailServer createSmtpMailServer = new DefaultTestSmtpMailServerImpl(); final MailServerSmtpBean requestMailServerSmtpBean = MailServerSmtpBeanUtil.toMailServerSmtpBean(createSmtpMailServer); assertNotNull(requestMailServerSmtpBean); - mailServerService.setMailServerSmtp(requestMailServerSmtpBean); + + assertThrows(BadRequestException.class, () -> { + mailServerService.setMailServerSmtp(requestMailServerSmtpBean); + }); } @Test - public void testGetPopMailServer() { + void testGetPopMailServer() { final PopMailServer popMailServer = new DefaultTestPopMailServerImpl(); doReturn(popMailServer).when(mailServerManager).getDefaultPopMailServer(); @@ -163,13 +163,13 @@ public void testGetPopMailServer() { } @Test - public void testGetPopMailServerIsNull() { + void testGetPopMailServerIsNull() { final MailServerPopBean bean = mailServerService.getMailServerPop(); assertNull(bean); } @Test - public void testPutPopMaiLServerUpdate() throws Exception { + void testPutPopMaiLServerUpdate() throws Exception { final PopMailServer defaultPopMailServer = new DefaultTestPopMailServerImpl(); doReturn(defaultPopMailServer).when(mailServerManager).getDefaultPopMailServer(); @@ -187,7 +187,7 @@ public void testPutPopMaiLServerUpdate() throws Exception { } @Test - public void testPutPopMaiLServerCreate() throws Exception { + void testPutPopMaiLServerCreate() throws Exception { final PopMailServer createPopMailServer = new DefaultTestPopMailServerImpl(); final MailServerPopBean requestMailServerPopBean = MailServerPopBeanUtil.toMailServerPopBean(createPopMailServer); assertNotNull(requestMailServerPopBean); @@ -205,7 +205,7 @@ public void testPutPopMaiLServerCreate() throws Exception { } @Test - public void testPutPopMaiLServerWithoutPort() throws Exception { + void testPutPopMaiLServerWithoutPort() throws Exception { final PopMailServer createPopMailServer = new DefaultTestPopMailServerImpl(); createPopMailServer.setPort(null); @@ -221,14 +221,17 @@ public void testPutPopMaiLServerWithoutPort() throws Exception { assertEquals(requestMailServerPopBean, responseMailServerPopBean); } - @Test(expected = BadRequestException.class) - public void testPutPopMaiLServerException() throws Exception { + @Test + void testPutPopMaiLServerException() throws Exception { doThrow(new MailException("POP test exception")).when(mailServerManager).create(any()); final PopMailServer createPopMailServer = new DefaultTestPopMailServerImpl(); final MailServerPopBean requestMailServerPopBean = MailServerPopBeanUtil.toMailServerPopBean(createPopMailServer); assertNotNull(requestMailServerPopBean); - mailServerService.setMailServerPop(requestMailServerPopBean); + + assertThrows(BadRequestException.class, () -> { + mailServerService.setMailServerPop(requestMailServerPopBean); + }); } } diff --git a/src/test/java/de/aservo/confapi/jira/service/SettingsBrandingServiceTest.java b/src/test/java/de/aservo/confapi/jira/service/SettingsBrandingServiceTest.java index fcdb0e5..e7d807a 100644 --- a/src/test/java/de/aservo/confapi/jira/service/SettingsBrandingServiceTest.java +++ b/src/test/java/de/aservo/confapi/jira/service/SettingsBrandingServiceTest.java @@ -1,7 +1,12 @@ package de.aservo.confapi.jira.service; +import com.atlassian.event.api.EventPublisher; +import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.config.properties.ApplicationProperties; +import com.atlassian.jira.config.properties.LnFDefaultColorProvider; +import com.atlassian.jira.config.properties.LogoProvider; import com.atlassian.jira.config.util.JiraHome; +import com.atlassian.jira.lookandfeel.LogoChoice; import com.atlassian.jira.lookandfeel.LookAndFeelProperties; import com.atlassian.jira.lookandfeel.upload.UploadService; import com.atlassian.jira.security.JiraAuthenticationContext; @@ -9,26 +14,29 @@ import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; import de.aservo.confapi.commons.exception.InternalServerErrorException; import de.aservo.confapi.commons.model.SettingsBrandingColorSchemeBean; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; - +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; import java.util.Map; import static de.aservo.confapi.jira.model.util.SettingsColourSchemeBeanUtilTest.getDummyBaseColourScheme; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -@RunWith(MockitoJUnitRunner.class) -public class SettingsBrandingServiceTest { +@ExtendWith(MockitoExtension.class) +class SettingsBrandingServiceTest { private ApplicationProperties applicationProperties; private UploadService uploadService; private LookAndFeelProperties lookAndFeelProperties; private SettingsBrandingServiceImpl settingsBrandingService; - @Before + @BeforeEach public void setup() { //when using powermock we cannot initialize with @Mock or @InjectMocks unfortunately applicationProperties = mock(ApplicationProperties.class); @@ -46,7 +54,7 @@ public void setup() { } @Test - public void testGetColourScheme() { + void testGetColourScheme() { Map dummyBaseColourScheme = getDummyBaseColourScheme(); doReturn(dummyBaseColourScheme).when(applicationProperties).asMap(); @@ -57,7 +65,7 @@ public void testGetColourScheme() { } @Test - public void testSetColourScheme() { + void testSetColourScheme() { SettingsBrandingColorSchemeBean schemeBean = SettingsBrandingColorSchemeBean.EXAMPLE_1; @@ -70,44 +78,48 @@ public void testSetColourScheme() { } //InternalServerErrorException -> FileNotFoundException is expected because no logofile is present in the filesystem at test time - @Test(expected = InternalServerErrorException.class) - public void testGetLogo() { - settingsBrandingService.getLogo(); - verify(uploadService).getLogoDirectory(); + @Test + void testGetLogo() { + assertThrows(InternalServerErrorException.class, () -> { + settingsBrandingService.getLogo(); + }); } - // @Test - // public void testSetLogo() { - // PowerMock.mockStatic(ComponentAccessor.class); - // expect(ComponentAccessor.getComponent(LnFDefaultColorProvider.class)).andStubReturn(mock(LnFDefaultColorProvider.class)); - // expect(ComponentAccessor.getComponent(LogoProvider.class)).andStubReturn(mock(LogoProvider.class)); - // expect(ComponentAccessor.getComponent(EventPublisher.class)).andStubReturn(mock(EventPublisher.class)); - // PowerMock.replay(ComponentAccessor.class); - // - // InputStream is = new ByteArrayInputStream("".getBytes()); - // settingsBrandingService.setLogo(is); - // - // verify(lookAndFeelProperties).setLogoChoice(LogoChoice.UPLOAD); - // } + @Test + void testSetLogo() { + InputStream is = new ByteArrayInputStream("".getBytes()); + + try (MockedStatic componentAccessorMockedStatic = mockStatic(ComponentAccessor.class)) { + componentAccessorMockedStatic.when(() -> ComponentAccessor.getComponent(LnFDefaultColorProvider.class)).thenReturn(mock(LnFDefaultColorProvider.class)); + componentAccessorMockedStatic.when(() -> ComponentAccessor.getComponent(LogoProvider.class)).thenReturn(mock(LogoProvider.class)); + componentAccessorMockedStatic.when(() -> ComponentAccessor.getComponent(EventPublisher.class)).thenReturn(mock(EventPublisher.class)); + + settingsBrandingService.setLogo(is); + } + + verify(lookAndFeelProperties).setLogoChoice(LogoChoice.UPLOAD); + } //InternalServerErrorException -> FileNotFoundException is expected because no logofile is present in the filesystem at test time - @Test(expected = InternalServerErrorException.class) - public void testGetFavicon() { - settingsBrandingService.getFavicon(); - verify(uploadService).getLogoDirectory(); + @Test + void testGetFavicon() { + assertThrows(InternalServerErrorException.class, () -> { + settingsBrandingService.getFavicon(); + }); } - // @Test - // public void testSetFavicon() { - // PowerMock.mockStatic(ComponentAccessor.class); - // expect(ComponentAccessor.getComponent(LnFDefaultColorProvider.class)).andStubReturn(mock(LnFDefaultColorProvider.class)); - // expect(ComponentAccessor.getComponent(LogoProvider.class)).andStubReturn(mock(LogoProvider.class)); - // expect(ComponentAccessor.getComponent(EventPublisher.class)).andStubReturn(mock(EventPublisher.class)); - // PowerMock.replay(ComponentAccessor.class); - // - // InputStream is = new ByteArrayInputStream("".getBytes()); - // settingsBrandingService.setFavicon(is); - // - // verify(lookAndFeelProperties).setFaviconChoice(LogoChoice.UPLOAD); - // } + @Test + void testSetFavicon() { + final InputStream is = new ByteArrayInputStream("".getBytes()); + + try (MockedStatic componentAccessorMockedStatic = mockStatic(ComponentAccessor.class)) { + componentAccessorMockedStatic.when(() -> ComponentAccessor.getComponent(LnFDefaultColorProvider.class)).thenReturn(mock(LnFDefaultColorProvider.class)); + componentAccessorMockedStatic.when(() -> ComponentAccessor.getComponent(LogoProvider.class)).thenReturn(mock(LogoProvider.class)); + componentAccessorMockedStatic.when(() -> ComponentAccessor.getComponent(EventPublisher.class)).thenReturn(mock(EventPublisher.class)); + + settingsBrandingService.setFavicon(is); + } + + verify(lookAndFeelProperties).setFaviconChoice(LogoChoice.UPLOAD); + } } diff --git a/src/test/java/de/aservo/confapi/jira/service/SettingsServiceTest.java b/src/test/java/de/aservo/confapi/jira/service/SettingsServiceTest.java index b381e7c..46bb073 100644 --- a/src/test/java/de/aservo/confapi/jira/service/SettingsServiceTest.java +++ b/src/test/java/de/aservo/confapi/jira/service/SettingsServiceTest.java @@ -3,20 +3,21 @@ import com.atlassian.jira.config.properties.ApplicationProperties; import de.aservo.confapi.commons.exception.BadRequestException; import de.aservo.confapi.commons.model.SettingsBean; -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 org.mockito.junit.jupiter.MockitoExtension; import java.net.URI; import static com.atlassian.jira.config.properties.APKeys.*; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; -@RunWith(MockitoJUnitRunner.class) -public class SettingsServiceTest { +@ExtendWith(MockitoExtension.class) +class SettingsServiceTest { private static final URI BASE_URL = URI.create("https://jira.atlassian.com"); private static final String MODE_PUBLIC = "public"; @@ -29,13 +30,13 @@ public class SettingsServiceTest { private SettingsServiceImpl settingsService; - @Before + @BeforeEach public void setup() { settingsService = new SettingsServiceImpl(applicationProperties); } @Test - public void testGetSettings() { + void testGetSettings() { doReturn(BASE_URL.toString()).when(applicationProperties).getString(JIRA_BASEURL); doReturn(MODE_PUBLIC).when(applicationProperties).getString(JIRA_MODE); doReturn(TITLE).when(applicationProperties).getString(JIRA_TITLE); @@ -52,7 +53,7 @@ public void testGetSettings() { } @Test - public void testSetSettings() { + void testSetSettings() { final SettingsBean settingsBean = new SettingsBean(); settingsBean.setBaseUrl(BASE_URL); settingsBean.setMode(MODE_PUBLIC); @@ -70,7 +71,7 @@ public void testSetSettings() { } @Test - public void testSetSettingsEmptyBean() { + void testSetSettingsEmptyBean() { final SettingsBean settingsBean = new SettingsBean(); settingsService.setSettings(settingsBean); @@ -81,22 +82,25 @@ public void testSetSettingsEmptyBean() { verify(applicationProperties, never()).setString(JIRA_CONTACT_ADMINISTRATORS_MESSSAGE, CONTACT_MESSAGE); } - @Test(expected = BadRequestException.class) - public void testSetSettingsUnsupportedMode() { + @Test + void testSetSettingsUnsupportedMode() { final SettingsBean settingsBean = new SettingsBean(); settingsBean.setMode("unsupported"); - settingsService.setSettings(settingsBean); + assertThrows(BadRequestException.class, () -> { + settingsService.setSettings(settingsBean); + }); } - @Test(expected = BadRequestException.class) - public void testSetSettingsInvalidCombination() { + @Test + void testSetSettingsInvalidCombination() { final SettingsBean settingsBean = new SettingsBean(); settingsBean.setMode(MODE_PUBLIC); - doReturn(true).when(applicationProperties).getOption(JIRA_OPTION_USER_EXTERNALMGT); - settingsService.setSettings(settingsBean); + assertThrows(BadRequestException.class, () -> { + settingsService.setSettings(settingsBean); + }); } } diff --git a/src/test/java/de/aservo/confapi/jira/util/MailProtocolUtilTest.java b/src/test/java/de/aservo/confapi/jira/util/MailProtocolUtilTest.java index 920a226..16595cc 100644 --- a/src/test/java/de/aservo/confapi/jira/util/MailProtocolUtilTest.java +++ b/src/test/java/de/aservo/confapi/jira/util/MailProtocolUtilTest.java @@ -1,30 +1,30 @@ package de.aservo.confapi.jira.util; import com.atlassian.mail.MailProtocol; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; -@RunWith(MockitoJUnitRunner.class) -public class MailProtocolUtilTest { +@ExtendWith(MockitoExtension.class) +class MailProtocolUtilTest { @Test - public void testFind() { + void testFind() { final MailProtocol protocolImap = MailProtocolUtil.find(MailProtocol.IMAP.getProtocol(), null); assertEquals(MailProtocol.IMAP, protocolImap); } @Test - public void testFindNotFoundDefaultValue() { + void testFindNotFoundDefaultValue() { final MailProtocol protocolDefault = MailProtocolUtil.find("abc", null); assertNull(protocolDefault); } @Test - public void testFindBlankDefaultValue() { + void testFindBlankDefaultValue() { final MailProtocol protocolDefault = MailProtocolUtil.find("", null); assertNull(protocolDefault); }