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

Commit

Permalink
Full upgrade of v1.3 compatibility tests.
Browse files Browse the repository at this point in the history
Changes for DBUnit 2.4.9
Renamed some of the newer test classes.
  • Loading branch information
Carlos Munoz committed May 2, 2013
1 parent 75fa946 commit 0a13f71
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 330 deletions.
77 changes: 0 additions & 77 deletions zanata-war/src/test/java/org/zanata/CompatibilityTest.java

This file was deleted.

Expand Up @@ -21,15 +21,19 @@
package org.zanata;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.io.FileUtils;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.ext.h2.H2DataTypeFactory;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.resteasy.client.ProxyFactory;
Expand All @@ -42,6 +46,9 @@
import org.zanata.arquillian.RemoteAfter;
import org.zanata.arquillian.RemoteBefore;
import org.zanata.rest.ResourceRequestEnvironment;
import org.zanata.rest.client.TestProxyFactory;
import org.zanata.rest.client.ZanataProxyFactory;
import org.zanata.rest.dto.VersionInfo;
import org.zanata.rest.helper.RemoteTestSignaler;

/**
Expand All @@ -50,11 +57,15 @@
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@RunWith(Arquillian.class)
public abstract class RawRestTest extends ZanataDbunitJpaTest
public abstract class RestTest extends ZanataDbunitJpaTest
{
public static final String DEPLOYMENT_NAME = "zanata-tests";
// Admin credentials
protected static final String ADMIN = "admin";
protected static final String ADMIN_KEY = "b6d7044e9ee3b2447c28fb7c50d86d98";
// Translator credentials
protected static final String TRANSLATOR = "demo";
protected static final String TRANSLATOR_KEY = "23456789012345678901234567890123";

// Authorized environment with valid credentials
private static final ResourceRequestEnvironment ENV_AUTHORIZED =
Expand Down Expand Up @@ -128,7 +139,7 @@ public boolean include(ArchivePath object)
return archive;
}*/

private static void addRemoteHelpers(WebArchive archive)
/*private static void addRemoteHelpers(WebArchive archive)
{
archive.addPackages(true, "org.zanata.rest.helper");
archive.addPackages(true, "org.zanata.arquillian");
Expand All @@ -146,7 +157,7 @@ private static void addAsResources( WebArchive archive, File directory )
archive.addAsResource(file, "org/zanata/test/model/" + file.getName());
}
}
}
}*/

