Skip to content

Commit

Permalink
Merge pull request #157 from orchitech/upgrade-commons
Browse files Browse the repository at this point in the history
Upgrade wrensec-commons. Remove PowerMock dependency.
  • Loading branch information
pavelhoral authored Sep 15, 2023
2 parents 4b359f6 + 810be54 commit e485263
Show file tree
Hide file tree
Showing 32 changed files with 239 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2014-2016 ForgeRock AS.
* Portions Copyright 2023 Wren Security
*/

package org.forgerock.openam.core.rest.cts;
Expand All @@ -26,6 +27,8 @@
import org.forgerock.openam.rest.resource.SSOTokenContext;
import org.forgerock.openam.utils.Config;
import org.forgerock.util.promise.Promise;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

Expand All @@ -40,20 +43,31 @@ public class CoreTokenResourceAuthzModuleTest {
SessionService mockService = mock(SessionService.class);
CoreTokenConfig mockCoreTokenConfig = mock(CoreTokenConfig.class);
Debug mockDebug = mock(Debug.class);
private SSOTokenContext mockSSOTokenContext;

@BeforeTest
public void beforeTest() {
given(mockConfig.get()).willReturn(mockService);
}

@BeforeMethod
public void setup() {
mockSSOTokenContext = mock(SSOTokenContext.class);
given(mockSSOTokenContext.asContext(SSOTokenContext.class)).willReturn(mockSSOTokenContext);
}

@AfterMethod
public void cleanup() {
mockSSOTokenContext = null;
}

@Test
public void shouldBlockAllAccessIfResourceDisabled() throws Exception {

//given
given(mockCoreTokenConfig.isCoreTokenResourceEnabled()).willReturn(false);
CoreTokenResourceAuthzModule testModule = new CoreTokenResourceAuthzModule(mockConfig, mockDebug,
mockCoreTokenConfig);
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);

//when
Promise<AuthorizationResult, ResourceException> result = testModule.authorize(mockSSOTokenContext);
Expand All @@ -71,7 +85,6 @@ public void shouldAuthorizeAccessToSuperUserIfResourceEnabled() throws Exception

CoreTokenResourceAuthzModule testModule = new CoreTokenResourceAuthzModule(mockConfig, mockDebug,
mockCoreTokenConfig);
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
SSOToken mockSSOToken = mock(SSOToken.class);

given(mockSSOTokenContext.getCallerSSOToken()).willReturn(mockSSOToken);
Expand All @@ -93,7 +106,6 @@ public void shouldBlockAllAccessIfResourceEnabledButNonSuperUser() throws Except
given(mockCoreTokenConfig.isCoreTokenResourceEnabled()).willReturn(true);
CoreTokenResourceAuthzModule testModule = new CoreTokenResourceAuthzModule(mockConfig, mockDebug,
mockCoreTokenConfig);
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
SSOToken mockSSOToken = mock(SSOToken.class);

given(mockSSOTokenContext.getCallerSSOToken()).willReturn(mockSSOToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2016 ForgeRock AS.
* Portions Copyright 2023 Wren Security
*/

package org.forgerock.openam.core.rest.session;
Expand Down Expand Up @@ -104,6 +105,7 @@ public String convertDNToRealm(String dn) {
};
sessionResource = new SessionResourceV2(ssoTokenManager, authUtilsWrapper,
sessionResourceUtil, sessionPropertyWhitelist, sessionService, partialSessionFactory);
given(mockContext.asContext(SSOTokenContext.class)).willReturn(mockContext);
given(mockContext.getCallerSSOToken()).willReturn(ssoToken);
}

Expand Down
25 changes: 0 additions & 25 deletions openam-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,31 +416,6 @@
<artifactId>fest-assert</artifactId>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-reflect</artifactId>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng-common</artifactId>
</dependency>

<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
package com.iplanet.dpro.session.service;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

import com.iplanet.am.util.SystemProperties;
import com.iplanet.services.naming.WebtopNaming;
import com.sun.identity.shared.Constants;
import com.sun.identity.shared.debug.Debug;
import org.forgerock.openam.session.SessionServiceURLService;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.mockito.MockedStatic;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
public class SessionServerConfigTest {

private MockedStatic<SystemProperties> systemProperties;
private MockedStatic<WebtopNaming> webtopNaming;

@PrepareForTest({ SystemProperties.class, WebtopNaming.class })
public class SessionServerConfigTest extends PowerMockTestCase {
@BeforeMethod
public void setup() {
this.systemProperties = mockStatic(SystemProperties.class);
this.webtopNaming = mockStatic(WebtopNaming.class);
}

@AfterMethod
public void cleanup() {
this.systemProperties.close();
this.webtopNaming.close();
}

@Test
public void localServerIsPrimaryServerIfNoSiteSetup() throws Exception {
// Given
PowerMockito.mockStatic(SystemProperties.class);
given(SystemProperties.get(Constants.AM_SERVER_PROTOCOL)).willReturn("http");
given(SystemProperties.get(Constants.AM_SERVER_HOST)).willReturn("openam.example.com");
given(SystemProperties.get(Constants.AM_SERVER_PORT)).willReturn("8080");
given(SystemProperties.get(Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR)).willReturn("/openam");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVER_PROTOCOL)).thenReturn("http");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVER_HOST)).thenReturn("openam.example.com");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVER_PORT)).thenReturn("8080");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR)).thenReturn("/openam");

