Skip to content

Commit

Permalink
feat(ZNTA-1302): add test cases for LegacyFileMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Mar 2, 2017
1 parent 3bf99e7 commit b0e5d6b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Expand Up @@ -51,36 +51,26 @@ public class LegacyFileMapper {
@Inject
private ProjectIterationDAO projectIterationDAO;

/**
*
* @param projectSlug
* @param iterationSlug
* @param clientDocId
* @param projectType
* @return
*/
String getServerDocId(String projectSlug, String iterationSlug,
String getServerDocId(@Nullable ProjectType serverProjectType,
String clientDocId, @Nullable ProjectType projectType) {
String suffix = getFilenameSuffix(projectSlug, iterationSlug, projectType, true);
String suffix = getFilenameSuffix(serverProjectType, projectType, true);
return StringUtils.removeEnd(clientDocId, suffix);
}

public String getFilenameSuffix(String projectSlug, String iterationSlug, @Nullable ProjectType clientProjectType, boolean forSourceFiles) {
public String getFilenameSuffix(@Nullable ProjectType serverProjectType, @Nullable ProjectType clientProjectType, boolean forSourceFiles) {
ProjectType projectType;
// check the database first
@Nullable ProjectType serverProjectType = getProjectType(projectSlug, iterationSlug);
if (serverProjectType != null) {
if (clientProjectType != null && serverProjectType != clientProjectType) {
log.warn("server project type '{}' doesn't match client project type '{}' for project '{}' iteration '{}'",
serverProjectType, clientProjectType, projectSlug, iterationSlug);
log.warn("server project type '{}' doesn't match client project type '{}'",
serverProjectType, clientProjectType);
}
projectType = serverProjectType;
} else {
if (clientProjectType != null) {
// otherwise use the client's projectType
projectType = clientProjectType;
log.info("server project type 'null' overridden by client project type '{}' for project '{}' iteration '{}'",
clientProjectType, projectSlug, iterationSlug);
log.info("server project type 'null' overridden by client project type '{}'",
clientProjectType);
} else {
throw new WebApplicationException(
"Unknown project type: please update your Zanata client, or ask project maintainer to set the project type");
Expand Down
Expand Up @@ -26,13 +26,16 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.zanata.common.ProjectType;
import org.zanata.dao.ProjectDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.test.CdiUnitRunner;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

/**
Expand All @@ -41,6 +44,8 @@
@RunWith(CdiUnitRunner.class)
@InRequestScope
public class LegacyFileMapperTest {
// private String projectSlug = "projectSlug";
// private String iterSlug = "iterSlug";

@Produces @Mock
ProjectDAO projectDAO;
Expand All @@ -58,7 +63,32 @@ public void before() {

@Test
public void serverPropertiesWithPropertiesHint() {
// mapper.getServerDocId()
String serverDocId = mapper.getServerDocId(ProjectType.Properties, "docId.properties", ProjectType.Properties);
assertThat(serverDocId).isEqualTo("docId");
}

@Test
public void serverNullWithPropertiesHint() {
String serverDocId = mapper.getServerDocId(null, "docId.properties", ProjectType.Properties);
assertThat(serverDocId).isEqualTo("docId");
}

@Test
public void serverFileWithAnyHint() {
String serverDocId = mapper.getServerDocId(ProjectType.File, "docId.properties", ProjectType.Properties);
assertThat(serverDocId).isEqualTo("docId.properties");
}

@Test(expected = WebApplicationException.class)
public void serverNullWithNullHint() {
mapper.getServerDocId(null, "docId.properties", null);
}

// FIXME
// @Test
// public void serverXliffWithPropertiesHint() {
// String serverDocId = mapper.getServerDocId(ProjectType.Xliff, "docId.properties", ProjectType.Properties);
// assertThat(serverDocId).isEqualTo("docId");
// }

}

0 comments on commit b0e5d6b

Please sign in to comment.