Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
WIP fix arquillian test and functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Oct 29, 2015
1 parent bb60874 commit 915db0a
Show file tree
Hide file tree
Showing 30 changed files with 192 additions and 116 deletions.
6 changes: 3 additions & 3 deletions functional-test/src/main/java/org/zanata/page/BasePage.java
Expand Up @@ -73,9 +73,9 @@ public class BasePage extends CorePage {
private By glossaryLink = By.id("glossary_link");
private By userAvatar = By.id("user--avatar");
private static final By BY_SIGN_IN = By.id("signin_link");
private static final By BY_SIGN_OUT = By.id("right_menu_sign_out_link");
private static final By BY_DASHBOARD_LINK = By.id("dashboard");
private static final By BY_ADMINISTRATION_LINK = By.id("administration");
private static final By BY_SIGN_OUT = By.id("banner_form:right_menu_sign_out_link");
private static final By BY_DASHBOARD_LINK = By.id("banner_form:dashboard");
private static final By BY_ADMINISTRATION_LINK = By.id("banner_form:administration");
private By searchInput = By.id("projectAutocomplete-autocomplete__input");
private By registrationLink = By.id("register_link_internal_auth");
private static final By contactAdminLink = By.linkText("Contact admin");
Expand Down
Expand Up @@ -30,7 +30,7 @@
*/
@Slf4j
public class VersionGeneralTab extends VersionBasePage {
private By versionIdField = By.id("settings-general_form:slugField:slug");
private By versionIdField = By.id("settings-general_form:slug:input:slug");
private By updateButton = By.id("settings-general_form:updateButton");

public VersionGeneralTab(WebDriver driver) {
Expand Down
Expand Up @@ -44,7 +44,7 @@
public class ServerSettingsTest extends ZanataTestCase {

@ClassRule
public HasEmailRule hasEmailRule = new HasEmailRule();
public static HasEmailRule hasEmailRule = new HasEmailRule();

@Test
public void setServerURLTest() {
Expand Down
Expand Up @@ -9,7 +9,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/test/data/sample")
@Path("/data/sample")
public interface SampleDataResource {


Expand Down
Expand Up @@ -31,7 +31,7 @@
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Path("/test/data/sample")
@Path("/data/sample")
@Slf4j
public class SampleDataResourceImpl implements SampleDataResource {

Expand Down
47 changes: 47 additions & 0 deletions zanata-test-war/src/main/java/org/zanata/rest/TestApplication.java
@@ -0,0 +1,47 @@
/*
* Copyright 2015, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* 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.zanata.rest;

import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.ApplicationPath;

import com.google.common.collect.ImmutableSet;

/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@ApplicationPath("/rest/test")
@ApplicationScoped
public class TestApplication extends javax.ws.rs.core.Application {

private static final ImmutableSet<Class<?>> CLASSES =
ImmutableSet.<Class<?>>builder()
.add(SampleDataResourceImpl.class).build();

@Override
public Set<Class<?>> getClasses() {
return CLASSES;
}
}
13 changes: 3 additions & 10 deletions zanata-war/pom.xml
Expand Up @@ -2162,16 +2162,9 @@
</dependency>

<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-seam2</artifactId>
<version>1.0.0.Beta1</version>
<exclusions>
<!-- This obsolete package duplicates some of the classes found in the newer descriptors-spi package -->
<exclusion>
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
<artifactId>shrinkwrap-descriptors-api</artifactId>
</exclusion>
</exclusions>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>

Expand Down
Expand Up @@ -241,25 +241,8 @@ public String getServerPath() {
String configuredValue = databaseBackedConfig.getServerHost();
if (configuredValue != null) {
return configuredValue;
} else if (defaultServerPath != null) {
return defaultServerPath;
} else {
createDefaultServerPath();
return defaultServerPath;
}
}


//@see comment at org.zanata.security.AuthenticationManager.onLoginCompleted()
public void createDefaultServerPath() {
java.util.Optional<HttpServletRequest> requestOpt =
HttpRequestAndSessionHolder.getRequest();
if (requestOpt.isPresent()) {
HttpServletRequest request = requestOpt.get();
defaultServerPath =
request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort()
+ request.getContextPath();
return HttpRequestAndSessionHolder.getDefaultServerPath();
}
}

Expand Down
22 changes: 13 additions & 9 deletions zanata-war/src/main/java/org/zanata/action/DashboardAction.java
Expand Up @@ -119,15 +119,15 @@ public String getUserLanguageTeams() {
HAccount account = accountDAO.findById(authenticatedAccount.getId());
return StringUtils.join(
Collections2.transform(account.getPerson()
.getLanguageMemberships(),
new Function<HLocale, Object>() {
@Nullable
@Override
public Object apply(@NonNull HLocale locale) {
return locale.retrieveDisplayName();
}
}),
", ");
.getLanguageMemberships(),
new Function<HLocale, Object>() {
@Nullable
@Override
public Object apply(@NonNull HLocale locale) {
return locale.retrieveDisplayName();
}
}),
", ");
}

private int countUserMaintainedProjects() {
Expand Down Expand Up @@ -215,6 +215,10 @@ public String getLastTranslatorMessage(HProject project) {
return "";
}

public boolean canCreateProject() {
return identity.hasPermission(new HProject(), "insert");
}

/**
* Project list filter. Pages its elements directly from the database.
*/
Expand Down
Expand Up @@ -1139,7 +1139,7 @@ private boolean checkViewObsolete() {
&& identity.hasPermission("HProject", "view-obsolete");
}

private class ProjectMaintainersAutocomplete extends MaintainerAutocomplete {
public class ProjectMaintainersAutocomplete extends MaintainerAutocomplete {

@Override
protected List<HPerson> getMaintainers() {
Expand Down
Expand Up @@ -2,6 +2,7 @@

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;
Expand All @@ -18,6 +19,7 @@
import java.io.IOException;

@Provider
@PreMatching
@SecurityPrecedence
@Slf4j
public class ZanataRestSecurityInterceptor implements ContainerRequestFilter {
Expand Down
Expand Up @@ -306,11 +306,6 @@ public void onLoginCompleted(@Observes LoginCompleted payload) {
userAccountServiceImpl.runRoleAssignmentRules(authenticatedAccount,
authenticatedCredentials, authType.name());
}
// make sure server path is populated. Here we are sure servlet request
// is available. In cases where it's not in database and
// there is no servlet request, the value will not be null.
// e.g. EmailBuilder triggered by JMS message
applicationConfiguration.createDefaultServerPath();
}

public boolean isAccountWaitingForActivation(String username) {
Expand Down
Expand Up @@ -627,7 +627,13 @@ public static final boolean canAccessRestPath(
* See annotation @Path in REST service class.
*/
private static boolean isTestServicePath(String servicePath) {
return servicePath != null && servicePath.startsWith("/test");
return servicePath != null
&& (
// when being called in RestLimitingFilter
servicePath.startsWith("/zanata/rest/test") ||
// when being called in ZanataRestSecurityInterceptor
servicePath.startsWith("/test")
);
}

private static class AutoCloseSession implements AutoCloseable {
Expand Down
Expand Up @@ -45,12 +45,19 @@ public class HttpRequestAndSessionHolder {
private static final ThreadLocal<HttpServletRequest> REQUEST =
new ThreadLocal<>();

private static String defaultServerPath;

void setRequest(@Observes @Initialized HttpServletRequest request) {
if (REQUEST.get() != null) {
throw new IllegalStateException(
"There is already a request for this thread");
}
REQUEST.set(request);
if (defaultServerPath == null) {
defaultServerPath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort()
+ request.getContextPath();
}
}

void removeRequest(@Observes @Destroyed HttpServletRequest request) {
Expand All @@ -76,4 +83,8 @@ public static Optional<HttpSession> getHttpSession(boolean create) {
}
return Optional.empty();
}

public static String getDefaultServerPath() {
return defaultServerPath;
}
}
Expand Up @@ -54,8 +54,10 @@
import org.zanata.file.GlobalDocumentId;
import org.zanata.file.SourceDocumentUpload;
import org.zanata.file.UserFileUploadTracker;
import org.zanata.model.HAccount;
import org.zanata.rest.DocumentFileUploadForm;
import org.zanata.rest.dto.ChunkUploadResponse;
import org.zanata.security.annotations.AuthenticatedLiteral;
import org.zanata.util.FileUtil;
import org.zanata.util.ServiceLocator;

Expand Down Expand Up @@ -123,10 +125,15 @@ private Optional<String> getCannotUploadReason() {
return Optional.absent();
}

// FIXME consolidate to one Optional
private Optional<Long> getAccountId() {
AuthenticatedAccountHome accountHome = ServiceLocator.instance().getInstance(
AuthenticatedAccountHome.class);
return Optional.fromNullable((Long) accountHome.getId());
java.util.Optional<HAccount> optionalAuthenticatedAccount =
ServiceLocator.instance().getOptionalInstance(
HAccount.class, new AuthenticatedLiteral());
if (optionalAuthenticatedAccount.isPresent()) {
return Optional.of(optionalAuthenticatedAccount.get().getId());
}
return Optional.absent();
}

@Override
Expand Down
Expand Up @@ -25,7 +25,7 @@
<span class="is-invisible">#{msgs['jsf.dashboard.projects.maintainedProjects.label']}</span>
</a>
</li>
<ui:fragment rendered="#{identity.hasPermission(projectHome.instance, 'insert')}">
<ui:fragment rendered="#{dashboardAction.canCreateProject()}">
<li class="tab__end">
<a href="#" jsfc="h:link"
id="create-project-link"
Expand Down
2 changes: 1 addition & 1 deletion zanata-war/src/main/webapp/WEB-INF/template/banner.xhtml
Expand Up @@ -102,7 +102,7 @@

<ui:fragment
rendered="#{identity.loggedIn and authenticatedAccountHome.instance.enabled}">
<h:form>
<h:form id="banner_form">
<nav class="header__user-nav js-dropdown">
<a href="#nav-user" class="header__user-nav__toggle js-dropdown__toggle"
id="user--avatar"
Expand Down
2 changes: 2 additions & 0 deletions zanata-war/src/test/java/org/zanata/DBUnitDataExtractor.java
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;

import org.apache.deltaspike.core.api.exclude.Exclude;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
Expand All @@ -26,6 +27,7 @@
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Slf4j
@Exclude
public class DBUnitDataExtractor {
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");

Expand Down
4 changes: 4 additions & 0 deletions zanata-war/src/test/java/org/zanata/MockResourceFactory.java
@@ -1,12 +1,16 @@
package org.zanata;

import org.apache.deltaspike.core.api.exclude.Exclude;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.InjectorFactory;
import org.jboss.resteasy.spi.PropertyInjector;
import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.resteasy.spi.ResteasyProviderFactory;


@Exclude
public class MockResourceFactory implements ResourceFactory {
private PropertyInjector propertyInjector;
private Object obj;
Expand Down
8 changes: 8 additions & 0 deletions zanata-war/src/test/java/org/zanata/RestTest.java
Expand Up @@ -28,6 +28,8 @@
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import org.apache.deltaspike.core.util.ProjectStageProducer;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
Expand All @@ -45,6 +47,7 @@
import org.zanata.arquillian.RemoteBefore;
import org.zanata.provider.DBUnitProvider;
import org.zanata.rest.ResourceRequestEnvironment;
import org.zanata.seam.SeamAutowire;
import org.zanata.util.ServiceLocator;

import com.google.common.collect.Lists;
Expand All @@ -66,6 +69,11 @@ public abstract class RestTest {
protected static final String TRANSLATOR_KEY =
"23456789012345678901234567890123";

static {
// Tell DeltaSpike to give more warning messages
ProjectStageProducer.setProjectStage(ProjectStage.IntegrationTest);
}

// Authorized environment with valid credentials
private static final ResourceRequestEnvironment ENV_AUTHORIZED =
new ResourceRequestEnvironment() {
Expand Down

0 comments on commit 915db0a

Please sign in to comment.