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

Commit

Permalink
Fix GlossaryTerm with TermComment relationship and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 12, 2012
1 parent ba01ea0 commit b5ba2f6
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 69 deletions.
Expand Up @@ -139,7 +139,7 @@ public void run() throws Exception

Glossary glossary = reader.extractGlossary(glossaryFile);
log.debug(glossary.toString());
ClientResponse<Glossary> response = glossaryResource.put(glossary);
ClientResponse<String> response = glossaryResource.put(glossary);
ClientUtility.checkResult(response, uri);
}
}
Expand Down
Expand Up @@ -31,7 +31,7 @@ public interface IGlossaryResource extends GlossaryResource

@Override
@PUT
public ClientResponse<Glossary> put(Glossary glossary);
public ClientResponse<String> put(Glossary glossary);

@Override
@DELETE
Expand Down
Expand Up @@ -73,7 +73,15 @@ public IVersionResource createIVersionResource()
@Override
public IGlossaryResource getGlossaryResource()
{
return null;
try
{
return createProxy(IGlossaryResource.class, new URI("/restv1/glossary"));
}
catch (URISyntaxException e)
{
log.debug("exception:" + e.getMessage());
throw new RuntimeException(e);
}
}

}
Expand Up @@ -33,6 +33,7 @@
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
Expand Down Expand Up @@ -87,6 +88,7 @@ public void setContent(String content)
}

