Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

PLF-6687 intranet site name embedded in source code #115

Merged
merged 2 commits into from Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions commons-component-common/pom.xml
Expand Up @@ -155,6 +155,22 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>

<!-- This dependency is used for test classes compilation but is erroneously reported as useless by mvn dependency:analyze -->
<dependency>
<groupId>org.exoplatform.commons</groupId>
Expand Down
Expand Up @@ -12,6 +12,10 @@
import org.exoplatform.container.component.ComponentRequestLifecycle;
import org.exoplatform.container.definition.PortalContainerConfig;
import org.exoplatform.container.xml.PortalContainerInfo;
import org.exoplatform.portal.application.PortalRequestContext;
import org.exoplatform.portal.config.UserPortalConfigService;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.webui.util.Util;
import org.exoplatform.services.jcr.RepositoryService;
import org.exoplatform.services.jcr.core.ManageableRepository;
import org.exoplatform.services.jcr.ext.app.SessionProviderService;
Expand Down Expand Up @@ -213,7 +217,26 @@ public static String getCurrentDomain() {
//
return sysDomain;
}


/**
* Get {@link SiteKey} of current site
* @return currentSite if available or default site in otherwise
*/
public static SiteKey getCurrentSite() {
PortalRequestContext pContext = null;
try {
pContext = Util.getPortalRequestContext();
} catch (NullPointerException e) {
pContext = null;
}
if (pContext != null) {
return pContext.getSiteKey();
} else {
UserPortalConfigService portalConfig = getService(UserPortalConfigService.class);
return SiteKey.portal(portalConfig.getDefaultPortal());
}
}

public static void startRequest(Object service)
{
if(service instanceof ComponentRequestLifecycle) {
Expand Down
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2015 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.exoplatform.commons.utils;

import org.exoplatform.portal.application.PortalRequestContext;
import org.exoplatform.portal.config.UserPortalConfigService;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.mop.SiteType;
import org.exoplatform.portal.webui.util.Util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

/**
* Test class for {@link CommonsUtils}
* @author <a href="mailto:tuyennt@exoplatform.com">Tuyen Nguyen The</a>.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({CommonsUtils.class, Util.class})
public class CommonsUtilsMockingTest {

@Test
public void testShouldReturnDefaultPortalSite() {
UserPortalConfigService userPortalConfig = mock(UserPortalConfigService.class);
when(userPortalConfig.getDefaultPortal()).thenReturn("intranet");

PowerMockito.mockStatic(CommonsUtils.class);
when(CommonsUtils.getService(UserPortalConfigService.class)).thenReturn(userPortalConfig);
when(CommonsUtils.getCurrentSite()).thenCallRealMethod();

SiteKey site = CommonsUtils.getCurrentSite();
assertEquals("intranet", site.getName());
assertEquals(SiteType.PORTAL, site.getType());
}

@Test
public void testShouldReturnCurrentSite() {
UserPortalConfigService userPortalConfig = mock(UserPortalConfigService.class);
when(userPortalConfig.getDefaultPortal()).thenReturn("intranet");

PortalRequestContext requestContext = mock(PortalRequestContext.class);

PowerMockito.mockStatic(Util.class);
when(Util.getPortalRequestContext()).thenReturn(requestContext);

PowerMockito.mockStatic(CommonsUtils.class);
when(CommonsUtils.getService(UserPortalConfigService.class)).thenReturn(userPortalConfig);
when(CommonsUtils.getCurrentSite()).thenCallRealMethod();

when(requestContext.getSiteKey()).thenReturn(SiteKey.portal("test_site"));
SiteKey site = CommonsUtils.getCurrentSite();
assertEquals("test_site", site.getName());
assertEquals(SiteType.PORTAL, site.getType());

when(requestContext.getSiteKey()).thenReturn(SiteKey.group("group_site"));
site = CommonsUtils.getCurrentSite();
assertEquals("group_site", site.getName());
assertEquals(SiteType.GROUP, site.getType());

when(requestContext.getSiteKey()).thenReturn(SiteKey.user("user_site"));
site = CommonsUtils.getCurrentSite();
assertEquals("user_site", site.getName());
assertEquals(SiteType.USER, site.getType());
}
}
Expand Up @@ -17,6 +17,7 @@
package org.exoplatform.commons.utils;

import org.exoplatform.commons.testing.BaseCommonsTestCase;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.services.jcr.RepositoryService;

public class CommonsUtilsTest extends BaseCommonsTestCase {
Expand Down
16 changes: 16 additions & 0 deletions pom.xml
Expand Up @@ -66,6 +66,7 @@
<org.gatein.portal.version>4.5.x-PLF-SNAPSHOT</org.gatein.portal.version>
<!-- Platform Project Dependencies -->
<org.exoplatform.platform-ui.version>4.5.x-SNAPSHOT</org.exoplatform.platform-ui.version>
<org.powermock.version>1.6.5</org.powermock.version>
</properties>
<dependencyManagement>
<!-- ### NEVER CHANGE THIS ORDER OF DEPMGT ### -->
Expand Down Expand Up @@ -295,6 +296,21 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock</artifactId>
<version>${org.powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${org.powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${org.powermock.version}</version>
</dependency>
<!-- This artifact isn't declared in kernel parent -->
<!-- We enforce it to have the good version in our packaging -->
<!-- remove it after migration to jcr 1.16.x-->
Expand Down