String primary = "01";
PowerMockito.mockStatic(WebtopNaming.class);
given(WebtopNaming.getServerID("http", "openam.example.com", "8080", "/openam")).willReturn(primary);
given(WebtopNaming.getAMServerID()).willReturn("01");
given(WebtopNaming.getLocalServer()).willReturn("http://openam.example.com:8080/openam");
webtopNaming.when(() -> WebtopNaming.getServerID("http", "openam.example.com", "8080", "/openam")).thenReturn(primary);
webtopNaming.when(WebtopNaming::getAMServerID).thenReturn("01");
webtopNaming.when(WebtopNaming::getLocalServer).thenReturn("http://openam.example.com:8080/openam");

// When
SessionServerConfig config = new SessionServerConfig(mock(Debug.class), mock(SessionServiceURLService.class));
Expand All @@ -43,19 +55,17 @@ public void localServerIsPrimaryServerIfNoSiteSetup() throws Exception {
@Test
public void localServerAndPrimaryServerDifferIfSiteSetup() throws Exception {
// Given
PowerMockito.mockStatic(SystemProperties.class);
given(SystemProperties.get(Constants.AM_SERVER_PROTOCOL)).willReturn("http");
given(SystemProperties.get(Constants.AM_SERVER_HOST)).willReturn("openam.example.com");
given(SystemProperties.get(Constants.AM_SERVER_PORT)).willReturn("8080");
given(SystemProperties.get(Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR)).willReturn("/openam");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVER_PROTOCOL)).thenReturn("http");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVER_HOST)).thenReturn("openam.example.com");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVER_PORT)).thenReturn("8080");
systemProperties.when(() -> SystemProperties.get(Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR)).thenReturn("/openam");

String primary = "01";
PowerMockito.mockStatic(WebtopNaming.class);
given(WebtopNaming.getServerID("http", "openam.example.com", "8080", "/openam")).willReturn(primary);
given(WebtopNaming.isSiteEnabled(anyString())).willReturn(true); // enable site
given(WebtopNaming.getSiteID(anyString())).willReturn("02");
given(WebtopNaming.getAMServerID()).willReturn("01");
given(WebtopNaming.getLocalServer()).willReturn("http://openam.example.com:8080/openam");
webtopNaming.when(() -> WebtopNaming.getServerID("http", "openam.example.com", "8080", "/openam")).thenReturn(primary);
webtopNaming.when(() -> WebtopNaming.isSiteEnabled(anyString())).thenReturn(true); // enable site
webtopNaming.when(() -> WebtopNaming.getSiteID(anyString())).thenReturn("02");
webtopNaming.when(WebtopNaming::getAMServerID).thenReturn("01");
webtopNaming.when(WebtopNaming::getLocalServer).thenReturn("http://openam.example.com:8080/openam");