@OneToMany(cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@IndexColumn(name = "pos", base = 0, nullable = false)
@JoinColumn(name = "glossaryTermId", nullable = false)
public List<HTermComment> getComments()
Expand Down
Expand Up @@ -29,7 +29,6 @@

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.validator.NotNull;

Expand Down Expand Up @@ -97,7 +96,6 @@ public void setPos(Integer pos)
@ManyToOne
@JoinColumn(name = "glossaryTermId", insertable = false, updatable = false, nullable = false)
// TODO PERF @NaturalId(mutable=false) for better criteria caching
@NaturalId
public HGlossaryTerm getGlossaryTerm()
{
return glossaryTerm;
Expand Down
Expand Up @@ -19,6 +19,8 @@
import javax.ws.rs.core.UriInfo;

import org.codehaus.enunciate.jaxrs.TypeHint;
import org.hibernate.Session;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
Expand Down Expand Up @@ -61,6 +63,9 @@ public class GlossaryService implements GlossaryResource, SecurityChecker
@In
private Identity identity;

@In
private Session session;

@In
private LocaleService localeServiceImpl;

Expand Down
Expand Up @@ -140,11 +140,17 @@
<column name="plural" type="boolean" valueBoolean="false"/>
</addColumn>
</changeSet>

<!--
Make sure db constraints are created.
Specific problem with MySQL and the MyISAM storage engine (doesn't allow foreign keys)
-->

<changeSet author="aeng@redhat.com" id="7.5">
<comment>Clear orphan records in HTermComment and HGlossaryTerm</comment>
<sql>delete from HTermComment where glossaryTermId NOT in (select id from HGlossaryTerm)</sql>
<sql>delete from HGlossaryTerm where glossaryEntryId NOT in (select id from HGlossaryEntry)</sql>
</changeSet>

<!--
Make sure db constraints are created.
Specific problem with MySQL and the MyISAM storage engine (doesn't allow foreign keys)
-->

<changeSet author="camunoz@redhat.com" id="8" dbms="mysql">
<comment>Change engine to INNODB on MySQL for every table created with liquibase</comment>
Expand Down
Expand Up @@ -27,18 +27,17 @@
import java.util.List;
import java.util.Map;

import org.dbunit.operation.DatabaseOperation;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.hibernate.Session;
import org.junit.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.common.LocaleId;
import org.zanata.dao.GlossaryDAO;
import org.zanata.dao.LocaleDAO;
import org.zanata.model.HGlossaryEntry;
import org.zanata.model.HGlossaryTerm;
import org.zanata.model.HLocale;
import org.zanata.service.impl.LocaleServiceImpl;

/**
Expand All @@ -57,16 +56,20 @@ <T> T createMock(String name, Class<T> toMock)
return mock;
}

private GlossaryDAO dao;
private GlossaryDAO glossaryDAO;
HGlossaryEntry entry;
LocaleServiceImpl localeService;

@BeforeMethod(firstTimeOnly = true)
public void beforeMethod()
{
glossaryDAO = new GlossaryDAO((Session) em.getDelegate());
}

@Test
public void testHashMap()
{

List<HGlossaryEntry> entryList = dao.getEntries();
List<HGlossaryEntry> entryList = glossaryDAO.getEntries();

for (HGlossaryEntry hGlossaryEntry : entryList)
{
Expand All @@ -82,58 +85,33 @@ public void testHashMap()
@Test
public void testTermsSize()
{
List<HGlossaryEntry> entryList = dao.getEntries();
List<HGlossaryEntry> entryList = glossaryDAO.getEntries();
assertThat(entryList.get(0).getGlossaryTerms().size(), is(3));
}

@Override
protected void prepareDBUnitOperations()
@Test
public void testDeleteGlossaries()
{
localeService = new LocaleServiceImpl();
LocaleDAO localeDAO = new LocaleDAO(getSession());
localeService.setLocaleDAO(localeDAO);

dao = new GlossaryDAO((Session) getEm().getDelegate());

localeService.save(LocaleId.EN_US);
localeService.save(LocaleId.DE);
localeService.save(LocaleId.ES);

entry = new HGlossaryEntry();
entry.setVersionNum(1);
entry.setCreationDate(new Date());
entry.setLastChanged(new Date());
List<HGlossaryEntry> hGlossaryEntries = glossaryDAO.getEntries();

entry.setSrcLocale(localeService.getByLocaleId(LocaleId.EN_US));
entry.setSourceRef("source ref");

// Glossary Term 1 - EN_US
setupTerm("TERM 1", localeService.getByLocaleId(LocaleId.EN_US));

// Glossary Term 2 - DE
setupTerm("TERM 2", localeService.getByLocaleId(LocaleId.DE));

// Glossary Term 3 - ES
setupTerm("TERM 3", localeService.getByLocaleId(LocaleId.ES));
for (HGlossaryEntry hGlossaryEntry : hGlossaryEntries)
{
glossaryDAO.makeTransient(hGlossaryEntry);
}
glossaryDAO.flush();

dao.makePersistent(entry);
dao.flush();
dao.clear();
assertThat(glossaryDAO.getEntries().size(), is(0));

assertThat(((Long)super.getSession().createQuery("select count(*) from HTermComment").uniqueResult()), is(0L));
assertThat(((Long)super.getSession().createQuery("select count(*) from HGlossaryTerm").uniqueResult()), is(0L));
}

private void setupTerm(String content, HLocale locale)
@Override
protected void prepareDBUnitOperations()
{
HGlossaryTerm term = new HGlossaryTerm(content);
term.setVersionNum(1);
term.setCreationDate(new Date());
term.setLastChanged(new Date());

// Glossary Term Locale
term.setLocale(locale);
term.setGlossaryEntry(entry);
beforeTestOperations.add(new DataSetOperation("org/zanata/test/model/GlossaryData.dbunit.xml", DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation("org/zanata/test/model/LocalesData.dbunit.xml", DatabaseOperation.CLEAN_INSERT));

entry.getGlossaryTerms().put(locale, term);
}
}

Expand Down
Expand Up @@ -158,7 +158,7 @@ public void putGlossary()
glossary.getGlossaryEntries().add(glossaryEntry1);
glossary.getGlossaryEntries().add(glossaryEntry2);

ClientResponse<Glossary> response = glossaryService.put(glossary);
ClientResponse<String> response = glossaryService.put(glossary);

assertThat(response.getStatus(), is(Status.CREATED.getStatusCode()));
}
Expand Down
@@ -1,7 +1,7 @@
package org.zanata.rest.service;


import java.net.URI;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import javax.ws.rs.core.MediaType;

Expand All @@ -12,18 +12,19 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataDBUnitSeamTest;
import org.zanata.common.LocaleId;
import org.zanata.rest.client.IGlossaryResource;
import org.zanata.rest.client.TestProxyFactory;
import org.zanata.rest.dto.VersionInfo;

import org.zanata.rest.service.SeamMockClientExecutor;
import org.zanata.rest.dto.Glossary;
import org.zanata.rest.dto.GlossaryEntry;
import org.zanata.rest.dto.GlossaryTerm;

@Test(groups = { "seam-tests" })
public abstract class GlossaryServiceSeamTest extends ZanataDBUnitSeamTest
public class GlossaryServiceSeamTest extends ZanataDBUnitSeamTest
{
private static final String AUTH_KEY = "b6d7044e9ee3b2447c28fb7c50d86d98";
private static final String USERNAME = "admin";
private static final String GLOSSARY_DATA_DBUNIT_XML = "org/zanata/test/model/GlossaryData.dbunit.xml";
private static final String ACCOUNT_DATA_DBUNIT_XML = "org/zanata/test/model/AccountData.dbunit.xml";
private static final String LOCALE_DATA_DBUNIT_XML = "org/zanata/test/model/LocalesData.dbunit.xml";

protected IGlossaryResource glossaryResource;

Expand All @@ -42,17 +43,45 @@ public ClientResponse execute(ClientExecutionContext ctx) throws Exception
@BeforeMethod
public void setup() throws Exception
{
TestProxyFactory clientRequestFactory = new TestProxyFactory(new URI("http://example.com/"), USERNAME, AUTH_KEY, new SeamMockClientExecutor(this), new VersionInfo("SNAPSHOT", ""));
TestProxyFactory clientRequestFactory = new TestProxyFactory(new SeamMockClientExecutor(this));
clientRequestFactory.registerPrefixInterceptor(new MetaTypeAccept());

glossaryResource = clientRequestFactory.getGlossaryResource();

}

@Override
protected void prepareDBUnitOperations()
{
beforeTestOperations.add(new DataSetOperation(GLOSSARY_DATA_DBUNIT_XML, DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(LOCALE_DATA_DBUNIT_XML, DatabaseOperation.CLEAN_INSERT));
beforeTestOperations.add(new DataSetOperation(ACCOUNT_DATA_DBUNIT_XML, DatabaseOperation.CLEAN_INSERT));
afterTestOperations.add(new DataSetOperation(ACCOUNT_DATA_DBUNIT_XML, DatabaseOperation.DELETE_ALL));
afterTestOperations.add(new DataSetOperation(GLOSSARY_DATA_DBUNIT_XML, DatabaseOperation.DELETE_ALL));
afterTestOperations.add(new DataSetOperation(LOCALE_DATA_DBUNIT_XML, DatabaseOperation.DELETE_ALL));
}

// FIXME write the tests!
@Test
public void testPutGlossaries()
{
Glossary glossary = new Glossary();
GlossaryEntry glossaryEntry1 = new GlossaryEntry();
glossaryEntry1.setSrcLang(LocaleId.EN_US);
glossaryEntry1.setSourcereference("TEST SOURCE REF DATA");

GlossaryTerm glossaryTerm1 = new GlossaryTerm();
glossaryTerm1.setLocale(LocaleId.EN_US);
glossaryTerm1.setContent("test data content 1 (source lang)");
glossaryTerm1.getComments().add("COMMENT 1");

glossaryEntry1.getGlossaryTerms().add(glossaryTerm1);

glossary.getGlossaryEntries().add(glossaryEntry1);

ClientResponse<String> response = glossaryResource.put(glossary);

ClientResponse<Glossary> response1 = glossaryResource.getEntries();
assertThat(response1.getEntity().getGlossaryEntries().size(), is(1));

}
}
Expand Up @@ -33,14 +33,14 @@
<classes>
<!-- FIXME get these tests working again
<class name="org.zanata.server.rpc.ActivateWorkspaceActionSeamTest"/>
<class name="org.zanata.rest.service.GlossaryServiceSeamTest"/>
<class name="org.zanata.dao.LocaleMemberDAOTest"/>
-->

<class name="org.zanata.rest.service.ProjectsServiceSeamTest"/>
<class name="org.zanata.rest.service.ResourceServiceSeamTest"/>
<class name="org.zanata.rest.service.TranslationServiceSeamTest"/>
<class name="org.zanata.rest.service.VersionSeamTest"/>
<class name="org.zanata.rest.service.GlossaryServiceSeamTest"/>
</classes>
</test>
<test name="Raw Rest Tests">
Expand Down

0 comments on commit b5ba2f6

Please sign in to comment.