@RemoteBefore
@Override
Expand Down Expand Up @@ -196,7 +207,10 @@ protected IDatabaseConnection getConnection()
try
{
DataSource dataSource = (DataSource)Naming.getInitialContext().lookup("java:jboss/datasources/zanataTestDatasource");
return new DatabaseConnection(dataSource.getConnection());
DatabaseConnection dbConn = new DatabaseConnection(dataSource.getConnection());
// NB: Specific to H2
dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
return dbConn;
}
catch (Exception e)
{
Expand Down Expand Up @@ -232,7 +246,7 @@ public final String getRestEndpointUrl(String resourceUrl)
* Gets the artifact's deployed url for REST endpoints.
*
* @return The full absolute root url of the deployed artifact.
* @see RawRestTest#getRestEndpointUrl(String)
* @see RestTest#getRestEndpointUrl(String)
*/
public final String getRestEndpointUrl()
{
Expand All @@ -249,4 +263,40 @@ public static final ResourceRequestEnvironment getAuthorizedEnvironment()
return ENV_AUTHORIZED;
}

/**
* Creates and returns a new instance of a proxy factory for the given credentials.
* This method aids with the testing of Rest API classes.
*
* @param username The username that the proxy factory will authenticate with.
* @param apiKey The apiKey for the user name.
* @return A new instance of a proxy factory to create Rest API resources.
*/
public final ZanataProxyFactory createClientProxyFactory( String username, String apiKey )
{
try
{
return new TestProxyFactory(new URI(getRestEndpointUrl()),
username,
apiKey,
null,
new VersionInfo("Test", "Test"));
}
catch (URISyntaxException e)
{
throw new RuntimeException(e);
}
}

protected <T> T createProxy( ZanataProxyFactory clientFactory, Class<T> clientClass, String baseUri )
{
try
{
return clientFactory.createProxy(clientClass, new URI( getRestEndpointUrl(baseUri) ));
}
catch (URISyntaxException e)
{
throw new RuntimeException(e);
}
}

}
11 changes: 7 additions & 4 deletions zanata-war/src/test/java/org/zanata/ZanataDbunitJpaTest.java
Expand Up @@ -17,9 +17,8 @@
import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.datatype.DataTypeException;
import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
//import org.testng.annotations.AfterMethod;
//import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -192,18 +191,22 @@ public DataSetOperation(String dataSetLocation, String dtdLocation, DatabaseOper
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(dataSetLocation);
try
{
FlatXmlDataSetBuilder dataSetBuilder = new FlatXmlDataSetBuilder();
dataSetBuilder.setColumnSensing(true);

InputStream dtdInput = null;
if (dtdLocation != null)
{
dtdInput = Thread.currentThread().getContextClassLoader().getResourceAsStream(dtdLocation);
}
if (dtdInput == null)
{
this.dataSet = new ReplacementDataSet(new FlatXmlDataSet(input));
this.dataSet = new ReplacementDataSet( dataSetBuilder.build(input) );
}
else
{
this.dataSet = new ReplacementDataSet(new FlatXmlDataSet(input, dtdInput));
dataSetBuilder.setMetaDataSetFromDtd( dtdInput );
this.dataSet = new ReplacementDataSet( dataSetBuilder.build(input) );
}
}
catch (Exception ex)
Expand Down
Expand Up @@ -82,6 +82,7 @@ public boolean include(ArchivePath object)
archive.addAsResource(new FileAsset(new File("src/test/jboss-embedded-bootstrap/META-INF/persistence.xml")), "META-INF/persistence.xml");
archive.addAsResource(new FileAsset(new File("src/main/webapp-jboss/WEB-INF/classes/META-INF/components.xml")), "META-INF/components.xml");
archive.addAsResource(new FileAsset(new File("src/test/resources/arquillian/components.properties")), "components.properties");
archive.addAsResource(new FileAsset(new File("src/test/resources/import.sql")), "import.sql");
archive.addAsResource("security.drl");
archive.addAsWebInfResource(new File("src/test/resources/arquillian/zanata.properties"),
"classes/zanata.properties");
Expand Down
Expand Up @@ -22,19 +22,18 @@

import javax.ws.rs.core.Response.Status;

