This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move searchBestMatchTargetTest to TranslationFinderTest
- Loading branch information
Showing
2 changed files
with
114 additions
and
43 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
zanata-war/src/test/java/org/zanata/service/impl/TranslationFinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.zanata.service.impl; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.assertj.core.api.Condition; | ||
import org.dbunit.operation.DatabaseOperation; | ||
import org.hibernate.search.impl.FullTextSessionImpl; | ||
import org.hibernate.search.jpa.Search; | ||
import org.mockito.MockitoAnnotations; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
import org.zanata.ImmutableDbunitJpaTest; | ||
import org.zanata.common.LocaleId; | ||
import org.zanata.dao.ProjectIterationDAO; | ||
import org.zanata.dao.TextFlowTargetDAO; | ||
import org.zanata.model.HDocument; | ||
import org.zanata.model.HProjectIteration; | ||
import org.zanata.model.HTextFlow; | ||
import org.zanata.model.HTextFlowTarget; | ||
import org.zanata.seam.SeamAutowire; | ||
import org.zanata.service.SearchIndexManager; | ||
import org.zanata.service.TranslationFinder; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
/** | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
*/ | ||
public class TranslationFinderTest extends ImmutableDbunitJpaTest { | ||
private SeamAutowire seam = SeamAutowire.instance(); | ||
|
||
@Override | ||
protected void prepareDBUnitOperations() { | ||
beforeTestOperations.add(new DataSetOperation( | ||
"org/zanata/test/model/ClearAllTables.dbunit.xml", | ||
DatabaseOperation.CLEAN_INSERT)); | ||
beforeTestOperations.add(new DataSetOperation( | ||
"org/zanata/test/model/LocalesData.dbunit.xml", | ||
DatabaseOperation.CLEAN_INSERT)); | ||
beforeTestOperations.add(new DataSetOperation( | ||
"org/zanata/test/model/TranslationMemoryData.dbunit.xml", | ||
DatabaseOperation.CLEAN_INSERT)); | ||
} | ||
|
||
@BeforeClass | ||
public void beforeClass() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
seam.reset() | ||
.use("entityManager", | ||
Search.getFullTextEntityManager(getEm())) | ||
.use("entityManagerFactory", getEmf()) | ||
.use("session", new FullTextSessionImpl(getSession())) | ||
.useImpl(AsyncTaskManagerServiceImpl.class) | ||
.ignoreNonResolvable(); | ||
seam.autowire(SearchIndexManager.class).reindex(true, true, false); | ||
} | ||
|
||
@Test | ||
public void testTextFlowTargetDAO() { | ||
TranslationFinder service = seam.autowire(TextFlowTargetDAO.class); | ||
searchBestMatchTargetTest(service); | ||
} | ||
|
||
@Test | ||
public void testTranslationMemoryServiceImpl() throws Exception { | ||
TranslationFinder service = seam.autowire(TranslationMemoryServiceImpl.class); | ||
searchBestMatchTargetTest(service); | ||
} | ||
|
||
/** | ||
* Makes sure that given two equal results, it will reuse the most recent | ||
* translation. | ||
*/ | ||
private void searchBestMatchTargetTest(TranslationFinder service) { | ||
ProjectIterationDAO projectIterationDAO = | ||
seam.autowire(ProjectIterationDAO.class); | ||
|
||
HProjectIteration version = | ||
projectIterationDAO.getBySlug("same-project", "same-version"); | ||
assert version != null; | ||
|
||
HDocument hDoc = version.getDocuments().get("/same/document0"); | ||
|
||
HTextFlow textFlow = hDoc.getTextFlows().get(0); | ||
Optional<HTextFlowTarget> match = | ||
service.searchBestMatchTransMemory(textFlow, | ||
LocaleId.DE, | ||
hDoc.getSourceLocaleId(), true, true, | ||
true); | ||
Assert.assertTrue(match.isPresent()); | ||
checkTargetContents(match.get(), "most recent content"); | ||
} | ||
|
||
private void checkTargetContents(HTextFlowTarget target, | ||
final String searchString) { | ||
assertThat(target.getContents()).has( | ||
new Condition<List<String>>("contains \""+searchString+"\"") { | ||
@Override | ||
public boolean matches(List<String> strings) { | ||
for (String value : strings) { | ||
if (value.contains(searchString)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters