Skip to content

Commit

Permalink
fix: fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jul 18, 2017
1 parent 8ced84d commit 54d9d7f
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 181 deletions.
Expand Up @@ -4,12 +4,16 @@

public class RestUtil {

public static String convertToDocumentURIId(String id) {
public static String convertToDocumentURIId(String docId) {
// NB this currently prevents us from allowing ',' in file names
if (id.startsWith("/")) {
return id.substring(1).replace('/', ',');
if (docId.startsWith("/")) {
return docId.substring(1).replace('/', ',');
}
return id.replace('/', ',');
return docId.replace('/', ',');
}

public static String convertFromDocumentURIId(String docIdWithNoSlash) {
return docIdWithNoSlash.replace(',', '/');
}

public static boolean isNotFound(Response response) {
Expand Down
Expand Up @@ -132,7 +132,7 @@ public ProcessStatus startSourceDocCreation(
*/
@Deprecated
@PUT
@Path("/projects/p/{projectSlug}/iterations/i/{iterationSlug}/r"
@Path("/projects/p/{projectSlug}/iterations/i/{iterationSlug}"
+ SourceDocResource.RESOURCE_SLUG_TEMPLATE)
/* Same as SourceDocResourceService.SERVICE_PATH */
@TypeHint(ProcessStatus.class)
Expand Down Expand Up @@ -166,8 +166,7 @@ public ProcessStatus startSourceDocCreationOrUpdate(
* e.g. "ext=gettext&ext=comment".
*/
@PUT
@Path("/projects/p/{projectSlug}/iterations/i/{iterationSlug}/resource"
+ SourceDocResource.RESOURCE_SLUG_TEMPLATE)
@Path("/projects/p/{projectSlug}/iterations/i/{iterationSlug}/resource")
@TypeHint(ProcessStatus.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "The contents of the response will indicate the process" +
Expand Down Expand Up @@ -274,8 +273,7 @@ ProcessStatus startTranslatedDocCreationOrUpdate(
" identifier which may be used to query for its status or a message" +
" indicating what happened.")
})
public
ProcessStatus startTranslatedDocCreationOrUpdateWithDocId(
public ProcessStatus startTranslatedDocCreationOrUpdateWithDocId(
@PathParam("projectSlug") String projectSlug,
@PathParam("iterationSlug") String iterationSlug,
@PathParam("locale") LocaleId locale,
Expand Down
Expand Up @@ -172,7 +172,7 @@ Response getResource(@PathParam("id") String idNoSlash,
/**
* Retrieves information for a source Document.
*
* @param id
* @param docId
* The document identifier.
* @param extensions
* The document extensions to fetch along with the document (e.g.
Expand All @@ -190,7 +190,7 @@ Response getResource(@PathParam("id") String idNoSlash,
@TypeHint(Resource.class)
@Path(DOCID_RESOURCE_PATH)
public
Response getResourceWithDocId(@QueryParam("id") @DefaultValue("") String id,
Response getResourceWithDocId(@QueryParam("docId") @DefaultValue("") String docId,
@QueryParam("ext") Set<String> extensions);

/**
Expand Down
Expand Up @@ -69,8 +69,7 @@
})
public interface TranslatedDocResource extends RestResource {
@SuppressWarnings("deprecation")
public static final String SERVICE_PATH =
ProjectIterationResource.SERVICE_PATH + "/r";
public static final String SERVICE_PATH = ProjectIterationResource.SERVICE_PATH;

/**
* Retrieves a set of translations for a given locale.
Expand Down
Expand Up @@ -40,7 +40,6 @@ public class MockAsynchronousProcessResource implements
private static final long serialVersionUID = 8841332691985560066L;

@Override
@SuppressWarnings("deprecation")
// TODO: remove this test when parent method is removed
public ProcessStatus startSourceDocCreation(String idNoSlash,
String projectSlug, String iterationSlug, Resource resource,
Expand Down
Expand Up @@ -70,9 +70,9 @@ public Response getResource(String idNoSlash, Set<String> extensions) {
}

@Override
public Response getResourceWithDocId(String id, Set<String> extensions) {
public Response getResourceWithDocId(String docId, Set<String> extensions) {
MockResourceUtil.validateExtensions(extensions);
return Response.ok(new Resource(id)).build();
return Response.ok(new Resource(docId)).build();
}

@Override
Expand Down
Expand Up @@ -557,9 +557,6 @@ private void pushSrcDocToServer(final String docId, final Resource srcDoc,
srcDoc.getName(), srcDoc.getTextFlows().size());

ConsoleUtils.startProgressFeedback();
// NB: Copy trans is set to false as using copy trans in this manner
// is deprecated.
// see PushCommand.copyTransForDocument
ProcessStatus status =
asyncProcessClient.startSourceDocCreationOrUpdateWithDocId(
getOpts().getProj(), getOpts().getProjectVersion(),
Expand Down
Expand Up @@ -54,8 +54,6 @@ public class AsyncProcessClient implements AsynchronousProcessResource {
}

@Override
@SuppressWarnings("deprecation")
// TODO: remove this test when parent method is removed
public ProcessStatus startSourceDocCreation(String idNoSlash,
String projectSlug, String iterationSlug, Resource resource,
Set<String> extensions, @DefaultValue("true") boolean copytrans) {
Expand Down
Expand Up @@ -62,9 +62,9 @@ public class SourceDocResourceClient {

public List<ResourceMeta> getResourceMeta(Set<String> extensions) {
Client client = factory.getClient();
WebTarget webResource = getBaseServiceResource(client);
WebTarget webResource = getBaseServiceResource(client).path("r");
if (extensions != null) {
webResource.path("r").queryParam("ext", extensions.toArray());
webResource.queryParam("ext", extensions.toArray());
}
return webResource.request(MediaType.APPLICATION_XML_TYPE)
.get(new GenericType<List<ResourceMeta>>() {});
Expand Down
Expand Up @@ -52,12 +52,8 @@ public class TransDocResourceClient {
baseUri = factory.getBaseUri();
}

public Response getTranslations(
String docId,
LocaleId locale,
Set<String> extensions,
boolean createSkeletons,
String eTag) {
public Response getTranslations(String docId, LocaleId locale,
Set<String> extensions, boolean createSkeletons, String eTag) {
Client client = factory.getClient();
Response response = getBaseServiceResource(client)
.path("resource")
Expand Down
Expand Up @@ -54,15 +54,13 @@ public void setUp() throws URISyntaxException {
@Test
public void testGetResourceMeta() {
List<ResourceMeta> resourceMeta = client.getResourceMeta(null);

assertThat(resourceMeta, Matchers.hasSize(2));
}

@Test
public void testGetResource() {
Resource resource = client.getResource("test",
Sets.newHashSet("gettext", "comment"));

assertThat(resource.getName(), Matchers.equalTo("test"));
}

Expand Down
Expand Up @@ -32,10 +32,10 @@
import org.zanata.common.LocaleId;
import org.zanata.dao.DocumentDAO;
import org.zanata.model.HDocument;
import org.zanata.rest.RestUtil;
import org.zanata.rest.dto.stats.ContainerTranslationStatistics;
import org.zanata.rest.dto.stats.TranslationStatistics;
import org.zanata.rest.dto.stats.TranslationStatistics.StatUnit;
import org.zanata.rest.service.URIHelper;
import org.zanata.rest.editor.service.resource.StatisticResource;
import com.google.common.collect.Lists;

Expand All @@ -56,7 +56,7 @@ public Response getDocumentStatistics(
@PathParam("versionSlug") String versionSlug,
@PathParam("docId") String docId,
@PathParam("localeId") String localeId) {
docId = URIHelper.convertFromDocumentURIId(docId);
docId = RestUtil.convertFromDocumentURIId(docId);
HDocument doc = documentDAO.getByProjectIterationAndDocId(projectSlug,
versionSlug, docId);
if (doc == null) {
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.zanata.model.type.TranslationSourceType;
import org.zanata.rest.NoSuchEntityException;
import org.zanata.rest.ReadOnlyEntityException;
import org.zanata.rest.RestUtil;
import org.zanata.rest.dto.ProcessStatus;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TranslationsResource;
Expand Down Expand Up @@ -69,6 +70,7 @@ public class AsynchronousProcessResourceService
implements AsynchronousProcessResource {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory
.getLogger(AsynchronousProcessResourceService.class);
private static final long serialVersionUID = -5915271018788588841L;

@Inject
private LocaleService localeServiceImpl;
Expand All @@ -83,8 +85,6 @@ public class AsynchronousProcessResourceService
@Inject
private ProjectIterationDAO projectIterationDAO;
@Inject
private ResourceUtils resourceUtils;
@Inject
private ZanataIdentity identity;

@Override
Expand All @@ -96,7 +96,7 @@ public ProcessStatus startSourceDocCreation(final String idNoSlash,
retrieveAndCheckIteration(projectSlug, iterationSlug, true);
// Check permission
identity.checkPermission(hProjectIteration, "import-template");
resourceUtils.validateExtensions(extensions); // gettext, comment
ResourceUtils.validateExtensions(extensions); // gettext, comment
HDocument document = documentDAO
.getByDocIdAndIteration(hProjectIteration, resource.getName());
// already existing non-obsolete document.
Expand All @@ -112,7 +112,7 @@ public ProcessStatus startSourceDocCreation(final String idNoSlash,
}
String name = "SourceDocCreation: " + projectSlug + "-" + iterationSlug
+ "-" + idNoSlash;
AsyncTaskHandle<HDocument> handle = new AsyncTaskHandle<HDocument>();
AsyncTaskHandle<HDocument> handle = new AsyncTaskHandle<>();
String keyId = asyncTaskHandleManager.registerTaskHandle(handle);
documentServiceImpl
.saveDocumentAsync(projectSlug, iterationSlug,
Expand All @@ -129,7 +129,7 @@ public ProcessStatus startSourceDocCreationOrUpdate(final String idNoSlash,
final String projectSlug, final String iterationSlug,
final Resource resource, final Set<String> extensions,
final boolean copytrans) {
String docId = URIHelper.convertFromDocumentURIId(idNoSlash);
String docId = RestUtil.convertFromDocumentURIId(idNoSlash);
return startSourceDocCreationOrUpdateProcess(projectSlug,
iterationSlug, resource, extensions, docId, copytrans);
}
Expand All @@ -151,7 +151,7 @@ private ProcessStatus startSourceDocCreationOrUpdateProcess(
}
HProjectIteration hProjectIteration =
retrieveAndCheckIteration(projectSlug, iterationSlug, true);
resourceUtils.validateExtensions(extensions); // gettext, comment
ResourceUtils.validateExtensions(extensions); // gettext, comment
// Check permission
identity.checkPermission(hProjectIteration, "import-template");
String name = "SourceDocCreationOrUpdate: " + projectSlug + "-"
Expand Down Expand Up @@ -198,7 +198,7 @@ public ProcessStatus startTranslatedDocCreationOrUpdate(
final TranslationsResource translatedDoc,
final Set<String> extensions, final String merge,
final boolean assignCreditToUploader) {
final String id = URIHelper.convertFromDocumentURIId(idNoSlash);
final String id = RestUtil.convertFromDocumentURIId(idNoSlash);
return startTranslatedDocCreationOrUpdateWithDocId(projectSlug,
iterationSlug, locale, translatedDoc, id, extensions, merge,
assignCreditToUploader);
Expand All @@ -217,7 +217,7 @@ public ProcessStatus startTranslatedDocCreationOrUpdateWithDocId(
this.getSecuredIteration(projectSlug, iterationSlug)
.getProject());
if (StringUtils.isBlank(docId)) {
throw new BadRequestException("missing id");
throw new BadRequestException("missing docId");
}
MergeType mergeType;
try {
Expand All @@ -231,7 +231,7 @@ public ProcessStatus startTranslatedDocCreationOrUpdateWithDocId(
final MergeType finalMergeType = mergeType;
String taskName = "TranslatedDocUpload: "+projectSlug+"-"+iterationSlug+"-"+
docId;
AsyncTaskHandle<HDocument> handle = new AsyncTaskHandle<HDocument>();
AsyncTaskHandle<HDocument> handle = new AsyncTaskHandle<>();
String keyId = asyncTaskHandleManager.registerTaskHandle(handle);
translationServiceImpl.translateAllInDocAsync(projectSlug,
iterationSlug, docId, locale, translatedDoc, extensions,
Expand All @@ -242,6 +242,7 @@ public ProcessStatus startTranslatedDocCreationOrUpdateWithDocId(
}

@Override
@SuppressWarnings("rawtypes")
public ProcessStatus getProcessStatus(String processId) {
AsyncTaskHandle handle =
asyncTaskHandleManager.getHandleByKeyId(processId);
Expand Down
Expand Up @@ -45,6 +45,7 @@
import org.zanata.model.HTextFlow;
import org.zanata.rest.NoSuchEntityException;
import org.zanata.rest.ReadOnlyEntityException;
import org.zanata.rest.RestUtil;
import org.zanata.rest.dto.LocaleDetails;
import org.zanata.rest.dto.ProcessStatus;
import org.zanata.rest.dto.ProjectIteration;
Expand Down Expand Up @@ -306,7 +307,7 @@ public Response getTransUnitStatus(
if (StringUtils.isEmpty(noSlashDocId)) {
return Response.status(Response.Status.NOT_FOUND).build();
}
String docId = URIHelper.convertFromDocumentURIId(noSlashDocId);
String docId = RestUtil.convertFromDocumentURIId(noSlashDocId);
HDocument document = documentDAO
.getByProjectIterationAndDocId(projectSlug, versionSlug, docId);
if (document == null) {
Expand Down

0 comments on commit 54d9d7f

Please sign in to comment.