import org.apache.http.util.EntityUtils;
import org.dbunit.operation.DatabaseOperation;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.resteasy.client.ClientResponse;
import org.junit.Test;
import org.zanata.CompatibilityTest;
import org.zanata.RestTest;
import org.zanata.v1_3.rest.client.IAccountResource;
import org.zanata.v1_3.rest.dto.Account;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class AccountCompatibilityITCase extends CompatibilityTest
public class AccountCompatibilityITCase extends RestTest
{

@Override
Expand All @@ -47,7 +46,7 @@ protected void prepareDBUnitOperations()
@RunAsClient
public void getAccountXml() throws Exception
{
IAccountResource accountClient = super.createProxy(IAccountResource.class, "/accounts/u/demo");
IAccountResource accountClient = super.createProxy(createClientProxyFactory(TRANSLATOR, TRANSLATOR_KEY), IAccountResource.class, "/accounts/u/demo");
ClientResponse<Account> accountResponse = accountClient.get();
Account account = accountResponse.getEntity();

Expand All @@ -68,7 +67,8 @@ public void putAccountXml() throws Exception
// New Account
Account a = new Account("aacount2@localhost.com", "Sample Account", "sampleaccount", "/9Se/pfHeUH8FJ4asBD6jQ==");

IAccountResource accountClient = super.createProxy(IAccountResource.class, "/accounts/u/sampleaccount");
IAccountResource accountClient = super.createProxy(createClientProxyFactory(ADMIN, ADMIN_KEY),
IAccountResource.class, "/accounts/u/sampleaccount");
ClientResponse putResponse = accountClient.put( a );

// Assert initial put
Expand Down
Expand Up @@ -30,8 +30,9 @@
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.junit.Test;
import org.zanata.CompatibilityTest;
import org.zanata.RestTest;
import org.zanata.rest.ResourceRequest;
import org.zanata.rest.client.ZanataProxyFactory;
import org.zanata.v1_3.rest.MediaTypes;
import org.zanata.v1_3.rest.client.IAccountResource;
import org.zanata.v1_3.rest.dto.Account;
Expand All @@ -41,7 +42,7 @@
import static org.zanata.util.RawRestTestUtils.assertJsonUnmarshal;
import static org.zanata.util.RawRestTestUtils.jsonUnmarshal;

public class AccountRawCompatibilityITCase extends CompatibilityTest
public class AccountRawCompatibilityITCase extends RestTest
{

@Override
Expand Down Expand Up @@ -88,9 +89,10 @@ public void putAccountJson() throws Exception
{
// New Account
Account a = new Account("aacount2@localhost.com", "Sample Account", "sampleaccount", "/9Se/pfHeUH8FJ4asBD6jQ==");

UnimplementedIAccountResource accountClient = super.createProxy(UnimplementedIAccountResource.class, "/accounts/u/sampleaccount");
IAccountResource originalAccountClient = super.createProxy(IAccountResource.class, "/accounts/u/sampleaccount");

ZanataProxyFactory proxyFactory = createClientProxyFactory(ADMIN, ADMIN_KEY);
UnimplementedIAccountResource accountClient = super.createProxy(proxyFactory, UnimplementedIAccountResource.class, "/accounts/u/sampleaccount");
IAccountResource originalAccountClient = super.createProxy(proxyFactory, IAccountResource.class, "/accounts/u/sampleaccount");
ClientResponse putResponse = accountClient.putJson( a );
putResponse.releaseConnection();

Expand Down
Expand Up @@ -27,9 +27,10 @@
import javax.ws.rs.core.Response.Status;

import org.dbunit.operation.DatabaseOperation;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.resteasy.client.ClientResponse;
import org.testng.annotations.Test;
import org.zanata.ZanataCompatibilityTest;
import org.junit.Test;
import org.zanata.RestTest;
import org.zanata.v1_3.rest.client.IProjectResource;
import org.zanata.v1_3.rest.dto.Project;
import org.zanata.v1_3.rest.dto.ProjectType;
Expand All @@ -40,20 +41,21 @@
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*
*/
@Test(groups = {"compatibility-tests", "seam-tests"} )
public class ProjectCompatibilityTest extends ZanataCompatibilityTest
public class ProjectCompatibilityITCase extends RestTest
{

@Override
protected void prepareDBUnitOperations()
{
beforeTestOperations.add(new DataSetOperation("org/zanata/test/model/AccountData.dbunit.xml", DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation("org/zanata/test/model/ProjectsData.dbunit.xml", DatabaseOperation.CLEAN_INSERT));
}

@Test
@RunAsClient
public void getProjectXml() throws Exception
{
IProjectResource projectClient = super.createProxy(IProjectResource.class, "/projects/p/sample-project");
IProjectResource projectClient = super.createProxy(createClientProxyFactory(TRANSLATOR, TRANSLATOR_KEY), IProjectResource.class, "/projects/p/sample-project");
ClientResponse<Project> projectResponse = projectClient.get();
Project project = projectResponse.getEntity();

Expand All @@ -66,24 +68,28 @@ public void getProjectXml() throws Exception
}

@Test
@RunAsClient
public void putProjectXml() throws Exception
{
// New Project
Project p = new Project("new-project", "New Project", ProjectType.IterationProject, "This is a New Sample Project");

IProjectResource projectClient = super.createProxy(IProjectResource.class, "/projects/p/new-project");
IProjectResource projectClient = super.createProxy(createClientProxyFactory(ADMIN, ADMIN_KEY),
IProjectResource.class, "/projects/p/new-project");
ClientResponse putResponse = projectClient.put( p );

// Assert initial put
assertThat(putResponse.getStatus(), is(Status.CREATED.getStatusCode()));

putResponse.releaseConnection();

// Modified Project
p.setDescription("This is an updated project");
putResponse = projectClient.put( p );

// Assert modification
assertThat(putResponse.getStatus(), is(Status.OK.getStatusCode()));

putResponse.releaseConnection();

// Retrieve again
Project p2 = projectClient.get().getEntity();
assertThat(p2.getId(), is(p.getId()));
Expand Down

0 comments on commit 0a13f71

Please sign in to comment.