// When
SessionServerConfig config = new SessionServerConfig(mock(Debug.class), mock(SessionServiceURLService.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,44 @@
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2013-2017 ForgeRock AS.
* Portions Copyright 2023 Wren Security
*/

package org.forgerock.openam.cts.impl;

import com.iplanet.am.util.SystemProperties;
import org.forgerock.opendj.ldap.DN;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.mockito.MockedStatic;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mockStatic;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;

/**
* @author robert.wapshott@forgerock.com
*/
@PrepareForTest(SystemProperties.class)
public class LDAPConfigTest extends PowerMockTestCase {
public class LDAPConfigTest {

private LDAPConfig config;
private MockedStatic<SystemProperties> systemProperties;

@BeforeMethod
public void setup() {
this.systemProperties = mockStatic(SystemProperties.class);
}

@AfterMethod
public void cleanup() {
this.systemProperties.close();
}

@Test
public void shouldIndicateHasChanged() {
// Given
PowerMockito.mockStatic(SystemProperties.class);
given(SystemProperties.get("test-root-suffix")).willReturn("badger");
systemProperties.when(() -> SystemProperties.get("test-root-suffix")).thenReturn("badger");

LDAPConfig config = new TestLDAPConfig();

Expand All @@ -53,8 +60,7 @@ public void shouldIndicateHasChanged() {
@Test
public void shouldIndicateHasNotChanged() {
// Given
PowerMockito.mockStatic(SystemProperties.class);
given(SystemProperties.get("test-root-suffix")).willReturn(null);
systemProperties.when(() -> SystemProperties.get("test-root-suffix")).thenReturn(null);

LDAPConfig config = new TestLDAPConfig();

Expand All @@ -65,8 +71,7 @@ public void shouldIndicateHasNotChanged() {
@Test
public void shouldReturnDefaultRootSuffix() {
// Given
PowerMockito.mockStatic(SystemProperties.class);
given(SystemProperties.get("test-root-suffix")).willReturn(null);
systemProperties.when(() -> SystemProperties.get("test-root-suffix")).thenReturn(null);

LDAPConfig config = new TestLDAPConfig();

Expand All @@ -80,8 +85,7 @@ public void shouldReturnDefaultRootSuffix() {
@Test
public void shouldReturnExternalRootSuffix() {
// Given
PowerMockito.mockStatic(SystemProperties.class);
given(SystemProperties.get("test-root-suffix")).willReturn("dc=google,dc=com");
systemProperties.when(() -> SystemProperties.get("test-root-suffix")).thenReturn("dc=google,dc=com");

LDAPConfig config = new TestLDAPConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2013-2016 ForgeRock AS.
* Portions Copyright 2023 Wren Security
*/
package org.forgerock.openam.cts.worker.process;

import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.verify;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;

import java.util.Arrays;
Expand All @@ -44,7 +46,7 @@ public class CTSWorkerBaseProcessTest {
public void setUp() throws Exception {
mockQuery = mock(CTSWorkerQuery.class);
mockFilter = mock(CTSWorkerFilter.class);
mockProcess = mock(CTSWorkerBaseProcess.class);
mockProcess = spy(CTSWorkerBaseProcess.class);
}

@AfterMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* "Portions copyright [year] [name of copyright owner]"
*
* Copyright 2013-2015 ForgeRock AS.
* Portions Copyright 2023 Wren Security.
*/

package org.forgerock.openam.services.email;
Expand All @@ -29,10 +30,7 @@
import com.sun.identity.sm.ServiceConfig;
import com.sun.identity.sm.ServiceConfigManager;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import javax.mail.MessagingException;
Expand All @@ -45,10 +43,11 @@
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class MailServerImplTest extends PowerMockTestCase {
public class MailServerImplTest {

private MailServerImpl mailServerMock;
private AMSendMail sendMailMock;
Expand Down Expand Up @@ -90,10 +89,10 @@ public void testSendMailWithOptions(){

@BeforeMethod
public void setup(){
ServiceConfigManager serviceConfigManagerMock = PowerMockito.mock(ServiceConfigManager.class);
ServiceConfig serviceConfigMock = PowerMockito.mock(ServiceConfig.class);
Debug debugMock = PowerMockito.mock(Debug.class);
sendMailMock = PowerMockito.mock(AMSendMail.class);
ServiceConfigManager serviceConfigManagerMock = mock(ServiceConfigManager.class);
ServiceConfig serviceConfigMock = mock(ServiceConfig.class);
Debug debugMock = mock(Debug.class);
sendMailMock = mock(AMSendMail.class);
Map<String, Set<String>> options = createOptionsMap();
try{
Mockito.doNothing().when(sendMailMock).postMail(eq(recipients), anyString(), anyString(), anyString());
Expand Down
Loading

0 comments on commit e485263

Please sign in to comment.