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

Commit

Permalink
wip: merge all tests to cdiunit
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos A. Munoz committed Mar 11, 2016
1 parent 17ca0a6 commit 90c2544
Show file tree
Hide file tree
Showing 27 changed files with 909 additions and 602 deletions.
Expand Up @@ -28,6 +28,7 @@
import javax.inject.Inject;
import javax.inject.Named;

import com.beust.jcommander.internal.Maps;
import org.zanata.util.IServiceLocator;
import org.zanata.util.Synchronized;
import org.zanata.ServerConstants;
Expand All @@ -49,18 +50,18 @@ public class DatabaseBackedConfig implements Serializable {

private static final long serialVersionUID = 1L;

private Map<String, String> configurationValues;
private final Map<String, String> configurationValues = Maps.newHashMap();

@Inject
private IServiceLocator serviceLocator;
private ApplicationConfigurationDAO applicationConfigurationDAO;

/**
* Resets the store by clearing out all values. This means that values will
* need to be reloaded as they are requested.
*/
@PostConstruct
public void reset() {
configurationValues = new HashMap<String, String>();
configurationValues.clear();
}

/**
Expand All @@ -76,10 +77,8 @@ public void reset(String key) {

private String getConfigValue(String key) {
if (!configurationValues.containsKey(key)) {
ApplicationConfigurationDAO appConfigDAO =
serviceLocator.getInstance(ApplicationConfigurationDAO.class);
HApplicationConfiguration configRecord =
appConfigDAO.findByKey(key);
applicationConfigurationDAO.findByKey(key);
String storedVal = null;
if (configRecord != null) {
storedVal = configRecord.getValue();
Expand Down
Expand Up @@ -124,9 +124,11 @@ private Set<String> getSubKeys(String base) {
Context ctx = new InitialContext();
NamingEnumeration<NameClassPair> pairs = ctx.list(base);
Set<String> results = new HashSet<String>();
while (pairs.hasMore()) {
NameClassPair pair = pairs.next();
results.add(pair.getName());
if(pairs != null) {
while (pairs.hasMore()) {
NameClassPair pair = pairs.next();
results.add(pair.getName());
}
}

return results;
Expand Down
Expand Up @@ -22,13 +22,9 @@


import java.io.Serializable;
import java.util.Iterator;
import java.util.Set;

import org.apache.deltaspike.core.api.exclude.Exclude;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import javax.inject.Inject;
import javax.inject.Named;
import org.zanata.util.ServiceLocator;

/**
* This permission resolver will use the
Expand All @@ -43,6 +39,9 @@
@javax.enterprise.context.ApplicationScoped
public class CustomPermissionResolver implements Serializable {

@Inject
private PermissionEvaluator evaluator;

private static final long serialVersionUID = 6302681723997573877L;

public boolean hasPermission(Object target, String action) {
Expand All @@ -57,9 +56,6 @@ public boolean hasPermission(Object target, String action) {
}

private boolean hasPermission(String action, Object... targets) {
PermissionEvaluator evaluator =
ServiceLocator.instance()
.getInstance(PermissionEvaluator.class);
return evaluator.checkPermission(action, targets);
}

Expand Down
38 changes: 38 additions & 0 deletions zanata-war/src/test/java/org/zanata/cdi/StaticProducer.java
@@ -0,0 +1,38 @@
/*
* Copyright 2016, 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.cdi;

import org.zanata.util.IServiceLocator;
import org.zanata.util.ServiceLocator;

import javax.enterprise.inject.Produces;

/**
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public interface StaticProducer {

@Produces
default IServiceLocator getServiceLocator() {
return ServiceLocator.instance();
}

}
Expand Up @@ -23,10 +23,18 @@
import com.google.inject.Injector;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.Session;
import org.jglue.cdiunit.InRequestScope;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.cdi.CDIAutowire;
import org.zanata.test.CdiUnitRunner;
import org.zanata.util.IServiceLocator;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -38,20 +46,14 @@
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@RunWith(CdiUnitRunner.class)
public class DatabaseBackedConfigTest extends ZanataDbunitJpaTest {
@Inject
private DatabaseBackedConfig databaseBackedConfig;

private Injector injector;

@Before
public void prepare() {
injector = new CDIAutowire()
.configure(binder -> {
binder.bind(Session.class).toInstance(getSession());
})
.getInjector();
databaseBackedConfig =
injector.getInstance(DatabaseBackedConfig.class);
@Produces
public Session getSession() {
return super.getSession();
}

@Override
Expand All @@ -67,12 +69,14 @@ protected void prepareDBUnitOperations() {
}

@Test
@InRequestScope
public void getHomeContent() {
assertThat(databaseBackedConfig.getHomeContent(),
equalTo("This is the home content"));
}

@Test
@InRequestScope
public void getNonExistentValue() throws Exception {
// This value is NOT provided in the DB Unit script above
assertThat(databaseBackedConfig.getAdminEmailAddress(), nullValue());
Expand Down
@@ -1,37 +1,47 @@
package org.zanata.rest.editor.service;

import com.google.inject.Injector;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.Session;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.jglue.cdiunit.AdditionalClasses;
import org.jglue.cdiunit.InRequestScope;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.cdi.CDIAutowire;
import org.zanata.cdi.StaticProducer;
import org.zanata.jpa.FullText;
import org.zanata.service.LocaleService;
import org.zanata.service.impl.LocaleServiceImpl;
import org.zanata.test.CdiUnitRunner;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.ws.rs.core.Response;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.zanata.cdi.AutowireUtils.mockInstances;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class LocalesServiceTest extends ZanataDbunitJpaTest {
@RunWith(CdiUnitRunner.class)
@AdditionalClasses({ LocaleServiceImpl.class })
public class LocalesServiceTest extends ZanataDbunitJpaTest implements
StaticProducer {

private Response okResponse;
private Response response;

@Inject
private LocalesService localesService;

private Injector injector;
@Produces
public Session getSession() {
return super.getSession();
}

private LocalesService localesService;
@Produces @FullText @Mock
private FullTextEntityManager fullTextEntityManager;

/**
* Implement this in a subclass.
Expand All @@ -49,27 +59,13 @@ protected void prepareDBUnitOperations() {
DatabaseOperation.CLEAN_INSERT));
}

@Before
public void initializeSeam() {
injector = new CDIAutowire()
.configure(binder -> {
binder.bind(Session.class).toInstance(getSession());
binder.bind(LocaleService.class).to(LocaleServiceImpl.class);
mockInstances(FullTextEntityManager.class, FullText.class, binder);
})
.getInjector();
localesService = injector.getInstance(LocalesService.class);

okResponse = Response.ok().build();
}

@After
public void afterMethod() {
okResponse = null;
response = null;
}

@Test
@InRequestScope
public void testGetLocales() {
response = localesService.get();
assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
Expand Down

0 comments on commit 90c2544

Please sign in to comment.